Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Av 131086 #1853

Open
wants to merge 15 commits into
base: AV-104274
Choose a base branch
from
47 changes: 47 additions & 0 deletions python/avi/migrationtools/nsxt_converter/conversion_util.py
Original file line number Diff line number Diff line change
@@ -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
49 changes: 33 additions & 16 deletions python/avi/migrationtools/nsxt_converter/monitor_converter.py
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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)
28 changes: 22 additions & 6 deletions python/avi/migrationtools/nsxt_converter/nsxt_config_converter.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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"
Expand All @@ -24,17 +30,27 @@ 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"
with open(output_config, "w", encoding='utf-8') as text_file:
json.dump(alb_config, text_file, indent=4)

pp = PrettyPrinter()
pp.pprint(alb_config)
pp.pprint(alb_config)
8 changes: 6 additions & 2 deletions python/avi/migrationtools/nsxt_converter/nsxt_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)



Expand All @@ -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)

5 changes: 4 additions & 1 deletion python/avi/migrationtools/nsxt_converter/nsxt_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
print(nsx_lb_config)
105 changes: 105 additions & 0 deletions python/avi/migrationtools/nsxt_converter/pools_converter.py
Original file line number Diff line number Diff line change
@@ -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)
Loading