def create_gateway(env, netUrl, loc, type, index, token, **kargs): """Create NF Gateway in MOP Environment.""" # Create AWS GW(s) url = 'https://gateway.' + env + '.netfoundry.io/rest/v1/dataCenters' # find dc id based on location code datacenters = nfreq(url, "get", token)['_embedded']['dataCenters'] dcId = None for dc in datacenters: if dc['locationCode'] == loc: dcId = dc['_links']['self']['href'].split('/')[6] gwUrl = netUrl + '/endpoints' if type == 'aws': gwType = 'AWSCPEGW' if type == 'azure': gwType = 'AZCPEGW' if type == 'vwan': gwType = 'AVWGW' # checking if gateway name was passed as one of **kargs try: gwName = kargs['gwName'] except KeyError: gwName = gwType + 'x' + str(index) + 'x' + loc.upper() new_gw = nfreq((gwUrl, { "name": gwName, "endpointType": gwType, "geoRegionId": None, "dataCenterId": dcId, "o365BreakoutNextHopIp": None }), "post", token) try: gwName = new_gw['name'] except TypeError as terr: print(terr.args) logging.debug(traceback.format_exc()) sys.exit(1) gwRegKey = new_gw['registrationKey'] gwUrl = new_gw['_links']['self']['href'] gwstatus = False writelog('\nWaiting for GW to be ready for service assignment!\n') while not gwstatus: try: result = nfreq(gwUrl, "get", token) if result['status'] == 300: writelog('\ngw is ready to assign service!\n') gwstatus = True except Exception as e: writelog(e) writelog('\nError checking GW status!\n') time.sleep(5) return gwName, gwRegKey
def create_network(env, name, token): """Create NF Resources in MOP Environment.""" # Create new Network specified by --name commandline argument net_name = name writelog('\nCreating new Network! \n') url = 'https://gateway.' + env + '.netfoundry.io/rest/v1/networks' start = datetime.now() network_id = nfreq((url, {"name": net_name}), "post", token) # "productFamily": "Ziti_Enabled", "productVersion" : "4.19.0-57168534" end = datetime.now() diff = end - start create_time = diff.seconds + diff.microseconds / (1000 * 1000.0) writelog("response_time = " + str(create_time)) try: netUrl = network_id['_links']['self']['href'] except Exception as e: writelog(e) writelog('\nnetwork not created!\n' + str(network_id)) sys.exit(0) start = datetime.now() try: net_status = False writelog('\nWaiting for network \"' + network_id['name'] + '\" to be ready @ 10 to 20 minutes! \n') while not net_status: now = datetime.now() delta = now - start minutes = delta.seconds / 60.0 result = nfreq(netUrl, "get", token) if result['status'] == 200: status = 'NOT READY' writelog('\nnetwork build status = ' + status + ' time waiting = ' + str(minutes) + ' Minutes') if result['status'] == 300: writelog('\nnetwork build status = FINISHED, time waited = ' + str(minutes) + 'Minutes') net_status = True if minutes > 30: writelog('\nExcesive time waitng for Network to transition!\n') writelog('deleting network: ' + url) nfreq(url, "delete", token) sys.exit(0) time.sleep(20) except Exception as e: writelog(e) writelog('\nError Unable to check network status!\n') sys.exit(0) return netUrl
def check_for_status(urlRestEndpoint, token): """ Check if NFN Resource status in MOP Environment. Check for status on the resource creation :param urlRestEndpoint: REST Url endpoint for resource :param token: session token for NF Console :return returndata: pass the response back from the status check """ itemStatus = False try: count = 0 while not itemStatus: if count > 6: writelog('Timed out waiting for a status change ' + urlRestEndpoint.split('/')[8] + '!') break returnData = nfreq(urlRestEndpoint, "get", token) if returnData['status'] == 300: writelog('item ' + urlRestEndpoint.split('/')[8] + ' is ready!') break else: time.sleep(10) count += 1 except Exception as e: writelog(e) return returnData['status']
def find_gateway(netUrl, name, token): """Find NF Gateway in MOP Environment.""" gwsUrl = netUrl + '/endpoints' try: gateways = nfreq(gwsUrl, "get", token)['_embedded']['endpoints'] gwUrl = '' for gateway in gateways: if gateway['name'] == name: gwUrl = gateway['_links']['self']['href'] return gwUrl return None except KeyError: return None
def delete_service(serviceUrl, token): """ Delete NFN Service in MOP Environment. :param serviceUrl: REST endpoint for the service marked for deletion :param token: seesion token for NF Console :return none: """ try: data = nfreq(serviceUrl, "delete", token) writelog(data) except Exception as e: writelog(e)
def find_network(env, name, token): """Find NF Network in MOP Environment.""" url = 'https://gateway.' + env + '.netfoundry.io/rest/v1/networks' networks = nfreq(url, "get", token)['_embedded']['networks'] writelog('\nNetworks found are\n') writelog(networks) for network in networks: writelog('\nNetwork name searched is ' + network['name']) writelog('\nNetwork name given is ' + name) if network['name'] == name: net_url = (network['_links']['self']['href']) return net_url writelog('network not found!') return None
def delete_appwan(appwanUrl, token): """ Delete NFN Appwan in MOP Environment. :param appwanUrl: REST endpoint for the appwan marked for deletion :param token: seesion token for NF Console :return none: """ try: # Check if item is ready checkStatus = check_for_status(appwanUrl, token) if checkStatus == 300: data = nfreq(appwanUrl, "delete", token) writelog(data) except Exception as e: writelog(e)
def find_service(netUrl, serviceName, token): """ Find NFN Service in MOP Environment. :param netUrl: REST Url endpoint for network :param serviceName: service name :param token: seesion token for NF Console :return serviceUrl: url of the found service """ servicesUrl = netUrl + '/services' services = nfreq(servicesUrl, "get", token)['_embedded']['services'] for service in services: if service['name'] == serviceName: serviceUrl = service['_links']['self']['href'] return serviceUrl return None
def get_token(env, client_id=None, client_secret=None): """Get the session token.""" if (not client_id) and (not client_secret): config = ConfigParser() config.sections() config.read(path.expanduser('~/.env')) client_id = config[env]['clientId'] client_secret = config[env]['clientSecret'] data = { "client_id": client_id, "client_secret": client_secret, "audience": "https://gateway." + env + ".netfoundry.io/", "grant_type": "client_credentials" } url = 'https://netfoundry-' + env + '.auth0.com/oauth/token' token = nfreq((url, data), 'post') return token['access_token']
def find_appwan(netUrl, appwanName, token): """ Find NFN Appwan in MOP Environment. :param netUrl: REST Url endpoint for network :param appwanName: appwan name :param token: seesion token for NF Console :return appwanUrl: url of the found appwan """ appwansUrl = netUrl + '/appWans' appwans = nfreq(appwansUrl, "get", token) if appwans.get('_embedded'): for appwan in appwans['_embedded']['appWans']: if appwan['name'] == appwanName: appwanUrl = appwan['_links']['self']['href'] else: appwanUrl = None return appwanUrl
def create_appwan(netUrl, appwanName, token): """ Create NFN Appwan in MOP Environment. :param netUrl: REST Url endpoint for network :param appwanName: appwan name :param token: seesion token for NF Console :return appwanUrl: url of the created appwan """ url = netUrl + '/appWans' try: returnData = nfreq((url, {"name": appwanName}), "post", token) appwanUrl = returnData['_links']['self']['href'] time.sleep(1) except Exception as e: writelog(e) writelog('Print error creating appwan for!') appwanUrl = "" return appwanUrl
def add_item2appwan(appwanUrl, itemUrl, token): """ Add items to NFN Appwan in MOP Environment. Add items to existing appwan if not create one :param env: enviroment, e.g. production, sandbox :param appwanUrl: REST Url endpoint for the appwan under build :param itemUrl: REST Url endpoint for item being added to appwan :return none: """ # Check if item is ready checkStatus = check_for_status(appwanUrl, token) # add item to appwan, e.g. gateway, client or service try: if checkStatus == 300: returnData = nfreq((appwanUrl + '/' + itemUrl.split('/')[7], { 'ids': [itemUrl.split('/')[8]] }), "post", token) writelog(returnData) except Exception as e: writelog(e)
def delete_gateway(gwUrl, token): """Delete NF Gateway in MOP Environment.""" data = nfreq(gwUrl, "delete", token) writelog(data)
def delete_network(netUrl, token): """Delete NF Network in MOP Environment.""" data = nfreq(netUrl, "delete", token) writelog(data)
def create_service(netUrl, gwUrl, attributes, token): """ Create NFN Service in MOP Environment. :param netUrl: REST Url endpoint for network :param gwUrl: REST Url endpoint for gateway :param serviceAttributes: service paramaters, e.g. service type or name, etc :param token: seesion token for NF Console :return serviceId, serviceUrl: created service details """ url = netUrl + '/services' gwId = gwUrl.split('/')[8] if attributes['type'] == 'host': # checking if service name is provided if attributes['name']: serviceName = attributes['name'] else: serviceName = attributes['gateway'] + '--' + str( attributes['ip']) + '--' + str(attributes['port']) data = { "serviceClass": "CS", "name": serviceName, "serviceInterceptType": "IP", "serviceType": "ALL", "endpointId": gwId, "pbrType": "WAN", "dataInterleaving": "NO", "transparency": "NO", "networkIp": attributes['ip'], "networkFirstPort": attributes['port'], "networkLastPort": attributes['port'], "interceptIp": attributes['ip'], "interceptFirstPort": attributes['port'], "interceptLastPort": attributes['port'] } if attributes['type'] == 'network': # checking if service name is provided if attributes['name']: serviceName = attributes['name'] else: serviceName = attributes['gateway'] + '--' + str( attributes['netIp']) + '--' + str(attributes['netCidr']) data = { "serviceClass": "GW", "name": serviceName, "serviceInterceptType": "IP", "serviceType": "ALL", "endpointId": gwId, "lowLatency": "NO", "dataInterleaving": "NO", "transparency": "NO", "multicast": "OFF", "dnsOptions": "NONE", "icmpTunnel": "YES", "cryptoLevel": "STRONG", "permanentConnection": "YES", "collectionLocation": "BOTH", "pbrType": "WAN", "rateSmoothing": "NO", "gatewayClusterId": None, "interceptIp": attributes['netIp'], "gatewayIp": attributes['netIp'], "gatewayCidrBlock": attributes['netCidr'], "localNetworkGateway": "YES" } returnData = nfreq((url, data), "post", token) serviceUrl = returnData['_links']['self']['href'] time.sleep(1) return serviceUrl, serviceName