def get_onprem_api_client(args): config = swagger_client.Configuration() config.verify_ssl = False passwd = getpass.getpass('Please enter vRNI password for username ' + args.vrniuser + ": ") logger.info("Getting api client for IP <{}>".format(args.platform_ip)) api_client = swagger_client.ApiClient(host="https://{}/api/ni".format(args.platform_ip)) auth_api = swagger_client.AuthenticationApi(api_client=api_client) if args.domain_type == "LOCAL" or args.domain_type == "LDAP": user_creds = swagger_client.UserCredential(username=args.vrniuser, password=passwd, domain=dict(domain_type=args.domain_type, value=args.domain_value)) auth_token = auth_api.create(user_creds) elif args.domain_type == "VIDM": if args.get_vidm_client_id: client_id = auth_api.get_vidm_oauth_clien_id() logger.info("client-id for vIDM is - '{}'".format(client_id.client_id)) return user_creds = swagger_client.VidmToken(vidm_token=args.vidm_token) auth_token = auth_api.create_vidm_user_token(user_creds) else: raise ValueError('Please give correct domain_type: LOCAL, LDAP or VIDM') config.api_key['Authorization'] = auth_token.token config.api_key_prefix['Authorization'] = 'NetworkInsight' config.api_client = api_client return api_client
def main(api_client, args): # Create data source API client object data_source_api = swagger_client.DataSourcesApi(api_client=api_client) with open("{}".format(args.data_sources_csv), 'rb') as csvFile: data_sources = csv.DictReader(csvFile) for data_source in data_sources: data_source_type = data_source['DataSourceType'] # Get the Proxy ID from Proxy IP if data_source['ProxyIP'] not in proxy_ip_to_id: proxy_id = get_node_entity_id(api_client, data_source['ProxyIP']) if not proxy_id: logger.info("Incorrect Proxy IP {}".format( data_source['ProxyIP'])) continue proxy_ip_to_id[data_source['ProxyIP']] = proxy_id else: proxy_id = proxy_ip_to_id[data_source['ProxyIP']] # Get vCenter ID for vCenter manager required for adding NSX vcenter_id = get_vcenter_manager_entity_id( data_source_api, data_source['ParentvCenter']) logger.info("Adding: <{}> <{}>".format(data_source_type, data_source['IP'])) # Get the Data source add api fn data_source_api_name = get_api_function_name(data_source_type) add_data_source_api_fn = getattr(data_source_api, data_source_api_name['add']) try: response = add_data_source_api_fn(body=get_add_request_body( data_source, proxy_id, vcenter_id)) logger.info("Successfully added: {} {} : Response : {}".format( data_source_type, data_source['IP'], response)) if data_source['snmp_version']: add_snmp_api_fn = getattr( data_source_api, data_source_api_name['snmp_config']) response = add_snmp_api_fn( id=response.entity_id, body=get_snmp_request_body(data_source)) logger.info( "Successfully added: {} {} snmp : Response : {}". format(data_source_type, data_source['IP'], response)) except ApiException as e: logger.exception( "Failed adding data source: {} : Error : {} ".format( data_source['IP'], json.loads(e.body))) auth_api = swagger_client.AuthenticationApi(api_client=api_client) auth_api.delete()
def get_api_client(api_key=None): if not api_key: # kapua-sys's API key api_key = '12345678kapua-password' # create an instance of the API class api_instance = swagger_client.AuthenticationApi() # ApiKeyCredentials | The API KEY authentication credential of a user. body = swagger_client.ApiKeyCredentials(api_key=api_key) # Authenticate an API user api_response = api_instance.authentication_api_key(body) # Set the token id as the Auth token configuration = swagger_client.Configuration() configuration.api_key['Authorization'] = api_response.token_id return swagger_client.ApiClient(configuration)
def main(api_client, args): # Create data source API client object data_source_api = swagger_client.DataSourcesApi(api_client=api_client) proxy_id = get_node_entity_id(api_client, args.proxy_ip) try: response = get_uani_datasource(data_source_api, args.device_ip_or_fqdn) if not response: response = data_source_api.add_generic_switch(body=get_add_request_body(args, proxy_id)) logger.info( "Successfully added: {} {} : Response : {}".format(data_source_type, args.device_ip_or_fqdn, response)) data_source_api.file_upload(id=response.entity_id, file=args.zip_file_path) logger.info( "Successfully uploaded zip file: {}".format(args.zip_file_path)) except ApiException as e: logger.exception( "Failed adding data source: {} : Error : {} ".format(args.device_ip_or_fqdn, json.loads(e.body))) finally: logger.info("Deleting API token") auth_api = swagger_client.AuthenticationApi(api_client=api_client) auth_api.delete()
def delete_token(args, api_client): if args.deployment_type == "onprem": logger.info("Deleting API token") auth_api = swagger_client.AuthenticationApi(api_client=api_client) auth_api.delete()
import time import swagger_client from swagger_client.rest import ApiException from pprint import pprint import json host = "https://127.0.0.1:8888" username = '******' password = '******' delete_list = [ 'test_200', 'test_201', 'test_202', 'test_203', 'test_204', 'test_205' ] #get a token api_instance = swagger_client.AuthenticationApi() api_instance.api_client.configuration.host = host api_instance.api_client.configuration.username = username api_instance.api_client.configuration.password = password api_response = api_instance.api_system_v1_auth_token_post() #print(api_response) #example API call api_instance = swagger_client.NetworkDeviceApi() api_instance.api_client.configuration.api_key[ 'X-Auth-Token'] = api_response.token api_instance.api_client.configuration.host = host api_instance.api_client.configuration.username = username api_instance.api_client.configuration.password = password #print(dir(api_instance))
def delete_token(api_client): logger.info("Deleting API token") auth_api = swagger_client.AuthenticationApi(api_client=api_client) auth_api.delete()