def execute_module(self): """ Execute the module :rtype: A dictionary :returns: The result from module execution """ result = {'changed': False} warnings = list() existing_radius_server_facts = self.get_radius_server_facts() commands, requests = self.set_config(existing_radius_server_facts) if commands and len(requests) > 0: if not self._module.check_mode: try: edit_config(self._module, to_request(self._module, requests)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) result['changed'] = True result['commands'] = commands changed_radius_server_facts = self.get_radius_server_facts() result['before'] = existing_radius_server_facts if result['changed']: result['after'] = changed_radius_server_facts result['warnings'] = warnings return result
def get_all_tacacs_server(self): """Get all the tacacs_server configured in the device""" request = [{"path": "data/openconfig-system:system/aaa/server-groups/server-group=TACACS/config", "method": GET}] tacacs_server_data = {} try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) if "openconfig-system:config" in response[0][1]: raw_tacacs_global_data = response[0][1].get("openconfig-system:config", {}) if 'openconfig-system-ext:auth-type' in raw_tacacs_global_data: tacacs_server_data['auth_type'] = raw_tacacs_global_data['openconfig-system-ext:auth-type'] if 'openconfig-system-ext:secret-key' in raw_tacacs_global_data: tacacs_server_data['key'] = raw_tacacs_global_data['openconfig-system-ext:secret-key'] if 'openconfig-system-ext:source-interface' in raw_tacacs_global_data: tacacs_server_data['source_interface'] = raw_tacacs_global_data['openconfig-system-ext:source-interface'] if 'openconfig-system-ext:timeout' in raw_tacacs_global_data: tacacs_server_data['timeout'] = raw_tacacs_global_data['openconfig-system-ext:timeout'] request = [{"path": "data/openconfig-system:system/aaa/server-groups/server-group=TACACS/servers", "method": GET}] hosts = [] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) raw_tacacs_server_list = [] if "openconfig-system:servers" in response[0][1]: raw_tacacs_server_list = response[0][1].get("openconfig-system:servers", {}).get('server', []) for tacacs_host in raw_tacacs_server_list: host_data = {} if 'address' in tacacs_host: host_data['name'] = tacacs_host['address'] cfg = tacacs_host.get('config', None) if cfg: if 'openconfig-system-ext:auth-type' in cfg: host_data['auth_type'] = cfg['openconfig-system-ext:auth-type'] if 'openconfig-system-ext:priority' in cfg: host_data['priority'] = cfg['openconfig-system-ext:priority'] if 'openconfig-system-ext:vrf' in cfg: host_data['vrf'] = cfg['openconfig-system-ext:vrf'] if 'timout' in cfg: host_data['timout'] = cfg['timout'] if tacacs_host.get('tacacs', None) and tacacs_host['tacacs'].get('config', None): tacas_cfg = tacacs_host['tacacs']['config'] if tacas_cfg.get('port', None): host_data['port'] = tacas_cfg['port'] if tacas_cfg.get('secret-key', None): host_data['key'] = tacas_cfg['secret-key'] if host_data: hosts.append(host_data) if hosts: tacacs_server_data['servers'] = {'host': hosts} return tacacs_server_data
def initiate_request(module): """Get all the data available in chassis""" url = module.params['url'] body = module.params['body'] method = module.params['method'] if method == "GET" or method == "DELETE": request = to_request(module, [{"path": url, "method": method}]) response = edit_config(module, request) elif method == "PATCH" or method == "PUT" or method == "POST": request = to_request(module, [{"path": url, "method": method, "data": body}]) response = edit_config(module, request) return response
def get_vlans(self): """Get all the l2_interfaces available in chassis""" request = [{ "path": "data/openconfig-interfaces:interfaces", "method": GET }] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) interfaces = {} if "openconfig-interfaces:interfaces" in response[0][1]: interfaces = response[0][1].get("openconfig-interfaces:interfaces", {}) if interfaces.get("interface"): interfaces = interfaces['interface'] ret_vlan_configs = {} for interface in interfaces: interface_name = interface.get("config").get("name") if "Vlan" in interface_name: vlan_id = interface_name.split("Vlan")[1] vlan_configs = { "vlan_id": vlan_id, "name": interface_name, } ret_vlan_configs.update({vlan_id: vlan_configs}) return ret_vlan_configs
def get_bgp_extcommunities(self): url = "data/openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/ext-community-sets" method = "GET" request = [{"path": url, "method": method}] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) bgp_extcommunities = [] if "openconfig-bgp-policy:ext-community-sets" in response[0][1]: temp = response[0][1].get( "openconfig-bgp-policy:ext-community-sets", {}) if "ext-community-set" in temp: bgp_extcommunities = temp["ext-community-set"] bgp_extcommunities_configs = [] for bgp_extcommunity in bgp_extcommunities: result = dict() name = bgp_extcommunity["ext-community-set-name"] member_config = bgp_extcommunity['config'] match = member_config['match-set-options'] permit_str = member_config.get('openconfig-bgp-policy-ext:action', None) members = member_config.get("ext-community-member", []) result['name'] = name result['match'] = match.lower() if permit_str and permit_str == 'PERMIT': result['permit'] = True else: result['permit'] = False result['members'] = dict() rt = list() soo = list() regex = list() for member in members: if member.startswith('route-target'): rt.append(':'.join(member.split(':')[1:])) elif member.startswith('route-origin'): soo.append(':'.join(member.split(':')[1:])) elif member.startswith('REGEX'): regex.append(':'.join(member.split(':')[1:])) result['type'] = 'standard' if regex and len(regex) > 0: result['type'] = 'expanded' result['members']['regex'] = regex if rt and len(rt) > 0: result['members']['route_target'] = rt if soo and len(soo) > 0: result['members']['route_origin'] = soo bgp_extcommunities_configs.append(result) # with open('/root/ansible_log.log', 'a+') as fp: # fp.write('facts bgp_extcommunities: ' + str(bgp_extcommunities_configs) + '\n') return bgp_extcommunities_configs
def get_default_port_breakout_modes(self): def_port_breakout_modes = [] request = [{ "path": "operations/sonic-port-breakout:breakout_capabilities", "method": POST }] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) raw_port_breakout_list = [] if "sonic-port-breakout:output" in response[0][1]: raw_port_breakout_list = response[0][1].get( "sonic-port-breakout:output", {}).get('caps', []) for port_breakout in raw_port_breakout_list: name = port_breakout.get('port', None) mode = port_breakout.get('defmode', None) if name and mode: if '[' in mode: mode = mode[:mode.index('[')] def_port_breakout_modes.append({'name': name, 'mode': mode}) return def_port_breakout_modes
def get_all_users(self): """Get all the users configured in the device""" request = [{ "path": "data/sonic-system-aaa:sonic-system-aaa/USER", "method": GET }] users = [] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) raw_users = [] if "sonic-system-aaa:USER" in response[0][1]: raw_users = response[0][1].get("sonic-system-aaa:USER", {}).get('USER_LIST', []) for raw_user in raw_users: name = raw_user.get('username', None) role = raw_user.get('role', []) if role and len(role) > 0: role = role[0] password = raw_user.get('password', None) user = {} if name and role and password: user['name'] = name user['role'] = role user['password'] = password if user: users.append(user) return users
def get_all_port_breakout(self): """Get all the port_breakout configured in the device""" request = [{"path": "operations/sonic-port-breakout:breakout_capabilities", "method": POST}] port_breakout_list = [] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) raw_port_breakout_list = [] if "sonic-port-breakout:output" in response[0][1]: raw_port_breakout_list = response[0][1].get("sonic-port-breakout:output", {}).get('caps', []) for port_breakout in raw_port_breakout_list: name = port_breakout.get('port', None) mode = port_breakout.get('defmode', None) if name and mode: if '[' in mode: mode = mode[:mode.index('[')] norm_port_breakout = {'name': name, 'mode': mode} mode = get_breakout_mode(self._module, name) if mode: norm_port_breakout['mode'] = mode port_breakout_list.append(norm_port_breakout) return port_breakout_list
def get_as_path_list(self): url = "data/openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/as-path-sets" method = "GET" request = [{"path": url, "method": method}] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) as_path_lists = [] if "openconfig-bgp-policy:as-path-sets" in response[0][1]: temp = response[0][1].get("openconfig-bgp-policy:as-path-sets", {}) if "as-path-set" in temp: as_path_lists = temp["as-path-set"] as_path_list_configs = [] for as_path in as_path_lists: result = dict() as_name = as_path["as-path-set-name"] member_config = as_path['config'] members = member_config.get("as-path-set-member", []) result['name'] = as_name result['members'] = members as_path_list_configs.append(result) # with open('/root/ansible_log.log', 'a+') as fp: # fp.write('as_path_list: ' + str(as_path_list_configs) + '\n') return as_path_list_configs
def execute_module(self): """ Execute the module :rtype: A dictionary :returns: The result from module execution """ result = {'changed': False} warnings = list() existing_users_facts = self.get_users_facts() commands, requests = self.set_config(existing_users_facts) auth_error = False if commands and len(requests) > 0: if not self._module.check_mode: try: edit_config(self._module, to_request(self._module, requests)) except ConnectionError as exc: try: json_obj = json.loads(str(exc).replace("'", '"')) if json_obj and type( json_obj) is dict and 401 == json_obj['code']: auth_error = True warnings.append( "Unable to get after configs as password got changed for current user" ) else: self._module.fail_json(msg=str(exc), code=exc.code) except Exception as err: self._module.fail_json(msg=str(exc), code=exc.code) result['changed'] = True result['commands'] = commands changed_users_facts = [] if not auth_error: changed_users_facts = self.get_users_facts() result['before'] = existing_users_facts if result['changed']: result['after'] = changed_users_facts result['warnings'] = warnings return result
def get_l3_interfaces(self): url = "data/openconfig-interfaces:interfaces/interface" method = "GET" request = [{"path": url, "method": method}] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) l3_lists = [] if "openconfig-interfaces:interface" in response[0][1]: l3_lists = response[0][1].get("openconfig-interfaces:interface", []) l3_configs = [] for l3 in l3_lists: l3_dict = dict() l3_name = l3["name"] if l3_name == "eth0": continue l3_dict['name'] = l3_name ip = None if l3.get('openconfig-vlan:routed-vlan'): ip = l3['openconfig-vlan:routed-vlan'] else: ip = l3.get('subinterfaces', {}).get('subinterface', [{}])[0] l3_ipv4 = list() if 'openconfig-if-ip:ipv4' in ip and 'addresses' in ip['openconfig-if-ip:ipv4'] and 'address' in ip['openconfig-if-ip:ipv4']['addresses']: for ipv4 in ip['openconfig-if-ip:ipv4']['addresses']['address']: if ipv4.get('config') and ipv4.get('config').get('ip'): temp = dict() temp['address'] = str(ipv4['config']['ip']) + '/' + str(ipv4['config']['prefix-length']) temp['secondary'] = ipv4['config']['openconfig-interfaces-ext:secondary'] l3_ipv4.append(temp) l3_ipv6 = list() if 'openconfig-if-ip:ipv6' in ip and 'addresses' in ip['openconfig-if-ip:ipv6'] and 'address' in ip['openconfig-if-ip:ipv6']['addresses']: for ipv6 in ip['openconfig-if-ip:ipv6']['addresses']['address']: if ipv6.get('config') and ipv6.get('config').get('ip'): temp = dict() temp['address'] = str(ipv6['config']['ip']) + '/' + str(ipv6['config']['prefix-length']) l3_ipv6.append(temp) l3_dict['ipv4'] = {'addresses': l3_ipv4} l3_dict['ipv6'] = {'addresses': l3_ipv6} if 'openconfig-if-ip:ipv6' in ip and 'config' in ip['openconfig-if-ip:ipv6'] and 'enabled' in ip['openconfig-if-ip:ipv6']['config']: l3_dict['ipv6']['enabled'] = ip['openconfig-if-ip:ipv6']['config']['enabled'] l3_configs.append(l3_dict) # with open('/root/ansible_log.log', 'a+') as fp: # fp.write('l3_configs: ' + str(l3_configs) + '\n') return l3_configs
def get_all_mclag_portchannel_members(self): """Get all the mclag portchannel members for the domain available in chassis""" request = [{"path": "data/openconfig-mclag:mclag/interfaces", "method": GET}] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) if ('openconfig-mclag:interfaces' in response[0][1]): data = response[0][1]['openconfig-mclag:interfaces'] else: data = {} return data
def get_bgp_as(module, vrf_name): as_val = None get_path = '%s=%s/%s/global/config' % (network_instance_path, vrf_name, protocol_bgp_path) request = {"path": get_path, "method": GET} try: response = edit_config(module, to_request(module, request)) except ConnectionError as exc: module.fail_json(msg=str(exc), code=exc.code) resp = response[0][1] if "openconfig-network-instance:config" in resp and 'as' in resp['openconfig-network-instance:config']: as_val = resp['openconfig-network-instance:config']['as'] return as_val
def get_aaa(self): """Get aaa details available in chassis""" request = [{"path": "data/openconfig-system:system/aaa", "method": GET}] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) if ('openconfig-system:aaa' in response[0][1]): if ('authentication' in response[0][1]['openconfig-system:aaa']): data = response[0][1]['openconfig-system:aaa']['authentication']['config'] else: data = {} return data
def get_bgp_communities(self): url = "data/openconfig-routing-policy:routing-policy/defined-sets/openconfig-bgp-policy:bgp-defined-sets/community-sets" method = "GET" request = [{"path": url, "method": method}] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) bgp_communities = [] if "openconfig-bgp-policy:community-sets" in response[0][1]: temp = response[0][1].get("openconfig-bgp-policy:community-sets", {}) if "community-set" in temp: bgp_communities = temp["community-set"] bgp_communities_configs = [] for bgp_community in bgp_communities: result = dict() name = bgp_community["community-set-name"] member_config = bgp_community['config'] match = member_config['match-set-options'] permit_str = member_config.get('openconfig-bgp-policy-ext:action', None) members = member_config.get("community-member", []) result['name'] = name result['match'] = match if permit_str and permit_str == 'PERMIT': result['permit'] = True else: result['permit'] = False if members: result['type'] = 'expanded' if 'REGEX' in members[ 0] else 'standard' else: result['type'] = '' if result['type'] == 'expanded': members = [':'.join(i.split(':')[1:]) for i in members] result[ 'local_as'] = True if "NO_EXPORT_SUBCONFED" in members else False result[ 'no_advertise'] = True if "NO_ADVERTISE" in members else False result['no_export'] = True if "NO_EXPORT" in members else False result['no_peer'] = True if "NOPEER" in members else False result['members'] = {'regex': members} bgp_communities_configs.append(result) # with open('/root/ansible_log.log', 'a+') as fp: # fp.write('bgp_communities: ' + str(bgp_communities_configs) + '\n') return bgp_communities_configs
def get_all_mclag(self): """Get all the mclag available in chassis""" request = [{"path": "data/openconfig-mclag:mclag/mclag-domains", "method": GET}] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) if ('openconfig-mclag:mclag-domains' in response[0][1]): data = response[0][1]['openconfig-mclag:mclag-domains'] else: data = {} if data: data = data['mclag-domain'][0]['state'] return data
def get_bgp_neighbors(module, vrf_name): neighbors_data = None get_path = '%s=%s/%s/neighbors' % (network_instance_path, vrf_name, protocol_bgp_path) request = {"path": get_path, "method": GET} try: response = edit_config(module, to_request(module, request)) except ConnectionError as exc: module.fail_json(msg=str(exc), code=exc.code) resp = response[0][1] if "openconfig-network-instance:neighbors" in resp: neighbors_data = resp['openconfig-network-instance:neighbors'] return neighbors_data
def get_all_vxlans_tunnels_vlan_map(self): """Get all the vxlan tunnels and vlan map available """ request = [{"path": "data/sonic-vxlan:sonic-vxlan", "method": GET}] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) vxlans_tunnels_vlan_map = {} if "sonic-vxlan:sonic-vxlan" in response[0][1]: vxlans_tunnels_vlan_map = response[0][1].get( "sonic-vxlan:sonic-vxlan", {}) return vxlans_tunnels_vlan_map
def get_anycast_addr(self): """Get system anycast address available in chassis""" request = [{ "path": "data/sonic-sag:sonic-sag/SAG_GLOBAL/SAG_GLOBAL_LIST/", "method": GET }] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) if ('sonic-sag:SAG_GLOBAL_LIST' in response[0][1]): data = response[0][1]['sonic-sag:SAG_GLOBAL_LIST'][0] else: data = {} return data
def get_all_bgp_globals(module, vrfs): """Get all BGP configurations available in chassis""" all_bgp_globals = [] for vrf_name in vrfs: get_path = '%s=%s/%s/global' % (network_instance_path, vrf_name, protocol_bgp_path) request = {"path": get_path, "method": GET} try: response = edit_config(module, to_request(module, request)) except ConnectionError as exc: module.fail_json(msg=str(exc), code=exc.code) for resp in response: if "openconfig-network-instance:global" in resp[1]: bgp_data = {'global': resp[1].get("openconfig-network-instance:global", {})} bgp_data.update({'vrf_name': vrf_name}) all_bgp_globals.append(bgp_data) return all_bgp_globals
def get_system(self): """Get system hostname available in chassis""" request = [{ "path": "data/openconfig-system:system/config", "method": GET }] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) if ('openconfig-system:config' in response[0][1]): data = response[0][1]['openconfig-system:config'] else: data = {} return data
def get_all_interfaces(self): """Get all the interfaces available in chassis""" all_interfaces = {} request = [{ "path": "data/openconfig-interfaces:interfaces", "method": GET }] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) if "openconfig-interfaces:interfaces" in response[0][1]: all_interfaces = response[0][1].get( "openconfig-interfaces:interfaces", {}) return all_interfaces['interface']
def get_all_vrfs(module): """Get all VRF configurations available in chassis""" all_vrfs = [] ret = [] request = {"path": "data/sonic-vrf:sonic-vrf/VRF/VRF_LIST", "method": GET} try: response = edit_config(module, to_request(module, request)) except ConnectionError as exc: module.fail_json(msg=str(exc), code=exc.code) if 'sonic-vrf:VRF_LIST' in response[0][1]: all_vrf_data = response[0][1].get('sonic-vrf:VRF_LIST', []) if all_vrf_data: for vrf_data in all_vrf_data: all_vrfs.append(vrf_data['vrf_name']) return all_vrfs
def get_all_vxlans_evpn_nvo_list(self): """Get all the evpn nvo list available """ request = [{ "path": "data/sonic-vxlan:sonic-vxlan/EVPN_NVO/EVPN_NVO_LIST", "method": GET }] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) vxlans_evpn_nvo_list = [] if "sonic-vxlan:EVPN_NVO_LIST" in response[0][1]: vxlans_evpn_nvo_list = response[0][1].get( "sonic-vxlan:EVPN_NVO_LIST", []) return vxlans_evpn_nvo_list
def initiate_request(module): """Get all the data available in chassis""" url = module.params['url'] body = module.params['body'] method = module.params['method'] if method == "GET" or method == "DELETE": request = to_request(module, [{"path": url, "method": method}]) elif method == "PATCH" or method == "PUT" or method == "POST": request = to_request(module, [{ "path": url, "method": method, "data": body }]) try: response = edit_config(module, request) except ConnectionError as exc: module.fail_json(msg=to_text(exc)) return response
def get_device_interface_naming_mode(module): intf_naming_mode = "" request = { "path": "data/sonic-device-metadata:sonic-device-metadata/DEVICE_METADATA/DEVICE_METADATA_LIST=localhost", "method": "get" } try: response = edit_config(module, to_request(module, request)) except ConnectionError as exc: module.fail_json(msg=str(exc), code=exc.code) if 'sonic-device-metadata:DEVICE_METADATA_LIST' in response[0][1]: device_meta_data = response[0][1].get( 'sonic-device-metadata:DEVICE_METADATA_LIST', []) if device_meta_data: intf_naming_mode = device_meta_data[0].get('intf_naming_mode', 'native') return intf_naming_mode
def get_naming(self): """Get interface_naming type available in chassis""" request = [{ "path": "data/sonic-device-metadata:sonic-device-metadata/DEVICE_METADATA/DEVICE_METADATA_LIST=localhost", "method": GET }] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) if ('sonic-device-metadata:DEVICE_METADATA_LIST' in response[0][1]): intf_data = response[0][1][ 'sonic-device-metadata:DEVICE_METADATA_LIST'] if 'intf_naming_mode' in intf_data[0]: data = intf_data[0] else: data = {} return data
def get_all_bgp_af_redistribute(module, vrfs, af_redis_params_map): """Get all BGP Global Address Family Redistribute configurations available in chassis""" all_af_redis_data = [] ret_redis_data = [] for vrf_name in vrfs: af_redis_data = {} request_path = '%s=%s/table-connections' % (network_instance_path, vrf_name) request = {"path": request_path, "method": GET} try: response = edit_config(module, to_request(module, request)) except ConnectionError as exc: module.fail_json(msg=str(exc), code=exc.code) if "openconfig-network-instance:table-connections" in response[0][1]: af_redis_data.update({vrf_name: response[0][1]['openconfig-network-instance:table-connections']}) if af_redis_data: all_af_redis_data.append(af_redis_data) if all_af_redis_data: for vrf_name in vrfs: key = vrf_name val = next((af_redis_data for af_redis_data in all_af_redis_data if vrf_name in af_redis_data), None) if not val: continue val = val[vrf_name] redis_data = val.get('table-connection', []) if not redis_data: continue filtered_redis_data = [] for e_cfg in redis_data: af_redis_data = get_from_params_map(af_redis_params_map, e_cfg) if af_redis_data: filtered_redis_data.append(af_redis_data) if filtered_redis_data: ret_redis_data.append({key: filtered_redis_data}) return ret_redis_data
def get_all_portchannels(self): """Get all the interfaces available in chassis""" request = [{ "path": "data/sonic-portchannel:sonic-portchannel", "method": GET }] try: response = edit_config(self._module, to_request(self._module, request)) except ConnectionError as exc: self._module.fail_json(msg=str(exc), code=exc.code) if response[0][1]: data = response[0][1]['sonic-portchannel:sonic-portchannel'] else: data = [] if data is not None: if "PORTCHANNEL_MEMBER" in data: portchannel_members_list = data["PORTCHANNEL_MEMBER"][ "PORTCHANNEL_MEMBER_LIST"] else: portchannel_members_list = [] if "PORTCHANNEL" in data: portchannel_list = data["PORTCHANNEL"]["PORTCHANNEL_LIST"] else: portchannel_list = [] if portchannel_list: for i in portchannel_list: if not any(d["name"] == i["name"] for d in portchannel_members_list): portchannel_members_list.append({ 'ifname': None, 'name': i['name'] }) if data: return portchannel_members_list else: return []
def get_breakout_mode(module, name): response = None mode = None component_name = name if "/" in name: component_name = name.replace("/", "%2f") url = "data/openconfig-platform:components/component=%s" % (component_name) request = [{"path": url, "method": GET}] try: response = edit_config(module, to_request(module, request)) except ConnectionError as exc: try: json_obj = json.loads(str(exc).replace("'", '"')) if json_obj and type(json_obj) is dict and 404 == json_obj['code']: response = None else: module.fail_json(msg=str(exc), code=exc.code) except Exception as err: module.fail_json(msg=str(exc), code=exc.code) if response and "openconfig-platform:component" in response[0][1]: raw_port_breakout = response[0][1]['openconfig-platform:component'][0] port_name = raw_port_breakout.get('name', None) port_data = raw_port_breakout.get('port', None) if port_name and port_data and 'openconfig-platform-port:breakout-mode' in port_data: if 'config' in port_data['openconfig-platform-port:breakout-mode']: cfg = port_data['openconfig-platform-port:breakout-mode'][ 'config'] channel_speed = cfg.get('channel-speed', None) num_channels = cfg.get('num-channels', None) if channel_speed and num_channels: speed = channel_speed.replace( 'openconfig-if-ethernet:SPEED_', '') speed = speed.replace('GB', 'G') mode = str(num_channels) + 'x' + speed return mode