Best Python code snippet using tempest_python
isolated_creds.py
Source:isolated_creds.py
...149 except Exception:150 if router:151 self._clear_isolated_router(router['id'], router['name'])152 if subnet:153 self._clear_isolated_subnet(subnet['id'], subnet['name'])154 if network:155 self._clear_isolated_network(network['id'], network['name'])156 raise157 return network, subnet, router158 def _create_network(self, name, tenant_id):159 resp_body = self.network_admin_client.create_network(160 name=name, tenant_id=tenant_id)161 return resp_body['network']162 def _create_subnet(self, subnet_name, tenant_id, network_id):163 base_cidr = netaddr.IPNetwork(CONF.network.tenant_network_cidr)164 mask_bits = CONF.network.tenant_network_mask_bits165 for subnet_cidr in base_cidr.subnet(mask_bits):166 try:167 if self.network_resources:168 resp_body = self.network_admin_client.\169 create_subnet(170 network_id=network_id, cidr=str(subnet_cidr),171 name=subnet_name,172 tenant_id=tenant_id,173 enable_dhcp=self.network_resources['dhcp'],174 ip_version=4)175 else:176 resp_body = self.network_admin_client.\177 create_subnet(network_id=network_id,178 cidr=str(subnet_cidr),179 name=subnet_name,180 tenant_id=tenant_id,181 ip_version=4)182 break183 except lib_exc.BadRequest as e:184 if 'overlaps with another subnet' not in str(e):185 raise186 else:187 message = 'Available CIDR for subnet creation could not be found'188 raise Exception(message)189 return resp_body['subnet']190 def _create_router(self, router_name, tenant_id):191 external_net_id = dict(192 network_id=CONF.network.public_network_id)193 resp_body = self.network_admin_client.create_router(194 router_name,195 external_gateway_info=external_net_id,196 tenant_id=tenant_id)197 return resp_body['router']198 def _add_router_interface(self, router_id, subnet_id):199 self.network_admin_client.add_router_interface_with_subnet_id(200 router_id, subnet_id)201 def get_primary_network(self):202 return self.isolated_net_resources.get('primary')[0]203 def get_primary_subnet(self):204 return self.isolated_net_resources.get('primary')[1]205 def get_primary_router(self):206 return self.isolated_net_resources.get('primary')[2]207 def get_admin_network(self):208 return self.isolated_net_resources.get('admin')[0]209 def get_admin_subnet(self):210 return self.isolated_net_resources.get('admin')[1]211 def get_admin_router(self):212 return self.isolated_net_resources.get('admin')[2]213 def get_alt_network(self):214 return self.isolated_net_resources.get('alt')[0]215 def get_alt_subnet(self):216 return self.isolated_net_resources.get('alt')[1]217 def get_alt_router(self):218 return self.isolated_net_resources.get('alt')[2]219 def get_credentials(self, credential_type):220 if self.isolated_creds.get(str(credential_type)):221 credentials = self.isolated_creds[str(credential_type)]222 else:223 if credential_type in ['primary', 'alt', 'admin']:224 is_admin = (credential_type == 'admin')225 credentials = self._create_creds(admin=is_admin)226 else:227 credentials = self._create_creds(roles=credential_type)228 self.isolated_creds[str(credential_type)] = credentials229 # Maintained until tests are ported230 LOG.info("Acquired isolated creds:\n credentials: %s"231 % credentials)232 if (CONF.service_available.neutron and233 not CONF.baremetal.driver_enabled):234 network, subnet, router = self._create_network_resources(235 credentials.tenant_id)236 self.isolated_net_resources[str(credential_type)] = (237 network, subnet, router,)238 LOG.info("Created isolated network resources for : \n"239 + " credentials: %s" % credentials)240 return credentials241 def get_primary_creds(self):242 return self.get_credentials('primary')243 def get_admin_creds(self):244 return self.get_credentials('admin')245 def get_alt_creds(self):246 return self.get_credentials('alt')247 def get_creds_by_roles(self, roles, force_new=False):248 roles = list(set(roles))249 # The roles list as a str will become the index as the dict key for250 # the created credentials set in the isolated_creds dict.251 exist_creds = self.isolated_creds.get(str(roles))252 # If force_new flag is True 2 cred sets with the same roles are needed253 # handle this by creating a separate index for old one to store it254 # separately for cleanup255 if exist_creds and force_new:256 new_index = str(roles) + '-' + str(len(self.isolated_creds))257 self.isolated_creds[new_index] = exist_creds258 del self.isolated_creds[str(roles)]259 # Handle isolated neutron resouces if they exist too260 if CONF.service_available.neutron:261 exist_net = self.isolated_net_resources.get(str(roles))262 if exist_net:263 self.isolated_net_resources[new_index] = exist_net264 del self.isolated_net_resources[str(roles)]265 return self.get_credentials(roles)266 def _clear_isolated_router(self, router_id, router_name):267 net_client = self.network_admin_client268 try:269 net_client.delete_router(router_id)270 except lib_exc.NotFound:271 LOG.warn('router with name: %s not found for delete' %272 router_name)273 def _clear_isolated_subnet(self, subnet_id, subnet_name):274 net_client = self.network_admin_client275 try:276 net_client.delete_subnet(subnet_id)277 except lib_exc.NotFound:278 LOG.warn('subnet with name: %s not found for delete' %279 subnet_name)280 def _clear_isolated_network(self, network_id, network_name):281 net_client = self.network_admin_client282 try:283 net_client.delete_network(network_id)284 except lib_exc.NotFound:285 LOG.warn('network with name: %s not found for delete' %286 network_name)287 def _cleanup_default_secgroup(self, tenant):288 net_client = self.network_admin_client289 resp_body = net_client.list_security_groups(tenant_id=tenant,290 name="default")291 secgroups_to_delete = resp_body['security_groups']292 for secgroup in secgroups_to_delete:293 try:294 net_client.delete_security_group(secgroup['id'])295 except lib_exc.NotFound:296 LOG.warn('Security group %s, id %s not found for clean-up' %297 (secgroup['name'], secgroup['id']))298 def _clear_isolated_net_resources(self):299 net_client = self.network_admin_client300 for cred in self.isolated_net_resources:301 network, subnet, router = self.isolated_net_resources.get(cred)302 LOG.debug("Clearing network: %(network)s, "303 "subnet: %(subnet)s, router: %(router)s",304 {'network': network, 'subnet': subnet, 'router': router})305 if (not self.network_resources or306 self.network_resources.get('router')):307 try:308 net_client.remove_router_interface_with_subnet_id(309 router['id'], subnet['id'])310 except lib_exc.NotFound:311 LOG.warn('router with name: %s not found for delete' %312 router['name'])313 self._clear_isolated_router(router['id'], router['name'])314 if (not self.network_resources or315 self.network_resources.get('subnet')):316 self._clear_isolated_subnet(subnet['id'], subnet['name'])317 if (not self.network_resources or318 self.network_resources.get('network')):319 self._clear_isolated_network(network['id'], network['name'])320 self.isolated_net_resources = {}321 def clear_isolated_creds(self):322 if not self.isolated_creds:323 return324 self._clear_isolated_net_resources()325 for creds in self.isolated_creds.itervalues():326 try:327 self._delete_user(creds.user_id)328 except lib_exc.NotFound:329 LOG.warn("user with name: %s not found for delete" %330 creds.username)...
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!!