Best Python code snippet using tempest_python
cred_client.py
Source:cred_client.py
...40 def create_project(self, name, description):41 pass42 def _check_role_exists(self, role_name):43 try:44 roles = self._list_roles()45 role = next(r for r in roles if r['name'] == role_name)46 except StopIteration:47 return None48 return role49 def create_user_role(self, role_name):50 if not self._check_role_exists(role_name):51 self.identity_client.create_role(role_name)52 def assign_user_role(self, user, project, role_name):53 role = self._check_role_exists(role_name)54 if not role:55 msg = 'No "%s" role found' % role_name56 raise lib_exc.NotFound(msg)57 try:58 self.identity_client.assign_user_role(project['id'], user['id'],59 role['id'])60 except lib_exc.Conflict:61 LOG.debug("Role %s already assigned on project %s for user %s" % (62 role['id'], project['id'], user['id']))63 @abc.abstractmethod64 def get_credentials(self, user, project, password):65 pass66 def delete_user(self, user_id):67 self.identity_client.delete_user(user_id)68 def _list_roles(self):69 roles = self.identity_client.list_roles()['roles']70 return roles71class V2CredsClient(CredsClient):72 def create_project(self, name, description):73 tenant = self.identity_client.create_tenant(74 name=name, description=description)['tenant']75 return tenant76 def get_credentials(self, user, project, password):77 return cred_provider.get_credentials(78 identity_version='v2',79 username=user['name'], user_id=user['id'],80 tenant_name=project['name'], tenant_id=project['id'],81 password=password)82 def delete_project(self, project_id):83 self.identity_client.delete_tenant(project_id)84class V3CredsClient(CredsClient):85 def __init__(self, identity_client, domain_name):86 super(V3CredsClient, self).__init__(identity_client)87 try:88 # Domain names must be unique, in any case a list is returned,89 # selecting the first (and only) element90 self.creds_domain = self.identity_client.list_domains(91 params={'name': domain_name})['domains'][0]92 except lib_exc.NotFound:93 # TODO(andrea) we could probably create the domain on the fly94 msg = "Configured domain %s could not be found" % domain_name95 raise exceptions.InvalidConfiguration(msg)96 def create_project(self, name, description):97 project = self.identity_client.create_project(98 name=name, description=description,99 domain_id=self.creds_domain['id'])['project']100 return project101 def get_credentials(self, user, project, password):102 return cred_provider.get_credentials(103 identity_version='v3',104 username=user['name'], user_id=user['id'],105 project_name=project['name'], project_id=project['id'],106 password=password,107 project_domain_name=self.creds_domain['name'])108 def delete_project(self, project_id):109 self.identity_client.delete_project(project_id)110 def _list_roles(self):111 roles = self.identity_client.list_roles()['roles']112 return roles113def get_creds_client(identity_client, project_domain_name=None):114 if isinstance(identity_client, v2_identity.IdentityClient):115 return V2CredsClient(identity_client)116 else:...
sensu.py
Source:sensu.py
...34 """35 """36 ret = []37 if role is None:38 roles = _list_roles()39 else:40 roles = [role]41 for role in roles:42 path = '/etc/sensu/conf.d/checks-' + role + '.json'43 if role in _list_roles() \44 and not __salt__['file.file_exists'](path):45 __salt__['state.sls'](role)46 __salt__['state.sls'](role + '.relate-sensu-api',47 pillar={'test': True})48 ret += _list_values(key='checks', pattern='checks-' + role)49 return ret50def list_clients():51 """52 """53 return _list_values(key='clients')54def list_commands(55 role=None,56 handler='default'):57 """58 """59 ret = []60 for (name, val) in list_checks(role=role):61 handlers = val.get('handlers', [])62 if handler in handlers:63 command = val.get('command', '').split()64 if command[0].split('.')[-1] == 'rb':65 command = [u'/opt/sensu/embedded/bin/ruby'] + command66 ret += [(name, command)]67 return ret68def list_handlers():69 """70 """71 return _list_values(key='handlers')72def list_mutators():73 """74 """75 return _list_values(key='mutators')76def _list_values(77 key=None,78 pattern='*'):79 """80 """81 ret = []82 paths = glob('/etc/sensu/conf.d/' + pattern + '.json')83 for path in paths:84 with open(path) as json:85 val = load(json).get(key)86 if isinstance(val, dict):87 ret += list(val.items())88 else:89 ret += [val] if val is not None else []90 return ret91def _list_roles():92 """93 """94 return set([r for r in list(__salt__['config.get']('roles')) +...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!