def network_create(): """Create a network based on environments variables.""" nft.clear_log() token = nft.get_token(environ.get('ENVIRONMENT'), environ.get('SMOKE_TEST_USER'), environ.get('SMOKE_TEST_PASS')) nfn_url = nfn.create_network(environ.get('ENVIRONMENT'), environ.get('NFN_NAME'), token) print(nfn_url)
def delete_avw_site(): """Delete AVW gateway site.""" # get session token token = nftn.get_token(os.environ.get('ENVIRONMENT'), os.environ.get('SMOKE_TEST_USER'), os.environ.get('SMOKE_TEST_PASS')) # get network url nfn_url = nfnk.find_network(os.environ.get('ENVIRONMENT'), os.environ.get('NFN_NAME'), token) # get AVW Site url of the first one, the assumption is that there is only one. avwSite = nfreq.nf_req(nfn_url + '/virtualWanSites', "get", token)['_embedded']['azureVirtualWanSites'][0] # Disconnect the VPN site under test from the Azure VPN Gateway vpn_site_connection_deletion(avwSite['name']) # Delete the site from the NF Network data = nfreq.nf_req(avwSite['_links']['self']['href'], "delete", token) return data
def create_avw_site(filename): """Create AVW gateway site.""" # environment used env = os.environ.get('ENVIRONMENT') # clear logoutput file nftn.clear_log() # get resources to configure from file try: with open(filename, 'r') as f: config = yaml.load(f, Loader=yaml.FullLoader) except Exception as e: writelog(str(e)) # get session token token = nftn.get_token(env, os.environ.get('SMOKE_TEST_USER'), os.environ.get('SMOKE_TEST_PASS')) # find configuration detali for avw gateway from file for gateway in config['gateway_list']: if gateway['cloud'] == 'vwan': loc = gateway['region'] gwName = gateway['names'][0] # url for NF Datacenters details url = 'https://gateway.' + env + '.netfoundry.io/rest/v1/dataCenters' # find dc id based on location code datacenters = nfreq.nf_req(url, "get", token)['_embedded']['dataCenters'] dcId = None for dc in datacenters: if dc['locationCode'] == loc: dcId = dc['_links']['self']['href'].split('/')[6] # get network url nfn_url = nfnk.find_network(env, os.environ.get('NFN_NAME'), token) # find gateway Id for avwsite gateway gwId = nfgw.find_gateway(nfn_url, gwName, token).split('/')[8] # build Azure Subscriptions Url for a given NF Enviroment API azureSubscriptionsURL = 'https://gateway.' + env + '.netfoundry.io/rest/v1/azureSubscriptions' # get Azure Subscriptions url of the first one, the assumption is that there is only one. print(nfreq.nf_req(azureSubscriptionsURL, "get", token)) print('--------------------------------------------') try: avwSiteUrl = nfreq.nf_req( azureSubscriptionsURL, "get", token)['_embedded']['azureSubscriptions'][0]['_links']['self'][ 'href'] + '/virtualWanSites' except KeyError as kerr: print(kerr.args) if kerr.args[0] == '_embedded': data = nfreq.nf_req( (azureSubscriptionsURL, { "name": "AVW Packet Test", "subscriptionId": os.environ.get('ARM_SUBSCRIPTION_ID'), "tenantId": os.environ.get('ARM_TENANT_ID'), "applicationId": os.environ.get('ARM_CLIENT_ID'), "applicationKey": os.environ.get('ARM_CLIENT_SECRET') }), "post", token) avwSiteUrl = data['_links']['self']['href'] + '/virtualWanSites' except TypeError as terr: print(terr.args) sys.exit(1) print(avwSiteUrl) # create avw vpn site azureVirtualWanId = "/subscriptions/" + os.environ.get('ARM_SUBSCRIPTION_ID') + "/resourceGroups/"\ + os.environ.get('GROUP_NAME') + "/providers/Microsoft.Network/virtualWans/"\ + os.environ.get('VWAN_NAME') print(azureVirtualWanId) createData = nfreq.nf_req( (avwSiteUrl, { "name": gwName, "endpointId": gwId, "azureResourceGroupName": os.environ.get('GROUP_NAME'), "azureVirtualWanId": azureVirtualWanId, "publicIpAddress": os.environ.get('AVW_SITE_PUBLIC_IP'), "dataCenterId": dcId, "bgp": { "localPeeringAddress": { "ipAddress": os.environ.get('AVW_SITE_PRIVATE_IP'), "asn": 65000 }, "bgpPeerWeight": 0, "deviceLinkSpeed": 0, "deviceVendor": None, "deviceModel": None, "neighborPeers": [{ "ipAddress": os.environ.get('AVW_SITE_PEER_PRIVATE_IP'), "asn": 65001 }], "advertiseLocal": True, "advertisedPrefixes": [] } }), "post", token) print(createData) url = createData['_links']['self']['href'] + "/deploy" deployData = nfreq.nf_req((url, {}), "put", token) # Connect the newly created site to the Azure VPN Gateway vpn_site_connection_creation(gwName) # keep checking until status changes to 'Connected' x = 0 while True: status = vpn_site_connection_get(gwName) if status.connection_status == 'Connected': print(status.connection_status) break else: time.sleep(60) if status.connection_status: print(status.connection_status + ", %s min passed" % x) else: print("None, %s min passed" % x) x = x + 1 if x == 60: break return createData, deployData, status
def main(filename): """Create NFN Resources in MOP Environment.""" # when processing string from POPEN need to strip escape characters ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]') clear_log() # get resources to configure from file try: with open(filename, 'r') as f: config = yaml.load(f, Loader=yaml.FullLoader) except Exception as e: writelog(str(e)) # deploy network if not already completed netName = config['network_name'] env = config['environment'] netAction = config['network_action'] # manage network (only one network) if netAction == 'get': # get a session token from Mop Environment that is used for this if os.environ.get('CLIENT_ID') and os.environ.get('CLIENT_SECRET'): token = nftn.get_token(env, os.environ.get('CLIENT_ID'), os.environ.get('CLIENT_SECRET')) else: token = nftn.get_token(env) writelog('Searching for network id') netUrl = nfnk.find_network(env, netName, token) if netUrl: writelog('Network Url found: %s' % netUrl) else: writelog('Network Url not found for the network "%s"' % netName) writelog('Create one if not already done so') sys.exit(1) elif netAction == 'create': # get a session token from Mop Environment that is used for this if os.environ.get('CLIENT_ID') and os.environ.get('CLIENT_SECRET'): token = nftn.get_token(env, os.environ.get('CLIENT_ID'), os.environ.get('CLIENT_SECRET')) else: token = nftn.get_token(env) netUrl = nfnk.create_network(env, netName, token) elif netAction == 'delete': # get a session token from Mop Environment that is used for this if os.environ.get('CLIENT_ID') and os.environ.get('CLIENT_SECRET'): token = nftn.get_token(env, os.environ.get('CLIENT_ID'), os.environ.get('CLIENT_SECRET')) else: token = nftn.get_token(env) netUrl = nfnk.find_network(env, netName, token) nfnk.delete_network(netUrl, token) # manage gateways (list of gateways) if config.get('gateway_list'): # if gateway options are enabled, the following code will be run if config['gateway_list']: if netAction != 'delete': for gateway in config['gateway_list']: if gateway['action'] == 'create': index = 0 while index < gateway['count']: if gateway['names']: name = gateway['names'][index] name, regkey = nfgw.create_gateway( env, netUrl, gateway['region'], gateway['cloud'], index, token, gwName=name) else: name, regkey = nfgw.create_gateway( env, netUrl, gateway['region'], gateway['cloud'], index, token) gateway['names'] = gateway['names'] + [name] index += 1 gateway['regkeys'] = gateway['regkeys'] + [regkey] if gateway['action'] == 'delete' and gateway['names']: delete_gateways(netUrl, gateway, token) if list( filter( lambda gateway: gateway['action'] == 'create' or gateway['action'] == 'create-terraform', config['gateway_list'])): # update config file update_config_file(filename, config) # create template for terraform nftmf.create_file(config) command = "terraform init -no-color %s" % \ os.path.expanduser(config['terraform']['work_dir']) terraform_command(command) command = "terraform workspace new -state=%s %s" % \ (os.path.expanduser(config['terraform']['work_dir']), env) sout, serr = terraform_command(command) newSerr = ansi_escape.sub('', serr).rstrip().lower().replace( '\"', '') if newSerr == ('workspace %s already exists' % env): command = "terraform workspace select %s" % env terraform_command(command) command = "terraform apply --auto-approve %s" % \ os.path.expanduser(config['terraform']['work_dir']) terraform_command(command) if list( filter( lambda gateway: gateway['action'] == 'delete' or gateway['action'] == 'delete-terraform', config['gateway_list'])): # update config file update_config_file(filename, config) command = "terraform init -no-color %s" % \ os.path.expanduser(config['terraform']['work_dir']) terraform_command(command) command = "terraform workspace select %s" % env terraform_command(command) command = "terraform destroy --auto-approve %s" % \ os.path.expanduser(config['terraform']['work_dir']) terraform_command(command) # manage deployment of gateways with terraform if config.get('terraform'): # if options for terraform are configured, execute the following conditional statements if config['terraform']['output'] == "yes": command = "terraform init -no-color %s" % \ os.path.expanduser(config['terraform']['work_dir']) terraform_command(command) command = "terraform workspace select %s" % env terraform_command(command) # command = "terraform output -state=%s" % \ # os.path.expanduser(config['terraform']['work_dir']) command = "terraform output -json" outs, errs = terraform_command(command) print(outs) # configure service(s) if config.get('services'): # if gateway options are enabled, the service(s) will be able # to be created and assigned to them if config['gateway_list'] and netAction != 'delete': for service in config['services']: writelog('Searching for Gateway Url') gwUrl = nfgw.find_gateway(netUrl, service['gateway'], token) if service['action'] == 'create': serviceUrl, serviceName = nfsrv.create_service( netUrl, gwUrl, service, token) service['name'] = serviceName if service['action'] == 'delete' and service['name']: serviceUrl = nfsrv.find_service(netUrl, service['name'], token) nfsrv.delete_service(serviceUrl, token) service['name'] = None # update config file update_config_file(filename, config) # configure appwan(s) if config.get('appwans'): # if services options are enabled, the appwan(s) will be able # to be created and assigned to them if config['services'] and netAction != 'delete': for appwan in config['appwans']: if appwan['name']: writelog('Searching for appwan') appwanUrl = nfaw.find_appwan(netUrl, appwan['name'], token) if appwanUrl is None: appwanUrl = nfaw.create_appwan(netUrl, appwan['name'], token) gwUrls = [] serviceUrls = [] writelog('Searching for Gateway Urls') for endpoint in appwan['endpoints']: gwUrls = gwUrls + [ nfgw.find_gateway(netUrl, endpoint, token) ] writelog('Searching for Service Urls') for service in appwan['services']: serviceUrls = serviceUrls + [ nfsrv.find_service(netUrl, service, token) ] items = [] if appwan['action'] == 'create': writelog('Adding endpoints and services to appwan') if gwUrls is not None: items = items + gwUrls if serviceUrls is not None: items = items + serviceUrls for item in items: if item: nfaw.add_item2appwan(appwanUrl, item, token) else: writelog( 'Item will not be added to Appwan, not found' ) if appwan['action'] == 'delete' and appwan['name']: writelog('Deleting appwan') nfaw.delete_appwan(appwanUrl, token) appwan['name'] = None else: writelog('appwan name is missing') # update config file update_config_file(filename, config)
def main(filename, action): """Manage creating and deploying to cloud NFN Cloud Gateways.""" # when processing string from POPEN need to strip escape characters ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]') # clear logoutput file nftn.clear_log() # get resources to configure from file try: with open(filename, 'r') as f: config = yaml.load(f, Loader=yaml.FullLoader) except Exception as e: writelog(str(e)) # initialized env variable with the passed Environment Variable env = environ.get('ENVIRONMENT') if action == 'create' or action == 'delete' or action == 'add': # get network url token = nftn.get_token(env, environ.get('SMOKE_TEST_USER'), environ.get('SMOKE_TEST_PASS')) writelog('Searching for network id') netUrl = nfnk.find_network(env, environ.get('NFN_NAME'), token) print(netUrl) # manage gateways (list of gateways) for gateway in config['gateway_list']: # need to add this be comparable with script using the yaml file as input for action if action == 'create-terraform': gateway['action'] = 'create-terraform' if action == 'delete-terraform': gateway['action'] = 'delete-terraform' if action == 'add-terraform': gateway['action'] = 'add-terraform' if action == 'create': gateway['action'] = 'create' index = 0 while index < gateway['count']: name, regkey = nfgw.create_gateway(env, netUrl, gateway['region'], gateway['cloud'], index, token) index += 1 gateway['names'] = gateway['names'] + [name] gateway['regkeys'] = gateway['regkeys'] + [regkey] if action == 'delete' and gateway['names']: gateway['action'] = 'delete' for name in gateway['names']: gateway_delete_update(netUrl, name, token, gateway['action']) gateway['names'] = [] gateway['regkeys'] = [] if action == 'add' and gateway['names']: gateway['action'] = 'add' for name in gateway['names']: regkey = gateway_delete_update(netUrl, name, token, gateway['action'], region=gateway['region'], cloud=gateway['cloud'], index=gateway['count'], env=env) gateway['regkeys'] = gateway['regkeys'] + [regkey] # update config file update_config_file(filename, config) if action == 'create-terraform': # create template for terraform nftmf.create_file(config) command = "terraform init -no-color %s" % path.expanduser( config['terraform']['work_dir']) terraform_command(command) command = "terraform workspace new -state=%s %s" % \ (path.expanduser(config['terraform']['work_dir']), env) sout, serr = terraform_command(command) newSerr = ansi_escape.sub('', serr).rstrip().lower().replace('\"', '') if newSerr == ('workspace %s already exists' % env): command = "terraform workspace select %s" % env terraform_command(command) # command = "terraform apply --auto-approve %s" % \ # os.path.expanduser(config['terraform']['work_dir']) # terraform_command(command) if action == 'delete-terraform': # update config file update_config_file(filename, config) command = "terraform init -no-color %s" % path.expanduser( config['terraform']['work_dir']) terraform_command(command) command = "terraform workspace select %s" % env terraform_command(command) # command = "terraform destroy --auto-approve %s" % # path.expanduser(config['terraform']['work_dir']) # terraform_command(command) if action == 'add-terraform': # create template for terraform nftmf.add_to_file(config) command = "terraform init -no-color %s" % path.expanduser( config['terraform']['work_dir']) terraform_command(command) command = "terraform workspace new -state=%s %s" %\ (path.expanduser(config['terraform']['work_dir']), env) sout, serr = terraform_command(command) newSerr = ansi_escape.sub('', serr).rstrip().lower().replace('\"', '') if newSerr == ('workspace %s already exists' % env): command = "terraform workspace select %s" % env terraform_command(command) # command = "terraform apply --auto-approve %s" % \ # path.expanduser(config['terraform']['work_dir']) # terraform_command(command) # manage deployment of gateways with terraform if config.get('terraform'): # if options for terraform are configured, execute the following conditional statements if config['terraform']['output'] == "yes": command = "terraform init -no-color %s" % \ path.expanduser(config['terraform']['work_dir']) terraform_command(command) command = "terraform workspace select %s" % env terraform_command(command) # command = "terraform output -state=%s" % \ # path.expanduser(config['terraform']['work_dir']) command = "terraform output -json" outs, errs = terraform_command(command) print(outs)