Example #1
0
def create(ctx, user_name, password, role_name, full_name, description, email,
           telephone, im, enabled, alert_enabled, alert_email,
           alert_email_prefix, external, default_cached, group_role,
           stored_vm_quota, deployed_vm_quota):
    try:
        if len(password) < 6:
            raise Exception('Password must be at least 6 characters long.')
        restore_session(ctx)
        client = ctx.obj['client']
        in_use_org_href = ctx.obj['profiles'].get('org_href')
        org = Org(client, in_use_org_href)
        role = org.get_role_record(role_name)
        role_href = role.get('href')
        result = org.create_user(
            user_name=user_name,
            password=password,
            role_href=role_href,
            full_name=full_name,
            description=description,
            email=email,
            telephone=telephone,
            im=im,
            alert_email=alert_email,
            alert_email_prefix=alert_email_prefix,
            stored_vm_quota=stored_vm_quota,
            deployed_vm_quota=deployed_vm_quota,
            is_group_role=group_role,
            is_default_cached=default_cached,
            is_external=external,
            is_alert_enabled=alert_enabled,
            is_enabled=enabled)
        stdout('User \'%s\' is successfully created.' % result.get('name'),
               ctx)
    except Exception as e:
        stderr(e, ctx)
Example #2
0
 def test_08_unlink_role_from_template(self):
     logged_in_org = self.client.get_org()
     org = Org(self.client, resource=logged_in_org)
     role_name = self.config['vcd']['role_name']
     role_record = org.get_role_record(role_name)
     role = Role(self.client, href=role_record.get('href'))
     role.unlink()
Example #3
0
def create(ctx, user_name, password, role_name, full_name, description, email,
           telephone, im, enabled, alert_enabled, alert_email,
           alert_email_prefix, external, default_cached, group_role,
           stored_vm_quota, deployed_vm_quota):
    try:
        restore_session(ctx)
        client = ctx.obj['client']
        in_use_org_href = ctx.obj['profiles'].get('org_href')
        org = Org(client, in_use_org_href)
        role = org.get_role_record(role_name)
        role_href = role.get('href')
        result = org.create_user(user_name=user_name,
                                 password=password,
                                 role_href=role_href,
                                 full_name=full_name,
                                 description=description,
                                 email=email,
                                 telephone=telephone,
                                 im=im,
                                 alert_email=alert_email,
                                 alert_email_prefix=alert_email_prefix,
                                 stored_vm_quota=stored_vm_quota,
                                 deployed_vm_quota=deployed_vm_quota,
                                 is_group_role=group_role,
                                 is_default_cached=default_cached,
                                 is_external=external,
                                 is_alert_enabled=alert_enabled,
                                 is_enabled=enabled)
        stdout('User \'%s\' is successfully created.' % result.get('name'),
               ctx)
    except Exception as e:
        stderr(e, ctx)
Example #4
0
    def create_users(cls):
        """Creates users for each of the roles in CommonRoles.

        Skips creating users which are already present in the organization.

        :raises: Exception: if the class variable _org_href is not populated.
        """
        cls._basic_check()
        if cls._org_href is None:
            raise Exception('Org ' + cls._config['vcd']['default_org_name'] +
                            ' doesn\'t exist.')

        org = Org(cls._sys_admin_client, href=cls._org_href)
        for role_enum in cls._user_name_for_roles.keys():
            user_name = cls._user_name_for_roles[role_enum]
            user_records = list(
                org.list_users(name_filter=('name', user_name)))
            if len(user_records) > 0:
                cls._logger.debug('Reusing existing user ' + user_name + '.')
                cls._user_href_for_user_names[user_name] = \
                    user_records[0].get('href')
                continue
            role = org.get_role_record(role_enum.value)
            cls._logger.debug('Creating user ' + user_name + '.')
            user_resource = org.create_user(
                user_name=user_name,
                password=cls._config['vcd']['default_org_user_password'],
                role_href=role.get('href'),
                is_enabled=True)

            cls._user_href_for_user_names[user_name] = \
                user_resource.get('href')
Example #5
0
 def test_03_get_rights(self):
     logged_in_org = self.client.get_org()
     org = Org(self.client, resource=logged_in_org)
     role_name = self.config['vcd']['role_name']
     role_record = org.get_role_record(role_name)
     role = Role(self.client, href=role_record.get('href'))
     rights = role.list_rights()
     assert len(rights) > 0
Example #6
0
 def test_04_create_role(self):
     logged_in_org = self.client.get_org()
     org = Org(self.client, resource=logged_in_org)
     role_name = self.config['vcd']['role_name']
     org.create_role(role_name, 'test description',
                     ('Disk: View Properties', ))
     role_record = org.get_role_record(role_name)
     assert self.config['vcd']['role_name'] == role_record.get('name')
Example #7
0
 def create_user(self, user_name, enabled=False):
     logged_in_org = self.client.get_org()
     org = Org(self.client, resource=logged_in_org)
     role = org.get_role_record(self.config['vcd']['role_name'])
     role_href = role.get('href')
     return org.create_user(user_name, "password", role_href, "Full Name",
                            "Description", "*****@*****.**", "408-487-9087",
                            "test_user_im", "*****@*****.**", "Alert Vcd:",
                            is_enabled=enabled)
Example #8
0
def add_right(ctx, role_name, rights, org_name):
    try:
        client = ctx.obj['client']
        if org_name is not None:
            org_href = client.get_org_by_name(org_name).get('href')
        else:
            org_href = ctx.obj['profiles'].get('org_href')
        org = Org(client, href=org_href)
        role_record = org.get_role_record(role_name)
        role = Role(client, href=role_record.get('href'))
        role.add_rights(list(rights), org)
        stdout('Rights added successfully to the role \'%s\'' % role_name, ctx)
    except Exception as e:
        stderr(e, ctx)
Example #9
0
def list_rights(ctx, role_name, org_name):
    try:
        client = ctx.obj['client']
        if org_name is not None:
            org_href = client.get_org_by_name(org_name).get('href')
        else:
            org_href = ctx.obj['profiles'].get('org_href')
        org = Org(client, href=org_href)
        role_record = org.get_role_record(role_name)
        role = Role(client, href=role_record.get('href'))
        rights = role.list_rights()
        stdout(rights, ctx)
    except Exception as e:
        stderr(e, ctx)
Example #10
0
def list_rights(ctx, role_name, org_name):
    try:
        restore_session(ctx)
        client = ctx.obj['client']
        if org_name is not None:
            org_href = client.get_org_by_name(org_name).get('href')
        else:
            org_href = ctx.obj['profiles'].get('org_href')
        org = Org(client, href=org_href)
        role_record = org.get_role_record(role_name)
        role = Role(client, href=role_record.get('href'))
        rights = role.list_rights()
        stdout(rights, ctx)
    except Exception as e:
        stderr(e, ctx)
Example #11
0
def add_right(ctx, role_name, rights, org_name):
    try:
        restore_session(ctx)
        client = ctx.obj['client']
        if org_name is not None:
            org_href = client.get_org_by_name(org_name).get('href')
        else:
            org_href = ctx.obj['profiles'].get('org_href')
        org = Org(client, href=org_href)
        role_record = org.get_role_record(role_name)
        role = Role(client, href=role_record.get('href'))
        role.add_rights(list(rights), org)
        stdout('Rights added successfully to the role \'%s\'' % role_name, ctx)
    except Exception as e:
        stderr(e, ctx)
Example #12
0
def link(ctx, role_name, org_name):
    try:
        restore_session(ctx)
        client = ctx.obj['client']
        if org_name is not None:
            org_href = client.get_org_by_name(org_name).get('href')
        else:
            org_href = ctx.obj['profiles'].get('org_href')
        org = Org(client, href=org_href)
        role_record = org.get_role_record(role_name)
        role = Role(client, href=role_record.get('href'))
        role.link()
        stdout('Role \'%s\' has been successfully linked'
               ' to it\'s template.' % role_name, ctx)
    except Exception as e:
        stderr(e, ctx)
Example #13
0
def link(ctx, role_name, org_name):
    try:
        restore_session(ctx)
        client = ctx.obj['client']
        if org_name is not None:
            org_href = client.get_org_by_name(org_name).get('href')
        else:
            org_href = ctx.obj['profiles'].get('org_href')
        org = Org(client, href=org_href)
        role_record = org.get_role_record(role_name)
        role = Role(client, href=role_record.get('href'))
        role.link()
        stdout('Role \'%s\' has been successfully linked'
               ' to it\'s template.' % role_name, ctx)
    except Exception as e:
        stderr(e, ctx)
Example #14
0
 def create_user(self, user_name, enabled=False):
     logged_in_org = self.client.get_org()
     org = Org(self.client, resource=logged_in_org)
     role = org.get_role_record(self.config['vcd']['role_name'])
     role_href = role.get('href')
     return org.create_user(user_name,
                            "password",
                            role_href,
                            "Full Name",
                            "Description",
                            "*****@*****.**",
                            "408-487-9087",
                            "test_user_im",
                            "*****@*****.**",
                            "Alert Vcd:",
                            is_enabled=enabled)
Example #15
0
def remove_right(ctx, role_name, rights, org_name):
    try:
        restore_session(ctx)
        client = ctx.obj['client']
        if org_name is not None:
            org_href = client.get_org_by_name(org_name).get('href')
        else:
            org_href = ctx.obj['profiles'].get('org_href')
        org = Org(client, href=org_href)
        role_record = org.get_role_record(role_name)
        role = Role(client, href=role_record.get('href'))
        role.remove_rights(list(rights))
        stdout('Removed rights successfully from the role \'%s\'' % role_name,
               ctx)
    except Exception as e:
        stderr(e, ctx)
    def create(self,
               description='',
               full_name='',
               email='',
               telephone='',
               im='',
               alert_email='',
               alert_email_prefix='',
               stored_vm_quota=0,
               deployed_vm_quota=0,
               is_group_role=False,
               is_default_cached=False,
               is_external=False,
               is_alert_enabled=False):
        logging.info("__INIT__create[User]")
        res = user_pb2.CreateUserResult()
        res.created = False

        context = self.context

        logged_in_org = self.client.get_org()
        org = Org(self.client, resource=logged_in_org)
        logging.info("__role_name__ %s org[%s]", self.role_name, org)
        role = org.get_role_record(self.role_name)
        role_href = role.get('href')

        try:
            result = org.create_user(self.name, self.password, role_href,
                                     full_name, description, email, telephone,
                                     im, alert_email, alert_email_prefix,
                                     stored_vm_quota, deployed_vm_quota,
                                     is_group_role, is_default_cached,
                                     is_external, is_alert_enabled,
                                     self.is_enabled)
            res.created = True
        except Exception as e:
            error_message = '__ERROR_create[user] failed for user {0}. __ErrorMessage__ {1}'.format(
                self.name, str(e))
            logging.warn(error_message)
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context.set_details(error_message)
            return res

        logging.info("__DONE__create[User]")
        return res
Example #17
0
    def create(self):
        params = self.params
        username = params.get('username')
        userpassword = params.get('userpassword')
        full_username = params.get('full_username')
        description = params.get('description')
        email = params.get('email')
        telephone = params.get('telephone')
        im = params.get('im')
        alert_email = params.get('alert_email')
        alert_email_prefix = params.get('alert_email_prefix')
        stored_vm_quota = params.get('stored_vm_quota')
        deployed_vm_quota = params.get('deployed_vm_quota')
        is_group_role = params.get('is_group_role')
        is_default_cached = params.get('is_default_cached')
        is_external = params.get('is_external')
        is_alert_enabled = params.get('is_alert_enabled')
        is_enabled = params.get('is_enabled')
        org_name = params.get('org_name', None)
        response = dict()
        response['changed'] = False

        if org_name:
            org_name = Org(self.client, resource=self.client.get_org_by_name(org_name))
        else:
            org_name = self.org
        role = org_name.get_role_record(params.get('role_name'))
        role_href = role.get('href')

        try:
            org_name.get_user(username)
        except EntityNotFoundException:
            org_name.create_user(
                username, userpassword, role_href, full_username, description,
                email, telephone, im, alert_email, alert_email_prefix,
                stored_vm_quota, deployed_vm_quota, is_group_role,
                is_default_cached, is_external, is_alert_enabled,
                is_enabled)
            response['msg'] = "User {} has been created.".format(username)
            response['changed'] = True
        else:
            response['warnings'] = "User {} is already present.".format(username)

        return response
    def create(self,
               description='',
               full_name='',
               email='',
               telephone='',
               im='',
               alert_email='',
               alert_email_prefix='',
               stored_vm_quota=0,
               deployed_vm_quota=0,
               is_group_role=False,
               is_default_cached=False,
               is_external=False,
               is_alert_enabled=False):
        logging.info("__INIT__create[User]")
        res = user_pb2.CreateUserResult()
        res.created = False

        context = self.context

        logged_in_org = self.client.get_org()
        org = Org(self.client, resource=logged_in_org)
        logging.info("__role_name__ %s org[%s]", self.role_name, org)
        role = org.get_role_record(self.role_name)
        role_href = role.get('href')

        try:
            result = org.create_user(
                self.name, self.password, role_href, full_name, description,
                email, telephone, im, alert_email, alert_email_prefix,
                stored_vm_quota, deployed_vm_quota, is_group_role,
                is_default_cached, is_external, is_alert_enabled,
                self.is_enabled)
            res.created = True
        except Exception as e:
            error_message = '__ERROR_create[user] failed for user {0}. __ErrorMessage__ {1}'.format(
                self.name, str(e))
            logging.warn(error_message)
            context.set_code(grpc.StatusCode.INVALID_ARGUMENT)
            context.set_details(error_message)
            return res

        logging.info("__DONE__create[User]")
        return res
Example #19
0
    def test_09_add_rights_to_Role(self):
        org_in_use = self.config['vcd']['org_in_use']
        org = Org(self.client,
                  href=self.client.get_org_by_name(org_in_use).get('href'))
        role_name = self.config['vcd']['role_name']
        right_name = self.config['vcd']['right_name']

        role_record = org.get_role_record(role_name)
        role = Role(self.client, href=role_record.get('href'))

        updated_role_resource = role.add_rights([right_name], org)
        success = False
        if hasattr(updated_role_resource, 'RightReferences') and \
                hasattr(updated_role_resource.RightReferences, 'RightReference'):
            for right in updated_role_resource.RightReferences.RightReference:
                if right.get('name') == right_name:
                    success = True
                    break
        assert success
class User(VcdAnsibleModule):
    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        logged_in_org = self.client.get_org()
        self.org = Org(self.client, resource=logged_in_org)

    def manage_states(self):
        state = self.params.get('state')
        if state == "present":
            return self.create()

        if state == "absent":
            return self.delete()

        if state == "update":
            return self.update()

    def create(self):
        params = self.params
        role = self.org.get_role_record(params.get('role_name'))
        role_href = role.get('href')
        username = params.get('username')
        userpassword = params.get('userpassword')
        full_username = params.get('full_username')
        description = params.get('description')
        email = params.get('email')
        telephone = params.get('telephone')
        im = params.get('im')
        alert_email = params.get('alert_email')
        alert_email_prefix = params.get('alert_email_prefix')
        stored_vm_quota = params.get('stored_vm_quota')
        deployed_vm_quota = params.get('deployed_vm_quota')
        is_group_role = params.get('is_group_role')
        is_default_cached = params.get('is_default_cached')
        is_external = params.get('is_external')
        is_alert_enabled = params.get('is_alert_enabled')
        is_enabled = params.get('is_enabled')
        response = dict()
        response['changed'] = False

        try:
            self.org.get_user(username)
        except EntityNotFoundException:
            self.org.create_user(
                username, userpassword, role_href, full_username, description,
                email, telephone, im, alert_email, alert_email_prefix,
                stored_vm_quota, deployed_vm_quota, is_group_role,
                is_default_cached, is_external, is_alert_enabled,
                is_enabled)
            response['msg'] = "User {} has been created.".format(username)
            response['changed'] = True
        else:
            response['warnings'] = "User {} is already present.".format(username)

        return response

    def delete(self):
        username = self.params.get('username')
        response = dict()
        response['changed'] = False

        try:
            self.org.get_user(username)
        except EntityNotFoundException:
            response['warnings'] = "User {} is not present.".format(username)
        else:
            self.org.delete_user(username)
            response['msg'] = "User {} has been deleted.".format(username)
            response['changed'] = True

        return response

    def update(self):
        username = self.params.get('username')
        enabled = self.params.get('is_enabled')
        response = dict()
        response['changed'] = False

        self.org.get_user(username)
        self.org.update_user(username, enabled)
        response['msg'] = "User {} has been updated".format(username)
        response['changed'] = True

        return response
Example #21
0
 def test_02_get_role(self):
     logged_in_org = self.client.get_org()
     org = Org(self.client, resource=logged_in_org)
     role = org.get_role_record(self.config['vcd']['role_name'])
     assert self.config['vcd']['role_name'] == role.get('name')
Example #22
0
class User(VcdAnsibleModule):
    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        logged_in_org = self.client.get_org()
        self.org = Org(self.client, resource=logged_in_org)

    def manage_states(self):
        state = self.params.get('state')
        if state == "present":
            return self.create()

        if state == "absent":
            return self.delete()

        if state == "update":
            return self.update()

    def create(self):
        params = self.params
        role = self.org.get_role_record(params.get('role_name'))
        role_href = role.get('href')
        username = params.get('username')
        userpassword = params.get('userpassword')
        full_username = params.get('full_username')
        description = params.get('description')
        email = params.get('email')
        telephone = params.get('telephone')
        im = params.get('im')
        alert_email = params.get('alert_email')
        alert_email_prefix = params.get('alert_email_prefix')
        stored_vm_quota = params.get('stored_vm_quota')
        deployed_vm_quota = params.get('deployed_vm_quota')
        is_group_role = params.get('is_group_role')
        is_default_cached = params.get('is_default_cached')
        is_external = params.get('is_external')
        is_alert_enabled = params.get('is_alert_enabled')
        is_enabled = params.get('is_enabled')
        response = dict()
        response['changed'] = False

        try:
            self.org.get_user(username)
        except EntityNotFoundException:
            self.org.create_user(
                username, userpassword, role_href, full_username, description,
                email, telephone, im, alert_email, alert_email_prefix,
                stored_vm_quota, deployed_vm_quota, is_group_role,
                is_default_cached, is_external, is_alert_enabled,
                is_enabled)
            response['msg'] = "User {} has been created.".format(username)
            response['changed'] = True
        else:
            response['msg'] = "User {} is already present.".format(username)

        return response

    def delete(self):
        username = self.params.get('username')
        response = dict()
        response['changed'] = False

        try:
            self.org.get_user(username)
        except EntityNotFoundException:
            response['msg'] = "User {} is not present.".format(username)
        else:
            self.org.delete_user(username)
            response['msg'] = "User {} has been deleted.".format(username)
            response['changed'] = True

        return response

    def update(self):
        username = self.params.get('username')
        enabled = self.params.get('is_enabled')
        response = dict()
        response['changed'] = False

        self.org.get_user(username)
        self.org.update_user(username, enabled)
        response['msg'] = "User {} has been updated".format(username)
        response['changed'] = True

        return response
Example #23
0
class Roles(VcdAnsibleModule):
    def __init__(self, **kwargs):
        super(Roles, self).__init__(**kwargs)
        self.org = Org(self.client, resource=self.client.get_org())

    def manage_states(self):
        state = self.params.get('state')
        if state == 'present':
            return self.create()

        if state == 'absent':
            return self.delete()

        if state == 'update':
            return self.update()

    def manage_operations(self):
        operation = self.params.get('operation')
        if operation == "list_rights":
            return self.list_rights()

        if operation == "list_roles":
            return self.list_roles()

    def create(self):
        role_name = self.params.get('role_name')
        role_description = self.params.get('role_description')
        role_rights = self.params.get('role_rights')
        response = dict()
        response['changed'] = False

        try:
            self.org.get_role_record(role_name)
        except EntityNotFoundException:
            self.org.create_role(role_name, role_description, role_rights)
            response['msg'] = 'Role {} has been created.'.format(role_name)
            response['changed'] = True
        else:
            response['warnings'] = 'Role {} is already present.'.format(
                role_name)

        return response

    def update(self):
        role_name = self.params.get('role_name')
        role_description = self.params.get('role_description')
        role_rights = self.params.get('role_rights')
        response = dict()
        response['changed'] = False

        role = self.org.get_role_record(role_name)
        role_resource = self.org.get_role_resource(role_name)
        role_resource.Description = E.Description(role_description)
        role_rights = tuple() if role_rights is None else role_rights

        for role_right in tuple(role_rights):
            role_right_record = self.org.get_right_record(role_right)
            role_resource.RightReferences.append(
                E.RightReference(name=role_right_record.get('name'),
                                 href=role_right_record.get('href'),
                                 type=EntityType.RIGHT.value))

        self.client.put_resource(role.get('href'), role_resource,
                                 EntityType.ROLE.value)
        response['msg'] = 'Role {} has been updated.'.format(role_name)
        response['changed'] = True

        return response

    def delete(self):
        role_name = self.params.get('role_name')
        response = dict()
        response['changed'] = False

        try:
            self.org.get_role_record(role_name)
            self.org.delete_role(role_name)
            response['msg'] = 'Role {} has been deleted.'.format(role_name)
            response['changed'] = True
        except EntityNotFoundException:
            response['warnings'] = 'Role {} is not present.'.format(role_name)

        return response

    def list_rights(self):
        response = dict()
        response['changed'] = False
        response['msg'] = self.org.list_rights_of_org()

        return response

    def list_roles(self):
        response = dict()
        response['changed'] = False
        response['msg'] = self.org.list_roles()

        return response
Example #24
0
except Exception:
    print("Org does not exist, creating: {0}".format(cfg.org))
    sys_admin_resource = client.get_admin()
    system = System(client, admin_resource=sys_admin_resource)
    admin_org_resource = system.create_org(cfg.org, "Test Org", True)
    org_record = client.get_org_by_name(cfg.org)
    org = Org(client, href=org_record.get('href'))
    print("Org now exists: {0}".format(org.get_name()))

# Ensure user exists on the org.
try:
    user_resource = org.get_user(cfg.user['name'])
    print("User already exists: {0}".format(cfg.user['name']))
except Exception:
    print("User does not exist, creating: {0}".format(cfg.user['name']))
    role_record = org.get_role_record(cfg.user['role'])
    user_resource = org.create_user(user_name=cfg.user['name'],
                                    password=cfg.user['password'],
                                    role_href=role_record.get('href'))
    print("User now exists: {0}".format(user_resource.get('name')))

# Ensure the user is enabled.  We could also do so when creating the user
# but this approach will also fix an existing user who is disabled.
user_dict = to_dict(user_resource)
if user_dict.get('IsEnabled') == 'true':
    print("User is enabled: {0}".format(user_dict.get('name')))
else:
    print("User is not enabled, enabling...")
    org.update_user(user_name=user_dict.get('name'), is_enabled=True)
    print("User is now enabled: {0}".format(user_dict.get('name')))
Example #25
0
except Exception:
    print("Org does not exist, creating: {0}".format(cfg.org))
    sys_admin_resource = client.get_admin()
    system = System(client, admin_resource=sys_admin_resource)
    admin_org_resource = system.create_org(cfg.org, "Test Org", True)
    org_record = client.get_org_by_name(cfg.org)
    org = Org(client, href=org_record.get('href'))
    print("Org now exists: {0}".format(org.get_name()))

# Ensure user exists on the org.
try:
    user_resource = org.get_user(cfg.user['name'])
    print("User already exists: {0}".format(cfg.user['name']))
except Exception:
    print("User does not exist, creating: {0}".format(cfg.user['name']))
    role_record = org.get_role_record(cfg.user['role'])
    user_resource = org.create_user(user_name=cfg.user['name'],
                                    password=cfg.user['password'],
                                    role_href=role_record.get('href'))
    print("User now exists: {0}".format(user_resource.get('name')))

# Ensure the user is enabled.  We could also do so when creating the user
# but this approach will also fix an existing user who is disabled.
user_dict = to_dict(user_resource)
if user_dict.get('IsEnabled') == 'true':
    print("User is enabled: {0}".format(user_dict.get('name')))
else:
    print("User is not enabled, enabling...")
    org.update_user(user_name=user_dict.get('name'), is_enabled=True)
    print("User is now enabled: {0}".format(user_dict.get('name')))