def test_clear_session_basic(self): ''' Basic usage of clear_session ''' switch_mode(user='******', password='******') rest.get('serviceengine') config = get_config() session = rest.get_session() switch_mode(session=session) assert config.sessions and config.session clear_session() config = get_config() context_key = config.get_context_key() assert config.session is None and context_key not in config.sessions
def delete_servers(pool_name, how_many, prefix, cleanup_backend=True): """ :param pool_name: :param how_many: :param prefix: :param cleanup_backend: :return: """ logger.info('delete servers from pool %s' % pool_name) config = infra_utils.get_config() context_key = config.get_context_key() pool = config.site_objs[context_key]['pool'].get(pool_name) for count in range(int(how_many)): handle = '%s%s' % (prefix, count + 1) _delete_server_backend(handle, pool, cleanup_backend) for count in range(int(how_many)): handle = '%s%s' % (prefix, count + 1) _delete_server_model(handle, pool) st, pool_json_ctrl = rest.get('pool', name=pool_name) pool_server_json = [] pool_server_dict = pool.servers for pool_server in pool_server_dict.values(): pool_server_json.append(pool_server.get_json()) pool_json_ctrl['servers'] = pool_server_json rest.put('pool', name=pool_name, data=pool_json_ctrl)
def test_controller_goes_down(self): ''' Test the request on sessions if the controller goes down Does the request on the sessions work when the controller comes back up ''' switch_mode(user='******', password='******') logger.info('Configuring cloud, This may take sometime..') setup_cloud(wait_for_cloud_ready=True) config = get_config() mode = config.get_mode() controller = config.get_vm_of_type('controller')[0] session = rest.get_session() data_1 = rest.get('serviceengine') cloud_obj = config.testbed[mode['site_name']].cloud_obj[mode['cloud']] controller_name = controller.name cloud_obj.powerOffVM(controller_name) try: rest.get('serviceengine') except Exception as e: logger.info('Expected ReadTimeout: ' + str(e)) cloud_obj.powerOnVM(controller_name) wait_until_n_cluster_nodes_ready() data_2 = rest.get('serviceengine') assert data_1 == data_2
def get_prefix_and_mask_of_nw(nw): config = infra_utils.get_config() mode = config.get_mode() site_name = mode['site_name'] ip = config.testbed[site_name].networks_queue.get_ip_for_network(nw) mask = get_mask_for_network(nw) return ip, mask
def block_ips_on_network(network, ip_last_octet_start, ip_last_octet_end): """ Block bunch on IP's on this network as used :param self: :param network: :param ip_last_octet_start: :param ip_last_octet_end: :return: """ config = infra_utils.get_config() mode = config.get_mode() site_name = mode['site_name'] for network_name, network_data in config.testbed[ site_name].networks_json.iteritems(): if network == network_data.get('name'): ip = network_data.get('ip') ip_last_octet = ip.split('.')[-1] if ip_last_octet_start <= ip_last_octet <= ip_last_octet_end: config.testbed[site_name]['networks_json']['ip'] = "" break else: logger_utils.fail( 'Network name not in network address dict of vcenter: %s' % network)
def create_microservice_group(msg_name, ms_name=[], **kwargs): """ :param msg_name: :param ms_name: :param kwargs: :return: """ tenant = infra_utils.get_config().get_mode(key='tenant') msg = { 'name': msg_name, 'tenant_uuid': rest.get_uuid_by_name('tenant', tenant), 'service_uuids': [] } for ms in ms_name: msg['service_uuids'].append(rest.get_uuid_by_name('microservice', ms)) try: rest.post('microservicegroup', name=msg_name, data=msg) # REVIEW why was this tenant_uuid? except Exception as e: if 'Micro service group with this Name and Tenant ref ' \ 'already exists' in str(e): logger.info('microservice group already exists, ignoring error') else: raise return msg_name
def run_commands(cli_instructs, ctlr_vm, **kwargs): config = infra_utils.get_config() cli_exe_path = suite_vars.workspace + ( "/python/bin/cli/bin/shell_client.py --user %s --password %s " % (ctlr_vm.user, ctlr_vm.password)) cli_instructs_path = '/tmp/%s' % uuid.uuid1() with open(cli_instructs_path, 'w') as fh: fh.write(string.join(cli_instructs, '\n')) cli_exec = '%s --address %s' % (cli_exe_path, ctlr_vm.ip) env = {} env['PYTHONPATH'] = suite_vars.workspace + '/python/lib' try: cat_proc = subprocess.Popen(['cat', cli_instructs_path], stdout=subprocess.PIPE, env=env) cli_proc = subprocess.Popen(cli_exec.split(), stdin=cat_proc.stdout, stdout=subprocess.PIPE, env=env) cat_proc.stdout.close() output = cli_proc.communicate()[0] logger.info(output) except OSError: logger_utils.fail('Could not open the shell at %s' % cli_exe_path) except subprocess.CalledProcessError: raise os.system('rm %s' % cli_instructs_path)
def __getitem__(self, cloud_name): if cloud_name not in self.__dict__ or self.__dict__[cloud_name] == None: from avi_objects.infra_utils import get_config config = get_config() mode = config.get_mode() cloud_name = mode.get('cloud') cloud_json = config.testbed[config.get_mode( key='site_name')].get_cloud_json(cloud_name=cloud_name) if cloud_json: cloud_type = cloud_json['vtype'] if cloud_type == 'CLOUD_AWS': configuration = cloud_json['aws_configuration'] self.__dict__[cloud_name] = Aws(configuration, cloud_name) elif cloud_type == 'CLOUD_AZURE': configuration = cloud_json['azure_configuration'] try: configuration['cc_info'] = config.testbed[ config.get_mode(key='site_name')].tb_json[ 'preConfiguration']['CloudConnectorUser'][0] except KeyError: logger.fail( 'CloudConnectorUser information for azure cloud is missing from testbed file' ) self.__dict__[cloud_name] = Azure(configuration, cloud_name) elif cloud_type == 'CLOUD_VCENTER': configuration = cloud_json['vcenter_configuration'] self.__dict__[cloud_name] = Vcenter( configuration, cloud_name) elif cloud_type == 'CLOUD_OPENSTACK': configuration = cloud_json['openstack_configuration'] self.__dict__[cloud_name] = OpenStack( configuration, cloud_name) else: self.__dict__[cloud_name] = Virtualization( cloud_type, cloud_name) else: import avi_objects.rest as rest cloud_type, configuration = rest.get_cloud_type( get_configuration=True) if cloud_type == 'aws': self.__dict__[cloud_name] = Aws(configuration, cloud_name) elif cloud_type == 'gcp': self.__dict__[cloud_name] = Gcp(configuration, cloud_name) elif cloud_type == 'azure': self.__dict__[cloud_name] = Azure(configuration, cloud_name) elif cloud_type == 'vcenter': self.__dict__[cloud_name] = Vcenter( configuration, cloud_name) elif cloud_type == 'baremetal': self.__dict__[cloud_name] = Baremetal( configuration, cloud_name) elif cloud_type == 'openstack': self.__dict__[cloud_name] = OpenStack( configuration, cloud_name) else: self.__dict__[cloud_name] = Virtualization( cloud_type, cloud_name) return self.__dict__[cloud_name]
def vrf_add_default_route(vrf_name, route_id, **kwargs): """ :param vrf_name: :param route_id: :param kwargs: :return: """ nh = kwargs.get('nh', None) network = kwargs.get('network', None) next_hop_last_octet = kwargs.get('next_hop_last_octet', '.1') json_data = vrf_get(vrf_name, **kwargs) json_data['vrf']['static_routes'] = {} json_data['vrf']['static_routes']['rt']['route_id'] = route_id json_data['vrf']['static_routes']['rt']['prefix']['ip_addr']['addr'] = '0.0.0.0' json_data['vrf']['static_routes']['rt']['prefix']['ip_addr']['type'] = 0 json_data['vrf']['static_routes']['rt']['prefix']['mask'] = 0 if nh: json_data['vrf']['static_routes']['rt']['next_hop']['addr'] = nh else: config = infra_utils.get_config() mode = config.get_mode() site_name = mode['site_name'] nw_ip = config.testbed[site_name].networks_json[network]['ip'] default_nh = ".".join(nw_ip.split('.')[0:3]) + next_hop_last_octet json_data['vrf']['static_routes']['rt']['next_hop']['addr'] = default_nh json_data['vrf']['static_routes']['rt']['next_hop']['type'] = 0 rest.put('vrfcontext', data=json_data)
def get_test_subnet_networkprefix(subnet): # Get the network details from vcenter object config = infra_utils.get_config() dvpgnw = config.testbed[config.site_name].networks_json[subnet] nw, mask = dvpgnw['ip'], dvpgnw['mask'] # Convert 10.50.0.0 -> 10.50 # TODO: Implement in clean way subnet = '.'.join([s for s in nw.split('.') if s != '0']) return subnet
def get_network_subnet(testbed_net): config = infra_utils.get_config() mode = config.get_mode() site_name = mode['site_name'] ip = infra_utils.get_ip_for_network(testbed_net) logger.info("##ip =%s" % ip) static_ip = ip.rsplit(".", 1)[0] + ".254" logger.info("##ip addr =%s static ip=%s" % (ip, static_ip)) return ip, static_ip
def network_set_subnet_static_range(net_name, ip_begin_octet, ip_end_octet): import string _, json_data = rest.get('network', name=net_name) json_subnet_found = None if 'configured_subnets' not in json_data: json_data['configured_subnets'] = list() if len(json_data['configured_subnets']) > 0: json_subnet_found = json_data['configured_subnets'][0] ''' for json_subnet in json_data['configured_subnets']: if (json_subnet['prefix']['ip_addr']['addr'] == subnet_ip) and \ (json_subnet['prefix']['mask'] == subnet_mask): json_subnet_found = json_subnet ''' config = infra_utils.get_config() if json_subnet_found: ip = json_data['configured_subnets'][0]['prefix']['ip_addr']['addr'] else: for network_name, network_data in config.testbed[ config.site_name].networks_json.iteritems(): if net_name == network_data.get('name'): ip = network_data.get('ip') break ip_parts = ip.split('.')[:-1] subnet_ip = string.join(ip_parts, '.') + '.0' if not json_subnet_found: json_subnet = dict() json_subnet['prefix'] = { 'mask': 24, 'ip_addr': { 'addr': subnet_ip, 'type': 'V4' } } json_data['configured_subnets'].append(json_subnet) json_subnet_found = json_subnet if 'static_ranges' in json_subnet_found.keys(): del json_subnet_found['static_ranges'] if 'static_ips' in json_subnet_found.keys(): del json_subnet_found['static_ips'] json_subnet_found['static_ranges'] = list() ip_range_begin = string.join(ip_parts, '.') + '.' + str(ip_begin_octet) ip_range_end = string.join(ip_parts, '.') + '.' + str(ip_end_octet) json_subnet_found['static_ranges'].append({ 'begin': { 'addr': ip_range_begin, 'type': 'V4' }, 'end': { 'addr': ip_range_end, 'type': 'V4' } }) rest.put('network', name=net_name, data=json_data)
def test_setup_cluster_vip_delete(self): """Initial setup of cluster vip""" cluster_vip = '10.152.139.105' session = create_session(ip=cluster_vip) get_config().switch_mode(session=session) remove_cluster_vip() get_config().switch_mode(session=None) for i in range(20): try: session = create_session(ip=cluster_vip) rsp = session.get('cluster') logger.info('cluster vip still active, retrying') time.sleep(5) if i==19: fail('cluster vip not removed after 10 tries') continue except Exception as e: print break
def reset_admin_user(username, password, old_password='******', **kwargs): data = json.dumps({ 'username': username, 'password': password, 'old_password': old_password, 'full_name': 'System Administrator', }) from avi_objects.infra_utils import get_config config = get_config() config.switch_mode(password=old_password) status_code, data = put('useraccount', data=data, check_status_code=False)
def test_session_with_each_controller(self, all_sessions): ''' This testcase shows how to maintain sessions to different clusters in the controller and work with them ''' config = get_config() controllers = get_vm_of_type('controller') for controller in controllers: config.switch_mode(session=all_sessions[controller.name]) out, resp_code = get('initial-data') logger.info("switch_mode session "+str(config.get_mode(key='session'))) config.switch_mode(session=None)
def get_network_name_by_alias(net_alias): """ :param net_alias: :return: """ config = infra_utils.get_config() for net, net_data in config.testbed[ config.site_name].networks_json.iteritems(): if net == net_alias: return net_data.get('name') return None
def get_cloud_type(get_configuration=False): from avi_objects.infra_utils import get_config config = get_config() mode = config.get_mode() cloud_name = mode.get('cloud') if not cloud_name: ret_val = (None, None) if get_configuration else None return ret_val _, data = get('cloud', name=cloud_name) vtype = data['vtype'] logger.info('get_cloud_type vtype=%s' % vtype) cloud_type = None configuration = None if vtype == 'CLOUD_NONE': cloud_type = None elif vtype == 'CLOUD_VCENTER': configuration = data['vcenter_configuration'] cloud_type = 'vcenter' elif vtype == 'CLOUD_OPENSTACK': configuration = data['openstack_configuration'] cloud_type = 'openstack' elif vtype == 'CLOUD_AWS': configuration = data['aws_configuration'] cloud_type = 'aws' elif vtype == 'CLOUD_MESOS': cloud_type = 'mesos' elif vtype == 'CLOUD_OSHIFT_K8S': cloud_type = 'openshift' elif vtype == 'CLOUD_AZURE': cloud_type = 'azure' cc_ref = data['azure_configuration']['cloud_credentials_ref'] _, cc_info = get('cloudconnectoruser', uuid=get_uuid_from_ref(cc_ref)) configuration = data['azure_configuration'] configuration['cc_info'] = cc_info elif vtype == 'CLOUD_LINUXSERVER': configuration = data['linuxserver_configuration'] cloud_details = data if 'ipam_provider_ref' in cloud_details: ipam_ref = cloud_details['ipam_provider_ref'] url_parsed = urlparse(ipam_ref) ipam_uuid = url_parsed.path.split('/')[-1] _, ipam_details = get('ipamdnsproviderprofile', uuid=ipam_uuid) if ipam_details['type'] == 'IPAMDNS_TYPE_AZURE': configuration['cc_info'] = ipam_details['azure_profile'] cloud_type = 'azure' elif ipam_details['type'] == 'IPAMDNS_TYPE_GCP': cloud_type = 'gcp' else: cloud_type = 'baremetal' if get_configuration: return cloud_type, configuration else: return cloud_type
def get_network_by_handle(handle): ''' Looks up network from handle In the case that handle is a network will return handle ''' config = infra_utils.get_config() if handle in config.testbed[config.site_name].networks_json: network = config.testbed[config.site_name].networks_json[handle] if not network: raise Exception('ERROR! network for handle %s does not exist' % handle) return network
def switch_session(down_vm=None, controller=None): from avi_objects.infra_utils import get_config config = get_config() context_key = config.get_context_key() if not isinstance(down_vm, list): down_vm = [down_vm] if not controller: for _controller in config.get_vm_of_type("controller"): if _controller not in down_vm: controller = _controller break if not controller: fail("No controllers running to switch to.") config.sessions[context_key] = create_session(controller=controller)
def delete_server(pool_name, server_handle, cleanup_backend=True): """ API Helps to delete server from given pool """ config = infra_utils.get_config() context_key = config.get_context_key() pool = config.site_objs[context_key]['pool'].get(pool_name) _delete_server_backend(server_handle, pool, cleanup_backend) _delete_server_model(server_handle, pool) st, pool_json_ctrl = rest.get('pool', name=pool_name) pool_server_json = [] pool_server_dict = pool.servers for pool_server in pool_server_dict.values(): pool_server_json.append(pool_server.get_json()) pool_json_ctrl['servers'] = pool_server_json rest.put('pool', name=pool_name, data=pool_json_ctrl)
def get_session(): # REVIEW: per discussion with Vivek et al, this should probably also refresh the session # to preemptively avoid session timeouts from avi_objects.infra_utils import get_config config = get_config() mode = config.get_mode() context_key = config.get_context_key() session = mode['session'] if session: return session try: session = config.sessions[context_key] except KeyError: config.sessions[context_key] = create_session() session = config.sessions[context_key] return session
def get_mask_for_network(network): """ :param self: :param network_name: :return: """ config = infra_utils.get_config() mode = config.get_mode() site_name = mode['site_name'] addr_dict = config.testbed[site_name].networks_json for network_name, network_data in addr_dict.iteritems(): if network == network_data.get('name'): return network_data.get('mask') else: logger_utils.fail( "vcenter get_mask_for_network error: not found network name: %s" % network)
def update_admin_user(username, password, old_password='******', **kwargs): data = json.dumps({ 'username': username, 'password': password, 'old_password': old_password, 'full_name': 'System Administrator', }) from avi_objects.infra_utils import get_config config = get_config() config.switch_mode(password=old_password) try: put('useraccount', data=data) except Exception as e: config.switch_mode(password=password) logger.debug('%s/%s failed:%s' % (username, old_password, e)) logger.debug('Assuming credentials are already %s/%s' % (username, password)) config.switch_mode(password=password) context_key = config.get_context_key() config.sessions[context_key] = create_session()
def get_ip_for_last_octet(network, last_octet): """ :param self: :param network: :param last_octet: :return: """ config = infra_utils.get_config() mode = config.get_mode() site_name = mode['site_name'] addr_dict = config.testbed[site_name].networks_json for network_name, network_data in addr_dict.iteritems(): if network == network_data.get('name'): _tmp = (addr_dict[network_name]['ip'].split(".")[:-1]) _tmp.append(str(last_octet)) ip = ".".join(_tmp) return ip else: logger_utils.fail( 'Network name not in network address dict of vcenter: %s' % network)
def vrf_add_ibgp_profile_peers(vrf_name, asnum, nw_names, md5): """ :param vrf_name: :param asnum: :param nw_names: :param md5: :return: """ json_data = vrf_get(vrf_name) if 'bgp_profile' not in json_data: json_data['bgp_profile'] = dict() json_data['bgp_profile']['local_as'] = int(asnum) json_data['bgp_profile']['ibgp'] = True if 'peers' not in json_data['bgp_profile']: json_data['bgp_profile']['peers'] = [] config = infra_utils.get_config() import lib.network_lib as network_lib for nw_name in nw_names: peer = dict() peer['remote_as'] = int(asnum) peer_ip = network_lib.get_ip_for_last_octet(nw_name, '1') peer['peer_ip'] = {'addr': peer_ip, 'type': 'V4'} peer['subnet'] = {'ip_addr': {'addr': peer_ip, 'type': 'V4'}, 'mask': int(network_lib.get_mask_for_network(nw_name))} peer['network_ref'] = '/api/network?name=' + nw_name peer['md5_secret'] = md5 peer['bfd'] = True peer_found = False for peer_data in json_data['bgp_profile']['peers']: if peer_data['peer_ip']['addr'] == peer_ip: print 'Peer %s already configured' % peer_ip peer_found = True break if not peer_found: json_data['bgp_profile']['peers'].append(peer) rest.put('vrfcontext', name=vrf_name, data=json_data)
def test_clear_session_two_users(self, create_new_user): ''' test_clear_session_two_users - Test clear_session with two user sessions - When all_sessions=True is passed, all the existing sessions should be removed ''' switch_mode(user='******', password='******') rest.get('serviceengine') config = get_config() context_key_admin = config.get_context_key() switch_mode(user='******', password='******') rest.get('serviceengine') context_key_test_user = config.get_context_key() assert context_key_test_user in config.sessions and context_key_admin in config.sessions clear_session() assert context_key_test_user not in config.sessions and context_key_admin in config.sessions rest.get('serviceengine') assert context_key_test_user in config.sessions and context_key_admin in config.sessions clear_session(all_sessions=True) assert context_key_test_user not in config.sessions and context_key_admin not in config.sessions clear_session(all_sessions=True)
def create_session(controller=None, ip=None, lazy_authentication=True): from avi_objects.infra_utils import get_config config = get_config() mode = config.get_mode() logger.trace('Creating Session with mode %s' % mode) tenant = mode['tenant'] user = mode['user'] password = mode['password'] if not controller: controller_list = config.get_vm_of_type('controller') controller = controller_list[0] if controller_list else None if not controller or controller.ip == '': logger.debug('No controllers found in testbed') return None controller_ip = controller.ip if ip: controller_ip = ip port = controller.api_port # REVIEW do we use this? api_version = get_api_version() session = avi_api.ApiSession(controller_ip, username=user, tenant=tenant, \ password=password, port=port, api_version=api_version, \ lazy_authentication=lazy_authentication) # session.authenticate_session() #due to AV-33802, AV-33873 return session
def test_clear_session_two_users(self, create_new_user): ''' Test clear_session - By default, clear_session() clears the current mode's session - When all_sessions=True is passed, it should clear all existing sessions ''' switch_mode(user='******', password='******') rest.get('serviceengine') config = get_config() context_key_admin = config.get_context_key() switch_mode(user='******', password='******') se_1 = rest.get('serviceengine') context_key_test_user = config.get_context_key() assert context_key_test_user in config.sessions and context_key_admin in config.sessions clear_session() assert context_key_test_user not in config.sessions and context_key_admin in config.sessions se_2 = rest.get('serviceengine') assert se_1 == se_2 assert context_key_test_user in config.sessions and context_key_admin in config.sessions clear_session(all_sessions=True) assert context_key_test_user not in config.sessions and context_key_admin not in config.sessions clear_session(all_sessions=True)
def vrf_del_ibgp_profile_peers(vrf_name, nw_names): """ :param vrf_name: :param nw_names: :return: """ json_data = vrf_get(vrf_name) if 'bgp_profile' not in json_data: logger.debug('No BGP Profile configured') return if 'peers' not in json_data['bgp_profile']: logger.debug('No BGP Peers configured') return config = infra_utils.get_config() import lib.network_lib as network_lib for nw_name in nw_names: peer_ip = network_lib.get_ip_for_last_octet(nw_name, '1') for i, peer_data in enumerate(json_data['bgp_profile']['peers']): if peer_data['peer_ip']['addr'] == peer_ip: logger.debug('Peer %s found for delete' % peer_ip) json_data['bgp_profile']['peers'].pop(i) break rest.put('vrfcontext', name=vrf_name, data=json_data)
def get_cloud_context(): from avi_objects.infra_utils import get_config config = get_config() mode = config.get_mode() return mode['cloud']