def convert(meta, gslb_config_dict, controller_ip, user_name, password, tenant_name, vs_state, output_dir, version, report_name, vs_level_status): vip_cluster_map = None # If controller ip present then only get configuration from controller. if controller_ip: session = ApiSession.get_session(controller_ip, user_name, password) resp = session.get('configuration/export?full_system=true') avi_config = json.loads(resp.text) sites = avi_config['Gslb'][0]['sites'] vip_cluster_map = get_vip_cluster_map(sites) avi_gslb_config = None try: avi_gslb_config = dict() avi_gslb_config['META'] = meta gslb_vs_converter = GslbVsConverter() avi_config = gslb_vs_converter.convert( gslb_config_dict, avi_gslb_config, vs_state, vip_cluster_map) ns_util.add_complete_conv_status( gslb_config_dict, output_dir, avi_config, report_name, vs_level_status) except: update_count('error') LOG.error('Error in config conversion', exc_info=True) return avi_gslb_config
def convert(meta, gslb_config_dict, controller_ip, user_name, password, tenant_name, vs_state, output_dir, version, report_name, vs_level_status): vip_cluster_map = None # If controller ip present then only get configuration from controller. if controller_ip: session = ApiSession.get_session(controller_ip, user_name, password) resp = session.get('configuration/export?full_system=true') avi_config = json.loads(resp.text) sites = avi_config['Gslb'][0]['sites'] vip_cluster_map = get_vip_cluster_map(sites) avi_gslb_config = None try: avi_gslb_config = dict() avi_gslb_config['META'] = meta gslb_vs_converter = GslbVsConverter() avi_config = gslb_vs_converter.convert(gslb_config_dict, avi_gslb_config, vs_state, vip_cluster_map) ns_util.add_complete_conv_status(gslb_config_dict, output_dir, avi_config, report_name, vs_level_status) except: update_count('error') LOG.error('Error in config conversion', exc_info=True) return avi_gslb_config
def get_ns_conf_dict(filepath): """ This function defines that create a dict netscalar commands :param filepath: Netscalar Input configuration file :return: None """ LOG.debug('Started parsing netscaler config file') netscaler_conf = dict() skipped_cmds = [] ns_constant.init() supported_commands = ns_constant.netscalar_command_status[ 'SupportedCommands'] try: result = parse_config_file(filepath) for line in result: cmd, offset = get_command(line, supported_commands) if offset: cmd_dict = dict() attr_list = [] key = line[offset] line = line[offset:] for token in line: if isinstance(token, list): if token[0] == "invoke" and 'policylabel' in token[1] \ and cmd == "bind cs policylabel": policyLabel = token[1].split(' ') cmd_dict.update({token[0]: policyLabel[1]}) elif token[0] == "invoke" and 'policylabel' in token[1] \ and cmd == "bind cs vserver": policyLabel = token[1].split(' ') cmd_dict.update({policyLabel[0]: policyLabel[1]}) else: cmd_dict.update({token[0]: token[1]}) else: attr_list.append(token) cmd_dict.update({'attrs': attr_list}) cmd_list = netscaler_conf.get(cmd, {}) obj = cmd_list.get(key, None) if obj: if isinstance(obj, list): obj.append(cmd_dict) else: obj_list = [obj, cmd_dict] cmd_list.update({key: obj_list}) else: cmd_list.update({key: cmd_dict}) netscaler_conf.update({cmd: cmd_list}) else: skipped_cmds.append(cmd) LOG.debug('File parsed successfully') except Exception as exception: update_count('error') print(exception) LOG.error('Error in parsing the file', exc_info=True) return netscaler_conf, skipped_cmds
def get_ns_conf_dict(filepath): """ This function defines that create a dict netscalar commands :param filepath: Netscalar Input configuration file :return: None """ LOG.debug('Started parsing netscaler config file') netscaler_conf = dict() skipped_cmds = [] ns_constant.init() supported_commands = ns_constant.netscalar_command_status['SupportedCommands'] try: result = parse_config_file(filepath) for line in result: cmd, offset = get_command(line, supported_commands) if offset: cmd_dict = dict() attr_list = [] key = line[offset] line = line[offset:] for token in line: if isinstance(token, list): if token[0] == "invoke" and 'policylabel' in token[1] \ and cmd == "bind cs policylabel": policyLabel = token[1].split(' ') cmd_dict.update({token[0]: policyLabel[1]}) elif token[0] == "invoke" and 'policylabel' in token[1] \ and cmd == "bind cs vserver": policyLabel = token[1].split(' ') cmd_dict.update({policyLabel[0]: policyLabel[1]}) else: cmd_dict.update({token[0]: token[1]}) else: attr_list.append(token) cmd_dict.update({'attrs':attr_list}) cmd_list = netscaler_conf.get(cmd, {}) obj = cmd_list.get(key, None) if obj: if isinstance(obj, list): obj.append(cmd_dict) else: obj_list = [obj, cmd_dict] cmd_list.update({key: obj_list}) else: cmd_list.update({key: cmd_dict}) netscaler_conf.update({cmd: cmd_list}) else: skipped_cmds.append(cmd) LOG.debug('File parsed successfully') except Exception as exception: update_count('error') print exception LOG.error('Error in parsing the file', exc_info=True) return netscaler_conf, skipped_cmds
def convert_ca_certs(self, intermediate_cert_config, input_dir, avi_config): for link_key in intermediate_cert_config.keys(): try: cert_link = intermediate_cert_config[link_key] LOG.debug('converting intermediate cert %s' % link_key) full_command = ns_util.get_netscalar_full_command( 'link ssl certKey', cert_link) ca_cert_name = cert_link['attrs'][1] cert_name = [ cert['name'] for cert in avi_config['SSLKeyAndCertificate'] if cert['name'] == ca_cert_name and cert['type'] == 'SSL_CERTIFICATE_TYPE_CA' ] if cert_name: LOG.warning('SSL ca cert is already exist for %s is %s' % (ca_cert_name, cert_name[0])) continue ca_cert = ns_util.upload_file(input_dir + os.path.sep + ca_cert_name) if ca_cert: cert = { "certificate": ca_cert.decode() if type(ca_cert) == bytes else ca_cert } ca_cert_obj = { 'name': ca_cert_name, 'tenant_ref': self.tenant_ref, 'certificate': cert, 'type': 'SSL_CERTIFICATE_TYPE_CA' } avi_config['SSLKeyAndCertificate'].append(ca_cert_obj) ns_util.add_status_row(cert_link['line_no'], 'link ssl certKey', ca_cert_name, full_command, STATUS_SUCCESSFUL, ca_cert_obj) LOG.debug('Successfully converted intermediate cert %s' % link_key) else: skipped_status = ('Skipped: Cert file not found or ' 'cannot read: %s' % full_command) ns_util.add_status_row(cert_link['line_no'], 'link ssl certKey', ca_cert_name, full_command, STATUS_SKIPPED, skipped_status) LOG.warn("Skipped intermediate cert cannot read file %s" % link_key) except: update_count('error') LOG.error("Cannot convert intermediate cert %s" % link_key, exc_info=True)
def convert_ca_certs(self, intermediate_cert_config, input_dir, avi_config): for link_key in intermediate_cert_config.keys(): try: cert_link = intermediate_cert_config[link_key] LOG.debug('converting intermediate cert %s' % link_key) full_command = ns_util.get_netscalar_full_command( 'link ssl certKey', cert_link) ca_cert_name = cert_link['attrs'][1] cert_name = [cert['name'] for cert in avi_config['SSLKeyAndCertificate'] if cert['name'] == ca_cert_name and cert['type'] == 'SSL_CERTIFICATE_TYPE_CA'] if cert_name: LOG.warning( 'SSL ca cert is already exist for %s is %s' % ( ca_cert_name, cert_name[0])) continue ca_cert = ns_util.upload_file( input_dir + os.path.sep + ca_cert_name) if ca_cert: cert = {"certificate": ca_cert} ca_cert_obj = { 'name': ca_cert_name, 'tenant_ref': self.tenant_ref, 'certificate': cert, 'type': 'SSL_CERTIFICATE_TYPE_CA' } avi_config['SSLKeyAndCertificate'].append(ca_cert_obj) ns_util.add_status_row( cert_link['line_no'], 'link ssl certKey', ca_cert_name, full_command, STATUS_SUCCESSFUL, ca_cert_obj) LOG.debug('Successfully converted intermediate cert %s' % link_key) else: skipped_status = ('Skipped: Cert file not found or ' 'cannot read: %s' % full_command) ns_util.add_status_row( cert_link['line_no'], 'link ssl certKey', ca_cert_name, full_command, STATUS_SKIPPED, skipped_status) LOG.warn("Skipped intermediate cert cannot read file %s" % link_key) except: update_count('error') LOG.error("Cannot convert intermediate cert %s" % link_key, exc_info=True)
def convert_http_profile(self, profile): """ This function defines that convert http profile :param profile: Object of http profile :return: http profile """ app_profile = dict() try: prof_name = profile['attrs'][0] # Added prefix for objects if self.prefix: prof_name = self.prefix + '-' + prof_name LOG.debug("Converting httpProfile: %s" % prof_name) app_profile['name'] = prof_name app_profile['tenant_ref'] = self.tenant_ref app_profile['type'] = 'APPLICATION_PROFILE_TYPE_HTTP' http_profile = dict() conn_mux = profile.get('conMultiplex', 'ENABLED') conn_mux = False if conn_mux == 'DISABLED' else True http_profile['connection_multiplexing_enabled'] = conn_mux xff_header = profile.get('clientIpHdrExpr', None) xff_enabled = True if xff_header else False http_profile['xff_enabled'] = xff_enabled # TODO: clientIpHdrExpr conversion to xff_alternate_name websockets = profile.get('websockets_enabled', profile.get( 'webSocket', 'DISABLED')) websockets = False if websockets == 'DISABLED' else True http_profile['websockets_enabled'] = websockets app_profile["http_profile"] = http_profile LOG.debug("Conversion completed successfully for httpProfile: %s" % prof_name) except: update_count('error') LOG.error("Error in convertion of httpProfile", exc_info=True) return app_profile
def convert_tcp_profile(self, profile): """ This function defines that convert tcp profile :param profile: Object of tcp profile :return: tcp profile """ ntwk_profile = None try: prof_name = profile['attrs'][0] # Added prefix for objects if self.prefix: prof_name = self.prefix + '-' + prof_name nagle = profile.get("nagle", 'DISABLED') nagle = False if nagle == 'DISABLED' else True mss = profile.get("mss", 0) mtu = True if int(mss) > 0 else False window = profile.get("bufferSize", 32768) ntwk_profile = { "profile": { "tcp_proxy_profile": { "nagles_algorithm": nagle, "max_segment_size": int(mss), "use_interface_mtu": mtu, "receive_window": int(int(window) / 1024), 'automatic': False }, "type": "PROTOCOL_TYPE_TCP_PROXY" }, "name": prof_name, "tenant_ref": self.tenant_ref } except: update_count('error') LOG.error("Error in convertion of tcpProfile", exc_info=True) return ntwk_profile
def convert(self, f5_config, avi_config, user_ignore, tenant_ref, merge_object_mapping, sys_dict): """ :param f5_config: parsed f5 config :param avi_config: dict of avi config :param user_ignore: Ignore config defined by user :param tenant_ref: tenant of which output to converted :param merge_object_mapping: flag for object merge :param sys_dict: baseline profile :return: """ if 'IpAddrGroup' not in avi_config: avi_config['IpAddrGroup'] = [] converted_objs = [] f5_datagroup_dict = f5_config.get('data-group', {}) user_ignore = user_ignore.get('data-group', {}) # Added variable to get total object count. progressbar_count = 0 total_size = len(f5_datagroup_dict.keys()) print("Converting Data groups...") for key in f5_datagroup_dict.keys(): progressbar_count += 1 data_group_type = None name = None skipped = [] # Added call to check the progress. msg = "data-group conversion started..." conv_utils.print_progress_bar(progressbar_count, total_size, msg, prefix='Progress', suffix='') dg_type = None try: tenant, name = conv_utils.get_tenant_ref(key) # dg_scope, name = key.split(" ") # name = name.split('/')[-1] LOG.debug("Converting datagroup: %s" % name) dg_config = f5_datagroup_dict[key] if tenant_ref: tenant = tenant_ref if self.prefix: name = '{}-{}'.format(self.prefix, name) dg_type = dg_config['type'] if dg_type == 'ip': ip_group = self.convert_ip_group(name, dg_config, avi_config, skipped, tenant) else: msg = 'data-group type not supported skipping ' \ 'conversion: %s' % name LOG.warning(msg) self.update_conv_status_for_skip(dg_type, name, msg) continue if not ip_group: continue # code to merge Data groups. if self.object_merge_check: conv_utils.update_skip_duplicates( ip_group, avi_config['IpAddrGroup'], 'ip_group', converted_objs, name, None, merge_object_mapping, dg_type, self.prefix, sys_dict['IpAddrGroup']) self.dg_count += 1 else: avi_config["IpAddrGroup"].append(ip_group) conv_status = conv_utils.get_conv_status( skipped, dict(), dict(), ip_group, user_ignore) self.update_conversion_status(conv_status, dg_type, name, ip_group) except: update_count('error') LOG.error("Failed to convert data-group : %s" % key, exc_info=True) self.update_conv_status_for_error(name, dg_type, key) count = len(avi_config["IpAddrGroup"]) LOG.debug("Converted %s ip group" % count) f5_config.pop('data-group', None)
def convert_ssl_service_profile(self, set_ssl_service, bind_ssl_service, ssl_key_and_cert, input_dir, ns_config, avi_config, set_ssl_service_command, bind_ssl_service_command, sysdict): """ This function defines that convert ssl profiles :param set_ssl_service: dict of set_ssl_service netscalar command :param bind_ssl_service: dict of bind_ssl_service netscalar command :param ssl_key_and_cert: dict of set_ssl_service netscalar command :param input_dir: path of input dir which keeps cert and key :param ns_config: It is dict of all netscalar commands which are supported by AVI :param avi_config: dict of AVI :param set_ssl_service_command: netscalar command set ssl :param bind_ssl_service_command: netscalar command bind ssl :return: None """ for key in set_ssl_service: try: self.progressbar_count += 1 ssl_service = set_ssl_service[key] full_set_ssl_service_command = \ ns_util.get_netscalar_full_command(set_ssl_service_command, ssl_service) ssl_profile_name = ssl_service['attrs'][0] ssl_profile_name = re.sub('[:]', '-', ssl_profile_name) # Added prefix for objects if self.prefix: updated_ssl_profile_name = '%s-%s' % (self.prefix, ssl_profile_name) else: updated_ssl_profile_name = ssl_profile_name ssl_profile = { 'name': updated_ssl_profile_name, 'tenant_ref': self.tenant_ref, 'accepted_versions': [] } # set ssl service sess_reuse = ssl_service.get('sessReuse', None) if sess_reuse == 'DISABLED': ssl_profile['enable_ssl_session_reuse'] = False if ssl_service.get('sessTimeout', None): ssl_profile['ssl_session_timeout'] = \ int(ssl_service.get('sessTimeout')) accepted_versions = [] if ssl_service.get('tls1', 'ENABLED') == 'ENABLED': accepted_versions.append({'type': 'SSL_VERSION_TLS1'}) if ssl_service.get('tls11', 'ENABLED') == 'ENABLED': accepted_versions.append({'type': 'SSL_VERSION_TLS1_1'}) if ssl_service.get('tls12', 'ENABLED') == 'ENABLED': accepted_versions.append({'type': 'SSL_VERSION_TLS1_2'}) if accepted_versions: ssl_profile['accepted_versions'] = accepted_versions else: ssl_profile['accepted_versions'].append( {'type': 'SSL_VERSION_TLS1_1'}) send_close_notify = ssl_service.get('sendCloseNotify', None) if send_close_notify == 'NO': ssl_profile['send_close_notify'] = False # bind ssl service binding_mapping = bind_ssl_service.get(ssl_profile_name, []) if isinstance(binding_mapping, dict): binding_mapping = [binding_mapping] obj = self.get_key_cert(binding_mapping, ssl_key_and_cert, input_dir, None, ns_config, bind_ssl_service_command) if obj.get('accepted_ciphers', None): # Todo supported only valid ciphers ssl_profile['accepted_ciphers'] = obj.get( 'accepted_ciphers') # ssl_profile['accepted_ciphers'] = 'AES:3DES:RC4' if obj.get('cert', None): avi_config["SSLKeyAndCertificate"].append(obj.get('cert')) if obj.get('pki', None): if self.object_merge_check: # Check pki profile is duplicate of other pki profile # then skipped this pki profile and increment of count # of pki_merge_count dup_of = ns_util.update_skip_duplicates(obj['pki'], avi_config['PKIProfile'], 'pki_profile', merge_object_mapping, obj['pki']['name'], None, self.prefix, sysdict['PKIProfile']) if dup_of: self.pki_merge_count += 1 else: avi_config["PKIProfile"].append(obj['pki']) else: avi_config["PKIProfile"].append(obj['pki']) if self.object_merge_check: # Check ssl profile is duplicate of other ssl profile then # skipped this application profile and increment of count # of ssl_merge_count # Added prefix for objects if self.prefix: ssl_profile_name = self.prefix + '-' + ssl_profile_name dup_of = ns_util.update_skip_duplicates( ssl_profile, avi_config['SSLProfile'], 'ssl_profile', merge_object_mapping, ssl_profile_name, None, self.prefix, sysdict['SSLProfile']) if dup_of: self.ssl_merge_count += 1 else: avi_config['SSLProfile'].append(ssl_profile) else: avi_config['SSLProfile'].append(ssl_profile) LOG.info('Conversion successful: %s' % full_set_ssl_service_command) conv_status = ns_util.get_conv_status( ssl_service, self.profile_set_ssl_service_skip, self.profile_set_ssl_service_indirect, [], user_ignore_val=self.profile_set_ssl_service_user_ignore) # Add summery in CSV/report for ssl service ns_util.add_conv_status( ssl_service['line_no'], set_ssl_service_command, key, full_set_ssl_service_command, conv_status, ssl_profile) LOG.debug("SSL profile conversion completed") except: update_count('error') LOG.error("Error in conversion of SSL Profile", exc_info=True) msg = "SSL Service conversion started..." ns_util.print_progress_bar(self.progressbar_count, self.total_size, msg, prefix='Progress', suffix='')
def convert_ssl_service_profile(self, set_ssl_service, bind_ssl_service, ssl_key_and_cert, input_dir, ns_config, avi_config, set_ssl_service_command, bind_ssl_service_command, sysdict): """ This function defines that convert ssl profiles :param set_ssl_service: dict of set_ssl_service netscalar command :param bind_ssl_service: dict of bind_ssl_service netscalar command :param ssl_key_and_cert: dict of set_ssl_service netscalar command :param input_dir: path of input dir which keeps cert and key :param ns_config: It is dict of all netscalar commands which are supported by AVI :param avi_config: dict of AVI :param set_ssl_service_command: netscalar command set ssl :param bind_ssl_service_command: netscalar command bind ssl :return: None """ for key in set_ssl_service: try: self.progressbar_count += 1 ssl_service = set_ssl_service[key] full_set_ssl_service_command = \ ns_util.get_netscalar_full_command(set_ssl_service_command, ssl_service) ssl_profile_name = ssl_service['attrs'][0] ssl_profile_name = re.sub('[:]', '-', ssl_profile_name) # Added prefix for objects if self.prefix: updated_ssl_profile_name = '%s-%s' % (self.prefix, ssl_profile_name) else: updated_ssl_profile_name = ssl_profile_name ssl_profile = { 'name': updated_ssl_profile_name, 'tenant_ref': self.tenant_ref, 'accepted_versions': [] } # set ssl service sess_reuse = ssl_service.get('sessReuse', None) if sess_reuse == 'DISABLED': ssl_profile['enable_ssl_session_reuse'] = False if ssl_service.get('sessTimeout', None): ssl_profile['ssl_session_timeout'] = \ int(ssl_service.get('sessTimeout')) accepted_versions = [] if ssl_service.get('tls1', 'ENABLED') == 'ENABLED': accepted_versions.append({'type': 'SSL_VERSION_TLS1'}) if ssl_service.get('tls11', 'ENABLED') == 'ENABLED': accepted_versions.append({'type': 'SSL_VERSION_TLS1_1'}) if ssl_service.get('tls12', 'ENABLED') == 'ENABLED': accepted_versions.append({'type': 'SSL_VERSION_TLS1_2'}) if accepted_versions: ssl_profile['accepted_versions'] = accepted_versions else: ssl_profile['accepted_versions'].append( {'type': 'SSL_VERSION_TLS1_1'}) send_close_notify = ssl_service.get('sendCloseNotify', None) if send_close_notify == 'NO': ssl_profile['send_close_notify'] = False # bind ssl service binding_mapping = bind_ssl_service.get(ssl_profile_name, []) if isinstance(binding_mapping, dict): binding_mapping = [binding_mapping] obj = self.get_key_cert(binding_mapping, ssl_key_and_cert, input_dir, None, ns_config, bind_ssl_service_command) if obj.get('accepted_ciphers', None): # Todo supported only valid ciphers ssl_profile['accepted_ciphers'] = obj.get( 'accepted_ciphers') ssl_profile['description'] = obj.get('ciphersuite') # ssl_profile['accepted_ciphers'] = 'AES:3DES:RC4' certs = obj.get('cert', []) avi_config["SSLKeyAndCertificate"] += certs if obj.get('pki', None): if self.object_merge_check: # Check pki profile is duplicate of other pki profile # then skipped this pki profile and increment of count # of pki_merge_count dup_of = ns_util.update_skip_duplicates(obj['pki'], avi_config['PKIProfile'], 'pki_profile', merge_object_mapping, obj['pki']['name'], None, self.prefix, sysdict['PKIProfile']) if dup_of: self.pki_merge_count += 1 else: avi_config["PKIProfile"].append(obj['pki']) else: avi_config["PKIProfile"].append(obj['pki']) if self.object_merge_check: # Check ssl profile is duplicate of other ssl profile then # skipped this application profile and increment of count # of ssl_merge_count # Added prefix for objects if self.prefix: ssl_profile_name = self.prefix + '-' + ssl_profile_name dup_of = ns_util.update_skip_duplicates( ssl_profile, avi_config['SSLProfile'], 'ssl_profile', merge_object_mapping, ssl_profile_name, None, self.prefix, sysdict['SSLProfile']) if dup_of: self.ssl_merge_count += 1 else: avi_config['SSLProfile'].append(ssl_profile) else: avi_config['SSLProfile'].append(ssl_profile) LOG.info('Conversion successful: %s' % full_set_ssl_service_command) conv_status = ns_util.get_conv_status( ssl_service, self.profile_set_ssl_service_skip, self.profile_set_ssl_service_indirect, [], user_ignore_val=self.profile_set_ssl_service_user_ignore) # Add summery in CSV/report for ssl service ns_util.add_conv_status( ssl_service['line_no'], set_ssl_service_command, key, full_set_ssl_service_command, conv_status, ssl_profile) LOG.debug("SSL profile conversion completed") except: update_count('error') LOG.error("Error in conversion of SSL Profile", exc_info=True) msg = "SSL Service conversion started..." ns_util.print_progress_bar(self.progressbar_count, self.total_size, msg, prefix='Progress', suffix='')
def convert(ns_config_dict, tenant_name, cloud_name, version, output_dir, input_dir, skipped_cmds, vs_state, object_merge_check, report_name, prefix, vs_name_dict, profile_path, redirect, key_passphrase=None, user_ignore={}, vs_level_status=False, vrf=None, segroup=None): """ This functions defines that it convert service/servicegroup to pool Convert pool group of netscalar bind lb vserver configuration :param ns_config_dict: Dict of netscalar commands :param tenant: Tenant :param cloud_ref: Cloud ref :param version: Version :param output_dir: Output dir for write AVI object after conversion :param input_dir: Input dir is to keep cert and keys :param skipped_cmds: List of skipped commands :param vs_state: VS state :param object_merge_check: Flag of object merge :param report_name: name of input file :param: prefix: prefix for objects :param key_passphrase: path of passphrase yaml file :param user_ignore: Dict of user ignore attributes :param vs_level_status: Add columns of vs reference overall skipped settings :return: None """ ssl_ciphers_yaml = 'ssl_ciphers.yaml' # load ssl ciphers with open(os.path.dirname(__file__) + '/%s' % ssl_ciphers_yaml) as stream: ssl_ciphers = yaml.safe_load(stream) LOG.debug('Conversion Started') tenant_ref = ns_util.get_object_ref(tenant_name, 'tenant') cloud_ref = ns_util.get_object_ref(cloud_name, 'cloud') try: # call meta from super class avi_config = dict() sys_dict = dict() merge_object_type = [ 'ApplicationProfile', 'NetworkProfile', 'SSLProfile', 'PKIProfile', 'ApplicationPersistenceProfile', 'HealthMonitor' ] # Constructed avi config dict and baseline object dict for objects # which can be merged for key in merge_object_type: sys_dict[key] = [] avi_config[key] = [] # Read the baseline json file and filled the baseline object dict if profile_path and os.path.exists(profile_path): with open(profile_path) as data: prof_data = json.load(data) for key in merge_object_type: sys_dict[key] = prof_data.get(key, []) monitor_converter = MonitorConverter(tenant_name, cloud_name, tenant_ref, cloud_ref, user_ignore, prefix, object_merge_check, version) monitor_converter.convert(ns_config_dict, avi_config, input_dir, sys_dict) profile_converter = ProfileConverter(tenant_name, cloud_name, tenant_ref, cloud_ref, ssl_ciphers, object_merge_check, user_ignore, prefix, key_passphrase) profile_converter.convert(ns_config_dict, avi_config, input_dir, sys_dict) service_converter = ServiceConverter(tenant_name, cloud_name, tenant_ref, cloud_ref, object_merge_check, user_ignore, prefix) service_converter.convert(ns_config_dict, avi_config, sys_dict, vrf) lbvs_converter = LbvsConverter(tenant_name, cloud_name, tenant_ref, cloud_ref, object_merge_check, version, user_ignore, prefix) lbvs_converter.convert(ns_config_dict, avi_config, vs_state, sys_dict, vs_name_dict, vrf, segroup) csvs_converter = CsvsConverter(tenant_name, cloud_name, tenant_ref, cloud_ref, object_merge_check, version, user_ignore, prefix) csvs_converter.convert(ns_config_dict, avi_config, vs_state, sys_dict, vs_name_dict, vrf, segroup) if object_merge_check: # Updating the reference for application persistence profile as we # are assigning reference at the time of profile creation ns_util.update_profile_ref( 'application_persistence_profile_ref', avi_config['Pool'], merge_object_mapping['app_persist_profile']) # Updating the reference for application profile as we # are assigning reference at the time of profile creation ns_util.update_profile_ref('application_profile_ref', avi_config['VirtualService'], merge_object_mapping['app_profile']) # Add status for skipped netscalar commands in CSV/report ns_util.update_status_for_skipped(skipped_cmds) if redirect: # Removing VS and changing the status in CSV which got redirected # Scenario for redirect - HTTP VS having no pool but redirect to # HTTPS VS ns_util.vs_redirect_http_to_https(avi_config, sys_dict) # Merging the pools in a pool group if pools are having same health # monitor ns_util.merge_pool(avi_config) # Add/update CSV/report ns_util.add_complete_conv_status(ns_config_dict, output_dir, avi_config, report_name, vs_level_status) LOG.debug('Conversion completed successfully') ns_util.cleanup_config(tmp_avi_config) ns_util.cleanup_dupof(avi_config) avi_config.pop('Lbvs', None) # Validating the aviconfig after generation ns_util.validation(avi_config) # added code to get fully converted virtual service. for key in avi_config: if key != 'META': if key == 'VirtualService': if vs_level_status: LOG.info( 'Total Objects of %s : %s (%s full conversions)' % (key, len(avi_config[key]), nsu.fully_migrated)) print 'Total Objects of %s : %s (%s full conversions)'\ % (key, len(avi_config[key]), nsu.fully_migrated) else: LOG.info('Total Objects of %s : %s' % (key, len(avi_config[key]))) print 'Total Objects of %s : %s' \ % (key, len(avi_config[key])) continue # Added code to print merged count. elif object_merge_check and key == 'SSLProfile': profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config[key]), abs(profile_converter.ssl_merge_count), abs(profile_converter.ssl_merge_count) + len(avi_config[key])) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'ApplicationProfile': profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config[key]), abs(app_merge_count['count']), abs(app_merge_count['count']) + len(avi_config[key])) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'NetworkProfile': profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config[key]), abs(profile_converter.network_merge_count), abs(profile_converter.network_merge_count) + len(avi_config[key])) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == \ 'ApplicationPersistenceProfile': profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config[key]), abs(app_per_merge_count['count']), abs(app_per_merge_count['count']) + len(avi_config[key])) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'HealthMonitor': monitor_merged_message = \ 'Total Objects of %s : %s (%s/%s monitor merged)' % \ (key, len(avi_config[key]), abs(monitor_converter.monitor_merge_count), abs(monitor_converter.monitor_merge_count) + len(avi_config[key])) LOG.info(monitor_merged_message) print monitor_merged_message continue LOG.info('Total Objects of %s : %s' % (key, len(avi_config[key]))) print 'Total Objects of %s : %s' % (key, len(avi_config[key])) except: update_count('warning') LOG.error('Error in config conversion', exc_info=True) return avi_config
def convert_monitor(self, ns_monitor, input_dir, netscalar_command, ns_monitor_complete_command): """ This functions defines that convert netscalar health monitor to AVI health monitor object :param ns_monitor: Object of health monitor :param input_dir: Input dir for command_code :param netscalar_command: Netscalar command for XL sheet status :param ns_monitor_complete_command: Full command for XL sheet status :return: health monitor object """ avi_monitor = dict() # Adding ssl-key and ssl-certificate for Secure ssl_attributes = None if ns_monitor.get('secure', []) == 'YES': # Force changing HTTP/HTTP-ECV to HTTPS in attributes if ns_monitor.get('attrs', []): attr = ns_monitor['attrs'] if 'HTTP' in attr: attr.append('HTTPS') attr.remove('HTTP') if 'HTTP-ECV' in attr: attr.append('HTTPS') attr.remove('HTTP-ECV') ns_monitor['attrs'] = attr # IF controller version is greater than 17.1 and type HTTP/S # add ssl-key and ssl-certificate # TODO: Remove this after all the clients are moved to # 17 version and above if parse_version(self.controller_version) >= parse_version('17.1')\ and 'HTTPS' in ns_monitor.get('attrs', []): profile_ref = ns_util.get_object_ref('System-Standard', 'sslprofile', 'admin') ssl_attributes = {'ssl_profile_ref': profile_ref} try: mon_name = ns_monitor['attrs'][0] # Added prefix for objects if self.prefix: mon_name = self.prefix + '-' + mon_name LOG.debug('Conversion started for monitor %s' % mon_name) avi_monitor["name"] = str(mon_name).strip().replace(" ", "_") avi_monitor["tenant_ref"] = self.tenant_ref recv_timeout = ns_monitor.get('resptimeout', '2') if 'MSEC' in recv_timeout.upper() or 'MIN' in recv_timeout.upper(): match_ob = re.findall('[0-9]+', recv_timeout) if 'MSEC' in recv_timeout.upper(): recv_timeout = int(math.ceil(float(match_ob[0]) / 1000)) else: recv_timeout = int(match_ob[0]) * 60 avi_monitor["receive_timeout"] = recv_timeout avi_monitor["failed_checks"] = ns_monitor.get('failureRetries', 3) interval = ns_monitor.get('interval', '5') if 'MIN' in interval.upper(): match_obj = re.findall('[0-9]+', ns_monitor.get('interval', '5')) interval = int(match_obj[0]) * 60 avi_monitor["send_interval"] = str(interval) if ns_monitor.get('destPort'): avi_monitor['monitor_port'] = ns_monitor.get('destPort') avi_monitor["successful_checks"] = ns_monitor.get( 'successRetries', 1) mon_type = ns_monitor['attrs'][1] if mon_type == 'PING': avi_monitor["type"] = "HEALTH_MONITOR_PING" elif mon_type == 'TCP': avi_monitor["type"] = "HEALTH_MONITOR_TCP" elif mon_type == 'TCP-ECV': avi_monitor["type"] = "HEALTH_MONITOR_TCP" send = ns_monitor.get("send", None) if send: send = send.replace('"', '') # Removed \\ from send. if '\\' in send: send = send.replace('\\', '"') response = ns_monitor.get('recv', None) if response: response = response.replace('"', '') # Removed \\ from response. if '\\' in response: response = response.replace('\\', '"') avi_monitor["tcp_monitor"] = { "tcp_request": send, "tcp_response": response, "tcp_half_open": False } elif mon_type == 'HTTPS': avi_monitor["type"] = "HEALTH_MONITOR_HTTPS" send = ns_monitor.get('httpRequest', None) if send is None: send = ns_monitor.get('send', None) if send: send = send.replace('"', '') # Removed \\ from send. if '\\' in send: send = send.replace('\\', '"') resp_code = ns_monitor.get('respCode', 'any') if resp_code: resp_code = ns_util.get_avi_resp_code(resp_code) response = ns_monitor.get('recv', None) if response: response = response.replace('"', '') # Removed \\ from response. if '\\' in response: response = response.replace('\\', '"') # TODO: Remove this after all the clients are moved to # 17 version and above if parse_version( self.controller_version) >= parse_version('17.1'): avi_monitor["https_monitor"] = { "http_request": send, "http_response_code": resp_code, "ssl_attributes": ssl_attributes, "http_response": response } if parse_version( self.controller_version) >= parse_version('17.1.6'): custom_header = ns_monitor.get('customHeaders') if custom_header: avi_monitor['https_monitor'].update({ 'exact_http_request': True, 'http_request': (send + ' HTTP/1.0' + "\r\n" + custom_header + "\r\n").replace('"', '').replace( '\\r\\n', '\r\n') if send else ('HTTP/1.0' + "\r\n" + custom_header + "\r\n").replace('"', '').replace( '\\r\\n', '\r\n') }) elif mon_type == 'HTTP': avi_monitor["type"] = "HEALTH_MONITOR_HTTP" send = ns_monitor.get('httpRequest', None) if send: send = send.replace('"', '') # Removed \\ from send. if '\\' in send: send = send.replace('\\', '"') resp_code = ns_monitor.get('respCode', None) if resp_code: resp_code = ns_util.get_avi_resp_code(resp_code) avi_monitor["http_monitor"] = { "http_request": send, "http_response_code": resp_code } if parse_version( self.controller_version) >= parse_version('17.1.6'): custom_header = ns_monitor.get('customHeaders') if custom_header: avi_monitor['http_monitor'].update({ 'exact_http_request': True, 'http_request': (send + ' HTTP/1.0' + "\r\n" + custom_header + "\r\n").replace('"', '').replace( '\\r\\n', '\r\n') if send else ('HTTP/1.0' + "\r\n" + custom_header + "\r\n").replace('"', '').replace( '\\r\\n', '\r\n') }) elif mon_type == 'HTTP-ECV': avi_monitor["type"] = "HEALTH_MONITOR_HTTP" send = ns_monitor.get("send", None) if send: send = send.replace('"', '') # Removed \\ from send. if '\\' in send: send = send.replace('\\', '"') response = ns_monitor.get('recv', None) if response: response = response.replace('"', '') # Removed \\ from response. if '\\' in response: response = response.replace('\\', '"') avi_monitor["http_monitor"] = { "http_request": send, "http_response_code": ["HTTP_ANY"], "http_response": response } elif mon_type == 'DNS': avi_monitor["type"] = "HEALTH_MONITOR_DNS" elif mon_type == 'USER': avi_monitor["type"] = "HEALTH_MONITOR_EXTERNAL" file_name = ns_monitor.get('scriptName') cmd_code = ns_util.upload_file(input_dir + os.path.sep + file_name) if not cmd_code: skipped_status = 'File not found %s : %s' % \ (input_dir + os.path.sep + file_name, ns_monitor_complete_command) LOG.warning(skipped_status) ns_util.add_status_row(ns_monitor['line_no'], netscalar_command, avi_monitor["name"], ns_monitor_complete_command, STATUS_MISSING_FILE, skipped_status) return None ext_monitor = { "command_code": cmd_code, "command_parameters": ns_monitor.get("scriptArgs", None) } avi_monitor["external_monitor"] = ext_monitor LOG.debug('Successfully converted monitor %s' % mon_name) except: update_count('error') LOG.error('Error converting monitor %s', exc_info=True) return avi_monitor
def convert(self, ns_config, avi_config, vs_state, sysdict, vs_name_dict, vrf=None, se_group=None): """ This function defines that it convert netscalar cs vs config to vs config of AVI :param ns_config: It is dict of all netscalar commands which are supported by AVI :param avi_config: It is dict of AVI output config :param vs_state: state of vs :return: None """ policy_converter = PolicyConverter( self.tenant_name, self.cloud_name, self.tenant_ref, self.cloud_ref, self.csvs_bind_skipped, self.csvs_na_attrs, self.csvs_ignore_vals, self.csvs_bind_user_ignore, self.prefix) cs_vs_conf = ns_config.get('add cs vserver', {}) lb_vs_conf = ns_config.get('add lb vserver', {}) bindings = ns_config.get('bind cs vserver', {}) ns_service = ns_config.get('add service', {}) ns_sg = ns_config.get('add serviceGroup', {}) lbvs_avi_conf = avi_config['VirtualService'] lb_vs_mapped = [] cs_vs_list = [] # get the total size of object. self.progressbar_count = len(lb_vs_conf) self.total_size = len(lb_vs_conf) + len(cs_vs_conf) avi_config['VirtualService'] = ns_util.remove_duplicate_objects( 'VirtualService', avi_config['VirtualService']) for cs_vs_index, key in enumerate(cs_vs_conf): try: # Increment count self.progressbar_count += 1 LOG.debug("Context Switch VS conversion started for: %s" % key) lbvs_bindings = [] cs_vs = cs_vs_conf[key] ns_add_cs_vserver_command = 'add cs vserver' ns_add_cs_vserver_complete_command = \ ns_util.get_netscalar_full_command( ns_add_cs_vserver_command, cs_vs) # Skipped this CS VS if it has type which are not supported if not cs_vs['attrs'][1] in self.csvs_supported_types: skipped_status = 'Skipped:Unsupported type %s of Context ' \ 'switch VS: %s' % (cs_vs['attrs'][1], key) LOG.warning(skipped_status) ns_util.add_status_row(cs_vs['line_no'], ns_add_cs_vserver_command, key, ns_add_cs_vserver_complete_command, STATUS_SKIPPED, skipped_status) continue tt = cs_vs.get('targetType', None) if tt and tt == 'GSLB': skipped_status = 'Skipped:Unsupported target type %s of ' \ 'Context switch VS: %s' % (tt, key) LOG.warning(skipped_status) # Skipped this CS VS if targetType is GSLB ns_util.add_status_row(cs_vs['line_no'], ns_add_cs_vserver_command, key, ns_add_cs_vserver_complete_command, STATUS_SKIPPED, skipped_status) continue vs_name = cs_vs['attrs'][0] if len(cs_vs['attrs']) < 3: skipped_status = 'Skipped:No IP, port found for Context ' \ 'switch VS: %s' % key LOG.warning(skipped_status) # Skipped this CS VS if there is no IP address and port ns_util.add_status_row(cs_vs['line_no'], ns_add_cs_vserver_command, key, ns_add_cs_vserver_complete_command, STATUS_SKIPPED, skipped_status) continue ip_addr = cs_vs['attrs'][2] port = cs_vs['attrs'][3] enable_ssl = False if vs_state == 'enable': enabled = (cs_vs.get('state', 'ENABLED') == 'ENABLED') else: enabled = False if cs_vs['attrs'][1] == 'SSL': enable_ssl = True updated_vs_name = re.sub('[:]', '-', vs_name) # Added prefix for objects if self.prefix: updated_vs_name = self.prefix + '-' + updated_vs_name # Regex to check Vs has IPV6 address if yes the Skipped if re.findall(ns_constants.IPV6_Address, ip_addr) \ or ip_addr == '0.0.0.0': skipped_status = "Skipped:Invalid VIP %s" \ % ns_add_cs_vserver_command LOG.warning(skipped_status) ns_util.add_status_row(cs_vs['line_no'], ns_add_cs_vserver_command, key, ns_add_cs_vserver_complete_command, STATUS_SKIPPED, skipped_status) continue # VIP object for virtual service vip = { 'ip_address': { 'addr': ip_addr, 'type': 'V4' }, 'vip_id': 0 } vs_obj = { 'name': updated_vs_name, 'tenant_ref': self.tenant_ref, 'cloud_ref': self.cloud_ref, 'type': 'VS_TYPE_NORMAL', 'enabled': True, 'traffic_enabled': enabled, 'services': [] } if vrf: vrf_ref = ns_util.get_object_ref( vrf, 'vrfcontext', tenant=self.tenant_name, cloud_name=self.cloud_name) vs_obj['vrf_ref'] = vrf_ref if se_group: se_group_ref = ns_util.get_object_ref( se_group, 'serviceenginegroup', tenant=self.tenant_name, cloud_name=self.cloud_name) vs_obj['se_group_ref'] = se_group_ref if parse_version( self.controller_version) >= parse_version('17.1'): vs_obj['vip'] = [vip] else: vs_obj['ip_address'] = vip['ip_address'] service = {'port': port, 'enable_ssl': enable_ssl} if port in ("0", "*"): service['port'] = "1" service['port_range_end'] = "65535" vs_obj['services'].append(service) bind_conf_list = bindings.get(vs_name, None) if not bind_conf_list: continue if isinstance(bind_conf_list, dict): bind_conf_list = [bind_conf_list] http_prof = cs_vs.get('httpProfileName', None) if http_prof: # Added prefix for objects if self.prefix: http_prof = self.prefix + '-' + http_prof # Get the merge application profile name if self.object_merge_check: http_prof = merge_object_mapping['app_profile'].get( http_prof) if ns_util.object_exist('ApplicationProfile', http_prof, sysdict) or ns_util.object_exist( 'ApplicationProfile', http_prof, avi_config): LOG.info('Conversion successful: Added application ' 'profile %s for %s' % (http_prof, updated_vs_name)) http_prof_ref = ns_util.get_object_ref( http_prof, OBJECT_TYPE_APPLICATION_PROFILE, self.tenant_name) vs_obj['application_profile_ref'] = http_prof_ref # Added additional attribute like xff_enabled etc to # application profile on basis of service or service # group command addition_attr = {} if bind_conf_list: for bindlist in bind_conf_list: if bindlist.get('attrs') and len( bindlist['attrs']) == 2: ser_conf = ns_service.get( bindlist['attrs'][1]) ser_cmd = 'add service' if not ser_conf: ser_conf = ns_sg.get( bindlist['attrs'][1]) ser_cmd = 'add serviceGroup' command = \ ns_util.get_netscalar_full_command( ser_cmd, ser_conf) if 'x-forwarded-for' in command: addition_attr['xff_enabled'] = True addition_attr[ 'ssl_everywhere_enabled'] = True clttimeout = cs_vs.get('cltTimeout', None) if clttimeout: addition_attr['clttimeout'] = clttimeout clt_cmd = ns_add_cs_vserver_command + \ ' cltTimeout %s' % clttimeout LOG.info('Conversion successful : %s' % clt_cmd) ns_util.add_prop_for_http_profile( http_prof, avi_config, sysdict, addition_attr) else: LOG.warning("%s application profile doesn't exist for " "%s vs" % (http_prof, updated_vs_name)) ntwk_prof = cs_vs.get('tcpProfileName', None) if ntwk_prof: # Added prefix for objects if self.prefix: ntwk_prof = self.prefix + '-' + ntwk_prof # Get the merge network profile name if self.object_merge_check: ntwk_prof = merge_object_mapping[ 'network_profile'].get(ntwk_prof) if ns_util.object_exist('NetworkProfile', ntwk_prof, sysdict) or ns_util.object_exist( 'NetworkProfile', ntwk_prof, avi_config): LOG.info( 'Conversion successful: Added network profile ' '%s for %s' % (ntwk_prof, updated_vs_name)) ntwk_prof_ref = \ ns_util.get_object_ref(ntwk_prof, OBJECT_TYPE_NETWORK_PROFILE, self.tenant_name) vs_obj['network_profile_ref'] = ntwk_prof_ref else: vs_obj['network_profile_ref'] = ns_util.get_object_ref( 'System-TCP-Proxy', 'networkprofile', tenant='admin') LOG.error( 'Error: Not found Network profile %s for %s' % (ntwk_prof, updated_vs_name)) if not http_prof and (cs_vs['attrs'][1]).upper() == 'DNS': vs_obj['application_profile_ref'] = ns_util.get_object_ref( 'System-DNS', 'applicationprofile', tenant='admin') vs_obj['network_profile_ref'] = ns_util.get_object_ref( 'System-UDP-Per-Pkt', 'networkprofile', tenant='admin') elif not http_prof and (cs_vs['attrs'][1]).upper() == 'UDP': vs_obj['application_profile_ref'] = ns_util.get_object_ref( 'System-L4-Application', 'applicationprofile', tenant='admin') vs_obj['network_profile_ref'] = ns_util.get_object_ref( 'System-UDP-Fast-Path', 'networkprofile', tenant='admin') elif not http_prof and (cs_vs['attrs'][1]).upper() in [ 'DNS_TCP', 'TCP' ]: vs_obj['application_profile_ref'] = ns_util.get_object_ref( 'System-L4-Application', 'applicationprofile', tenant='admin') vs_obj['network_profile_ref'] = ns_util.get_object_ref( 'System-TCP-Proxy', 'networkprofile', tenant='admin') elif not http_prof and (cs_vs['attrs'][1]).upper() == 'SSL': # Added Custom Profile with http to https redirect enable ns_migration_profile = ns_util.create_http_to_https_custom_profile( ) app_name = [ app_p for app_p in avi_config['ApplicationProfile'] if app_p['name'] == ns_migration_profile['name'] ] if not app_name: avi_config['ApplicationProfile'].append( ns_migration_profile) vs_obj['application_profile_ref'] = ns_util.get_object_ref( ns_migration_profile['name'], 'applicationprofile', tenant='admin') # Adding L4 as a default profile when SSL_BRIDGE elif not http_prof and (cs_vs['attrs'][1]).upper() == \ 'SSL_BRIDGE': vs_obj['application_profile_ref'] = ns_util.get_object_ref( 'System-L4-Application', 'applicationprofile', tenant='admin') default_pool_group = None lb_vserver_bind_conf = None if enable_ssl: ssl_mappings = ns_config.get('bind ssl vserver', {}) ssl_bindings = ssl_mappings.get(key, []) if isinstance(ssl_bindings, dict): ssl_bindings = [ssl_bindings] for mapping in ssl_bindings: if 'CA' in mapping: pki_ref = mapping['attrs'][0] # Added prefix for objects if self.prefix: pki_ref = self.prefix + '-' + pki_ref if self.object_merge_check: pki_ref = merge_object_mapping[ 'pki_profile'].get(pki_ref) if [ pki_profile for pki_profile in ( sysdict['PKIProfile'] + avi_config["PKIProfile"]) if pki_profile['name'] == pki_ref ]: pki_ref = ns_util.get_object_ref( pki_ref, OBJECT_TYPE_PKI_PROFILE, self.tenant_name) app_profile_name = \ ns_util.update_application_profile( http_prof, pki_ref, self.tenant_ref, updated_vs_name, avi_config, sysdict) if app_profile_name: app_profile_with_pki_profile_ref = \ ns_util.get_object_ref(app_profile_name, OBJECT_TYPE_APPLICATION_PROFILE, self.tenant_name) vs_obj['application_profile_ref'] = \ app_profile_with_pki_profile_ref LOG.info('Added: %s PKI profile %s' % (pki_ref, key)) elif 'certkeyName' in mapping: avi_ssl_ref = 'ssl_key_and_certificate_refs' ckname = mapping['certkeyName'] # Added prefix for objects if self.prefix: ckname = self.prefix + '-' + ckname if [ obj for obj in avi_config['SSLKeyAndCertificate'] if obj['name'] == ckname ]: updated_ssl_ref = \ ns_util.get_object_ref(ckname, OBJECT_TYPE_SSL_KEY_AND_CERTIFICATE, self.tenant_name) vs_obj[avi_ssl_ref] = [updated_ssl_ref] elif [ obj for obj in avi_config['SSLKeyAndCertificate'] if obj['name'] == ckname + '-dummy' ]: updated_ssl_ref = \ ns_util.get_object_ref(ckname + '-dummy', OBJECT_TYPE_SSL_KEY_AND_CERTIFICATE, self.tenant_name) vs_obj[avi_ssl_ref] = [updated_ssl_ref] else: LOG.warning( 'Could not find ssl key cert, so adding ' 'default cert as system default insted') vs_obj[avi_ssl_ref] = [ ns_util.get_object_ref( 'System-Default-Cert', 'sslkeyandcertificate', 'admin') ] continue vs_obj[avi_ssl_ref] = [updated_ssl_ref] ssl_vs_mapping = ns_config.get('set ssl vserver', {}) mapping = ssl_vs_mapping.get(key, None) ssl_profile_name = re.sub('[:]', '-', key) # Added prefix for objects if self.prefix: ssl_profile_name = self.prefix + '-' + ssl_profile_name # Get the merge ssl profile name if self.object_merge_check: ssl_profile_name = merge_object_mapping[ 'ssl_profile'].get(ssl_profile_name, None) if mapping and [ ssl_profile for ssl_profile in (sysdict['SSLProfile'] + avi_config["SSLProfile"]) if ssl_profile['name'] == ssl_profile_name ]: updated_ssl_profile_ref = ns_util.get_object_ref( ssl_profile_name, OBJECT_TYPE_SSL_PROFILE, self.tenant_name) # Changed ssl profile name to ssl profile ref. vs_obj['ssl_profile_ref'] = updated_ssl_profile_ref LOG.debug('Added: %s SSL profile %s' % (key, key)) for bind_conf in bind_conf_list: if 'lbvserver' in bind_conf: lbvs_bindings.append(bind_conf['lbvserver']) default_pool_group = bind_conf['lbvserver'] lb_vserver_bind_conf = bind_conf LOG.debug("CS VS %s context switch between lb vs: %s" % (key, lbvs_bindings)) case_sensitive = False \ if cs_vs.get('caseSensitive', '') == 'OFF' else True # Convert netscalar policy to AVI http policy set # Sending enable_ssl to policy in order to have protocol in case # it is not provided thru redirect action url policy = policy_converter.convert(bind_conf_list, ns_config, avi_config, tmp_used_pool_group_ref, redirect_pools, 'bind cs vserver', case_sensitive, enable_ssl) for binding in lbvs_bindings: if self.prefix: binding = '%s-%s' % (self.prefix, binding) lb_vs_obj = [ obj for obj in lbvs_avi_conf if obj['name'] == binding ] if lb_vs_obj: lb_vs_obj = lb_vs_obj[0] lb_vs_mapped.append(lb_vs_obj) lb_vs_obj = copy.deepcopy(lb_vs_obj) lb_vs_obj.update(vs_obj) vs_obj = lb_vs_obj else: policy_vs = [ obj for obj in avi_config['Lbvs'] if binding in obj and obj[binding].get('redirect_url', None) ] if policy_vs: redirect_rule = { 'index': 999, 'redirect_action': { 'keep_query': True, 'status_code': "HTTP_REDIRECT_STATUS_CODE_302", 'host': { 'tokens': [{ 'str_value': policy_vs[0][binding] ['redirect_url'], 'type': "URI_TOKEN_TYPE_HOST", 'start_index': 0, 'end_index': 65535 }], 'type': "URI_PARAM_TYPE_TOKENIZED" }, 'protocol': "HTTPS", 'port': 443 }, 'enable': True, "name": '%s-default-redirect' % updated_vs_name } if policy and policy.get('http_request_policy', None): policy['http_request_policy']['rules'].append( redirect_rule) elif policy: policy['http_request_policy']['rules'] = [ redirect_rule ] else: policy = { 'name': "vs-%s-HTTP-Policy-Set" % updated_vs_name, 'tenant_ref': self.tenant_ref, 'http_request_policy': { 'rules': [redirect_rule] }, 'is_internal_policy': False } ns_util.update_status_target_lb_vs_to_indirect( binding) else: continue vs_obj.pop('pool_group_ref', None) # Took lbvs policy if any and clone for csvs lbvs_policy = vs_obj.pop('http_policies', []) if lbvs_policy: for policy_attr in lbvs_policy: policy_ref = policy_attr.get('http_policy_set_ref') policy_name = ns_util.get_name(policy_ref) if \ policy_ref else None policy_objs = [ ob for ob in avi_config['HTTPPolicySet'] if ob['name'] == policy_name ] policy_obj = policy_objs[0] if policy_objs else {} if policy_obj: ns_util.add_policy(policy_obj, updated_vs_name, avi_config, tmp_policy_ref, vs_obj, self.tenant_name, self.cloud_name, self.prefix, tmp_used_pool_group_ref) # Add the http policy set reference to VS in AVI if policy: policy['name'] = policy['name'] + updated_vs_name # Added fix for same policy referred in multiple vs ns_util.add_policy(policy, updated_vs_name, avi_config, tmp_policy_ref, vs_obj, self.tenant_name, self.cloud_name, self.prefix, tmp_used_pool_group_ref) # Add reference of pool group to VS if default_pool_group: pool_group_ref = '%s-poolgroup' % default_pool_group updated_pool_group_ref = re.sub('[:]', '-', pool_group_ref) # Added prefix for objects if self.prefix: updated_pool_group_ref = self.prefix + '-' + \ updated_pool_group_ref pools = [ pool_group['name'] for pool_group in avi_config['PoolGroup'] if pool_group['name'] == updated_pool_group_ref ] if pools: ns_util.update_status_target_lb_vs_to_indirect( default_pool_group) # clone the pool group if it is referenced to other VS # to http policy set if updated_pool_group_ref in tmp_used_pool_group_ref: updated_pool_group_ref = ns_util.clone_pool_group( updated_pool_group_ref, vs_name, avi_config, self.tenant_name, self.cloud_name, userprefix=self.prefix) avi_pool_group_ref = ns_util.get_object_ref( updated_pool_group_ref, OBJECT_TYPE_POOL_GROUP, self.tenant_name, self.cloud_name) vs_obj['pool_group_ref'] = avi_pool_group_ref if lb_vserver_bind_conf: bind_cs_vserver_command = 'bind cs vserver' bind_cs_vserver_complete_command = ns_util. \ get_netscalar_full_command(bind_cs_vserver_command, lb_vserver_bind_conf) LOG.debug('Conversion successful : %s' % bind_cs_vserver_complete_command) conv_status = ns_util.get_conv_status( bind_conf, self.csvs_bind_skipped, [], [], user_ignore_val=self.csvs_bind_user_ignore) ns_util.add_conv_status(lb_vserver_bind_conf['line_no'], bind_cs_vserver_command, lb_vserver_bind_conf['attrs'][0], bind_cs_vserver_complete_command, conv_status, vs_obj) # Verify that this cs vs has share the same VIP of another vs # If yes then skipped this cs vs is_shared = ns_util.is_shared_same_vip( vs_obj, cs_vs_list, avi_config, self.tenant_name, self.cloud_name, self.tenant_ref, self.cloud_ref, self.controller_version, self.prefix) if is_shared: skipped_status = 'Skipped: %s Same vip shared by another ' \ 'virtual service' % vs_name LOG.warning(skipped_status) ns_util.add_status_row(cs_vs['line_no'], ns_add_cs_vserver_command, key, ns_add_cs_vserver_complete_command, STATUS_SKIPPED, skipped_status) continue # Added code to skipped L4 VS if pool or pool group not present if vs_obj.get('application_profile_ref'): app_name = ns_util.get_name( vs_obj['application_profile_ref']) application_profile_obj = [ obj for obj in (sysdict['ApplicationProfile'] + avi_config['ApplicationProfile']) if obj['name'] == app_name ] if (application_profile_obj and application_profile_obj[0][ 'type'] == 'APPLICATION_PROFILE_TYPE_L4') or app_name == \ 'System-L4-Application': if not vs_obj.get('pool_ref', vs_obj.get('pool_group_ref')): vs_conv_status = STATUS_SKIPPED skipped_status = "Skipped:Failed to convert L4 VS "\ "dont have pool or pool group ref"\ " %s" % vs_name LOG.debug(skipped_status) ns_util.add_status_row( cs_vs['line_no'], ns_add_cs_vserver_command, key, ns_add_cs_vserver_complete_command, vs_conv_status, skipped_status) continue if vs_obj.get('pool_group_ref'): tmp_used_pool_group_ref.append(updated_pool_group_ref) cs_vs_list.append(vs_obj) # Add summery of this cs vs in CSV/report conv_status = ns_util.get_conv_status( cs_vs, self.csvs_skip_attrs, self.csvs_na_attrs, [], ignore_for_val=self.csvs_ignore_vals, user_ignore_val=self.csvs_user_ignore) ns_util.add_conv_status(cs_vs['line_no'], ns_add_cs_vserver_command, key, ns_add_cs_vserver_complete_command, conv_status, vs_obj) LOG.debug("Context Switch VS conversion completed for: %s" % key) except: update_count('error') LOG.error('Error in cs vs conversion for: %s' % key, exc_info=True) # Calling progress bar function. msg = "VirtualService Conversion started..." ns_util.print_progress_bar(self.progressbar_count, self.total_size, msg, prefix='Progress', suffix='') vs_list = [obj for obj in lbvs_avi_conf if obj not in lb_vs_mapped] vs_list += cs_vs_list avi_config['VirtualService'] = vs_list ns_util.get_vs_if_shared_vip(avi_config, self.controller_version) # Update the index value of all policy rules as per their priority ns_util.set_rules_index_for_http_policy_set(avi_config) ns_util.clean_virtual_service_from_avi_config(avi_config, self.controller_version)
def convert(self, ns_config, avi_config, sysdict, vrf): """ Converts service or service groups bound to VS to avi Pool entity :param ns_config: Netscaler parsed config :param avi_config: Avi converted config :param tenant_ref: Tenant :param cloud_ref: Cloud ref :return: None """ used_pool_ref = [] groups = ns_config.get('bind lb vserver', {}) # Added total size. self.total_size = self.total_size + len(groups) lb_vs_conf = ns_config.get('add lb vserver', {}) avi_config['PoolGroup'] = [] set_lb_groups = ns_config.get('set lb group', {}) bind_lb_groups = ns_config.get('bind lb group', {}) ns_bind_lb_vserver_command = 'bind lb vserver' # Conversion set ssl service netscalar commands to pool in AVI self.service_convert(ns_config, avi_config, sysdict, vrf) ns_dns = ns_config.get('add dns addRec', {}) for dns_key in ns_dns: dns_obj = ns_dns.get(dns_key, []) if isinstance(dns_obj, dict): dns_obj = [dns_obj] ns_dns_command = 'add dns addRec' for element in dns_obj: ns_dns_complate_command = ns_util.get_netscalar_full_command( ns_dns_command, element) # Add status indirect to all add dns addRec netscalar commands # which are indirectly converted to AVI ns_util.add_status_row( element['line_no'], ns_dns_command, dns_key, ns_dns_complate_command, STATUS_INDIRECT) for group_key in groups: # incremented progress bar self.progressbar_count += 1 try: if not group_key: skipped_status = 'Skipped: No bind lb vserver found. ' \ 'Skipped pool' % group_key LOG.warning(skipped_status) # Skipped service if could not found bind lb vs ns_util.add_status_row( None, ns_bind_lb_vserver_command, group_key, None, STATUS_SKIPPED, skipped_status) continue group = groups.get(group_key) if isinstance(group, dict): group = [group] lb_vs = lb_vs_conf.get(group_key) ns_bind_lb_vserver_complete_command = \ ns_util.get_netscalar_full_command( ns_bind_lb_vserver_command, group[0]) if not lb_vs: for element in group: # Skipped service if could not found add lb vs skipped_status = 'Skipped: No add lb vserver found. ' \ 'Skipped pool %s' % element['attrs'][0] ns_bind_lb_vserver_complete_command = \ ns_util.get_netscalar_full_command( ns_bind_lb_vserver_command, element) ns_util.add_status_row( element['line_no'], ns_bind_lb_vserver_command, element['attrs'][0], ns_bind_lb_vserver_complete_command, STATUS_INCOMPLETE_CONFIGURATION) LOG.warning(skipped_status) continue ns_algo = lb_vs.get('lbMethod', 'LEASTCONNECTIONS') service_type = lb_vs['attrs'][1] algo = ns_util.get_avi_lb_algorithm(ns_algo) pg_members = [] for element in group: if len(element['attrs']) < 2: # Skipped this service if it doen not have any server continue full_cmd = ns_util.get_netscalar_full_command( ns_bind_lb_vserver_command, element) service = element['attrs'][1] pool_name = re.sub('[:]', '-', service + '-pool') # Added prefix for objects if self.prefix: pool_name = self.prefix + '-' + pool_name pool = [pool for pool in avi_config['Pool'] if pool['name'] == pool_name] if pool: if pool_name in used_pool_ref: # Cloning the pool if it is attached to more than # one VS pool_name = ns_util.clone_pool( pool_name, group_key, avi_config, userprefix=self.prefix) pool[0]['lb_algorithm'] = algo if not pool[0].get('health_monitor_refs') and not \ pool[0].get('use_service_port', False): if service_type == 'TCP': pool[0]['health_monitor_refs'] = [ ns_util.get_object_ref( 'tcp-default', OBJECT_TYPE_HEALTH_MONITOR, self.tenant_name)] else: pool[0]['health_monitor_refs'] = [ ns_util.get_object_ref( 'ping-default', OBJECT_TYPE_HEALTH_MONITOR, self.tenant_name)] updated_pool_ref = ns_util.get_object_ref( pool_name, OBJECT_TYPE_POOL, self.tenant_name, self.cloud_name) pg_members.append({'pool_ref': updated_pool_ref, 'ratio': 1, 'priority_label': '10' }) used_pool_ref.append(pool_name) LOG.info('Conversion successful : %s' % full_cmd) # Add summery of add server in CSV/report # Changed the status to INDIRECT for binding of pool to # VS ns_util.add_status_row( element['line_no'], ns_bind_lb_vserver_command, element['attrs'][0], full_cmd, STATUS_INDIRECT) else: # Skipped add server if pool not found in AVI skipped_status = 'Skipped :Pool is not created %s' \ % element['attrs'][0] LOG.warning(skipped_status) ns_util.add_status_row( element['line_no'], ns_bind_lb_vserver_command, element['attrs'][0], full_cmd, STATUS_SKIPPED, skipped_status) pg_name = group_key + '-poolgroup' pg_name = re.sub('[:]', '-', pg_name) # Added prefix for objects if self.prefix: pg_name = self.prefix + '-' + pg_name if pg_members: pool_group = { 'name': pg_name, 'members': pg_members, 'tenant_ref': self.tenant_ref, 'cloud_ref': self.cloud_ref } avi_config['PoolGroup'].append(pool_group) except Exception as e: update_count('error') LOG.error('Error in bind lb vserver conversion bound to: %s' % group_key, exc_info=True) msg = "PoolGroup Conversion started..." ns_util.print_progress_bar(self.progressbar_count, self.total_size, msg, prefix='Progress', suffix='') # Support for set lb group and bind lb group for set_lb_group_key in set_lb_groups: set_lb_group = set_lb_groups.get(set_lb_group_key) set_lb_group_mappings = bind_lb_groups.get(set_lb_group['attrs'][0], []) persistenceType = set_lb_group.get('persistenceType', '') profile_name = '%s-persistance-profile' % set_lb_group['attrs'][0] ns_set_lb_group_command = 'set lb group' ns_set_lb_group_complate_command = \ ns_util.get_netscalar_full_command(ns_set_lb_group_command, set_lb_group) # Added prefix before object if self.prefix: profile_name = self.prefix + '-' + profile_name if persistenceType in self.lbvs_supported_persist_types: application_persistence_profile = \ ns_util.convert_persistance_prof(set_lb_group, profile_name, self.tenant_ref) app_persist_profile_name = \ application_persistence_profile['name'] if self.object_merge_check: dup_of = ns_util.update_skip_duplicates( application_persistence_profile, avi_config[ 'ApplicationPersistenceProfile'], 'app_persist_profile', merge_object_mapping, app_persist_profile_name, persistenceType, self.prefix, sysdict['ApplicationPersistenceProfile']) if dup_of: app_per_merge_count['count'] += 1 app_persist_profile_name = merge_object_mapping[ 'app_persist_profile'].get( app_persist_profile_name, None) else: avi_config['ApplicationPersistenceProfile'].append( application_persistence_profile) else: avi_config['ApplicationPersistenceProfile'].append( application_persistence_profile) application_persistence_profile_ref = \ ns_util.get_object_ref(app_persist_profile_name, OBJECT_TYPE_APPLICATION_PERSISTENCE_PROFILE, self.tenant_name) # Added status successful in CSV/report if application # persistence profile create ns_util.add_status_row( set_lb_group['line_no'], ns_set_lb_group_command, set_lb_group['attrs'][0], ns_set_lb_group_complate_command, STATUS_SUCCESSFUL, application_persistence_profile) else: skipped_status = 'Skipped:Persistance type %s not ' \ 'supported by Avi' % persistenceType LOG.warning(skipped_status) # Skipped set lb group if type perstistence profile not # supported by AVI ns_util.add_status_row( set_lb_group['line_no'], ns_set_lb_group_command, set_lb_group['attrs'][0], ns_set_lb_group_complate_command, STATUS_SKIPPED, skipped_status) continue if isinstance(set_lb_group_mappings, dict): set_lb_group_mappings = [set_lb_group_mappings] for bind_lb_group in set_lb_group_mappings: ns_bind_lb_group_command = 'bind lb group' ns_bind_lb_group_complate_command = \ ns_util.get_netscalar_full_command(ns_bind_lb_group_command, bind_lb_group) pool_group_name = bind_lb_group['attrs'][1] + '-poolgroup' # Added prefix for objects if self.prefix: pool_group_name = self.prefix + '-' + pool_group_name pool_group = [pool_group for pool_group in avi_config['PoolGroup'] if pool_group['name'] == pool_group_name] # Skipped if pool group not found in AVI if not pool_group: skipped_status = "Skipped: Pool group %s not found" \ % bind_lb_group['attrs'][1] LOG.warning(skipped_status) ns_util.add_status_row( bind_lb_group['line_no'], ns_bind_lb_group_command, bind_lb_group['attrs'][0], ns_bind_lb_group_complate_command, STATUS_SKIPPED, skipped_status) continue for pool_member in pool_group[0]['members']: pool_name = \ pool_member['pool_ref'].split('&')[1].split('=')[1] pool = [pool for pool in avi_config['Pool'] if pool['name'] == pool_name] if pool: pool[0]['application_persistence_profile_ref'] = \ application_persistence_profile_ref # add successful statusin CSV/report for bind lb group # updated pool with application perstistence profile ref ns_util.add_status_row( bind_lb_group['line_no'], ns_bind_lb_group_command, bind_lb_group['attrs'][0], ns_bind_lb_group_complate_command, STATUS_SUCCESSFUL, pool_group[0])
def convert(self, f5_config, avi_config, user_ignore, tenant_ref, cloud_name, merge_object_mapping, sys_dict, vrf=None, segroup=None): """ :param f5_config: parsed f5 config dict :param avi_config: dict for avi conversion :param user_ignore: Ignore config defined by user :param tenant_ref: tenant for which config need to be converted :param cloud_name: cloud for which config need to be converted :param merge_object_mapping: flag for merge object :param sys_dict: baseline profile dict :return: """ pool_list = [] pool_config = f5_config.get('pool', {}) user_ignore = user_ignore.get('pool', {}) avi_config['VrfContext'] = [] avi_config['PoolGroup'] = [] avi_config['PriorityLabels'] = {} # Initialize Global vrf context object vrf_context = { "name": 'global', "system_default": True, "tenant_ref": conv_utils.get_object_ref('admin', 'tenant'), "cloud_ref": conv_utils.get_object_ref(cloud_name, 'cloud'), "static_routes": [] } avi_config['VrfContext'].append(vrf_context) total_size = len(pool_config.keys()) # Added variable to get total object count. progressbar_count = 0 print "Converting Pools..." for pool_name in pool_config.keys(): progressbar_count += 1 LOG.debug("Converting Pool: %s" % pool_name) f5_pool = pool_config[pool_name] if not f5_pool: msg = "Empty pool skipped for conversion :%s" % pool_name LOG.debug(msg) conv_utils.add_status_row('pool', None, pool_name, conv_const.STATUS_SKIPPED, msg) continue if 'gateway-failsafe-device' in f5_pool: msg = ("Not supported gateway-failsafe-device, pool skipped " "for conversion :%s" % pool_name) LOG.debug(msg) conv_utils.add_status_row('pool', None, pool_name, conv_const.STATUS_SKIPPED, msg) continue try: converted_objs = self.convert_pool( pool_name, f5_config, avi_config, user_ignore, tenant_ref, cloud_name, merge_object_mapping, sys_dict, vrf, segroup) pool_list += converted_objs['pools'] if 'pg_obj' in converted_objs: avi_config['PoolGroup'].extend(converted_objs['pg_obj']) LOG.debug("Conversion successful for Pool: %s" % pool_name) except: update_count('error') LOG.error("Failed to convert pool: %s" % pool_name, exc_info=True) conv_utils.add_status_row('pool', None, pool_name, conv_const.STATUS_ERROR) # Added call to check progress. msg = "Pool and PoolGroup conversion started..." conv_utils.print_progress_bar(progressbar_count, total_size, msg, prefix='Progress', suffix='') avi_config['Pool'] = pool_list LOG.debug("Converted %s pools" % len(pool_list)) f5_config.pop('pool', {})
def convert_monitor(self, ns_monitor, input_dir, netscalar_command, ns_monitor_complete_command): """ This functions defines that convert netscalar health monitor to AVI health monitor object :param ns_monitor: Object of health monitor :param input_dir: Input dir for command_code :param tenant_ref: Tenant :param cloud_ref: Cloud ref :return: health monitor object """ avi_monitor = dict() try: LOG.debug('Conversion started for monitor %s' % ns_monitor['attrs'][0]) avi_monitor["name"] = (ns_monitor['attrs'][0]).strip().\ replace(" ", "_") avi_monitor["tenant_ref"] = self.tenant_ref avi_monitor["receive_timeout"] = ns_monitor.get('resptimeout', 2) avi_monitor["failed_checks"] = ns_monitor.get('failureRetries', 3) interval = ns_monitor.get('interval', '5') if 'MIN' in interval.upper(): matchObj = re.findall('[0-9]+', ns_monitor.get('interval', '5')) interval = int(matchObj[0]) * 60 avi_monitor["send_interval"] = str(interval) if ns_monitor.get('destPort'): avi_monitor['monitor_port'] = ns_monitor.get('destPort') avi_monitor["successful_checks"] = ns_monitor.get( 'successRetries', 1) mon_type = ns_monitor['attrs'][1] if mon_type == 'PING': avi_monitor["type"] = "HEALTH_MONITOR_PING" elif mon_type == 'TCP': avi_monitor["type"] = "HEALTH_MONITOR_TCP" elif mon_type == 'HTTP': avi_monitor["type"] = "HEALTH_MONITOR_HTTP" send = ns_monitor.get('httpRequest', None) respCode = ns_monitor.get('respCode', None) if respCode: respCode = ns_util.get_avi_resp_code(respCode) avi_monitor["http_monitor"] = { "http_request": send, "http_response_code": respCode } elif mon_type == 'HTTP-ECV': avi_monitor["type"] = "HEALTH_MONITOR_HTTP" send = ns_monitor.get("send", None) response = ns_monitor.get('recv', None) avi_monitor["http_monitor"] = { "http_request": send, "http_response_code": ["HTTP_ANY"], "http_response": response } elif mon_type == 'DNS': avi_monitor["type"] = "HEALTH_MONITOR_DNS" elif mon_type == 'USER': avi_monitor["type"] = "HEALTH_MONITOR_EXTERNAL" file_name = ns_monitor.get('scriptName') cmd_code = ns_util.upload_file(input_dir + os.path.sep + file_name) if not cmd_code: skipped_status = 'File not found %s : %s' % \ (input_dir + os.path.sep + file_name, ns_monitor_complete_command) LOG.warning(skipped_status) ns_util.add_status_row(ns_monitor['line_no'], netscalar_command, avi_monitor["name"], ns_monitor_complete_command, STATUS_MISSING_FILE, skipped_status) return None ext_monitor = { "command_code": cmd_code, "command_parameters": ns_monitor.get("scriptArgs", None) } avi_monitor["external_monitor"] = ext_monitor LOG.debug('Successfully converted monitor %s' % ns_monitor['attrs'][0]) except: update_count('error') LOG.error('Error converting monitor %s', exc_info=True) return avi_monitor
def convert(self, f5_config, avi_config, user_ignore, tenant_ref, merge_object_mapping, sys_dict): """ :param f5_config: parsed f5 config :param avi_config: dict of avi config :param user_ignore: Ignore config defined by user :param tenant_ref: tenant of which output to converted :param merge_object_mapping: flag for object merge :param sys_dict: baseline profile :return: """ if 'IpAddrGroup' not in avi_config: avi_config['IpAddrGroup'] = [] converted_objs = [] f5_datagroup_dict = f5_config.get('data-group', {}) user_ignore = user_ignore.get('data-group', {}) # Added variable to get total object count. progressbar_count = 0 total_size = len(f5_datagroup_dict.keys()) print "Converting Data groups..." for key in f5_datagroup_dict.keys(): progressbar_count += 1 data_group_type = None name = None skipped = [] # Added call to check the progress. msg = "data-group conversion started..." conv_utils.print_progress_bar(progressbar_count, total_size, msg, prefix='Progress', suffix='') dg_type = None try: tenant, name = conv_utils.get_tenant_ref(key) # dg_scope, name = key.split(" ") # name = name.split('/')[-1] LOG.debug("Converting datagroup: %s" % name) dg_config = f5_datagroup_dict[key] if tenant_ref != 'admin': tenant = tenant_ref if self.prefix: name = '{}-{}'.format(self.prefix, name) dg_type = dg_config['type'] if dg_type == 'ip': ip_group = self.convert_ip_group(name, dg_config, avi_config, skipped, tenant) else: msg = 'data-group type not supported skipping ' \ 'conversion: %s' % name LOG.warning(msg) self.update_conv_status_for_skip(dg_type, name, msg) continue if not ip_group: continue # code to merge Data groups. if self.object_merge_check: conv_utils.update_skip_duplicates( ip_group, avi_config['IpAddrGroup'], 'ip_group', converted_objs, name, None, merge_object_mapping, dg_type, self.prefix, sys_dict['IpAddrGroup']) self.dg_count += 1 else: avi_config["IpAddrGroup"].append(ip_group) conv_status = conv_utils.get_conv_status( skipped, dict(), dict(), ip_group, user_ignore) self.update_conversion_status(conv_status, dg_type, name, ip_group) except: update_count('error') LOG.error("Failed to convert data-group : %s" % key, exc_info=True) self.update_conv_status_for_error(name, dg_type, key) count = len(avi_config["IpAddrGroup"]) LOG.debug("Converted %s ip group" % count) f5_config.pop('data-group', None)
def convert(meta, ns_config_dict, tenant_name, cloud_name, version, output_dir, input_dir, skipped_cmds, vs_state, object_merge_check,report_name, prefix, vs_name_dict, profile_path, redirect, key_passphrase=None, user_ignore={}, vs_level_status=False, vrf=None, segroup=None): """ This functions defines that it convert service/servicegroup to pool Convert pool group of netscalar bind lb vserver configuration :param ns_config_dict: Dict of netscalar commands :param tenant: Tenant :param cloud_ref: Cloud ref :param version: Version :param output_dir: Output dir for write AVI object after conversion :param input_dir: Input dir is to keep cert and keys :param skipped_cmds: List of skipped commands :param vs_state: VS state :param object_merge_check: Flag of object merge :param report_name: name of input file :param: prefix: prefix for objects :param key_passphrase: path of passphrase yaml file :param user_ignore: Dict of user ignore attributes :param vs_level_status: Add columns of vs reference overall skipped settings :return: None """ ssl_ciphers_yaml = 'ssl_ciphers.yaml' # load ssl ciphers with open(os.path.dirname(__file__) + '/%s'% ssl_ciphers_yaml) as stream: ssl_ciphers = yaml.safe_load(stream) LOG.debug('Conversion Started') tenant_ref = ns_util.get_object_ref(tenant_name, 'tenant') cloud_ref = ns_util.get_object_ref(cloud_name, 'cloud') try: # call meta from super class avi_config = dict() sys_dict = dict() avi_config['META'] = meta # avi_obj.meta(tenant_name, version) merge_object_type = ['ApplicationProfile', 'NetworkProfile', 'SSLProfile', 'PKIProfile', 'ApplicationPersistenceProfile', 'HealthMonitor'] # Constructed avi config dict and baseline object dict for objects # which can be merged for key in merge_object_type: sys_dict[key] = [] avi_config[key] = [] # Read the baseline json file and filled the baseline object dict if profile_path and os.path.exists(profile_path): with open(profile_path) as data: prof_data = json.load(data) for key in merge_object_type: sys_dict[key] = prof_data.get(key, []) monitor_converter = MonitorConverter( tenant_name, cloud_name, tenant_ref, cloud_ref, user_ignore, prefix, object_merge_check, version) monitor_converter.convert(ns_config_dict, avi_config, input_dir, sys_dict) profile_converter = ProfileConverter( tenant_name, cloud_name,tenant_ref, cloud_ref, ssl_ciphers, object_merge_check, user_ignore, prefix, key_passphrase) profile_converter.convert(ns_config_dict, avi_config, input_dir, sys_dict) service_converter = ServiceConverter( tenant_name, cloud_name,tenant_ref, cloud_ref, object_merge_check, user_ignore, prefix) service_converter.convert(ns_config_dict, avi_config, sys_dict, vrf) lbvs_converter = LbvsConverter( tenant_name, cloud_name, tenant_ref, cloud_ref, object_merge_check, version, user_ignore, prefix) lbvs_converter.convert(ns_config_dict, avi_config, vs_state, sys_dict, vs_name_dict, vrf, segroup) csvs_converter = CsvsConverter( tenant_name, cloud_name, tenant_ref, cloud_ref, object_merge_check, version, user_ignore, prefix) csvs_converter.convert(ns_config_dict, avi_config, vs_state, sys_dict, vs_name_dict, vrf, segroup) if object_merge_check: # Updating the reference for application persistence profile as we # are assigning reference at the time of profile creation ns_util.update_profile_ref('application_persistence_profile_ref', avi_config['Pool'], merge_object_mapping['app_persist_profile']) # Updating the reference for application profile as we # are assigning reference at the time of profile creation ns_util.update_profile_ref('application_profile_ref', avi_config['VirtualService'], merge_object_mapping['app_profile']) # Add status for skipped netscalar commands in CSV/report ns_util.update_status_for_skipped(skipped_cmds) if redirect: # Removing VS and changing the status in CSV which got redirected # Scenario for redirect - HTTP VS having no pool but redirect to # HTTPS VS ns_util.vs_redirect_http_to_https(avi_config, sys_dict) # Merging the pools in a pool group if pools are having same health # monitor ns_util.merge_pool(avi_config) # Add/update CSV/report ns_util.add_complete_conv_status(ns_config_dict, output_dir, avi_config, report_name, vs_level_status) LOG.debug('Conversion completed successfully') ns_util.cleanup_config(tmp_avi_config) ns_util.cleanup_dupof(avi_config) avi_config.pop('Lbvs', None) # Validating the aviconfig after generation ns_util.validation(avi_config) # added code to get fully converted virtual service. for key in avi_config: if key != 'META': if key == 'VirtualService': if vs_level_status: LOG.info('Total Objects of %s : %s (%s full conversions)' % (key,len(avi_config[key]), nsu.fully_migrated)) print 'Total Objects of %s : %s (%s full conversions)'\ % (key, len(avi_config[key]), nsu.fully_migrated) else: LOG.info( 'Total Objects of %s : %s' % (key, len(avi_config[key]))) print 'Total Objects of %s : %s' \ % (key, len(avi_config[key])) continue # Added code to print merged count. elif object_merge_check and key == 'SSLProfile': profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config[key]), abs(profile_converter.ssl_merge_count), abs(profile_converter.ssl_merge_count) + len(avi_config[key])) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'ApplicationProfile': profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config[key]), abs(app_merge_count['count']), abs(app_merge_count['count']) + len(avi_config[key])) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'NetworkProfile': profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config[key]), abs(profile_converter.network_merge_count), abs(profile_converter.network_merge_count) + len(avi_config[key])) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == \ 'ApplicationPersistenceProfile': profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config[key]), abs(app_per_merge_count['count']), abs(app_per_merge_count['count']) + len(avi_config[key])) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'HealthMonitor': monitor_merged_message = \ 'Total Objects of %s : %s (%s/%s monitor merged)' % \ (key, len(avi_config[key]), abs(monitor_converter.monitor_merge_count), abs(monitor_converter.monitor_merge_count) + len(avi_config[key])) LOG.info(monitor_merged_message) print monitor_merged_message continue LOG.info('Total Objects of %s : %s' % (key, len(avi_config[key]))) print 'Total Objects of %s : %s' % (key, len(avi_config[key])) except: update_count('warning') LOG.error('Error in config conversion', exc_info=True) return avi_config
def convert(self, ns_config, avi_config, vs_state, sysdict, vs_name_dict, vrf=None, segroup=None): """ This function defines that it convert netscalar lb vs config to vs config of AVI :param ns_config: It is dict of all netscalar commands which are supported by AVI :param avi_config: It is dict of AVI output config :param vs_state: state of vs :return: None """ lb_vs_conf = ns_config.get('add lb vserver', {}) bind_lb_vs_config = ns_config.get('bind lb vserver', {}) cs_vs_conf = ns_config.get('add cs vserver', {}) ns_service = ns_config.get('add service', {}) ns_sg = ns_config.get('add serviceGroup', {}) avi_config['VirtualService'] = [] avi_config['Lbvs'] = [] tmp_avi_config['VirtualService'] = [] # get the total size of object. self.total_size = len(lb_vs_conf) + len(cs_vs_conf) avi_config['HTTPPolicySet'] = [] if parse_version(self.controller_version) >= parse_version('17.1'): avi_config['VsVip'] = [] supported_types = [ 'HTTP', 'TCP', 'UDP', 'SSL', 'SSL_BRIDGE', 'SSL_TCP', 'DNS', 'DNS_TCP' ] policy_converter = PolicyConverter(self.tenant_name, self.cloud_name, self.tenant_ref, self.cloud_ref, self.lbvs_skip_attrs, self.lbvs_na_attrs, self.lbvs_ignore_vals, self.lbvs_user_ignore, self.prefix) print "Converting VirtualServices..." for key in lb_vs_conf.keys(): try: LOG.debug('LB VS conversion started for: %s' % key) # Increment the count self.progressbar_count += 1 lb_vs = lb_vs_conf[key] type = lb_vs['attrs'][1] cmd = 'add lb vserver' full_cmd = ns_util.get_netscalar_full_command(cmd, lb_vs) # Skipped this lb vs if it has type which are not supported if type not in supported_types: skipped_status = 'Skipped:Unsupported type %s of LB VS: ' \ '%s' % (type, key) LOG.warning(skipped_status) ns_util.add_status_row(lb_vs['line_no'], cmd, key, full_cmd, STATUS_SKIPPED, skipped_status) continue # Skipped VS if type is not SSL but persistence is of SSL type if type != 'SSL' and lb_vs.get('persistenceType') == \ 'SSLSESSION': skipped_status = "Skipped:Secure persistence is applicable"\ " only if SSL is enabled for Virtual " \ "Service %s" % key LOG.warning(skipped_status) ns_util.add_status_row(lb_vs['line_no'], cmd, key, full_cmd, STATUS_SKIPPED, skipped_status) continue enable_ssl = False # removing 'SSL_BRIDGE' 'SSL_TCP' so as to have L4 app profile if type in ['SSL']: enable_ssl = True vs_name = key ip_addr = lb_vs['attrs'][2] port = lb_vs['attrs'][3] if vs_state == 'enable': enabled = (lb_vs.get('state', 'ENABLED') == 'ENABLED') else: enabled = False pool_group_name = '%s-poolgroup' % vs_name # Added prefix for objects if self.prefix: pool_group_name = self.prefix + '-' + pool_group_name pool_group_name = re.sub('[:]', '-', pool_group_name) pool_group = [ pool_group for pool_group in avi_config.get("PoolGroup", []) if pool_group['name'] == pool_group_name ] pool_group_ref = None if pool_group: pool_group_ref = pool_group_name redirect_url = lb_vs.get('redirectURL', None) backup_server = lb_vs.get('backupVServer', None) updated_vs_name = re.sub('[:]', '-', vs_name) # Added ns vs dict vs_name_dict['lbvs'][updated_vs_name] = vs_name # Added prefix for objects if self.prefix: updated_vs_name = self.prefix + '-' + updated_vs_name # # Regex to check Vs has IPV6 address if yes the Skipped # if re.findall(ns_constants.IPV6_Address, ip_addr) or \ # ip_addr == '0.0.0.0': # if redirect_url: # redirect_dict = {updated_vs_name: redirect_url} # avi_config['Lbvs'].append(redirect_dict) # if backup_server: # backup_server_name = {} # skipped_status = "Skipped:Invalid VIP %s" % full_cmd # LOG.warning(skipped_status) # ns_util.add_status_row( # lb_vs['line_no'], cmd, key, full_cmd, STATUS_SKIPPED, # skipped_status) # continue # VIP object for virtual service vip = { 'ip_address': { 'addr': ip_addr, 'type': 'V4' }, 'vip_id': 0 } vs_obj = { 'name': updated_vs_name, 'type': 'VS_TYPE_NORMAL', 'tenant_ref': self.tenant_ref, 'cloud_ref': self.cloud_ref, 'enabled': enabled, 'services': [], } if vrf: vrf_ref = ns_util.get_object_ref( vrf, 'vrfcontext', tenant=self.tenant_name, cloud_name=self.cloud_name) vs_obj['vrf_ref'] = vrf_ref if segroup: se_group_ref = ns_util.get_object_ref( segroup, 'serviceenginegroup', tenant=self.tenant_name, cloud_name=self.cloud_name) vs_obj['se_group_ref'] = se_group_ref if parse_version(self.controller_version) >= \ parse_version('17.1'): vs_obj['vip'] = [vip] else: vs_obj['ip_address'] = vip['ip_address'] bind_conf_list = bind_lb_vs_config.get(key, None) # Skipped this lb vs if it do not have any bind lb vserver if (not bind_conf_list) and (not redirect_url): continue if isinstance(bind_conf_list, dict): bind_conf_list = [bind_conf_list] policy = None if bind_conf_list: # Convert netscalar policy to AVI http policy set # Sending enable_ssl to policy in order to have protocol # in case it is not provided thru redirect action url policy = policy_converter.convert(bind_conf_list, ns_config, avi_config, [], redirect_pools, 'bind lb vserver', True, enable_ssl) # TODO move duplicate code for adding policy to vs in ns_util # Convert netscalar policy to AVI http policy set if policy: if policy['name'] in tmp_policy_ref: # Cloned the policy if it is already used policy = ns_util.clone_http_policy_set( policy, updated_vs_name, avi_config, self.tenant_name, self.cloud_name, used_pool_group_ref, userprefix=self.prefix) tmp_policy_ref.append(policy['name']) updated_http_policy_ref = ns_util.get_object_ref( policy['name'], OBJECT_TYPE_HTTP_POLICY_SET, self.tenant_name) http_policies = { 'index': 11, 'http_policy_set_ref': updated_http_policy_ref } vs_obj['http_policies'] = [] vs_obj['http_policies'].append(http_policies) avi_config['HTTPPolicySet'].append(policy) if pool_group: # clone the pool group if it is referenced to other # VS ot http policy set if pool_group_ref in used_pool_group_ref: pool_group_ref = ns_util.clone_pool_group( pool_group_ref, vs_name, avi_config, self.tenant_name, self.cloud_name, userprefix=self.prefix) pool_group_ref = re.sub('[:]', '-', pool_group_ref) updated_pool_group = [ pg for pg in avi_config.get('PoolGroup', []) if pg['name'] == pool_group_ref ] vs_obj['pool_group_ref'] = ns_util.get_object_ref( pool_group_ref, OBJECT_TYPE_POOL_GROUP, self.tenant_name, self.cloud_name) pool_group = updated_pool_group[0] http_prof = lb_vs.get('httpProfileName', None) persistence_attached = False persistence_type = lb_vs.get('persistenceType', '') if http_prof: # Added prefix for objects if self.prefix: http_prof = self.prefix + '-' + http_prof # Get the merge application profile name if self.object_merge_check: http_prof = merge_object_mapping['app_profile'].get( http_prof) if ns_util.object_exist('ApplicationProfile', http_prof, sysdict) or ns_util.object_exist( 'ApplicationProfile', http_prof, avi_config): LOG.info( 'Conversion successful: Added application profile ' '%s for %s' % (http_prof, updated_vs_name)) http_prof_ref = \ ns_util.get_object_ref(http_prof, OBJECT_TYPE_APPLICATION_PROFILE, self.tenant_name) vs_obj['application_profile_ref'] = http_prof_ref # Added additional attribute like xff_enabled etc to # application profile on basis of service or service # group command addition_attr = {} if bind_conf_list: for bindlist in bind_conf_list: if bindlist.get('attrs') and len( bindlist['attrs']) == 2: ser_conf = ns_service.get( bindlist['attrs'][1]) ser_cmd = 'add service' if not ser_conf: ser_conf = ns_sg.get( bindlist['attrs'][1]) ser_cmd = 'add serviceGroup' command =\ ns_util.get_netscalar_full_command( ser_cmd, ser_conf) if ser_conf else '' if 'x-forwarded-for' in command: addition_attr['xff_enabled'] = True addition_attr[ 'ssl_everywhere_enabled'] = True clttimeout = lb_vs.get('cltTimeout', None) if clttimeout: addition_attr['clttimeout'] = clttimeout clt_cmd = cmd + '%s cltTimeout %s' % (key, clttimeout) LOG.info('Conversion successful : %s' % clt_cmd) ns_util.add_prop_for_http_profile( http_prof, avi_config, sysdict, addition_attr) else: LOG.warning("%s application profile doesn't exist for " "%s vs" % (http_prof, updated_vs_name)) ntwk_prof = lb_vs.get('tcpProfileName', None) if ntwk_prof: # Added prefix for objects if self.prefix: ntwk_prof = self.prefix + '-' + ntwk_prof # Get the merge network profile name if self.object_merge_check: ntwk_prof = merge_object_mapping[ 'network_profile'].get(ntwk_prof) if ns_util.object_exist('NetworkProfile', ntwk_prof, sysdict) or ns_util.object_exist( 'NetworkProfile', ntwk_prof, avi_config): LOG.info( 'Conversion successful: Added network profile ' '%s for %s' % (ntwk_prof, updated_vs_name)) ntwk_prof_ref = \ ns_util.get_object_ref(ntwk_prof, OBJECT_TYPE_NETWORK_PROFILE, self.tenant_name) vs_obj['network_profile_ref'] = ntwk_prof_ref else: LOG.warning("%s netwrok profile doesn't exist for " "%s vs" % (ntwk_prof, updated_vs_name)) if not http_prof and (lb_vs['attrs'][1]).upper() == 'DNS': vs_obj['application_profile_ref'] = ns_util.get_object_ref( 'System-DNS', 'applicationprofile', tenant='admin') vs_obj['network_profile_ref'] = ns_util.get_object_ref( 'System-UDP-Per-Pkt', 'networkprofile', tenant='admin') elif not http_prof and (lb_vs['attrs'][1]).upper() == 'UDP': vs_obj['application_profile_ref'] = ns_util.get_object_ref( 'System-L4-Application', 'applicationprofile', tenant='admin') vs_obj['network_profile_ref'] = ns_util.get_object_ref( 'System-UDP-Fast-Path', 'networkprofile', tenant='admin') elif not http_prof and (lb_vs['attrs'][1]).upper() in [ 'DNS_TCP', 'TCP' ]: vs_obj['application_profile_ref'] = ns_util.get_object_ref( 'System-L4-Application', 'applicationprofile', tenant='admin') vs_obj['network_profile_ref'] = ns_util.get_object_ref( 'System-TCP-Proxy', 'networkprofile', tenant='admin') elif not http_prof and (lb_vs['attrs'][1]).upper() == 'SSL': # Added Custom Profile with http to https redirect enable ns_migration_profile = ns_util.create_http_to_https_custom_profile( ) app_name = [ app_p for app_p in avi_config['ApplicationProfile'] if app_p['name'] == ns_migration_profile['name'] ] if not app_name: avi_config['ApplicationProfile'].append( ns_migration_profile) vs_obj['application_profile_ref'] = ns_util.get_object_ref( ns_migration_profile['name'], 'applicationprofile', tenant='admin') # Adding L4 as a default profile when SSL_BRIDGE and SSL_TCP elif not http_prof and (lb_vs['attrs'][1]).upper() \ in ['SSL_BRIDGE', "SSL_TCP"]: vs_obj['application_profile_ref'] = ns_util.get_object_ref( 'System-L4-Application', 'applicationprofile', tenant='admin') LOG.debug( "Defaulted to L4 profile for '%s' VS of type %s" % (updated_vs_name, lb_vs['attrs'][1])) # Defaulting to 'client ip' persistence profile if pool_group and persistence_type != 'NONE': persistence_attached = self.update_pool_for_persist( avi_config, pool_group, 'System-Persistence-Client-IP') LOG.debug("Defaulted to Client IP persistence profile " "for '%s' Pool group in '%s' VS of type %s" % (pool_group_ref, updated_vs_name, lb_vs['attrs'][1])) if pool_group_ref and not persistence_attached: if persistence_type in self.lbvs_supported_persist_types: profile_name = '%s-persistance-profile' % vs_name # Added prefix for objects if self.prefix: profile_name = self.prefix + '-' + profile_name persist_profile = ns_util.convert_persistance_prof( lb_vs, profile_name, self.tenant_ref) persist_profile_name = persist_profile['name'] if self.object_merge_check: dup_of = ns_util.update_skip_duplicates( persist_profile, avi_config['ApplicationPersistenceProfile'], 'app_persist_profile', merge_object_mapping, persist_profile_name, persistence_type, self.prefix, sysdict['ApplicationPersistenceProfile']) if dup_of: app_per_merge_count['count'] += 1 persist_profile_name = merge_object_mapping[ 'app_persist_profile'].get( persist_profile_name, None) else: avi_config[ 'ApplicationPersistenceProfile'].append( persist_profile) else: avi_config['ApplicationPersistenceProfile'].append( persist_profile) self.update_pool_for_persist(avi_config, pool_group, persist_profile_name) elif not persistence_type == 'NONE': LOG.warning( 'Persistance type %s not supported by Avi' % persistence_type) # Update fail action of pool as FAIL_ACTION_HTTP_REDIRECT in AVI # if lb vs has redirect url if redirect_url: fail_action = ns_util.get_redirect_fail_action( redirect_url) if pool_group: pool_group["fail_action"] = fail_action backup_configured = False if backup_server: # Add backup pool of poolgroup if this lb vs has an ip # backup vserver try: backup_pool_group_ref = backup_server + '-poolgroup' backup_pool_group_ref = re.sub('[:]', '-', backup_pool_group_ref) # Added prefix for objects if self.prefix: backup_pool_group_ref = self.prefix + '-' + \ backup_pool_group_ref backup_pool_group = [ pg for pg in avi_config.get("PoolGroup", []) if pg['name'] == backup_pool_group_ref ] backup_pool_ref = ns_util.get_name( backup_pool_group[0]['members'][0]['pool_ref']) # Added backupvserver to poolgroup new_backup_pool_ref = ns_util.clone_pool( backup_pool_ref, pool_group['name'], avi_config, userprefix=self.prefix) new_backup_pool_ref = ns_util.get_object_ref( new_backup_pool_ref, OBJECT_TYPE_POOL, self.tenant_name, self.cloud_name) backup_pool = { 'type': 'FAIL_ACTION_BACKUP_POOL', 'backup_pool': { 'backup_pool_ref': new_backup_pool_ref } } pool_group['fail_action'] = backup_pool backup_configured = True except Exception as e: update_count('warning') # Skipped lb vs if backup pool is found in AVI LOG.error('No Backup pool found: %s' % full_cmd) ns_util.add_status_row(lb_vs['line_no'], cmd, key, full_cmd, STATUS_INDIRECT) continue elif ip_addr == "0.0.0.0" and not redirect_url and not \ backup_server: # Skipped lb vs if it has ip 0.0.0.0 and does not have # backup pool and redirect url if pool_group: ns_util.add_status_row(lb_vs['line_no'], cmd, key, full_cmd, STATUS_INDIRECT) LOG.warning( '%s %s Skipped VS, Service point to %s server' ' and not have redirect action and backup ' 'vserver' % (cmd, key, ip_addr)) else: msg = ('%s %s Skipped VS, Invalid VIP %s and do not ' 'have service assigned, redirect action and ' 'backup vserver' % (cmd, key, ip_addr)) ns_util.add_status_row(lb_vs['line_no'], cmd, key, full_cmd, STATUS_SKIPPED, avi_object=msg) LOG.warning(msg) continue # Regex to check Vs has IPV6 address if yes the Skipped if re.findall(ns_constants.IPV6_Address, ip_addr) \ or ip_addr == '0.0.0.0': # Added condition to handel redirect_url and backupvserver if redirect_url: redirect_dict = { updated_vs_name: { "redirect_url": redirect_url } } avi_config['Lbvs'].append(redirect_dict) if backup_server: backup_server_name = { updated_vs_name: { "backupvserver": pool_group['name'] } } avi_config['Lbvs'].append(backup_server_name) vs_conv_status = STATUS_SKIPPED if backup_configured: vs_conv_status = STATUS_INDIRECT skipped_status = "Skipped:Invalid VIP %s" % full_cmd LOG.warning(skipped_status) ns_util.add_status_row(lb_vs['line_no'], cmd, key, full_cmd, vs_conv_status, skipped_status) continue service = {'port': port, 'enable_ssl': enable_ssl} if port in ("0", "*"): service['port'] = "1" service['port_range_end'] = "65535" vs_obj['services'].append(service) if redirect_url and not pool_group: redirect_pools.update({vs_obj['name']: redirect_url}) ns_util.create_http_policy_set_for_redirect_url( vs_obj, redirect_url, avi_config, self.tenant_name, self.tenant_ref, enable_ssl) if redirect_url: if parse_version( self.controller_version) >= parse_version('17.1'): ns_util.create_update_vsvip(ip_addr, avi_config['VsVip'], self.tenant_ref, self.cloud_ref, prefix=self.prefix) # Added prefix for objects if self.prefix: ip_addr = self.prefix + '-' + ip_addr updated_vsvip_ref = ns_util.get_object_ref( ip_addr + '-vsvip', 'vsvip', self.tenant_name, self.cloud_name) vs_obj['vsvip_ref'] = updated_vsvip_ref tmp_avi_config['VirtualService'].append(vs_obj) # Marked redirect url as status indirect ns_util.add_status_row(lb_vs['line_no'], cmd, key, full_cmd, STATUS_INDIRECT, vs_obj) else: # Verify that this lb vs has share the same VIP of another # vs If yes then skipped this lb vs is_shared = ns_util.is_shared_same_vip( vs_obj, avi_config['VirtualService'], avi_config, self.tenant_name, self.cloud_name, self.tenant_ref, self.cloud_ref, self.controller_version, self.prefix) if is_shared: skipped_status = 'Skipped: %s Same vip shared by ' \ 'another virtual service' % vs_name LOG.warning(skipped_status) ns_util.add_status_row(lb_vs['line_no'], cmd, key, full_cmd, STATUS_SKIPPED, skipped_status) continue if enable_ssl: ssl_mappings = ns_config.get('bind ssl vserver', {}) ssl_bindings = ssl_mappings.get(key, []) if isinstance(ssl_bindings, dict): ssl_bindings = [ssl_bindings] for mapping in ssl_bindings: if 'CA' in mapping: pki_ref = mapping['attrs'][0] # Added prefix for objects if self.prefix: pki_ref = self.prefix + '-' + pki_ref if self.object_merge_check: pki_ref = merge_object_mapping[ 'pki_profile'].get(pki_ref) if [ pki_profile for pki_profile in ( sysdict["PKIProfile"] + avi_config["PKIProfile"]) if pki_profile['name'] == pki_ref ]: pki_ref = ns_util.get_object_ref( pki_ref, OBJECT_TYPE_PKI_PROFILE, self.tenant_name) app_profile_name = \ ns_util.update_application_profile( http_prof, pki_ref, self.tenant_ref, updated_vs_name, avi_config, sysdict) if app_profile_name: app_profile_with_pki_profile_ref = \ ns_util.get_object_ref(app_profile_name, OBJECT_TYPE_APPLICATION_PROFILE, self.tenant_name) vs_obj['application_profile_ref'] = \ app_profile_with_pki_profile_ref LOG.info('Added: %s PKI profile %s' % (pki_ref, key)) elif 'certkeyName' in mapping: avi_ssl_ref = 'ssl_key_and_certificate_refs' ckname = mapping['certkeyName'] if self.prefix: ckname = self.prefix + '-' + ckname if [ obj for obj in avi_config['SSLKeyAndCertificate'] if obj['name'] == ckname ]: updated_ssl_ref = ns_util.get_object_ref( ckname, OBJECT_TYPE_SSL_KEY_AND_CERTIFICATE, self.tenant_name) vs_obj[avi_ssl_ref] = [updated_ssl_ref] elif [ obj for obj in avi_config['SSLKeyAndCertificate'] if obj['name'] == ckname + '-dummy' ]: updated_ssl_ref = ns_util.get_object_ref( ckname + '-dummy', OBJECT_TYPE_SSL_KEY_AND_CERTIFICATE, self.tenant_name) vs_obj[avi_ssl_ref] = [updated_ssl_ref] else: LOG.warning( 'Could not find ssl key cert, so adding ' 'default cert as system default insted') vs_obj[avi_ssl_ref] = [ ns_util.get_object_ref( 'System-Default-Cert', 'sslkeyandcertificate', 'admin') ] continue vs_obj[avi_ssl_ref] = [updated_ssl_ref] ssl_vs_mapping = ns_config.get('set ssl vserver', {}) mapping = ssl_vs_mapping.get(key, None) ssl_profile_name = re.sub('[:]', '-', key) # Added prefix for objects if self.prefix: ssl_profile_name = self.prefix + '-' + ssl_profile_name # Get the merge ssl profile name if self.object_merge_check: ssl_profile_name = \ merge_object_mapping['ssl_profile'].get( ssl_profile_name, None) if mapping and [ ssl_profile for ssl_profile in (sysdict['SSLProfile'] + avi_config["SSLProfile"]) if ssl_profile['name'] == ssl_profile_name ]: updated_ssl_profile_ref = ns_util.get_object_ref( ssl_profile_name, OBJECT_TYPE_SSL_PROFILE, self.tenant_name) # Changed ssl profile name to ssl profile ref. vs_obj['ssl_profile_ref'] = updated_ssl_profile_ref LOG.debug('Added: %s SSL profile %s' % (key, key)) # Added code to skipped L4 VS if pool or pool group not present if vs_obj.get('application_profile_ref'): app_name = ns_util.get_name( vs_obj['application_profile_ref']) application_profile_obj = [ obj for obj in (sysdict['ApplicationProfile'] + avi_config['ApplicationProfile']) if obj['name'] == app_name ] if (application_profile_obj and application_profile_obj[0][ 'type'] == 'APPLICATION_PROFILE_TYPE_L4') or app_name == \ 'System-L4-Application': if not vs_obj.get('pool_ref', vs_obj.get('pool_group_ref')): vs_conv_status = STATUS_SKIPPED skipped_status = "Skipped:Failed to convert L4 VS "\ "dont have pool or pool group ref"\ " %s" % full_cmd LOG.debug(skipped_status) ns_util.add_status_row(lb_vs['line_no'], cmd, key, full_cmd, vs_conv_status, skipped_status) continue # Added this line here in order to track if a pool group is # used in VS directly or through policy if vs_obj.get('pool_group_ref'): used_pool_group_ref.append(pool_group_ref) avi_config['VirtualService'].append(vs_obj) # Add summery of this lb vs in CSV/report conv_status = ns_util.get_conv_status( lb_vs, self.lbvs_skip_attrs, self.lbvs_na_attrs, self.lbvs_indirect_list, ignore_for_val=self.lbvs_ignore_vals, user_ignore_val=self.lbvs_user_ignore) ns_util.add_conv_status(lb_vs['line_no'], cmd, key, full_cmd, conv_status, vs_obj) LOG.debug('LB VS conversion completed for: %s' % key) except: update_count('error') LOG.error('Error in lb vs conversion for: %s' % key, exc_info=True) # Calling progress bar function. msg = "VirtualService Conversion started..." ns_util.print_progress_bar(self.progressbar_count, self.total_size, msg, prefix='Progress', suffix='')
def convert_monitor(self, ns_monitor, input_dir, netscalar_command, ns_monitor_complete_command): """ This functions defines that convert netscalar health monitor to AVI health monitor object :param ns_monitor: Object of health monitor :param input_dir: Input dir for command_code :param tenant_ref: Tenant :param cloud_ref: Cloud ref :return: health monitor object """ avi_monitor = dict() try: LOG.debug('Conversion started for monitor %s' % ns_monitor['attrs'][0]) avi_monitor["name"] = (ns_monitor['attrs'][0]).strip().\ replace(" ", "_") avi_monitor["tenant_ref"] = self.tenant_ref avi_monitor["receive_timeout"] = ns_monitor.get('resptimeout', 2) avi_monitor["failed_checks"] = ns_monitor.get('failureRetries', 3) interval = ns_monitor.get('interval', '5') if 'MIN' in interval.upper(): matchObj = re.findall('[0-9]+', ns_monitor.get('interval', '5')) interval = int(matchObj[0]) * 60 avi_monitor["send_interval"] = str(interval) if ns_monitor.get('destPort'): avi_monitor['monitor_port'] = ns_monitor.get('destPort') avi_monitor["successful_checks"] = ns_monitor.get( 'successRetries', 1) mon_type = ns_monitor['attrs'][1] if mon_type == 'PING': avi_monitor["type"] = "HEALTH_MONITOR_PING" elif mon_type == 'TCP': avi_monitor["type"] = "HEALTH_MONITOR_TCP" elif mon_type == 'HTTP': avi_monitor["type"] = "HEALTH_MONITOR_HTTP" send = ns_monitor.get('httpRequest', None) respCode = ns_monitor.get('respCode', None) if respCode: respCode = ns_util.get_avi_resp_code(respCode) avi_monitor["http_monitor"] = { "http_request": send, "http_response_code": respCode } elif mon_type == 'HTTP-ECV': avi_monitor["type"] = "HEALTH_MONITOR_HTTP" send = ns_monitor.get("send", None) response = ns_monitor.get('recv', None) avi_monitor["http_monitor"] = { "http_request": send, "http_response_code": ["HTTP_ANY"], "http_response": response } elif mon_type == 'DNS': avi_monitor["type"] = "HEALTH_MONITOR_DNS" elif mon_type == 'USER': avi_monitor["type"] = "HEALTH_MONITOR_EXTERNAL" file_name = ns_monitor.get('scriptName') cmd_code = ns_util.upload_file( input_dir + os.path.sep + file_name) if not cmd_code: skipped_status = 'File not found %s : %s' % \ (input_dir + os.path.sep + file_name, ns_monitor_complete_command) LOG.warning(skipped_status) ns_util.add_status_row(ns_monitor['line_no'], netscalar_command, avi_monitor["name"], ns_monitor_complete_command, STATUS_MISSING_FILE, skipped_status) return None ext_monitor = { "command_code": cmd_code, "command_parameters": ns_monitor.get("scriptArgs", None) } avi_monitor["external_monitor"] = ext_monitor LOG.debug('Successfully converted monitor %s' % ns_monitor['attrs'][0]) except: update_count('error') LOG.error('Error converting monitor %s', exc_info=True) return avi_monitor
def convert(self, f5_config, avi_config, user_ignore, tenant_ref, merge_object_mapping, sys_dict): """ :param f5_config: parsed f5 config :param avi_config: dict of avi config :param user_ignore: Ignore config defined by user :param tenant_ref: tenant of which output to converted :param merge_object_mapping: flag for object merge :param sys_dict: baseline profile :return: """ avi_config['hash_algorithm'] = [] converted_objs = [] f5_persistence_dict = f5_config.get('persistence') user_ignore = user_ignore.get('persistence', {}) # Added variable to get total object count. progressbar_count = 0 total_size = len(f5_persistence_dict.keys()) print "Converting Persistence Profiles..." for key in f5_persistence_dict.keys(): progressbar_count += 1 persist_mode = None name = None skipped = [] # Added call to check the progress. msg = "persistence conversion started..." conv_utils.print_progress_bar(progressbar_count, total_size, msg, prefix='Progress', suffix='') if key == 'global-settings': msg = 'persist mode not supported skipping conversion: %s' \ % name LOG.warning(msg) self.update_conv_status_for_skip(None, name, msg) continue try: persist_mode, name = key.split(" ") LOG.debug("Converting persistence profile: %s" % name) profile = f5_persistence_dict[key] prof_conv = ProfileConfigConv() profile = prof_conv.update_with_default_profile( persist_mode, profile, f5_persistence_dict, name) tenant, name = conv_utils.get_tenant_ref(name) if tenant_ref != 'admin': tenant = tenant_ref if self.prefix: name = '{}-{}'.format(self.prefix, name) # Enabled the cookie support if persist_mode == "cookie": persist_profile = self.convert_cookie(name, profile, skipped, tenant) if not persist_profile: continue u_ignore = user_ignore.get('cookie', []) elif persist_mode == "ssl": persist_profile = self.convert_ssl( name, profile, skipped, self.indirect, tenant) u_ignore = user_ignore.get('ssl', []) elif persist_mode == "source-addr": persist_profile = self.convert_source_addr( name, profile, skipped, tenant) u_ignore = user_ignore.get('source-addr', []) elif persist_mode == "hash": avi_config['hash_algorithm'].append(name) skipped = profile.keys() LOG.warn('hash-persistence profile %s will be mapped ' 'indirectly to Pool -> Load Balance Algorithm' % name) conv_status = { 'status': conv_const.STATUS_PARTIAL, 'skipped': skipped } msg = 'Indirectly mapped to Pool -> Load Balance Algorithm' conv_utils.add_conv_status( 'profile', "hash-persistence", name, conv_status, msg) continue else: msg = 'persist mode not supported skipping conversion: %s'\ % name LOG.warning(msg) self.update_conv_status_for_skip(persist_mode, name, msg) continue if not persist_profile: continue # code to merge applicaation persistence profile. if self.object_merge_check: conv_utils.update_skip_duplicates(persist_profile, avi_config['ApplicationPersistenceProfile'], 'app_per_profile', converted_objs, name, None, merge_object_mapping, persist_mode, self.prefix, sys_dict['ApplicationPersistenceProfile']) self.app_per_count += 1 else: avi_config["ApplicationPersistenceProfile"].append( persist_profile) ignore_for_defaults = {"app-service": "none", "mask": "none"} conv_status = conv_utils.get_conv_status( skipped, self.indirect, ignore_for_defaults, profile, u_ignore) self.update_conversion_status(conv_status, persist_mode, name, persist_profile) except: update_count('error') LOG.error("Failed to convert persistance profile : %s" % key, exc_info=True) self.update_conv_status_for_error(name, persist_mode, key) count = len(avi_config["ApplicationPersistenceProfile"]) LOG.debug("Converted %s persistence profiles" % count) f5_config.pop('persistence')
def convert(f5_config, output_dir, vs_state, input_dir, version, object_merge_check, controller_version, report_name, prefix, con_snatpool, user_ignore, profile_path, tenant='admin', cloud_name='Default-Cloud', keypassphrase=None, vs_level_status=False, vrf=None, segroup=None, custom_mappings=None): """ Converts f5 config to avi config pops the config lists for conversion of each type from f5 config and remaining marked as skipped in the conversion status file :param f5_config: dict representation of f5 config from the file :param output_dir: Folder path to put output files :param vs_state: State of created Avi VS object :param input_dir: Location of cert and external monitor script files :param version: Version of F5 config :param object_merge_check: Flag for object merge :param controller_version: Version of controller :param report_name: output file name :param prefix : prefix for objects :param con_snatpool : flag for snat conversion :param user_ignore: Ignore config defined by user :param profile_path: path of default profile path :param tenant: Tenant for which config need to be converted :param cloud_name: cloud for which config need to be converted :param keypassphrase: path of keypassphrase file. :param vs_level_status: flag to add cloumn of vs reference. :param vrf: vrf name to write vrf_ref value :param segroup: segroup ref value for VS :param custom_mappings: custom mappings to overwrite monitor or map irules :return: Converted avi objects """ avi_config_dict = {} sys_dict = {} partition_vs_mapping = {} try: # load the yaml file attribute in f5_attributes. f5_attributes = conv_const.init(version) merge_object_type = [ 'ApplicationProfile', 'NetworkProfile', 'SSLProfile', 'PKIProfile', 'SSLKeyAndCertificate', 'ApplicationPersistenceProfile', 'HealthMonitor', 'IpAddrGroup' ] for key in merge_object_type: sys_dict[key] = [] avi_config_dict[key] = [] if profile_path and os.path.exists(profile_path): with open(profile_path) as data: prof_data = json.load(data) for key in merge_object_type: sys_dict[key] = prof_data.get(key, []) profile_conv = ProfileConfigConv.get_instance(version, f5_attributes, object_merge_check, prefix, keypassphrase) profile_conv.convert(f5_config, avi_config_dict, input_dir, user_ignore, tenant, cloud_name, merge_object_mapping, sys_dict) # Added ssl profile merge flag. mon_conv = MonitorConfigConv.get_instance(version, f5_attributes, prefix, object_merge_check) mon_conv.convert(f5_config, avi_config_dict, input_dir, user_ignore, tenant, cloud_name, controller_version, merge_object_mapping, sys_dict, custom_mappings) pool_conv = PoolConfigConv.get_instance(version, f5_attributes, prefix) pool_conv.convert(f5_config, avi_config_dict, user_ignore, tenant, cloud_name, merge_object_mapping, sys_dict, vrf, segroup) persist_conv = PersistenceConfigConv.get_instance( version, f5_attributes, prefix, object_merge_check) persist_conv.convert(f5_config, avi_config_dict, user_ignore, tenant, merge_object_mapping, sys_dict) policy_conv = PolicyConfigConv.get_instance(version, prefix) policy_conv.convert(f5_config, avi_config_dict, tenant) vs_conv = VSConfigConv.get_instance(version, f5_attributes, prefix, con_snatpool, custom_mappings) vs_conv.convert(f5_config, avi_config_dict, vs_state, user_ignore, tenant, cloud_name, controller_version, merge_object_mapping, sys_dict, vrf, segroup, partition_vs_mapping) dg_conv = DataGroupConfigConv.get_instance(version, prefix, merge_object_mapping, f5_attributes) dg_conv.convert(f5_config, avi_config_dict, user_ignore, tenant, merge_object_mapping, sys_dict) # Updating application profile from L4 to http if service has ssl enable conv_utils.update_app_profile(avi_config_dict, sys_dict) # Updated network profile to TCP PROXY if application profile is HTTP conv_utils.update_network_profile(avi_config_dict, sys_dict) # Disabled the net to static route conversion # conv_utils.net_to_static_route(f5_config, avi_config_dict) # Updating the ssl profile ref for monitors with merged name conv_utils.update_monitor_ssl_ref(avi_config_dict, merge_object_mapping, sys_dict) conv_utils.add_tenants(avi_config_dict) conv_utils.cleanup_config(avi_config_dict) # Validating the aviconfig after generation conv_utils.validation(avi_config_dict) # Clone cross tenant references test_clone_obj = CloneObjects(avi_config_dict) test_clone_obj.find_clone_all() except: update_count('warning') LOG.error("Conversion error", exc_info=True) datascript_objs = ['data-group'] # Added support node as not applicable na_list_objs = f5_attributes['na_list_objs'] # Marked route as skipped accept_list = ['snatpool', 'route'] for f5_type in f5_config.keys(): f5_obj = f5_config[f5_type] for key in f5_obj.keys(): sub_type = None if ' ' in key: sub_type, key = key.rsplit(' ', 1) if f5_type in datascript_objs: conv_utils.add_status_row(f5_type, sub_type, key, conv_const.STATUS_DATASCRIPT) elif f5_type in na_list_objs: conv_utils.add_status_row(f5_type, sub_type, key, conv_const.STATUS_NOT_APPLICABLE, f5_type + " object not applicable") elif f5_type in accept_list: msg = (" skipped because of object " "associated with this is skipped") conv_utils.add_status_row(f5_type, sub_type, key, conv_const.STATUS_SKIPPED, f5_type + msg) else: conv_utils.add_status_row(f5_type, sub_type, key, conv_const.STATUS_NOT_SUPPORTED) # Add f5 converter status report in xslx report conv_utils.add_complete_conv_status(output_dir, avi_config_dict, report_name, vs_level_status) for key in avi_config_dict: if key != 'META': if key == 'VirtualService': if vs_level_status: LOG.info('Total Objects of %s : %s (%s full conversions)' % (key, len(avi_config_dict[key]), conversion_util.fully_migrated)) print 'Total Objects of %s : %s (%s full conversions)' \ % (key, len(avi_config_dict[key]), conversion_util.fully_migrated) else: LOG.info('Total Objects of %s : %s' % (key, len(avi_config_dict[key]))) print 'Total Objects of %s : %s' \ % (key, len(avi_config_dict[key])) continue # Added code to print merged count. elif object_merge_check and key == 'SSLProfile': mergedfile = len(avi_config_dict[key]) - ssl_count['count'] profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), ssl_count['count']) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'ApplicationProfile': mergedfile = len(avi_config_dict[key]) - \ profile_conv.app_count profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), profile_conv.app_count) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'NetworkProfile': mergedfile = len(avi_config_dict[key]) - \ profile_conv.net_count profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), profile_conv.net_count) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'HealthMonitor': mergedmon = len(avi_config_dict[key]) - mon_conv.mon_count monitor_merged_message = \ 'Total Objects of %s : %s (%s/%s monitor merged)' % \ (key, len(avi_config_dict[key]), abs(mergedmon), mon_conv.mon_count) LOG.info(monitor_merged_message) print monitor_merged_message continue elif object_merge_check and key == 'PKIProfile': mergedfile = len(avi_config_dict[key]) - \ profile_conv.pki_count profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), profile_conv.pki_count) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'ApplicationPersistenceProfile': mergedfile = len(avi_config_dict[key]) - \ persist_conv.app_per_count profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), persist_conv.app_per_count) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'SSLKeyAndCertificate': mergedfile = len(avi_config_dict[key]) - \ profile_conv.certkey_count certkey_merged_message = \ 'Total Objects of %s : %s (%s/%s cert key merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), profile_conv.certkey_count) LOG.info(certkey_merged_message) print certkey_merged_message continue LOG.info('Total Objects of %s : %s' % (key, len(avi_config_dict[key]))) print 'Total Objects of %s : %s' % (key, len(avi_config_dict[key])) return avi_config_dict, partition_vs_mapping
def service_convert(self, ns_config, avi_config, sysdict, vrf): """ This function is defines that convert service to pool :param ns_config: Dict of netscalar commands :param avi_config: Dict of AVI :return: None """ used_pool_ref = [] avi_config['Pool'] = [] ns_services = ns_config.get('add service', {}) bind_service_group = ns_config.get('bind serviceGroup', {}) ns_servers = ns_config.get('add server', {}) ns_dns = ns_config.get('add dns addRec', {}) bind_ns_service = ns_config.get('bind service', {}) ns_service_groups = ns_config.get('add serviceGroup', {}) set_ssl_service_group = ns_config.get('set ssl serviceGroup', {}) set_ssl_service = ns_config.get('set ssl service', {}) bind_ssl_service = ns_config.get('bind ssl service', {}) bind_ssl_service_group = ns_config.get('bind ssl serviceGroup', {}) # Get the total size of object self.total_size = self.total_size + len(ns_services) + \ len(ns_service_groups) print "Converting Pools..." for key in ns_services: try: # Added count to increment progress. self.progressbar_count += 1 service = ns_services.get(key, {}) service_command = 'add service' service_name = key service_netscalar_full_command = \ ns_util.get_netscalar_full_command(service_command, service) server, use_service_port = self.convert_ns_service( service, ns_servers, ns_dns) if not server: LOG.warning('Skipped:No server found %s' % service_netscalar_full_command) # Skipped service if No sserver node ns_util.add_status_row( service['line_no'], service_command, service_name, service_netscalar_full_command, STATUS_INCOMPLETE_CONFIGURATION) continue pool_name = re.sub('[:]', '-', service_name + '-pool') # Addded prefix for objects if self.prefix: pool_name = self.prefix + '-' + pool_name pool_obj = { 'name': pool_name, 'servers': [server], 'health_monitor_refs': [], 'tenant_ref': self.tenant_ref, 'cloud_ref': self.cloud_ref } if vrf: vrf_ref = ns_util.get_object_ref(vrf, 'vrfcontext', tenant=self.tenant_name, cloud_name=self.cloud_name) pool_obj['vrf_ref'] = vrf_ref if use_service_port: pool_obj['use_service_port'] = use_service_port # Add health monitor reference to pool monitor_refs = self.get_service_montor(service_name, bind_ns_service, avi_config, sysdict) if monitor_refs: pool_obj['health_monitor_refs'] = list(set(monitor_refs)) ssl_service = set_ssl_service.get(key, None) if ssl_service: bind_ssl_service_conf = bind_ssl_service.get(key, []) if isinstance(bind_ssl_service_conf, dict): bind_ssl_service_conf = [bind_ssl_service_conf] for service_conf in bind_ssl_service_conf: if service_conf.get('CA', None): # Added prefix for objects pkiname = self.prefix + '-' + service_conf['CA'] \ if self.prefix else service_conf['CA'] if self.object_merge_check: pkiname = merge_object_mapping[ 'pki_profile'].get(pkiname, None) if [pki for pki in (sysdict['PKIProfile'] + avi_config['PKIProfile']) if pki['name'] == pkiname]: updated_pki_ref = ns_util.get_object_ref( pkiname, OBJECT_TYPE_PKI_PROFILE, self.tenant_name) pool_obj['pki_profile_ref'] = updated_pki_ref if service_conf.get('certkeyName', None): # Added prefix for objects certname = self.prefix + '-' + service_conf[ 'certkeyName'] if self.prefix else \ service_conf['certkeyName'] if [key_cert for key_cert in avi_config[ 'SSLKeyAndCertificate'] if key_cert[ 'name'] == certname]: ssl_key_cert_ref = ns_util.get_object_ref( certname, OBJECT_TYPE_SSL_KEY_AND_CERTIFICATE, self.tenant_name) pool_obj['ssl_key_and_certificate_ref'] = \ ssl_key_cert_ref elif [key_cert for key_cert in avi_config[ 'SSLKeyAndCertificate'] if key_cert[ 'name'] == certname + '-dummy']: ssl_key_cert_ref = ns_util.get_object_ref( certname + '-dummy', OBJECT_TYPE_SSL_KEY_AND_CERTIFICATE, self.tenant_name) pool_obj['ssl_key_and_certificate_ref'] = \ ssl_key_cert_ref ssl_profile_name = re.sub('[:]', '-', key) # Added prefix for objects if self.prefix: ssl_profile_name = self.prefix + '-' + ssl_profile_name if self.object_merge_check: # Get the merge ssl profile name ssl_profile_name = merge_object_mapping[ 'ssl_profile'].get(ssl_profile_name, None) if [ssl_prof for ssl_prof in (sysdict['SSLProfile'] + avi_config['SSLProfile']) if ssl_prof['name'] == ssl_profile_name]: updated_ssl_profile_ref = ns_util.get_object_ref( ssl_profile_name, OBJECT_TYPE_SSL_PROFILE, self.tenant_name) pool_obj['ssl_profile_ref'] = updated_ssl_profile_ref # added condition to attach default ssl profile if SSL is # present in the command elif service.get('attrs') and len(service['attrs']) >= 2 and \ 'SSL' in service['attrs']: pool_obj['ssl_profile_ref'] = ns_util.get_object_ref( 'System-Standard', OBJECT_TYPE_SSL_PROFILE, 'admin') # Remove health monitor reference of http type if pool # has ssl profile or pki profile or ssl cert key # ELSE remove health monitor of https type if pool_obj.get('pki_profile_ref', None) or \ pool_obj.get('ssl_key_and_certificate_ref', None) or \ pool_obj.get('ssl_profile_ref', None): ns_util.remove_http_mon_from_pool(avi_config, pool_obj, sysdict) else: ns_util.remove_https_mon_from_pool(avi_config, pool_obj, sysdict) if len(pool_obj['health_monitor_refs']) > 6: pool_obj['health_monitor_refs'] = \ pool_obj['health_monitor_refs'][:6] # Updated the reference of HM in pool by deriving it from name if pool_obj['health_monitor_refs']: updated_health_monitor_ref = [] for health_monitor_ref in pool_obj['health_monitor_refs']: updated_health_monitor_ref.append( ns_util.get_object_ref( health_monitor_ref, OBJECT_TYPE_HEALTH_MONITOR, self.tenant_name)) pool_obj['health_monitor_refs'] = updated_health_monitor_ref avi_config['Pool'].append(pool_obj) LOG.warning('Conversion successful: %s' % service_netscalar_full_command) # Add summery of this service in CSV/report conv_status = ns_util.get_conv_status( service, self.nsservice_bind_lb_skipped, [], [], user_ignore_val=self.nsservice_bind_lb_user_ignore) ns_util.add_conv_status( service['line_no'], service_command, service_name, service_netscalar_full_command, conv_status, pool_obj) except: update_count('error') LOG.error('Error in pool conversion for: %s' % key, exc_info=True) # Calling progressbar function. msg = "Pool Conversion started..." ns_util.print_progress_bar(self.progressbar_count, self.total_size, msg,prefix='Progress', suffix='') for group_key in ns_service_groups: try: # Added count to increment progress. self.progressbar_count += 1 service_group_command = 'add serviceGroup' service_group = ns_service_groups.get(group_key, {}) service_group_name = group_key service_group_netscalar_full_command = \ ns_util.get_netscalar_full_command( service_group_command, service_group) bind_groups = bind_service_group.get(service_group['attrs'][0], []) servers, monitor_ref, use_service_port = \ self.convert_ns_service_group(bind_groups, ns_servers, ns_dns, avi_config, sysdict) if not servers: LOG.warning('Skipped:No server found %s' % service_group_netscalar_full_command) # Skipped this service group if No server found ns_util.add_status_row(service_group['line_no'], service_group_command, service_group_name, service_group_netscalar_full_command, STATUS_INCOMPLETE_CONFIGURATION) continue pool_name = re.sub('[:]', '-', service_group_name + '-pool') # Added prefix for objects if self.prefix: pool_name = self.prefix + '-' + pool_name status_flag = False # Skipped the servers if length > 400 if len(servers) > 400: servers = servers[0:400] status_flag = True pool_obj = { 'name': pool_name, 'servers': servers, 'health_monitor_refs': [], 'tenant_ref': self.tenant_ref, 'cloud_ref': self.cloud_ref } # Added code to disable translation port if use_service_port: pool_obj['use_service_port'] = use_service_port # Add health monitor reference to pool if monitor_ref and [monitor for monitor in avi_config['HealthMonitor'] if monitor['name'] == monitor_ref]: pool_obj['health_monitor_refs'].append(monitor_ref) ssl_service_group = set_ssl_service_group.get(group_key, None) if ssl_service_group: bind_ssl_service_group_conf = \ bind_ssl_service_group.get(group_key, []) if isinstance(bind_ssl_service_group_conf, dict): bind_ssl_service_group_conf = [ bind_ssl_service_group_conf] for ssl_service_conf in bind_ssl_service_group_conf: if ssl_service_conf.get('CA', None): # Added prefix for objects pkiname = '%s-%s' % (self.prefix, ssl_service_conf['CA']) if self.prefix \ else ssl_service_conf['CA'] if self.object_merge_check: pkiname = merge_object_mapping[ 'pki_profile'].get(pkiname, None) if [pki for pki in (sysdict['PKIProfile'] + avi_config['PKIProfile']) if pki['name'] == pkiname]: updated_pki_ref = ns_util.get_object_ref( pkiname, OBJECT_TYPE_PKI_PROFILE, self.tenant_name) pool_obj['pki_profile_ref'] = updated_pki_ref if ssl_service_conf.get('certkeyName', None): certname = self.prefix + '-' + \ ssl_service_conf['certkeyName'] if \ self.prefix else ssl_service_conf['certkeyName'] if [key_cert for key_cert in avi_config[ 'SSLKeyAndCertificate'] if key_cert['name'] == certname]: ssl_key_cert_ref = ns_util.get_object_ref( certname, OBJECT_TYPE_SSL_KEY_AND_CERTIFICATE, self.tenant_name) pool_obj['ssl_key_and_certificate_ref'] = \ ssl_key_cert_ref elif [key_cert for key_cert in avi_config[ 'SSLKeyAndCertificate'] if key_cert[ 'name'] == certname + '-dummy']: ssl_key_cert_ref = ns_util.get_object_ref( certname + '-dummy', OBJECT_TYPE_SSL_KEY_AND_CERTIFICATE, self.tenant_name) pool_obj['ssl_key_and_certificate_ref'] = \ ssl_key_cert_ref ssl_profile_name = re.sub('[:]', '-', group_key) # Added prefix for objects if self.prefix: ssl_profile_name = self.prefix + '-' + ssl_profile_name if self.object_merge_check: # Get the merge ssl profile name ssl_profile_name = merge_object_mapping[ 'ssl_profile'].get(ssl_profile_name, None) if [ssl_prof for ssl_prof in (sysdict['SSLProfile'] + avi_config['SSLProfile']) if ssl_prof['name'] == ssl_profile_name]: updated_ssl_profile_ref = ns_util.get_object_ref( ssl_profile_name, OBJECT_TYPE_SSL_PROFILE, self.tenant_name) pool_obj['ssl_profile_ref'] = updated_ssl_profile_ref # added condition to attach default ssl profile if SSL is # present in the command elif (service_group.get('attrs') and len(service_group['attrs']) >= 2 and 'SSL' in service_group['attrs']): pool_obj['ssl_profile_ref'] = ns_util.get_object_ref( 'System-Standard', OBJECT_TYPE_SSL_PROFILE, 'admin') # Remove health monitor reference of http type if pool # has ssl profile or pki profile or ssl cert key # ELSE remove health monitor of https type if pool_obj.get('pki_profile_ref', None) or \ pool_obj.get('ssl_key_and_certificate_ref', None) or \ pool_obj.get('ssl_profile_ref', None): ns_util.remove_http_mon_from_pool(avi_config, pool_obj, sysdict) else: ns_util.remove_https_mon_from_pool(avi_config, pool_obj, sysdict) if len(pool_obj['health_monitor_refs']) > 6: pool_obj['health_monitor_refs'] = \ pool_obj['health_monitor_refs'][:6] # Updated the reference of HM in pool by deriving it from name if pool_obj['health_monitor_refs']: updated_health_monitor_ref = [] for health_monitor_ref in pool_obj['health_monitor_refs']: updated_health_monitor_ref.append( ns_util.get_object_ref(health_monitor_ref, OBJECT_TYPE_HEALTH_MONITOR, self.tenant_name)) pool_obj['health_monitor_refs'] = updated_health_monitor_ref avi_config['Pool'].append(pool_obj) LOG.warning('Conversion successful: %s' % service_group_netscalar_full_command) # Add summery of this service group in CSV/report conv_status = ns_util.get_conv_status( service_group, self.nsservice_bind_lb_skipped, [], [], user_ignore_val=self.nsservice_bind_lb_user_ignore) # If len of servers > 400 then considering only 400 servers # And status of the pool is partial if status_flag: conv_status['status'] = STATUS_PARTIAL conv_status['skipped'].append("Skipped:length of servers " \ "greater than 400") ns_util.add_conv_status( service_group['line_no'], service_group_command, service_group_name, service_group_netscalar_full_command, conv_status, pool_obj) except: update_count('error') LOG.error('Error in pool conversion for: %s' % group_key, exc_info=True) # Calling progress bar function. msg = "Pool Conversion started..." ns_util.print_progress_bar(self.progressbar_count, self.total_size, msg, prefix='Progress', suffix='')
def convert(self, f5_config, avi_config, vs_state, user_ignore, tenant, cloud_name, controller_version, merge_object_mapping, sys_dict, vrf=None, segroup=None, partition_mapping=None): """ :param f5_config: Parsed f5 config dict :param avi_config: dict for avi conversion :param vs_state: State of created Avi VS object :param user_ignore: Ignore config defined by user :param tenant: Tenant for which config need to be converted :param cloud_name: cloud for which config need to be converted :param controller_version: AVI controller version :param merge_object_mapping: flag for merge object :param sys_dict: baseline profile dict :param vrf: vrf user input to put vrf ref in VS object :param segroup: segroup user input to put se-group ref in VS object :return: """ f5_snat_pools = f5_config.get("snatpool", {}) vs_config = f5_config.get("virtual", {}) avi_config['VirtualService'] = [] avi_config['VSDataScriptSet'] = [] avi_config['NetworkSecurityPolicy'] = [] avi_config['VsVip'] = [] print "Converting VirtualServices ..." # Added variable to get total object count. total_size = len(vs_config.keys()) progressbar_count = 0 for vs_name in vs_config.keys(): progressbar_count += 1 try: LOG.debug("Converting VS: %s" % vs_name) f5_vs = vs_config[vs_name] vs_type = [key for key in f5_vs.keys() if key in self.unsupported_types] if vs_type: msg = ("VS type: %s not supported by Avi skipped VS: %s" % (vs_type, vs_name)) LOG.warn(msg) conv_utils.add_status_row('virtual', None, vs_name, final.STATUS_SKIPPED, msg) continue mapping = self.create_partition_mapping(f5_vs, vs_name) partition_mapping.update(mapping) vs_obj = self.convert_vs( vs_name, f5_vs, vs_state, avi_config, f5_snat_pools, user_ignore, tenant, cloud_name, controller_version, merge_object_mapping, sys_dict, f5_config, vrf) if vs_obj: if segroup: segroup_ref = conv_utils.get_object_ref( segroup, 'serviceenginegroup', tenant=tenant, cloud_name=cloud_name) vs_obj['se_group_ref'] = segroup_ref avi_config['VirtualService'].append(vs_obj) LOG.debug("Conversion successful for VS: %s" % vs_name) except: update_count('error') LOG.error("Failed to convert VS: %s" % vs_name, exc_info=True) # Added call to get the progress. msg = "virtualservice conversion started..." conv_utils.print_progress_bar( progressbar_count, total_size, msg, prefix='Progress', suffix='') LOG.debug("Converted %s VS" % len(avi_config['VirtualService'])) f5_config.pop("virtual", {})
def convert_ns_service_group(self, ns_service_group, ns_servers, ns_dns, avi_config, sysdict): """ This function defines that returns the monitor ref and servers :param ns_service_group: Object of service group :param ns_servers: List of servers :param ns_dns: list of dns addrec :return: servers and monitor ref """ servers = [] monitor_name = None if isinstance(ns_service_group, dict): ns_service_group = [ns_service_group] use_service_port = False for server_binding in ns_service_group: attrs = server_binding.get('attrs') ns_bind_service_group_command = 'bind serviceGroup' group_status = \ ns_util.get_conv_status( server_binding, self.nsservice_bind_sg_skip, [], [], user_ignore_val=self.nsservice_bind_sg_user_ignore) ns_bind_service_group_complete_command = \ ns_util.get_netscalar_full_command( ns_bind_service_group_command, server_binding) if server_binding.get('monitorName', None): monitor_name = server_binding.get('monitorName') # Added prefix for objects if self.prefix: monitor_name = self.prefix + '-' + monitor_name if self.object_merge_check: # Get the merge health monitor name monitor_name = merge_object_mapping['health_monitor'].get( monitor_name, None) monitor = [monitor for monitor in (sysdict['HealthMonitor'] + avi_config['HealthMonitor']) if monitor['name'] == monitor_name] if not monitor: monitor_name = '%s-%s' % (monitor_name, 'dummy') monitor = [monitor for monitor in avi_config['HealthMonitor'] if monitor['name'] == monitor_name] if monitor: # Add summery of service group in CSV/report ns_util.add_conv_status( server_binding['line_no'], ns_bind_service_group_command, attrs[0], ns_bind_service_group_complete_command, group_status, monitor[0]) else: msg = ('External Health monitor: %s because bind service' 'is not in server' % ns_bind_service_group_complete_command) LOG.warning(msg) # Skipped bind service group if doen not server ns_util.add_status_row( server_binding['line_no'], ns_bind_service_group_command, attrs[0], ns_bind_service_group_complete_command, STATUS_EXTERNAL_MONITOR, msg) continue server = ns_servers.get(attrs[1]) if not server: # Skipped bind service group if doen not server ns_util.add_status_row( server_binding['line_no'], ns_bind_service_group_command, attrs[0], ns_bind_service_group_complete_command, STATUS_INCOMPLETE_CONFIGURATION) LOG.error('Skipped server : %s' % ns_bind_service_group_complete_command) continue ns_add_server_command = 'add server' ns_add_server_complete_command = \ ns_util.get_netscalar_full_command( ns_add_server_command, server) status = ns_util.get_conv_status( server, self.nsservice_server_skip, [], [], user_ignore_val=self.nsservice_server_user_ignore) ip_addr = server['attrs'][1] # Get the server name server_name = server['attrs'][0] if ip_addr in ns_dns: if isinstance(ns_dns[ip_addr], list): ip_addr = ns_dns[ip_addr][0]['attrs'][1] elif isinstance(ns_dns[ip_addr], dict): ip_addr = ns_dns[ip_addr]['attrs'][1] enabled = True state = server.get('state', 'ENABLED') if not state == 'ENABLED': enabled = False port = attrs[2] if port in ("*", "0"): port = "1" use_service_port = True # Added regex to match ip address matches = re.findall('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$', ip_addr) server_obj = { 'ip': { 'addr': ip_addr, 'type': 'V4' }, 'port': port, 'enabled': enabled, 'health_monitor': server_binding.get('monitorName') } if server_name: # Get the server name server_obj['hostname'] = server_name if not matches: try: # If dns then resolve it get the ip address from it. ip_addr = socket.gethostbyname(ip_addr) server_obj['ip']['addr'] = ip_addr except: update_count('warning') # Skipped this server if it does not have an Ip ns_util.add_status_row( server['line_no'], ns_add_server_command, server['attrs'][0], ns_add_server_complete_command, STATUS_INCOMPLETE_CONFIGURATION) LOG.warning('Not found IP of server : %s %s' % (ns_add_server_command, attrs[1])) ns_util.add_status_row( server_binding['line_no'], ns_bind_service_group_command, attrs[0], ns_bind_service_group_complete_command, STATUS_INCOMPLETE_CONFIGURATION) LOG.error('Skipped server : %s' % ns_bind_service_group_complete_command) server_obj = None if server_obj: servers.append(server_obj) # Add summery of add server in CSV/report ns_util.add_conv_status( server['line_no'], ns_add_server_command, server['attrs'][0], ns_add_server_complete_command, status, server_obj) # Add summery of service group in CSV/report ns_util.add_conv_status( server_binding['line_no'], ns_bind_service_group_command, attrs[0], ns_bind_service_group_complete_command, group_status, server_obj) return servers, monitor_name, use_service_port
def convert(f5_config, output_dir, vs_state, input_dir, version, object_merge_check, controller_version, report_name, prefix, con_snatpool, user_ignore, profile_path, tenant='admin', cloud_name='Default-Cloud', keypassphrase=None, vs_level_status=False, vrf=None, segroup=None, custom_mappings=None): """ Converts f5 config to avi config pops the config lists for conversion of each type from f5 config and remaining marked as skipped in the conversion status file :param f5_config: dict representation of f5 config from the file :param output_dir: Folder path to put output files :param vs_state: State of created Avi VS object :param input_dir: Location of cert and external monitor script files :param version: Version of F5 config :param object_merge_check: Flag for object merge :param controller_version: Version of controller :param report_name: output file name :param prefix : prefix for objects :param con_snatpool : flag for snat conversion :param user_ignore: Ignore config defined by user :param profile_path: path of default profile path :param tenant: Tenant for which config need to be converted :param cloud_name: cloud for which config need to be converted :param keypassphrase: path of keypassphrase file. :param vs_level_status: flag to add cloumn of vs reference. :param vrf: vrf name to write vrf_ref value :param segroup: segroup ref value for VS :param custom_mappings: custom mappings to overwrite monitor or map irules :return: Converted avi objects """ avi_config_dict = {} sys_dict = {} partition_vs_mapping = {} try: # load the yaml file attribute in f5_attributes. f5_attributes = conv_const.init(version) merge_object_type = ['ApplicationProfile', 'NetworkProfile', 'SSLProfile', 'PKIProfile', 'SSLKeyAndCertificate', 'ApplicationPersistenceProfile', 'HealthMonitor', 'IpAddrGroup'] for key in merge_object_type: sys_dict[key] = [] avi_config_dict[key] = [] if profile_path and os.path.exists(profile_path): with open(profile_path) as data: prof_data = json.load(data) for key in merge_object_type: sys_dict[key] = prof_data.get(key, []) profile_conv = ProfileConfigConv.get_instance( version, f5_attributes, object_merge_check, prefix, keypassphrase) profile_conv.convert(f5_config, avi_config_dict, input_dir, user_ignore, tenant, cloud_name, merge_object_mapping, sys_dict) # Added ssl profile merge flag. mon_conv = MonitorConfigConv.get_instance( version, f5_attributes, prefix, object_merge_check) mon_conv.convert(f5_config, avi_config_dict, input_dir, user_ignore, tenant, cloud_name, controller_version, merge_object_mapping, sys_dict, custom_mappings) pool_conv = PoolConfigConv.get_instance(version, f5_attributes, prefix) pool_conv.convert(f5_config, avi_config_dict, user_ignore, tenant, cloud_name, merge_object_mapping, sys_dict, vrf, segroup) persist_conv = PersistenceConfigConv.get_instance( version, f5_attributes, prefix, object_merge_check) persist_conv.convert(f5_config, avi_config_dict, user_ignore, tenant, merge_object_mapping, sys_dict) policy_conv = PolicyConfigConv.get_instance(version, prefix) policy_conv.convert(f5_config, avi_config_dict, tenant) vs_conv = VSConfigConv.get_instance(version, f5_attributes, prefix, con_snatpool, custom_mappings) vs_conv.convert(f5_config, avi_config_dict, vs_state, user_ignore, tenant, cloud_name, controller_version, merge_object_mapping, sys_dict, vrf, segroup, partition_vs_mapping) dg_conv = DataGroupConfigConv.get_instance( version, prefix, merge_object_mapping, f5_attributes) dg_conv.convert(f5_config, avi_config_dict, user_ignore, tenant, merge_object_mapping, sys_dict) # Updating application profile from L4 to http if service has ssl enable conv_utils.update_app_profile(avi_config_dict, sys_dict) # Updated network profile to TCP PROXY if application profile is HTTP conv_utils.update_network_profile(avi_config_dict, sys_dict) # Disabled the net to static route conversion # conv_utils.net_to_static_route(f5_config, avi_config_dict) # Updating the ssl profile ref for monitors with merged name conv_utils.update_monitor_ssl_ref(avi_config_dict, merge_object_mapping, sys_dict) conv_utils.add_tenants(avi_config_dict) conv_utils.cleanup_config(avi_config_dict) # Validating the aviconfig after generation conv_utils.validation(avi_config_dict) # Clone cross tenant references test_clone_obj = CloneObjects(avi_config_dict) test_clone_obj.find_clone_all() except: update_count('warning') LOG.error("Conversion error", exc_info=True) datascript_objs = ['data-group'] # Added support node as not applicable na_list_objs = f5_attributes['na_list_objs'] # Marked route as skipped accept_list = ['snatpool', 'route'] for f5_type in f5_config.keys(): f5_obj = f5_config[f5_type] for key in f5_obj.keys(): sub_type = None if ' ' in key: sub_type, key = key.rsplit(' ', 1) if f5_type in datascript_objs: conv_utils.add_status_row(f5_type, sub_type, key, conv_const.STATUS_DATASCRIPT) elif f5_type in na_list_objs: conv_utils.add_status_row(f5_type, sub_type, key, conv_const.STATUS_NOT_APPLICABLE, f5_type + " object not applicable") elif f5_type in accept_list: msg = (" skipped because of object " "associated with this is skipped") conv_utils.add_status_row(f5_type, sub_type, key, conv_const.STATUS_SKIPPED, f5_type + msg) else: conv_utils.add_status_row(f5_type, sub_type, key, conv_const.STATUS_NOT_SUPPORTED) # Add f5 converter status report in xslx report conv_utils.add_complete_conv_status( output_dir, avi_config_dict, report_name, vs_level_status) for key in avi_config_dict: if key != 'META': if key == 'VirtualService': if vs_level_status: LOG.info('Total Objects of %s : %s (%s full conversions)' % (key, len(avi_config_dict[key]), conversion_util.fully_migrated)) print 'Total Objects of %s : %s (%s full conversions)' \ % (key, len(avi_config_dict[key]), conversion_util.fully_migrated) else: LOG.info('Total Objects of %s : %s' % (key, len(avi_config_dict[key]))) print 'Total Objects of %s : %s' \ % (key, len(avi_config_dict[key])) continue # Added code to print merged count. elif object_merge_check and key == 'SSLProfile': mergedfile = len(avi_config_dict[key]) - ssl_count['count'] profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), ssl_count['count']) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'ApplicationProfile': mergedfile = len(avi_config_dict[key]) - \ profile_conv.app_count profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), profile_conv.app_count) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'NetworkProfile': mergedfile = len(avi_config_dict[key]) - \ profile_conv.net_count profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), profile_conv.net_count) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'HealthMonitor': mergedmon = len(avi_config_dict[key]) - mon_conv.mon_count monitor_merged_message = \ 'Total Objects of %s : %s (%s/%s monitor merged)' % \ (key, len(avi_config_dict[key]), abs(mergedmon), mon_conv.mon_count) LOG.info(monitor_merged_message) print monitor_merged_message continue elif object_merge_check and key == 'PKIProfile': mergedfile = len(avi_config_dict[key]) - \ profile_conv.pki_count profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), profile_conv.pki_count) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'ApplicationPersistenceProfile': mergedfile = len(avi_config_dict[key]) - \ persist_conv.app_per_count profile_merged_message = \ 'Total Objects of %s : %s (%s/%s profile merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), persist_conv.app_per_count) LOG.info(profile_merged_message) print profile_merged_message continue elif object_merge_check and key == 'SSLKeyAndCertificate': mergedfile = len(avi_config_dict[key]) - \ profile_conv.certkey_count certkey_merged_message = \ 'Total Objects of %s : %s (%s/%s cert key merged)' % \ (key, len(avi_config_dict[key]), abs(mergedfile), profile_conv.certkey_count) LOG.info(certkey_merged_message) print certkey_merged_message continue LOG.info('Total Objects of %s : %s' % (key, len( avi_config_dict[key]))) print 'Total Objects of %s : %s' % (key, len( avi_config_dict[key])) return avi_config_dict, partition_vs_mapping
def convert(self, f5_config, avi_config, vs_state, user_ignore, tenant, cloud_name, controller_version, merge_object_mapping, sys_dict, vrf=None, segroup=None): """ :param f5_config: Parsed f5 config dict :param avi_config: dict for avi conversion :param vs_state: State of created Avi VS object :param user_ignore: Ignore config defined by user :param tenant: Tenant for which config need to be converted :param cloud_name: cloud for which config need to be converted :param controller_version: AVI controller version :param merge_object_mapping: flag for merge object :param sys_dict: baseline profile dict :param vrf: vrf user input to put vrf ref in VS object :param segroup: segroup user input to put se-group ref in VS object :return: """ f5_snat_pools = f5_config.get("snatpool", {}) vs_config = f5_config.get("virtual", {}) avi_config['VirtualService'] = [] avi_config['VSDataScriptSet'] = [] avi_config['NetworkSecurityPolicy'] = [] avi_config['VsVip'] = [] print "Converting VirtualServices ..." # Added variable to get total object count. total_size = len(vs_config.keys()) progressbar_count = 0 for vs_name in vs_config.keys(): progressbar_count += 1 try: LOG.debug("Converting VS: %s" % vs_name) f5_vs = vs_config[vs_name] vs_type = [key for key in f5_vs.keys() if key in self.unsupported_types] if vs_type: msg = ("VS type: %s not supported by Avi skipped VS: %s" % (vs_type, vs_name)) LOG.warn(msg) conv_utils.add_status_row('virtual', None, vs_name, final.STATUS_SKIPPED, msg) continue vs_obj = self.convert_vs( vs_name, f5_vs, vs_state, avi_config, f5_snat_pools, user_ignore, tenant, cloud_name, controller_version, merge_object_mapping, sys_dict, f5_config, vrf) if vs_obj: if segroup: segroup_ref = conv_utils.get_object_ref( segroup, 'serviceenginegroup', tenant=tenant, cloud_name=cloud_name) vs_obj['se_group_ref'] = segroup_ref avi_config['VirtualService'].append(vs_obj) LOG.debug("Conversion successful for VS: %s" % vs_name) except: update_count('error') LOG.error("Failed to convert VS: %s" % vs_name, exc_info=True) # Added call to get the progress. msg = "virtualservice conversion started..." conv_utils.print_progress_bar( progressbar_count, total_size, msg, prefix='Progress', suffix='') LOG.debug("Converted %s VS" % len(avi_config['VirtualService'])) f5_config.pop("virtual", {})
def convert_monitor(self, ns_monitor, input_dir, netscalar_command, ns_monitor_complete_command): """ This functions defines that convert netscalar health monitor to AVI health monitor object :param ns_monitor: Object of health monitor :param input_dir: Input dir for command_code :param netscalar_command: Netscalar command for XL sheet status :param ns_monitor_complete_command: Full command for XL sheet status :return: health monitor object """ avi_monitor = dict() # Adding ssl-key and ssl-certificate for Secure ssl_attributes = None if ns_monitor.get('secure', []) == 'YES': # Force changing HTTP/HTTP-ECV to HTTPS in attributes if ns_monitor.get('attrs', []): attr = ns_monitor['attrs'] if 'HTTP' in attr: attr.append('HTTPS') attr.remove('HTTP') if 'HTTP-ECV' in attr: attr.append('HTTPS') attr.remove('HTTP-ECV') ns_monitor['attrs'] = attr # IF controller version is greater than 17.1 and type HTTP/S # add ssl-key and ssl-certificate # TODO: Remove this after all the clients are moved to # 17 version and above if parse_version(self.controller_version) >= parse_version('17.1')\ and 'HTTPS' in ns_monitor.get('attrs', []): profile_ref = ns_util.get_object_ref('System-Standard', 'sslprofile', 'admin') ssl_attributes = { 'ssl_profile_ref': profile_ref } try: mon_name = ns_monitor['attrs'][0] # Added prefix for objects if self.prefix: mon_name = self.prefix + '-' + mon_name LOG.debug('Conversion started for monitor %s' % mon_name) avi_monitor["name"] = str(mon_name).strip().replace(" ", "_") avi_monitor["tenant_ref"] = self.tenant_ref recv_timeout = ns_monitor.get('resptimeout', '2') if 'MSEC' in recv_timeout.upper() or 'MIN' in recv_timeout.upper(): match_ob = re.findall('[0-9]+', recv_timeout) if 'MSEC' in recv_timeout.upper(): recv_timeout = int(math.ceil(float(match_ob[0])/1000)) else: recv_timeout = int(match_ob[0]) * 60 avi_monitor["receive_timeout"] = recv_timeout avi_monitor["failed_checks"] = ns_monitor.get('failureRetries', 3) interval = ns_monitor.get('interval', '5') if 'MIN' in interval.upper(): match_obj = re.findall('[0-9]+', ns_monitor.get( 'interval', '5')) interval = int(match_obj[0]) * 60 avi_monitor["send_interval"] = str(interval) if ns_monitor.get('destPort'): avi_monitor['monitor_port'] = ns_monitor.get('destPort') avi_monitor["successful_checks"] = ns_monitor.get( 'successRetries', 1) mon_type = ns_monitor['attrs'][1] if mon_type == 'PING': avi_monitor["type"] = "HEALTH_MONITOR_PING" elif mon_type == 'TCP': avi_monitor["type"] = "HEALTH_MONITOR_TCP" elif mon_type == 'TCP-ECV': avi_monitor["type"] = "HEALTH_MONITOR_TCP" send = ns_monitor.get("send", None) if send: send = send.replace('"', '') # Removed \\ from send. if '\\' in send: send = send.replace('\\', '"') response = ns_monitor.get('recv', None) if response: response = response.replace('"', '') # Removed \\ from response. if '\\' in response: response = response.replace('\\', '"') avi_monitor["tcp_monitor"] = { "tcp_request": send, "tcp_response": response, "tcp_half_open": False } elif mon_type == 'HTTPS': avi_monitor["type"] = "HEALTH_MONITOR_HTTPS" send = ns_monitor.get('httpRequest', None) if send is None: send = ns_monitor.get('send', None) if send: send = send.replace('"', '') # Removed \\ from send. if '\\' in send: send = send.replace('\\', '"') resp_code = ns_monitor.get('respCode', 'any') if resp_code: resp_code = ns_util.get_avi_resp_code(resp_code) response = ns_monitor.get('recv', None) if response: response = response.replace('"', '') # Removed \\ from response. if '\\' in response: response = response.replace('\\', '"') # TODO: Remove this after all the clients are moved to # 17 version and above if parse_version(self.controller_version) >= parse_version( '17.1'): avi_monitor["https_monitor"] = { "http_request": send, "http_response_code": resp_code, "ssl_attributes": ssl_attributes, "http_response": response } if parse_version(self.controller_version) >= parse_version( '17.1.6'): custom_header = ns_monitor.get('customHeaders') if custom_header: avi_monitor['https_monitor'].update({ 'exact_http_request': True, 'http_request': (send + ' HTTP/1.0' + "\r\n" + custom_header + "\r\n").replace('"', '').replace('\\r\\n', '\r\n') if send else ('HTTP/1.0' + "\r\n" + custom_header + "\r\n").replace('"', '').replace('\\r\\n', '\r\n') }) elif mon_type == 'HTTP': avi_monitor["type"] = "HEALTH_MONITOR_HTTP" send = ns_monitor.get('httpRequest', None) if send: send = send.replace('"', '') # Removed \\ from send. if '\\' in send: send = send.replace('\\', '"') resp_code = ns_monitor.get('respCode', None) if resp_code: resp_code = ns_util.get_avi_resp_code(resp_code) avi_monitor["http_monitor"] = { "http_request": send, "http_response_code": resp_code } if parse_version(self.controller_version) >= parse_version( '17.1.6'): custom_header = ns_monitor.get('customHeaders') if custom_header: avi_monitor['http_monitor'].update({ 'exact_http_request': True, 'http_request': (send + ' HTTP/1.0' + "\r\n" + custom_header + "\r\n").replace('"', '').replace('\\r\\n', '\r\n') if send else ('HTTP/1.0' + "\r\n" + custom_header + "\r\n").replace('"', '').replace('\\r\\n', '\r\n') }) elif mon_type == 'HTTP-ECV': avi_monitor["type"] = "HEALTH_MONITOR_HTTP" send = ns_monitor.get("send", None) if send: send = send.replace('"', '') # Removed \\ from send. if '\\' in send: send = send.replace('\\', '"') response = ns_monitor.get('recv', None) if response: response = response.replace('"', '') # Removed \\ from response. if '\\' in response: response = response.replace('\\', '"') avi_monitor["http_monitor"] = { "http_request": send, "http_response_code": ["HTTP_ANY"], "http_response": response } elif mon_type == 'DNS': avi_monitor["type"] = "HEALTH_MONITOR_DNS" elif mon_type == 'USER': avi_monitor["type"] = "HEALTH_MONITOR_EXTERNAL" file_name = ns_monitor.get('scriptName') cmd_code = ns_util.upload_file( input_dir + os.path.sep + file_name) if not cmd_code: skipped_status = 'File not found %s : %s' % \ (input_dir + os.path.sep + file_name, ns_monitor_complete_command) LOG.warning(skipped_status) ns_util.add_status_row( ns_monitor['line_no'], netscalar_command, avi_monitor["name"], ns_monitor_complete_command, STATUS_MISSING_FILE, skipped_status) return None ext_monitor = { "command_code": cmd_code, "command_parameters": ns_monitor.get("scriptArgs", None) } avi_monitor["external_monitor"] = ext_monitor LOG.debug('Successfully converted monitor %s' % mon_name) except: update_count('error') LOG.error('Error converting monitor %s', exc_info=True) return avi_monitor