diff --git a/python/avi/migrationtools/nsxt_converter/conversion_util.py b/python/avi/migrationtools/nsxt_converter/conversion_util.py new file mode 100644 index 0000000000..23a1b9a78f --- /dev/null +++ b/python/avi/migrationtools/nsxt_converter/conversion_util.py @@ -0,0 +1,47 @@ +def get_obj_url_uuid(path,unique_id): + url = path + "-" +unique_id + uuid = url.split('/')[-1] + return url,uuid +def get_tenant_ref(name): + tenant="admin" + if name and name.startswith('/'): + parts = name.split('/', 2) + tenant = parts[1] + if not parts[2]: + LOG.warning('Invalid tenant ref : %s' % name) + elif name and '/' in name: + parts = name.split('/') + # Changed the index to get the tenant and name in case of + # prefixed name + tenant = parts[-2] + if tenant.lower() == 'common': + tenant = 'admin' + if '/' in name: + name = name.split('/')[-1] + if ' ' in tenant: + tenant = tenant.split(' ')[-1] + return tenant,name +def get_object_ref( object_name, object_type, + prefix=None,cloud_name='Default-cloud'): + if prefix: + object_name = prefix + '-' + object_name + if object_type == 'tenant': + ref = '/api/tenant/?name=%s' %(object_name) + elif object_type == 'cloud': + ref = '/api/%s/?tenant=admin&name=%s' % (object_type, object_name) + elif object_type == 'vrfcontext': + ref = '/api/%s/?tenant=admin&name=%s&cloud=%s' % ( + object_type, object_name, cloud_name) + else: + ref='/api/%s/?tenant=admin&name=%s' % (object_type,object_name ) + return ref +<<<<<<< HEAD + + + + + + + +======= +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 diff --git a/python/avi/migrationtools/nsxt_converter/monitor_converter.py b/python/avi/migrationtools/nsxt_converter/monitor_converter.py index bcfab16f46..0ac99aa0c8 100644 --- a/python/avi/migrationtools/nsxt_converter/monitor_converter.py +++ b/python/avi/migrationtools/nsxt_converter/monitor_converter.py @@ -1,46 +1,48 @@ import com.vmware.nsx_policy.model_client as model_client +from avi.migrationtools.nsxt_converter import conversion_util + + def get_alb_response_codes(response_codes): if not response_codes: return None HttpResponseCode = model_client.ALBHealthMonitorHttp codes = list() for code in response_codes: - if code<200: + if code < 200: if HttpResponseCode.HTTP_RESPONSE_CODE_1XX not in codes: codes.append(HttpResponseCode.HTTP_RESPONSE_CODE_1XX) - elif code>199 and code<300: + elif code > 199 and code < 300: if HttpResponseCode.HTTP_RESPONSE_CODE_2XX not in codes: codes.append(HttpResponseCode.HTTP_RESPONSE_CODE_2XX) - elif code>299 and code<400: + elif code > 299 and code < 400: if HttpResponseCode.HTTP_RESPONSE_CODE_3XX not in codes: codes.append(HttpResponseCode.HTTP_RESPONSE_CODE_3XX) - elif code>399 and code<500: + elif code > 399 and code < 500: if HttpResponseCode.HTTP_RESPONSE_CODE_4XX not in codes: codes.append(HttpResponseCode.HTTP_RESPONSE_CODE_4XX) - elif code>499 and code<600: + elif code > 499 and code < 600: if HttpResponseCode.HTTP_RESPONSE_CODE_5XX not in codes: codes.append(HttpResponseCode.HTTP_RESPONSE_CODE_5XX) return codes def update_alb_type(lb_hm, alb_hm): - if lb_hm['resource_type'] == 'LBHttpMonitorProfile': alb_hm['type'] = 'HEALTH_MONITOR_HTTP' alb_hm['http_monitor'] = dict( - http_request=lb_hm['request_url'], + http_request=lb_hm.get('request_url'), http_request_body=lb_hm.get('request_body'), http_response=lb_hm.get('response_body'), - http_response_code=get_alb_response_codes(lb_hm['response_status_codes']), + http_response_code=get_alb_response_codes(lb_hm.get('response_status_codes')), ) elif lb_hm['resource_type'] == 'LBHttpsMonitorProfile': alb_hm['type'] = 'HEALTH_MONITOR_HTTPS' alb_hm['https_monitor'] = dict( - http_request=lb_hm['request_url'], + http_request=lb_hm.get('request_url'), http_request_body=lb_hm.get('request_body'), http_response=lb_hm.get('response_body'), - http_response_code=get_alb_response_codes(lb_hm['response_status_codes']), + http_response_code=get_alb_response_codes(lb_hm.get('response_status_codes')), ) elif lb_hm['resource_type'] == 'LBIcmpMonitorProfile': alb_hm['type'] = 'HEALTH_MONITOR_PING' @@ -49,20 +51,35 @@ def update_alb_type(lb_hm, alb_hm): elif lb_hm['resource_type'] == 'LbUdpMonitorProfile': alb_hm['type'] = 'HEALTH_MONITOR_UDP' + alb_hm['tenant_ref'] = conversion_util.get_object_ref('admin', 'tenant') -def convert(alb_config, nsx_lb_config): +<<<<<<< HEAD + +def convert(alb_config, nsx_lb_config, cloud_name, prefix): +======= +def convert(alb_config, nsx_lb_config,cloud_name,prefix): +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 alb_config['HealthMonitor'] = list() for lb_hm in nsx_lb_config['LbMonitorProfiles']: if lb_hm['resource_type'] == 'LBPassiveMonitorProfile': continue + if prefix: +<<<<<<< HEAD + name = '%s-%s' % (prefix, lb_hm['display_name']) + else: + name = lb_hm.get('display_name') +======= + name='%s-%s' % (prefix,lb_hm['display_name']) + else : + name=lb_hm.get('display_name') +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 alb_hm = dict( - name=lb_hm['display_name'], - failed_checks=lb_hm['fall_count'], - receive_timeout=lb_hm['timeout'], - send_interval=lb_hm['interval'], + name=name, + failed_checks=lb_hm.get('fall_count'), + receive_timeout=lb_hm.get('timeout'), + send_interval=lb_hm.get('interval'), monitor_port=lb_hm.get('monitor_port', None), ) update_alb_type(lb_hm, alb_hm) - alb_config['HealthMonitor'].append(alb_hm) diff --git a/python/avi/migrationtools/nsxt_converter/nsxt_config_converter.py b/python/avi/migrationtools/nsxt_converter/nsxt_config_converter.py index 0bc6516cbf..625a6da97b 100644 --- a/python/avi/migrationtools/nsxt_converter/nsxt_config_converter.py +++ b/python/avi/migrationtools/nsxt_converter/nsxt_config_converter.py @@ -1,7 +1,9 @@ - import sys -from avi.migrationtools.nsxt_converter import nsxt_client as nsx_client_util + +from avi.migrationtools.ace_converter import pool_converter +from avi.migrationtools.nsxt_converter import nsxt_client as nsx_client_util, pools_converter, vs_converter from avi.migrationtools.nsxt_converter import monitor_converter +from avi.migrationtools.nsxt_converter import profiles_converter from vmware.vapi.bindings.struct import PrettyPrinter from com.vmware.vapi.std.errors_client import NotFound from com.vmware.nsx.loadbalancer_client import Pools @@ -14,7 +16,11 @@ import json -def convert(nsx_ip, nsx_un, nsx_pw, nsx_port, output_dir): +<<<<<<< HEAD +def convert(nsx_ip, nsx_un, nsx_pw, nsx_port, output_dir, cloud_name, prefix): +======= +def convert(nsx_ip, nsx_un, nsx_pw, nsx_port, output_dir,cloud_name,prefix): +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 nsx_util = NSXUtil(nsx_un, nsx_pw, nsx_ip, nsx_port) nsx_lb_config = nsx_util.get_nsx_config() input_path = output_dir + os.path.sep + nsx_ip + os.path.sep + "input" @@ -24,12 +30,22 @@ def convert(nsx_ip, nsx_un, nsx_pw, nsx_port, output_dir): with open(input_config, "w", encoding='utf-8') as text_file: json.dump(nsx_lb_config, text_file, indent=4) - alb_config = dict() # Result Config - monitor_converter.convert(alb_config, nsx_lb_config) +<<<<<<< HEAD + monitor_converter.convert(alb_config, nsx_lb_config, cloud_name, prefix) + profiles_converter.convert(alb_config, nsx_lb_config, cloud_name, prefix) + pools_converter.convert(alb_config, nsx_lb_config, cloud_name, prefix) + # vs_converter.convert(alb_config, nsx_lb_config, cloud_name, prefix) +======= + monitor_converter.convert(alb_config, nsx_lb_config,cloud_name,prefix) + profiles_converter.convert(alb_config, nsx_lb_config,cloud_name,prefix) + pools_converter.convert(alb_config, nsx_lb_config,cloud_name,prefix) + vs_converter.convert(alb_config,nsx_lb_config,cloud_name,prefix) +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 output_path = output_dir + os.path.sep + nsx_ip + os.path.sep + "output" + print(output_path) if not os.path.exists(output_path): os.makedirs(output_path) output_config = output_path + os.path.sep + "avi_config.json" @@ -37,4 +53,4 @@ def convert(nsx_ip, nsx_un, nsx_pw, nsx_port, output_dir): json.dump(alb_config, text_file, indent=4) pp = PrettyPrinter() - pp.pprint(alb_config) \ No newline at end of file + pp.pprint(alb_config) diff --git a/python/avi/migrationtools/nsxt_converter/nsxt_converter.py b/python/avi/migrationtools/nsxt_converter/nsxt_converter.py index f5e9d2246f..1dacb4586c 100644 --- a/python/avi/migrationtools/nsxt_converter/nsxt_converter.py +++ b/python/avi/migrationtools/nsxt_converter/nsxt_converter.py @@ -6,7 +6,7 @@ def conver_lb_config(args): output_file_path = args.output_file_path if args.output_file_path else 'output' - nsxt_config_converter.convert(args.nsxt_ip, args.nsxt_user, args.nsxt_passord, args.nsxt_port, output_file_path) + nsxt_config_converter.convert(args.nsxt_ip, args.nsxt_user, args.nsxt_passord, args.nsxt_port, output_file_path,args.cloud_name,args.prefix) @@ -33,7 +33,11 @@ def conver_lb_config(args): parser.add_argument('-o', '--output_file_path', help='Folder path for output files to be created in', ) - + parser.add_argument('--vrf', + help='Update the available vrf ref with the custom vrf' + 'reference') + parser.add_argument('--cloud_name', required=True,help='cloud name for auto upload') + parser.add_argument('--prefix' ,help='Prefix for objects') args = parser.parse_args() conver_lb_config(args) diff --git a/python/avi/migrationtools/nsxt_converter/nsxt_util.py b/python/avi/migrationtools/nsxt_converter/nsxt_util.py index 2f87478867..7122c3377c 100644 --- a/python/avi/migrationtools/nsxt_converter/nsxt_util.py +++ b/python/avi/migrationtools/nsxt_converter/nsxt_util.py @@ -14,9 +14,12 @@ def get_nsx_config(self): nsx_lb_config["LBServices"] = self.nsx_api_client.infra.LbServices.list().to_dict().get("results", []) nsx_lb_config["LbMonitorProfiles"] = self.nsx_api_client.infra.LbMonitorProfiles.list().to_dict().get("results", []) nsx_lb_config["LbPools"] = self.nsx_api_client.infra.LbPools.list().to_dict().get("results", []) + nsx_lb_config["LbAppProfiles"] = self.nsx_api_client.infra.LbAppProfiles.list().to_dict().get("results", []) + nsx_lb_config["LBVirtualServers"] = self.nsx_api_client.infra.LbVirtualServers.list().to_dict().get("results", []) + return nsx_lb_config if __name__ == "__main__": nsx_util = NSXUtil('admin', 'Admin!23Admin', '10.168.204.70', '443') nsx_lb_config = nsx_util.get_nsx_config() - print(nsx_lb_config) \ No newline at end of file + print(nsx_lb_config) diff --git a/python/avi/migrationtools/nsxt_converter/pools_converter.py b/python/avi/migrationtools/nsxt_converter/pools_converter.py new file mode 100644 index 0000000000..f3778659b1 --- /dev/null +++ b/python/avi/migrationtools/nsxt_converter/pools_converter.py @@ -0,0 +1,105 @@ +from avi.migrationtools.nsxt_converter import nsxt_client as nsx_client_util, profiles_converter, conversion_util + + +<<<<<<< HEAD +def update_alb_type(lb_pl, alb_pl, prefix): + if lb_pl['algorithm'] == 'ROUND_ROBIN': +======= + +def update_alb_type(lb_pl, alb_pl,prefix): + + if lb_pl['algorithm']=='ROUND_ROBIN': +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 + alb_pl['lb_algorithm'] = 'LB_ALGORITHM_ROUND_ROBIN' + elif lb_pl['algorithm'] == 'LEAST_CONNECTION': + alb_pl['lb_algorithm'] = 'LB_ALGORITHM_LEAST_CONNECTION' + elif lb_pl['algorithm'] == 'IP_HASH': + alb_pl['lb_algorithm'] = 'LB_ALGORITHM_CONSISTENT_HASH_SOURCE_IP_ADDRESS' +<<<<<<< HEAD + alb_pl['min_servers_up'] = lb_pl.get('min_active_members') + alb_pl['max_concurrent_connections'] = lb_pl.get('max_concurrent_connections_per_server') +======= + alb_pl['min_servers_up']=lb_pl.get('min_active_members') + alb_pl['max_concurrent_connections']=lb_pl.get('max_concurrent_connections_per_server') +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 + + alb_pl['servers'] = dict( + ip=dict( + + )) + if lb_pl.get('members'): + for member in lb_pl.get('members'): +<<<<<<< HEAD + alb_pl['servers']['enabled'] = member.get('admin_state') + alb_pl['servers']['ip']['addr'] = member.get('ip_address') + alb_pl['servers']['ip']['type'] = 'V4' + alb_pl['servers']['port'] = member.get('port') + alb_pl['servers']['ratio'] = member.get('weight') + alb_pl['servers']['description'] = member.get('display_name') + # todo for snat_translation to preserve_client_ip in application profile + + alb_pl['conn_pool_properties'] = dict( +======= + alb_pl['servers']['enabled']=member.get('admin_state') + alb_pl['servers']['ip']['addr']=member.get('ip_address') + alb_pl['servers']['ip']['type']='V4' + alb_pl['servers']['port']=member.get('port') + alb_pl['servers']['ratio']=member.get('weight') + alb_pl['servers']['description']=member.get('display_name') + #todo for snat_translation to preserve_client_ip in application profile + + + alb_pl['ConnPoolProperties']=dict( +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 + upstream_connpool_server_max_cache=lb_pl.get('tcp_multiplexing_number') + + ) + if lb_pl.get('member_group'): + alb_pl['nsx_membergroup'] = dict( + nsx_securitygroup=lb_pl['member_group']['group_path'] + ) + +<<<<<<< HEAD + if lb_pl['member_group']['port']: + alb_pl['default_server_port'] = lb_pl['member_group']['port'] + alb_pl['tenant_ref'] = conversion_util.get_object_ref('admin', 'tenant') + if lb_pl.get('active_monitor_paths'): + tenant, name = conversion_util.get_tenant_ref(lb_pl.get('active_monitor_paths')[0]) + alb_pl['health_monitor_ref'] = conversion_util.get_object_ref(name, 'healthmonitor', prefix) + + +def convert(alb_config, nsx_lb_config, cloud_name, prefix): + alb_config['Pool'] = list() + for lb_pl in nsx_lb_config['LbPools']: + if prefix: + name = '%s-%s' % (prefix, lb_pl['display_name']) + else: + name = lb_pl['display_name'] + alb_pl = dict( + name=name + ) + update_alb_type(lb_pl, alb_pl, prefix) +======= + if(lb_pl['member_group']['port']): + alb_pl['default_server_port']=lb_pl['member_group']['port'] + alb_pl['tenant_ref'] = conversion_util.get_object_ref('admin', 'tenant') + if lb_pl.get('active_monitor_paths'): + tenant,name=conversion_util.get_tenant_ref(lb_pl.get('active_monitor_paths')[0]) + alb_pl['health_monitor_ref'] = conversion_util.get_object_ref(name,'healthmonitor',prefix) + + +def convert(alb_config, nsx_lb_config,cloud_name,prefix): + alb_config['Pool'] = list() + for lb_pl in nsx_lb_config['LbPools']: + if prefix: + name='%s-%s' % (prefix,lb_pl['display_name']) + else : + name=lb_pl['display_name'] + alb_pl=dict( + name=name + ) + update_alb_type(lb_pl, alb_pl,prefix) +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 + alb_pl['cloud_ref'] = conversion_util.get_object_ref(cloud_name, 'cloud') + + alb_config['Pool'].append(alb_pl) diff --git a/python/avi/migrationtools/nsxt_converter/profiles_converter.py b/python/avi/migrationtools/nsxt_converter/profiles_converter.py new file mode 100644 index 0000000000..90aeb24843 --- /dev/null +++ b/python/avi/migrationtools/nsxt_converter/profiles_converter.py @@ -0,0 +1,72 @@ +from avi.migrationtools.nsxt_converter import nsxt_client as nsx_client_util, conversion_util + + +def update_alb_type(lb_pr, alb_pr): +<<<<<<< HEAD + alb_pr['tenant_ref'] = conversion_util.get_object_ref('admin', 'tenant') +======= + + alb_pr['tenant_ref']=conversion_util.get_object_ref('admin','tenant') +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 + + if lb_pr['resource_type'] == 'LBHttpProfile': + alb_pr['type'] = 'APPLICATION_PROFILE_TYPE_HTTP' + + alb_pr['http_profile'] = dict( +<<<<<<< HEAD + xff_enabled=lb_pr.get('xForwardedFor', False), + http_to_https=lb_pr.get('httpRedirectToHttps', False), +======= + xff_enabled=lb_pr.get('xForwardedFor',False), + http_to_https=lb_pr.get('httpRedirectToHttps',False), +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 + keepalive_timeout=lb_pr.get('idle_timeout'), + client_max_header_size=lb_pr.get('request_header_size'), + client_max_body_size=lb_pr.get('requestBodySize') + ) + elif lb_pr['resource_type'] == 'LBFastTcpProfile': + alb_pr['profile'] = dict( + type='APPLICATION_PROFILE_TYPE_TCP', + tcp_fast_path_profile=dict( + session_idle_timeout=lb_pr.get('idle_timeout')) + ) + + elif lb_pr['resource_type'] == 'LBFastUdpProfile': + alb_pr['profile'] = dict( + type='APPLICATION_PROFILE_TYPE_UDP', + udp_fast_path_profile=dict( + session_idle_timeout=lb_pr.get('idle_timeout') + + ) + ) + +<<<<<<< HEAD + +def convert(alb_config, nsx_lb_config, cloud_name, prefix): +======= +def convert(alb_config, nsx_lb_config,cloud_name,prefix): +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 + alb_config['ApplicationProfile'] = list() + alb_config['NetworkProfile'] = list() + for lb_pr in nsx_lb_config['LbAppProfiles']: + if prefix: +<<<<<<< HEAD + name = '%s-%s' % (prefix, lb_pr.get('display_name')) + else: + name = lb_pr.get('display_name') + alb_pr = dict( + name=name +======= + name='%s-%s' % (prefix,lb_pr.get('display_name')) + else : + name=lb_pr.get('display_name') + alb_pr=dict( + name=name +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 + ) + update_alb_type(lb_pr, alb_pr) + if lb_pr['resource_type'] == 'LBHttpProfile': + alb_config['ApplicationProfile'].append(alb_pr) + + else: + alb_config['NetworkProfile'].append(alb_pr) diff --git a/python/avi/migrationtools/nsxt_converter/vs_converter.py b/python/avi/migrationtools/nsxt_converter/vs_converter.py new file mode 100644 index 0000000000..bbf5406a55 --- /dev/null +++ b/python/avi/migrationtools/nsxt_converter/vs_converter.py @@ -0,0 +1,66 @@ +from avi.migrationtools.nsxt_converter import conversion_util + + +<<<<<<< HEAD +def update_alb_type(lb_vs, alb_vs, prefix): + alb_vs['enabled'] = lb_vs.get('enabled') + + alb_vs['services'] = dict( + port=lb_vs.get('ports')[0] + ) + + alb_vs['ssl_profile_ref'] = lb_vs.get('client_ssl_profile_binding') + alb_vs['tenant_ref'] = conversion_util.get_object_ref('admin', 'tenant') + + +def convert(alb_config, nsx_lb_config, cloud_name, prefix): +======= +def update_alb_type(lb_vs, alb_vs,prefix): + + alb_vs['enabled'] = lb_vs.get('enabled') + + alb_vs['services']=dict( + port=lb_vs.get('ports')[0] + ) + + + alb_vs['ssl_profile_ref']=lb_vs.get('client_ssl_profile_binding') + alb_vs['tenant_ref'] = conversion_util.get_object_ref('admin', 'tenant') + + + +def convert(alb_config, nsx_lb_config,cloud_name,prefix): +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 + alb_config['VirtualService'] = list() + alb_config['vsvip'] = list() + for lb_vs in nsx_lb_config['LBVirtualServers']: + if prefix: +<<<<<<< HEAD + name = '%s-%s' % (prefix, lb_vs['display_name']) + else: + name = lb_vs['display_name'] + vsvip = dict( +======= + name='%s-%s' % (prefix,lb_vs['display_name']) + else : + name=lb_vs['display_name'] + vsvip=dict( +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 + ip_address=dict( + addr=lb_vs['ip_address'], + type="V4" + ) + ) + alb_vs = dict( + name=name, + + ) + +<<<<<<< HEAD + update_alb_type(lb_vs, alb_vs, prefix) +======= + update_alb_type(lb_vs, alb_vs,prefix) +>>>>>>> 5fb000a9b7a05505177061270c1bc49d9cb4e675 + alb_vs['cloud_ref'] = conversion_util.get_object_ref(cloud_name, 'cloud') + alb_config['VirtualService'].append(alb_vs) + alb_config['vsvip'].append(vsvip)