keystone_user 12 KB
Newer Older
1 2 3 4 5 6 7 8
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Based on Jimmy Tang's implementation

DOCUMENTATION = '''
---
module: keystone_user
9
version_added: "1.2"
10
short_description: Manage OpenStack Identity (keystone) users, tenants and roles
11 12 13 14 15 16 17 18 19 20 21 22
description:
   - Manage users,tenants, roles from OpenStack.
options:
   login_user:
     description:
        - login username to authenticate to keystone
     required: false
     default: admin
   login_password:
     description:
        - Password of login user
     required: false
Michael DeHaan committed
23
     default: 'yes'
24 25 26 27 28
   login_tenant_name:
     description:
        - The tenant login_user belongs to
     required: false
     default: None
29
     version_added: "1.3"
30 31 32 33 34 35 36 37 38 39 40 41
   token:
     description:
        - The token to be uses in case the password is not specified
     required: false
     default: None
   endpoint:
     description:
        - The keystone url for authentication
     required: false
     default: 'http://127.0.0.1:35357/v2.0/'
   user:
     description:
42
        - The name of the user that has to added/removed from OpenStack
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
     required: false
     default: None
   password:
     description:
        - The password to be assigned to the user
     required: false
     default: None
   tenant:
     description:
        - The tenant name that has be added/removed
     required: false
     default: None
   description:
     description:
        - A description for the tenant
     required: false
     default: None
   email:
     description:
        - An email address for the user
     required: false
     default: None
   role:
     description:
        - The name of the role to be assigned or created
     required: false
     default: None
   state:
     description:
        - Indicate desired state of the resource
     choices: ['present', 'absent']
     default: present
requirements: [ python-keystoneclient ]
76 77 78
author: Lorin Hochstein
'''

79 80 81 82 83 84 85 86 87 88 89
EXAMPLES = '''
# Create a tenant
- keystone_user: tenant=demo tenant_description="Default Tenant"

# Create a user
- keystone_user: user=john tenant=demo password=secrete

# Apply the admin role to the john user in the demo tenant
- keystone_user: role=admin user=john tenant=demo
'''

90 91 92 93 94 95 96 97
try:
    from keystoneclient.v2_0 import client
except ImportError:
    keystoneclient_found = False
else:
    keystoneclient_found = True


98
def authenticate(endpoint, token, login_user, login_password, login_tenant_name):
99 100 101 102 103
    """Return a keystone client object"""

    if token:
        return client.Client(endpoint=endpoint, token=token)
    else:
104 105
        return client.Client(auth_url=endpoint, username=login_user,
                             password=login_password, tenant_name=login_tenant_name)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302


def tenant_exists(keystone, tenant):
    """ Return True if tenant already exists"""
    return tenant in [x.name for x in keystone.tenants.list()]


def user_exists(keystone, user):
    """" Return True if user already exists"""
    return user in [x.name for x in keystone.users.list()]


def get_tenant(keystone, name):
    """ Retrieve a tenant by name"""
    tenants = [x for x in keystone.tenants.list() if x.name == name]
    count = len(tenants)
    if count == 0:
        raise KeyError("No keystone tenants with name %s" % name)
    elif count > 1:
        raise ValueError("%d tenants with name %s" % (count, name))
    else:
        return tenants[0]


def get_user(keystone, name):
    """ Retrieve a user by name"""
    users = [x for x in keystone.users.list() if x.name == name]
    count = len(users)
    if count == 0:
        raise KeyError("No keystone users with name %s" % name)
    elif count > 1:
        raise ValueError("%d users with name %s" % (count, name))
    else:
        return users[0]


def get_role(keystone, name):
    """ Retrieve a role by name"""
    roles = [x for x in keystone.roles.list() if x.name == name]
    count = len(roles)
    if count == 0:
        raise KeyError("No keystone roles with name %s" % name)
    elif count > 1:
        raise ValueError("%d roles with name %s" % (count, name))
    else:
        return roles[0]


def get_tenant_id(keystone, name):
    return get_tenant(keystone, name).id


def get_user_id(keystone, name):
    return get_user(keystone, name).id


def ensure_tenant_exists(keystone, tenant_name, tenant_description,
                         check_mode):
    """ Ensure that a tenant exists.

        Return (True, id) if a new tenant was created, (False, None) if it
        already existed.
    """

    # Check if tenant already exists
    try:
        tenant = get_tenant(keystone, tenant_name)
    except KeyError:
        # Tenant doesn't exist yet
        pass
    else:
        if tenant.description == tenant_description:
            return (False, tenant.id)
        else:
            # We need to update the tenant description
            if check_mode:
                return (True, tenant.id)
            else:
                tenant.update(description=tenant_description)
                return (True, tenant.id)

    # We now know we will have to create a new tenant
    if check_mode:
        return (True, None)

    ks_tenant = keystone.tenants.create(tenant_name=tenant_name,
                                        description=tenant_description,
                                        enabled=True)
    return (True, ks_tenant.id)


def ensure_tenant_absent(keystone, tenant, check_mode):
    """ Ensure that a tenant does not exist

         Return True if the tenant was removed, False if it didn't exist
         in the first place
    """
    if not tenant_exists(keystone, tenant):
        return False

    # We now know we will have to delete the tenant
    if check_mode:
        return True


def ensure_user_exists(keystone, user_name, password, email, tenant_name,
                       check_mode):
    """ Check if user exists

        Return (True, id) if a new user was created, (False, id) user alrady
        exists
    """

    # Check if tenant already exists
    try:
        user = get_user(keystone, user_name)
    except KeyError:
        # Tenant doesn't exist yet
        pass
    else:
        # User does exist, we're done
        return (False, user.id)

    # We now know we will have to create a new user
    if check_mode:
        return (True, None)

    tenant = get_tenant(keystone, tenant_name)

    user = keystone.users.create(name=user_name, password=password,
                                 email=email, tenant_id=tenant.id)
    return (True, user.id)


def ensure_role_exists(keystone, user_name, tenant_name, role_name,
                       check_mode):
    """ Check if role exists

        Return (True, id) if a new role was created or if the role was newly
        assigned to the user for the tenant. (False, id) if the role already
        exists and was already assigned to the user ofr the tenant.

    """
    # Check if the user has the role in the tenant
    user = get_user(keystone, user_name)
    tenant = get_tenant(keystone, tenant_name)
    roles = [x for x in keystone.roles.roles_for_user(user, tenant)
                     if x.name == role_name]
    count = len(roles)

    if count == 1:
        # If the role is in there, we are done
        role = roles[0]
        return (False, role.id)
    elif count > 1:
        # Too many roles with the same name, throw an error
        raise ValueError("%d roles with name %s" % (count, role_name))

    # At this point, we know we will need to make changes
    if check_mode:
        return (True, None)

    # Get the role if it exists
    try:
        role = get_role(keystone, role_name)
    except KeyError:
        # Role doesn't exist yet
        role = keystone.roles.create(role_name)

    # Associate the role with the user in the admin
    keystone.roles.add_user_role(user, role, tenant)
    return (True, role.id)


def ensure_user_absent(keystone, user, check_mode):
    raise NotImplementedError("Not yet implemented")


def ensure_role_absent(keystone, uesr, tenant, role, check_mode):
    raise NotImplementedError("Not yet implemented")


def main():

    module = AnsibleModule(
        argument_spec=dict(
            user=dict(required=False),
            password=dict(required=False),
            tenant=dict(required=False),
            tenant_description=dict(required=False),
            email=dict(required=False),
            role=dict(required=False),
            state=dict(default='present', choices=['present', 'absent']),
            endpoint=dict(required=False,
                          default="http://127.0.0.1:35357/v2.0"),
            token=dict(required=False),
            login_user=dict(required=False),
303 304
            login_password=dict(required=False),
            login_tenant_name=dict(required=False)
305 306 307
        ),
        supports_check_mode=True,
        mutually_exclusive=[['token', 'login_user'],
308 309
                            ['token', 'login_password'],
                            ['token', 'login_tenant_name']]
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    )

    if not keystoneclient_found:
        module.fail_json(msg="the python-keystoneclient module is required")

    user = module.params['user']
    password = module.params['password']
    tenant = module.params['tenant']
    tenant_description = module.params['tenant_description']
    email = module.params['email']
    role = module.params['role']
    state = module.params['state']
    endpoint = module.params['endpoint']
    token = module.params['token']
    login_user = module.params['login_user']
    login_password = module.params['login_password']
326
    login_tenant_name = module.params['login_tenant_name']
327

328
    keystone = authenticate(endpoint, token, login_user, login_password, login_tenant_name)
329 330 331 332 333 334 335

    check_mode = module.check_mode

    try:
        d = dispatch(keystone, user, password, tenant, tenant_description,
                     email, role, state, endpoint, token, login_user,
                     login_password, check_mode)
336
    except Exception, e:
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
        if check_mode:
            # If we have a failure in check mode
            module.exit_json(changed=True,
                             msg="exception: %s" % e.message)
        else:
            module.fail_json(msg=e.message)
    else:
        module.exit_json(**d)


def dispatch(keystone, user=None, password=None, tenant=None,
             tenant_description=None, email=None, role=None,
             state="present", endpoint=None, token=None, login_user=None,
             login_password=None, check_mode=False):
    """ Dispatch to the appropriate method.

        Returns a dict that will be passed to exit_json

        tenant  user  role   state
        ------  ----  ----  --------
          X                  present     ensure_tenant_exists
          X                  absent      ensure_tenant_absent
          X      X           present     ensure_user_exists
          X      X           absent      ensure_user_absent
          X      X     X     present     ensure_role_exists
          X      X     X     absent      ensure_role_absent


        """
    changed = False
    id = None
    if tenant and not user and not role and state == "present":
        changed, id = ensure_tenant_exists(keystone, tenant,
                                           tenant_description, check_mode)
    elif tenant and not user and not role and state == "absent":
        changed = ensure_tenant_absent(keystone, tenant, check_mode)
    elif tenant and user and not role and state == "present":
        changed, id = ensure_user_exists(keystone, user, password,
                                         email, tenant, check_mode)
    elif tenant and user and not role and state == "absent":
        changed = ensure_user_absent(keystone, user, check_mode)
    elif tenant and user and role and state == "present":
        changed, id = ensure_role_exists(keystone, user, tenant, role,
                                         check_mode)
    elif tenant and user and role and state == "absent":
        changed = ensure_role_absent(keystone, user, tenant, role, check_mode)
    else:
        # Should never reach here
        raise ValueError("Code should never reach here")

    return dict(changed=changed, id=id)

389
# import module snippets
390
from ansible.module_utils.basic import *
391 392
if __name__ == '__main__':
    main()