示例#1
0
    def create_users(cls):
        """Creates users for each of the roles in CommonRoles, skips creating
            users which are already present in the org.

        :return: Nothing

        :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 = org.list_users(name_filter=('name', user_name))
            if len(list(user_records)) > 0:
                print('Reusing existing user ' + user_name + '.')
                continue
            role = org.get_role_record(role_enum.value)
            print('Creating user ' + user_name + '.')
            org.create_user(
                user_name=user_name,
                password=cls._config['vcd']['default_org_user_password'],
                role_href=role.get('href'),
                is_enabled=True)
示例#2
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')
示例#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)
示例#4
0
文件: user.py 项目: vmware/vca-cli
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)
示例#5
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)
示例#6
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
示例#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(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)
    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
    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
示例#10
0
 def test_create_user(self):
     logged_in_org = self.client.get_org()
     org = Org(self.client, resource=logged_in_org, is_admin=True)
     role = org.get_role(self.config['vcd']['role_name'])
     role_href = role.get('href')
     user_name = self.config['vcd']['user_name'].join(
         random.sample(string.ascii_lowercase, 8))
     user = org.create_user(user_name,
                            "password",
                            role_href,
                            "Full Name",
                            "Description",
                            "*****@*****.**",
                            "408-487-9087",
                            "test_user_im",
                            "*****@*****.**",
                            "Alert Vcd:",
                            is_enabled=True)
     assert user_name == user.get('name')
示例#11
0
    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')))

# Ensure VDC exists.  If we create it reload the org as it affects
# org resource contents and future calls might fail otherwise.
示例#12
0
    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')))

# Ensure VDC exists.  If we create it reload the org as it affects
# org resource contents and future calls might fail otherwise.
示例#13
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
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