From 6bcbdcbd3207a735c159f3e5081f90150696d321 Mon Sep 17 00:00:00 2001 From: MUTHU-RAKESH-27 <19cs127@psgitech.ac.in> Date: Thu, 31 Aug 2023 14:25:34 +0530 Subject: [PATCH 1/9] Network_intent.py (Global_Pool, Reserve_Pool, Network), Network_intent.yml --- playbooks/network_intent.yml | 81 +++- plugins/modules/network_intent.py | 719 ++++++++++++++++++++++++++---- 2 files changed, 688 insertions(+), 112 deletions(-) diff --git a/playbooks/network_intent.yml b/playbooks/network_intent.yml index e144efc628..5c5da42b25 100644 --- a/playbooks/network_intent.yml +++ b/playbooks/network_intent.yml @@ -22,9 +22,9 @@ - GlobalPoolDetails: settings: ippool: - - ipPoolName: Global_Pool1 + - ipPoolName: Global_Pool2 gateway: "" #use this for updating - IpAddressSpace: IPv4 #required when we are creating + IpAddressSpace: IPv4 #required when w2 are creating ipPoolCidr: 100.0.0.0/8 #required when we are creating type: Generic dhcpServerIps: [] #use this for updating @@ -32,10 +32,11 @@ # prev_name: Global_Pool ReservePoolDetails: ipv6AddressSpace: false - ipv4GlobalPool: 100.0.0.0/8 + ipv4GlobalPool: 100.0.0.0/8 ipv4Prefix: true - ipv4PrefixLength: 8 + ipv4PrefixLength: 9 ipv4DnsServers: [] + ipv4Subnet: 100.128.0.0 name: IP_Pool_3 siteName: Global/Chennai/Trill slaacSupport: true @@ -49,24 +50,24 @@ domainName: cisco.co primaryIpAddress: 10.0.0.2 secondaryIpAddress: 10.0.0.3 - # clientAndEndpoint_aaa: #works only if we system settigns is set - # # ipAddress: string #Mandatory for ISE, sec ip for AAA - # network: 10.0.0.9 - # protocol: RADIUS - # servers: AAA - # # sharedSecret: string #ISE + clientAndEndpoint_aaa: #works only if we system settigns is set + # ipAddress: 10.197.156.42 #Mandatory for ISE, sec ip for AAA + network: 10.0.0.20 + protocol: RADIUS + servers: AAA + # sharedSecret: string #ISE messageOfTheday: bannerMessage: hello - retainExistingBanner: "true" + retainExistingBanner: true netflowcollector: ipAddress: 10.0.0.4 port: 443 - # network_aaa: #works only if we system settigns is set - # # ipAddress: string #Mandatory for ISE, sec ip for AAA - # network: 10.0.0.8 - # protocol: RADIUS - # servers: AAA - # # sharedSecret: string #ISE + network_aaa: #works only if we system settigns is set + # ipAddress: string #Mandatory for ISE, sec ip for AAA + network: 10.0.0.20 + protocol: TACACS + servers: AAA + # sharedSecret: string #ISE ntpServer: - 10.0.0.5 snmpServer: @@ -79,4 +80,50 @@ - 10.0.0.7 timezone: GMT siteName: Global/Chennai + # DeviceCredentialsDetails: + # cliCredential: + # description: CLI 1 + # username: user1 + # password: 12345 + # enablePassword: 12345 + # # credentialName: CLI #use this field for update + # snmpV2cRead: + # description: SNMP Read 1 + # readCommunity: user1 + # # credentialName: SNMP Read 1 #use this for update + # snmpV2cWrite: + # description: SNMP Write 1 + # writeCommunity: user1 + # # credentialName: SNMP Write 1 #use this for update + # snmpV3: + # authPassword: 12345678 + # authType: SHA + # snmpMode: AUTHPRIV + # privacyPassword: 12345678 + # privacyType: AES128 + # username: user1 + # description: SNMP v3 1 + # credentialName: SNMP v3 1 #use this for update + # httpsRead: + # name: HTTP Read 1 + # username: user1 + # password: 12345 + # port: 443 + # credentialName: HTTP Read 1 #use this field for update + # httpsWrite: + # name: HTTP Write 1 + # username: user1 + # password: 12345 + # port: 443 + # credentialName: HTTP Write 1 #use this field for update + # AssignDeviceCredential: + # cliName: CLI 1 + # snmpV2ReadName: SNMP Read 1 + # snmpV2WriteName: SNMP Write 1 + # snmpV3Name: SNMP v3 1 + # httpReadName: HTTP Read 1 + # httpWriteName: HTTP Write 1 + # siteName: Global/Chennai/Trill + # delCredentialName: CLI 1 #use this for delete + diff --git a/plugins/modules/network_intent.py b/plugins/modules/network_intent.py index 32d1116a65..f10a9806e1 100644 --- a/plugins/modules/network_intent.py +++ b/plugins/modules/network_intent.py @@ -1,7 +1,11 @@ -# This module will work for global_pool, Reserve_ip_pool and network (under network it will not work for network_aaa and clientEndpoint_aaa) +# This module will work for global_pool, Reserve_ip_pool and network +from ast import And import copy +import ipaddress +from random import choices +from urllib import response try: from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import ( @@ -33,6 +37,8 @@ def __init__(self, module): self.want_reserve = {} self.have_net = {} self.want_net = {} + self.have_dev = {} + self.want_dev = {} self.site_id = None self.validated = [] dnac_params = self.get_dnac_params(self.params) @@ -77,7 +83,19 @@ def validate_input(self): "dhcpServer": {"required": False, "type": 'list'}, "domainName": {"required": False, "type": 'string'}, "primaryIpAddress": {"required": False, "type": 'string'}, - "secondaryIpAddress": {"required": False, "type": 'string'} + "secondaryIpAddress": {"required": False, "type": 'string'}, + "servers" : {"required": False, "type": 'string', "choices": ["ISE", "AAA"]}, + "ipAddress" : {"required": False, "type": 'string'}, + "network": {"required": False, "type": 'string'}, + "protocol": {"required": False, "type": 'string', choices: ["RADIUS", "TACACS"]}, + "bannerMessage": {"required": False, "type": 'string'}, + "retainExistingBanner": {"required": False, "type": 'string'}, + "sharedSecret": {"required": False, "type": 'string'}, + "configureDnacIP": {"required": False, "type": 'string'}, + "ipAddresses": {"required": False, "type": 'list'}, + "timezone": {"required": False, "type": 'string'}, + "ntpServer": {"required": False, "type": 'list'} + } if self.config: msg = None @@ -87,17 +105,165 @@ def validate_input(self): # Validate template params if self.config[0].get("GlobalPoolDetails") is not None: temp = self.config[0].get("GlobalPoolDetails").get("settings").get("ippool") + if self.config[0].get("ReservePoolDetails") is not None: temp1 = [self.config[0].get("ReservePoolDetails")] + if self.config[0].get("NetworkManagementDetails") is not None: - temp2.append(self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dhcpServer")) - temp2.append(self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("domainName")) - temp2.append(self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("primaryIpAddress")) - temp2.append(self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("secondaryIpAddress")) + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("dhcpServer"): + temp2.append({"dhcpServer": self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("dhcpServer")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("dnsServer"): + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("dnsServer").get("domainName"): + temp2.append({"domainName": self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("dnsServer").get("domainName")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("dnsServer").get("primaryIpAddress"): + temp2.append(self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("dnsServer").get("primaryIpAddress")) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("dnsServer").get("secondaryIpAddress"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("dnsServer").get("secondaryIpAddress")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa"): + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("ipAddress"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("ipAddress")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("network"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("network")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("protocol"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("protocol")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("servers"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("servers")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("sharedSecret"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("sharedSecret")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("messageOfTheday"): + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("messageOfTheday").get("bannerMessage"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("messageOfTheday").get("bannerMessage")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("messageOfTheday").get("retainExistingBanner"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("messageOfTheday").get("retainExistingBanner")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector"): + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector").get("ipAddress"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector").get("ipAddress")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector").get("port"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector").get("port")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa"): + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("ipAddress"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("ipAddress")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("network"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("network")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("protocol"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("protocol")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("servers"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("servers")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("sharedSecret"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("sharedSecret")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("ntpServer"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("ntpServer")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("snmpServer"): + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("snmpServer").get("configureDnacIP"): + temp2.append({"secondaryIpAddress": self.config[0] + .get("NetworkManagementDetails") \ + .get("settings").get("snmpServer").get("configureDnacIP")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("snmpServer").get("ipAddresses"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("snmpServer").get("ipAddresses")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("syslogServer"): + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("syslogServer").get("configureDnacIP"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("syslogServer").get("configureDnacIP")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("syslogServer").get("ipAddresses"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("syslogServer").get("ipAddresses")}) + + if self.config[0].get("NetworkManagementDetails") \ + .get("settings").get("timezone"): + temp2.append({"secondaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("timezone")}) temp = temp + temp1 valid_temp, invalid_params = validate_list_of_dicts( @@ -228,15 +394,18 @@ def get_current_pool(self, pool): def get_current_res(self, res): log(str(res)) - res_values = dict( - name = res.get("groupName"), - site_id = res.get("siteId"), - ) - + res_values = { + "name": res.get("groupName"), + "site_id": res.get("siteId"), + } if len(res.get("ipPools")) == 1: + res_values.update({"ipv4DhcpServers": res.get("ipPools")[0].get("dhcpServerIps")}) res_values.update({"ipv4DnsServers": res.get("ipPools")[0].get("dnsServerIps")}) - res_values.update({"ipv4GateWay": res.get("ipPools")[0].get("gateways")[0]}) + if res.get("ipPools")[0].get("gateways") != []: + res_values.update({"ipv4GateWay": res.get("ipPools")[0].get("gateways")[0]}) + else: + res_values.update({"ipv4GateWay": ""}) res_values.update({"ipv6AddressSpace": "False"}) elif len(res.get("ipPools")) == 2: @@ -247,7 +416,10 @@ def get_current_res(self, res): res_values.update({"ipv6AddressSpace": "True"}) res_values.update({"ipv4DhcpServers": res.get("ipPools")[1].get("dhcpServerIps")}) res_values.update({"ipv4DnsServers": res.get("ipPools")[1].get("dnsServerIps")}) - res_values.update({"ipv4GateWay": res.get("ipPools")[1].get("gateways")[0]}) + if res.get("ipPools")[1].get("gateways") != []: + res_values.update({"ipv4GateWay": res.get("ipPools")[1].get("gateways")[0]}) + else: + res_values.update({"ipv4GateWay": ""}) elif res.get("ipPools")[1].get("ipv6") is False: res_values.update({"ipv4DhcpServers": res.get("ipPools")[1].get("dhcpServerIps")}) @@ -256,8 +428,11 @@ def get_current_res(self, res): res_values.update({"ipv6AddressSpace": "True"}) res_values.update({"ipv4DhcpServers": res.get("ipPools")[0].get("dhcpServerIps")}) res_values.update({"ipv4DnsServers": res.get("ipPools")[0].get("dnsServerIps")}) - res_values.update({"ipv4GateWay": res.get("ipPools")[0].get("gateways")[0]}) - + if res.get("ipPools")[0].get("gateways") != []: + res_values.update({"ipv4GateWay": res.get("ipPools")[0].get("gateways")[0]}) + else: + res_values.update({"ipv4GateWay": ""}) + log(str(res_values)) return res_values @@ -288,43 +463,133 @@ def get_current_net(self, site_id): ntpserver_details = get_dict_result(response, "key", "ntp.server") timezone_details = get_dict_result(response, "key", "timezone.site") messageoftheday_details = get_dict_result(response, "key", "device.banner") + network_aaa = get_dict_result(response, "key", "aaa.network.server.1") + network_aaa_pan = get_dict_result(response, "key", "aaa.server.pan.network") + log(str(syslog_details)) + clientAndEndpoint_aaa = get_dict_result(response, "key", "aaa.endpoint.server.1") + clientAndEndpoint_aaa_pan = get_dict_result(response, "key", "aaa.server.pan.endpoint") - snmp_details - log(str(dhcp_details)) - log(str(dns_details)) + log(str(network_aaa)) + log(str(clientAndEndpoint_aaa)) net_values = { "settings": { - "dhcpServer": dhcp_details.get("value"), - "dnsServer": { - "domainName": dns_details.get("value")[0].get("domainName"), - "primaryIpAddress": dns_details.get("value")[0].get("primaryIpAddress"), - "secondaryIpAddress": dns_details.get("value")[0].get("secondaryIpAddress") - }, + # "dhcpServer": dhcp_details.get("value"), + + # "dnsServer": { + # "domainName": dns_details.get("value")[0].get("domainName"), + # "primaryIpAddress": dns_details.get("value")[0].get("primaryIpAddress"), + # "secondaryIpAddress": dns_details.get("value")[0].get("secondaryIpAddress") + # }, + "snmpServer": { "configureDnacIP": snmp_details.get("value")[0].get("configureDnacIP"), "ipAddresses": snmp_details.get("value")[0].get("ipAddresses"), }, + "syslogServer": { "configureDnacIP": syslog_details.get("value")[0].get("configureDnacIP"), "ipAddresses": syslog_details.get("value")[0].get("ipAddresses"), }, + "netflowcollector": { "ipAddress": netflow_details.get("value")[0].get("ipAddress"), "port": netflow_details.get("value")[0].get("port"), "configureDnacIP": netflow_details.get("value")[0].get("configureDnacIP"), }, - "ntpServer": ntpserver_details.get("value"), + + # "ntpServer": ntpserver_details.get("value"), + "timezone": timezone_details.get("value")[0], - "messageOfTheday": { - "bannerMessage": messageoftheday_details.get("value")[0].get("bannerMessage"), - "retainExistingBanner": messageoftheday_details.get("value")[0].get("retainExistingBanner"), - } + + # "messageOfTheday": { + # "bannerMessage": messageoftheday_details.get("value")[0].get("bannerMessage"), + # "retainExistingBanner": messageoftheday_details \ + # .get("value")[0].get("retainExistingBanner"), + # }, + + # "network_aaa": { + # "network": network_aaa.get("value")[0].get("ipAddress"), + # "protocol": network_aaa.get("value")[0].get("protocol"), + # "ipAddress": network_aaa_pan.get("value")[0] + # }, + + # "clientAndEndpoint_aaa": { + # "network": clientAndEndpoint_aaa.get("value")[0].get("ipAddress"), + # "protocol": clientAndEndpoint_aaa.get("value")[0].get("protocol"), + # "ipAddress": clientAndEndpoint_aaa_pan.get("value")[0], + # } } } - + if dhcp_details != None: + net_values.get("settings").update({"dhcpServer": dhcp_details.get("value")}) + + if dns_details != None: + net_values.get("settings").update({"dnsServer": { + "domainName": dns_details.get("value")[0].get("domainName"), + "primaryIpAddress": dns_details.get("value")[0].get("primaryIpAddress"), + "secondaryIpAddress": dns_details.get("value")[0] \ + .get("secondaryIpAddress") + } + }) + + # if snmp_details != None: + # net_values.get("settings").update({"snmpServer": { + # "configureDnacIP": snmp_details \ + # .get("value")[0].get("configureDnacIP"), + # "ipAddresses": snmp_details.get("value")[0].get("ipAddresses"), + # } + # }) + + # if syslog_details != None: + # net_values.get("settings").update({"syslogServer": { + # "configureDnacIP": syslog_details \ + # .get("value")[0].get("configureDnacIP"), + # "ipAddresses": syslog_details.get("value")[0].get("ipAddresses"), + # } + # }) + + # if netflow_details != None: + # net_values.get("settings").update({"netflowcollector": { + # "ipAddress": netflow_details.get("value")[0].get("ipAddress"), + # "port": netflow_details.get("value")[0].get("port"), + # "configureDnacIP": netflow_details.get("value")[0].get("configureDnacIP"), + # } + # }) + + if ntpserver_details != None: + net_values.get("settings").update({"ntpServer": ntpserver_details.get("value")}) + + # if timezone_details != None: + # net_values.get("settings").update({"timezone": timezone_details.get("value")[0]}) + + if messageoftheday_details != None: + net_values.get("settings").update({"messageOfTheday": { + "bannerMessage": messageoftheday_details \ + .get("value")[0].get("bannerMessage"), + "retainExistingBanner": messageoftheday_details \ + .get("value")[0].get("retainExistingBanner"), + } + }) + + if network_aaa and network_aaa_pan: + net_values.get("settings").update({"network_aaa": { + "network": network_aaa.get("value")[0].get("ipAddress"), + "protocol": network_aaa.get("value")[0].get("protocol"), + "ipAddress": network_aaa_pan.get("value")[0] + } + }) + + if clientAndEndpoint_aaa and clientAndEndpoint_aaa_pan: + net_values.get("settings").update({"clientAndEndpoint_aaa": { + "network": clientAndEndpoint_aaa.get("value")[0].get("ipAddress"), + "protocol": clientAndEndpoint_aaa.get("value")[0].get("protocol"), + "ipAddress": clientAndEndpoint_aaa_pan.get("value")[0], + } + }) + log(str(net_values)) return net_values def get_site_id(self, site_name): @@ -486,7 +751,7 @@ def get_have(self): if self.params.get("config")[0].get("ReservePoolDetails") is not None: have_reserve = {} (res_exists, res_details, res_id) = self.res_exists() - + log(str(res_exists)) if self.log: log("Reservation Exists: " + str(res_exists) \ + "\n Reserved Pool: " + str(res_details)) @@ -509,16 +774,16 @@ def get_have(self): site_name = self.params.get("config")[0].get("NetworkManagementDetails").get("siteName") if site_name is None: - self.module.fail_json(msg="Mandatory Parameter siteName missings", response=[]) + self.module.fail_json(msg="Mandatory Parameter siteName missing", response=[]) - site_id_net = self.get_site_id(site_name) + site_id = self.get_site_id(site_name) - if site_id_net is None: + if site_id is None: self.module.fail_json(msg="Invalid siteName", response=[]) - have_net["site_id"] = site_id_net - have_net["net_details"] = self.get_current_net(site_id_net) - + have_net["site_id"] = site_id + have_net["net_details"] = self.get_current_net(site_id) + log(str(have_net)) self.have_net = have_net @@ -630,6 +895,8 @@ def get_want(self): .get("ReservePoolDetails").get("ipv4DhcpServers"), "ipv4DnsServers": self.params.get("config")[0] \ .get("ReservePoolDetails").get("ipv4DnsServers"), + "ipv4Subnet": self.params.get("config")[0] \ + .get("ReservePoolDetails").get("ipv4Subnet"), "ipv6GlobalPool": self.params.get("config")[0] \ .get("ReservePoolDetails").get("ipv6GlobalPool"), "ipv6Prefix": self.params.get("config")[0] \ @@ -640,6 +907,8 @@ def get_want(self): .get("ReservePoolDetails").get("ipv6GateWay"), "ipv6DhcpServers": self.params.get("config")[0] \ .get("ReservePoolDetails").get("ipv6DhcpServers"), + "ipv6Subnet": self.params.get("config")[0] \ + .get("ReservePoolDetails").get("ipv6Subnet"), "ipv6DnsServers": self.params.get("config")[0] \ .get("ReservePoolDetails").get("ipv6DnsServers"), "ipv4TotalHost": self.params.get("config")[0] \ @@ -690,6 +959,7 @@ def get_want(self): del want_reserve['ipv4Prefix'] del want_reserve['ipv4PrefixLength'] del want_reserve['ipv4TotalHost'] + del want_reserve['ipv4Subnet'] self.want_reserve = want_reserve @@ -697,56 +967,287 @@ def get_want(self): log(str(self.params)) want_net = { "settings": { - "dhcpServer": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("dhcpServer"), + "dhcpServer": { + + }, "dnsServer": { - "domainName": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("domainName"), - "primaryIpAddress": self.params.get("config")[0] \ - .get("NetworkManagementDetails").get("settings") \ - .get("dnsServer").get("primaryIpAddress"), - "secondaryIpAddress": self.params.get("config")[0] \ - .get("NetworkManagementDetails").get("settings") \ - .get("dnsServer").get("secondaryIpAddress") + }, "snmpServer": { - "configureDnacIP": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("snmpServer").get("configureDnacIP"), - "ipAddresses": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("snmpServer").get("ipAddresses") + }, "syslogServer": { - "configureDnacIP": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("syslogServer").get("configureDnacIP"), - "ipAddresses": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("syslogServer").get("ipAddresses") + }, "netflowcollector": { - "ipAddress": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("ipAddress"), - "port": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("port"), - "configureDnacIP": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("configureDnacIP") + }, - "ntpServer": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("ntpServer"), - "timezone": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("timezone"), + "ntpServer": { + + }, + "timezone":"", "messageOfTheday": { - "bannerMessage": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday").get("bannerMessage"), - "retainExistingBanner": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday").get("retainExistingBanner") + + }, + "network_aaa": { + + }, + "clientAndEndpoint_aaa": { + } } } + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("dhcpServer"): + want_net.get("settings").update({"dhcpServer": + self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("dhcpServer") + }) + else: + del want_net.get("settings")["dhcpServer"] + + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("ntpServer"): + want_net.get("settings").update({"ntpServer": + self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("ntpServer") + }) + else: + del want_net.get("settings")["ntpServer"] + + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("timezone"): + want_net.get("settings")["timezone"] = \ + self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("timezone") + else: + del want_net.get("settings")["timezone"] + + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("dnsServer"): + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("dnsServer").get("domainName"): + want_net.get("settings").get("dnsServer").update({ + "domainName": self.params.get("config")[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("dnsServer").get("domainName") + }) + + if self.params.get("config")[0] \ + .get("NetworkManagementDetails").get("settings"): + want_net.get("settings").get("dnsServer").update({ + "primaryIpAddress": self.params.get("config")[0] \ + .get("NetworkManagementDetails").get("settings") \ + .get("dnsServer").get("primaryIpAddress") + }) + + if self.params.get("config")[0] \ + .get("NetworkManagementDetails").get("settings") \ + .get("dnsServer").get("secondaryIpAddress"): + want_net.get("settings").get("dnsServer").update({ + "secondaryIpAddress": self.params.get("config")[0] \ + .get("NetworkManagementDetails").get("settings") \ + .get("dnsServer").get("secondaryIpAddress") + }) + else: + del want_net.get("settings")["dnsServer"] + + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("snmpServer"): + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("snmpServer").get("configureDnacIP"): + want_net.get("settings").get("snmpServer").update({ + "configureDnacIP": self.params.get("config")[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("snmpServer").get("configureDnacIP") + }) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("snmpServer").get("ipAddresses"): + want_net.get("settings").get("snmpServer").update({ + "ipAddresses": self.params.get("config")[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("snmpServer").get("ipAddresses") + }) + else: + del want_net.get("settings")["snmpServer"] + + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("syslogServer"): + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("syslogServer").get("configureDnacIP"): + want_net.get("settings").get("syslogServer").update({ + "configureDnacIP": self.params.get("config")[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("syslogServer").get("configureDnacIP") + }) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("syslogServer").get("ipAddresses"): + want_net.get("settings").get("syslogServer").update({ + "ipAddresses": self.params.get("config")[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("syslogServer").get("ipAddresses") + }) + else: + del want_net.get("settings")["syslogServer"] + + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector"): + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector").get("ipAddress"): + want_net.get("settings").get("netflowcollector").update({ + "ipAddress": self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector").get("ipAddress") + }) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector").get("port"): + want_net.get("settings").get("netflowcollector").update({ + "port": self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector").get("port") + }) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector").get("configureDnacIP"): + want_net.get("settings").get("netflowcollector").update({ + "configureDnacIP": self.params.get("config")[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("netflowcollector").get("configureDnacIP") + }) + else: + del want_net.get("settings")["netflowcollector"] + + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("messageOfTheday"): + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("messageOfTheday").get("bannerMessage"): + want_net.get("settings").get("messageOfTheday").update({ + "bannerMessage": self.params.get("config")[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("messageOfTheday").get("bannerMessage") + }) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("messageOfTheday").get("retainExistingBanner"): + want_net.get("settings").get("messageOfTheday").update({ + "retainExistingBanner": self.params.get("config")[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("messageOfTheday").get("retainExistingBanner") + }) + else: + del want_net.get("settings")["messageOfTheday"] + + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa"): + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("ipAddress"): + want_net.get("settings").get("network_aaa").update({ + "ipAddress": self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("ipAddress") + }) + else: + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("servers") == "ISE": + self.module.fail_json( + msg="missing parameter ipAddress", response=[] + ) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("network"): + want_net.get("settings").get("network_aaa").update({ + "network": self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("network") + }) + else: + self.module.fail_json( + msg="missing parameter network", response=[] + ) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("protocol"): + want_net.get("settings").get("network_aaa").update({ + "protocol": self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("protocol") + }) + else: + self.module.fail_json( + msg="missing parameter protocol", response=[] + ) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("servers"): + want_net.get("settings").get("network_aaa").update({ + "servers": self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("servers") + }) + else: + self.module.fail_json( + msg="missing parameter servers", response=[] + ) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("sharedSecret"): + want_net.get("settings").get("network_aaa").update({ + "sharedSecret": self.params.get("config")[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("network_aaa").get("sharedSecret") + }) + else: + del want_net.get("settings")["network_aaa"] + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa"): + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("ipAddress"): + want_net.get("settings").get("clientAndEndpoint_aaa").update({ + "ipAddress": self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("ipAddress") + }) + else: + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("servers") == "ISE": + self.module.fail_json( + msg="missing parameter ipAddress", response=[] + ) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("network"): + want_net.get("settings").get("clientAndEndpoint_aaa").update({ + "network": self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("network") + }) + else: + self.module.fail_json( + msg="missing parameter network", response=[] + ) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("protocol"): + want_net.get("settings").get("clientAndEndpoint_aaa").update({ + "protocol": self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("protocol") + }) + else: + self.module.fail_json( + msg="missing parameter protocol", response=[] + ) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("servers"): + want_net.get("settings").get("clientAndEndpoint_aaa").update({ + "servers": self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("servers") + }) + else: + self.module.fail_json( + msg="missing parameter servers", response=[] + ) + if self.params.get("config")[0].get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("sharedSecret"): + want_net.get("settings").get("clientAndEndpoint_aaa").update({ + "sharedSecret": self.params.get("config")[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("clientAndEndpoint_aaa").get("sharedSecret") + }) + else: + del want_net.get("settings")["clientAndEndpoint_aaa"] + log(str(want_net)) self.want_net = want_net + def get_execution_details(self, execid): response = None + log(str(execid)) response = self.dnac._exec( family="task", function='get_business_api_execution_details', @@ -758,6 +1259,7 @@ def get_execution_details(self, execid): return response + def get_diff_merge(self): if self.params.get("config")[0].get("GlobalPoolDetails") is not None: @@ -835,7 +1337,7 @@ def get_diff_merge(self): if pool_created or pool_updated: if response and isinstance(response, dict): executionid = response.get("executionId") - + log(str(executionid)) while True: execution_details = self.get_execution_details(executionid) if execution_details.get("status") == "SUCCESS": @@ -921,7 +1423,7 @@ def get_diff_merge(self): # self.module.exit_json(**self.result) else: - #creating New Pool + #creating New Reservation res_params = self.want_reserve log(str(res_params)) if not self.want_reserve.get("name") or \ @@ -981,24 +1483,27 @@ def get_diff_merge(self): if self.params.get("config")[0].get("NetworkManagementDetails") is not None: net_updated = False + net_created = False if self.have_net: log("entered") obj_params = [ ("settings", "settings"), ("siteName", "siteName") ] + if self.want_net.get("settings").get("timezone") is None: + self.module.fail_json(msg="Missing required parameter timezone", response=[]) + # if len(self.have_net) == 10: if self.requires_update(self.have_net.get("net_details"), self.want_net, obj_params): log("Network update requires") - #Pool Exists log(str(self.have_net)) log(str(self.want_net)) - res_params = copy.deepcopy(self.want_net) - res_params.update({"site_id": self.have_net.get("site_id")}) + net_params = copy.deepcopy(self.want_net) + net_params.update({"site_id": self.have_net.get("site_id")}) response = self.dnac._exec( family="network_settings", function='update_network', - params=res_params, + params=net_params, ) log("Network Updated") @@ -1011,9 +1516,30 @@ def get_diff_merge(self): self.result["msg"] = "Network doesn't need an update" self.module.exit_json(**self.result) + # else: + # #creating New Pool + # net_params = self.want_reserve + # log(str(res_params)) + # if self.want_net.get("settings").get("timezone") is None: + # self.module.fail_json(msg="Missing required parameter timezone", response=[]) + + # net_params = copy.deepcopy(self.want_net) + # net_params.update({"site_id": self.have_net.get("site_id")}) + # response = self.dnac._exec( + # family="network_settings", + # function='create_network', + # params=net_params, + # ) + + # log("Network Updated") + # log(str(response)) + # net_updated = True + if net_updated: if response and isinstance(response, dict): executionid = response.get("executionId") + log(str(response)) + log(str(executionid)) while True: execution_details = self.get_execution_details(executionid) @@ -1029,13 +1555,16 @@ def get_diff_merge(self): break if net_updated: - log("Network Updated Successfully") - self.result['msg'] = "Network Updated Successfully" + log("Network has been changed Successfully") + self.result['msg'] = "Network Updated successfully" self.result['response'] = self.want_net - + # elif net_created: + # log("Network Created Successfully") + # self.result['msg'] = "Network Created successfully" + # self.result['response'] = self.want_net else: log("Pool doesn't need a update") - self.result['msg'] = "Pool doesn't requires an update" + self.result['msg'] = "Network doesn't requires an update" self.result['response'] = self.have_net def get_res_id_by_name(self, name): @@ -1088,16 +1617,16 @@ def get_diff_delete(self): if response and isinstance(response, dict): executionid = response.get("executionId") while True: - execution_details = self.get_execution_details(executionid) - if execution_details.get("status") == "SUCCESS": + task_details = self.get_task_details(executionid) + if task_details.get("status") == "SUCCESS": self.result['changed'] = True - self.result['response'] = execution_details + self.result['response'] = task_details log(str(response)) self.result['msg'] = "Ip subpool reservation released successfully" - elif execution_details.get("bapiError"): - self.module.fail_json(msg=execution_details.get("bapiError"), - response=execution_details) + elif task_details.get("bapiError"): + self.module.fail_json(msg=task_details.get("bapiError"), + response=task_details) else: self.module.fail_json(msg="Reserved Ip Subpool Not Found", response=[]) @@ -1115,16 +1644,16 @@ def get_diff_delete(self): if response and isinstance(response, dict): executionid = response.get("executionId") while True: - execution_details = self.get_execution_details(executionid) - if execution_details.get("status") == "SUCCESS": + task_details = self.get_task_details(executionid) + if task_details.get("status") == "SUCCESS": self.result['changed'] = True - self.result['response'] = execution_details + self.result['response'] = task_details log(str(response)) self.result['msg'] = "Pool deleted successfully" - elif execution_details.get("bapiError"): - self.module.fail_json(msg=execution_details.get("bapiError"), - response=execution_details) + elif task_details.get("bapiError"): + self.module.fail_json(msg=task_details.get("bapiError"), + response=task_details) else: self.module.fail_json(msg="Pool Not Found", response=[]) From a4ec5e3444894a0fcbda9497c59364cce6169159 Mon Sep 17 00:00:00 2001 From: MUTHU-RAKESH-27 <19cs127@psgitech.ac.in> Date: Mon, 4 Sep 2023 12:21:16 +0530 Subject: [PATCH 2/9] a --- hello.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 hello.txt diff --git a/hello.txt b/hello.txt new file mode 100644 index 0000000000..e69de29bb2 From a51217cc397928ac32342babfe2450ae1f5da474 Mon Sep 17 00:00:00 2001 From: MUTHU-RAKESH-27 <19cs127@psgitech.ac.in> Date: Mon, 4 Sep 2023 12:49:44 +0530 Subject: [PATCH 3/9] device with error --- plugins/modules/network_intent.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/modules/network_intent.py b/plugins/modules/network_intent.py index f10a9806e1..c1165470da 100644 --- a/plugins/modules/network_intent.py +++ b/plugins/modules/network_intent.py @@ -25,7 +25,6 @@ ) from ansible.module_utils.basic import AnsibleModule - class DnacNetwork: def __init__(self, module): self.module = module @@ -89,7 +88,7 @@ def validate_input(self): "network": {"required": False, "type": 'string'}, "protocol": {"required": False, "type": 'string', choices: ["RADIUS", "TACACS"]}, "bannerMessage": {"required": False, "type": 'string'}, - "retainExistingBanner": {"required": False, "type": 'string'}, + "retainExistingBanner": {"required": False, "type": 'bool'}, "sharedSecret": {"required": False, "type": 'string'}, "configureDnacIP": {"required": False, "type": 'string'}, "ipAddresses": {"required": False, "type": 'list'}, @@ -265,7 +264,7 @@ def validate_input(self): .get("NetworkManagementDetails") \ .get("settings").get("timezone")}) - temp = temp + temp1 + temp = temp + temp1 + temp2 valid_temp, invalid_params = validate_list_of_dicts( temp, temp_spec ) @@ -462,7 +461,7 @@ def get_current_net(self, site_id): netflow_details = get_dict_result(response, "key", "netflow.collector") ntpserver_details = get_dict_result(response, "key", "ntp.server") timezone_details = get_dict_result(response, "key", "timezone.site") - messageoftheday_details = get_dict_result(response, "key", "device.banner") + messageoftheday_details = get_dict_result(response, "key", "banner.setting") network_aaa = get_dict_result(response, "key", "aaa.network.server.1") network_aaa_pan = get_dict_result(response, "key", "aaa.server.pan.network") log(str(syslog_details)) @@ -641,6 +640,7 @@ def pool_exists(self): response = response.get("response") current_details = get_dict_result(response, "ipPoolName", name) + log(str(name)) log(str(current_details)) if current_details: pool_exists = True @@ -985,7 +985,7 @@ def get_want(self): "ntpServer": { }, - "timezone":"", + "timezone": "", "messageOfTheday": { }, From 8e6281179bdd66f4c4b6679bc833d5c57a103085 Mon Sep 17 00:00:00 2001 From: Muthu Rakesh <114211490+MUTHU-RAKESH-27@users.noreply.github.com> Date: Wed, 6 Sep 2023 10:44:17 +0530 Subject: [PATCH 4/9] Update network_intent.py Similar to the template_intent.py skeleton --- plugins/modules/network_intent.py | 671 ++++++++++++++---------------- 1 file changed, 301 insertions(+), 370 deletions(-) diff --git a/plugins/modules/network_intent.py b/plugins/modules/network_intent.py index c1165470da..7db40ed6ea 100644 --- a/plugins/modules/network_intent.py +++ b/plugins/modules/network_intent.py @@ -1,35 +1,21 @@ # This module will work for global_pool, Reserve_ip_pool and network -from ast import And +from __future__ import absolute_import, division, print_function + import copy -import ipaddress -from random import choices -from urllib import response - -try: - from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import ( - AnsibleArgSpecValidator, - ) -except ImportError: - ANSIBLE_UTILS_IS_INSTALLED = False -else: - ANSIBLE_UTILS_IS_INSTALLED = True +from ansible.module_utils.basic import AnsibleModule from ansible_collections.cisco.dnac.plugins.module_utils.dnac import ( - DNACSDK, - dnac_argument_spec, + DnacBase, validate_list_of_dicts, - log, get_dict_result, dnac_compare_equality, ) -from ansible.module_utils.basic import AnsibleModule -class DnacNetwork: +class DnacNetwork(DnacBase): + """Class containing member attributes for network intent module""" def __init__(self, module): - self.module = module - self.params = module.params - self.config = copy.deepcopy(module.params.get("config")) + super().__init__(module) self.have = {} self.want = {} self.have_reserve = {} @@ -39,96 +25,89 @@ def __init__(self, module): self.have_dev = {} self.want_dev = {} self.site_id = None - self.validated = [] - dnac_params = self.get_dnac_params(self.params) - log(str(dnac_params)) - self.dnac = DNACSDK(params=dnac_params) - self.log = dnac_params.get("dnac_log") - - self.result = dict(changed=False, diff=[], response=[], warnings=[]) - def get_state(self): - return self.params.get("state") def validate_input(self): + """Validate the fields provided in the playbook""" + temp_spec = { - "IpAddressSpace": {"required": False, "type": 'string'}, - "dhcpServerIps": {"required": False, "type": 'list'}, - "dnsServerIps": {"required": False, "type": 'list'}, - "gateway": {"required": False, "type": 'string'}, - "ipPoolCidr": {"required": False, "type": 'string'}, - "ipPoolName": {"required": False, "type": 'string'}, - "name": {"required": False, "type": 'string'}, - "prev_name": {"required": False, "type": 'string'}, - "type": {"required": False, "type": "string", \ + "IpAddressSpace": {"type": 'string'}, + "dhcpServerIps": {"type": 'list'}, + "dnsServerIps": {"type": 'list'}, + "gateway": {"type": 'string'}, + "ipPoolCidr": {"type": 'string'}, + "ipPoolName": {"type": 'string'}, + "name": {"type": 'string'}, + "prev_name": {"type": 'string'}, + "type": {"type": "string", \ "choices": ["Generic", "tunnel", "LAN", "WAN", "management", "service"]}, - "ipv6AddressSpace": {"required": False, "type": 'string'}, - "ipv4GlobalPool": {"required": False, "type": 'string'}, - "ipv4Prefix": {"required": False, "type": 'string'}, - "ipv4PrefixLength": {"required": False, "type": 'string'}, - "ipv4GateWay": {"required": False, "type": 'string'}, - "ipv4DhcpServers": {"required": False, "type": 'list'}, - "ipv4DnsServers": {"required": False, "type": 'list'}, - "ipv6GlobalPool": {"required": False, "type": 'string'}, - "ipv6Prefix": {"required": False, "type": 'string'}, - "ipv6PrefixLength": {"required": False, "type": 'integer'}, - "ipv6GateWay": {"required": False, "type": 'string'}, - "ipv6DhcpServers": {"required": False, "type": 'list'}, - "ipv6DnsServers": {"required": False, "type": 'list'}, - "ipv4TotalHost": {"required": False, "type": 'integer'}, - "ipv6TotalHost": {"required": False, "type": 'integet'}, - "slaacSupport": {"required": False, "type": 'string'}, - "siteName": {"required": False, "type": 'string'}, - "dhcpServer": {"required": False, "type": 'list'}, - "domainName": {"required": False, "type": 'string'}, - "primaryIpAddress": {"required": False, "type": 'string'}, - "secondaryIpAddress": {"required": False, "type": 'string'}, - "servers" : {"required": False, "type": 'string', "choices": ["ISE", "AAA"]}, - "ipAddress" : {"required": False, "type": 'string'}, - "network": {"required": False, "type": 'string'}, - "protocol": {"required": False, "type": 'string', choices: ["RADIUS", "TACACS"]}, - "bannerMessage": {"required": False, "type": 'string'}, - "retainExistingBanner": {"required": False, "type": 'bool'}, - "sharedSecret": {"required": False, "type": 'string'}, - "configureDnacIP": {"required": False, "type": 'string'}, - "ipAddresses": {"required": False, "type": 'list'}, - "timezone": {"required": False, "type": 'string'}, - "ntpServer": {"required": False, "type": 'list'} + "ipv6AddressSpace": {"type": 'bool'}, + "ipv4GlobalPool": {"type": 'string'}, + "ipv4Prefix": {"type": 'bool'}, + "ipv4PrefixLength": {"type": 'string'}, + "ipv4GateWay": {"type": 'string'}, + "ipv4DhcpServers": {"type": 'list'}, + "ipv4DnsServers": {"type": 'list'}, + "ipv6GlobalPool": {"type": 'string'}, + "ipv6Prefix": {"type": 'bool'}, + "ipv6PrefixLength": {"type": 'integer'}, + "ipv6GateWay": {"type": 'string'}, + "ipv6DhcpServers": {"type": 'list'}, + "ipv6DnsServers": {"type": 'list'}, + "ipv4TotalHost": {"type": 'integer'}, + "ipv6TotalHost": {"type": 'integer'}, + "slaacSupport": {"type": 'bool'}, + "siteName": {"type": 'string'}, + "dhcpServer": {"type": 'list'}, + "domainName": {"type": 'string'}, + "primaryIpAddress": {"type": 'string'}, + "secondaryIpAddress": {"type": 'string'}, + "servers" : {"type": 'string', "choices": ["ISE", "AAA"]}, + "ipAddress" : {"type": 'string'}, + "network": {"type": 'string'}, + "protocol": {"type": 'string', "choices": ["RADIUS", "TACACS"]}, + "bannerMessage": {"type": 'string'}, + "retainExistingBanner": {"type": 'bool'}, + "sharedSecret": {"type": 'string'}, + "configureDnacIP": {"type": 'bool'}, + "ipAddresses": {"type": 'list'}, + "timezone": {"type": 'string'}, + "ntpServer": {"type": 'list'} } if self.config: - msg = None - temp = [] - temp1 = [] - temp2 = [] + globalPool_details = [] + ReservePool_details = [] + network_details = [] # Validate template params if self.config[0].get("GlobalPoolDetails") is not None: - temp = self.config[0].get("GlobalPoolDetails").get("settings").get("ippool") + globalPool_details = self.config[0].get("GlobalPoolDetails").get("settings").get("ippool") if self.config[0].get("ReservePoolDetails") is not None: - temp1 = [self.config[0].get("ReservePoolDetails")] + ReservePool_details = [self.config[0].get("ReservePoolDetails")] if self.config[0].get("NetworkManagementDetails") is not None: if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("dhcpServer"): - temp2.append({"dhcpServer": self.config[0].get("NetworkManagementDetails") \ + network_details.append({"dhcpServer": self.config[0].get("NetworkManagementDetails") \ .get("settings").get("dhcpServer")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("dnsServer"): if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("dnsServer").get("domainName"): - temp2.append({"domainName": self.config[0].get("NetworkManagementDetails") \ + network_details.append({"domainName": self.config[0].get("NetworkManagementDetails") \ .get("settings").get("dnsServer").get("domainName")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("dnsServer").get("primaryIpAddress"): - temp2.append(self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("primaryIpAddress")) + network_details.append({"primaryIpAddress": self.config[0] \ + .get("NetworkManagementDetails") \ + .get("settings").get("dnsServer").get("primaryIpAddress")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("dnsServer").get("secondaryIpAddress"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("dnsServer").get("secondaryIpAddress")}) @@ -136,31 +115,31 @@ def validate_input(self): .get("settings").get("clientAndEndpoint_aaa"): if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("ipAddress"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("ipAddress")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("network"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("network")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("protocol"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("protocol")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("servers"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("servers")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("sharedSecret"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("sharedSecret")}) @@ -168,13 +147,13 @@ def validate_input(self): .get("settings").get("messageOfTheday"): if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("messageOfTheday").get("bannerMessage"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("messageOfTheday").get("bannerMessage")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("messageOfTheday").get("retainExistingBanner"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("messageOfTheday").get("retainExistingBanner")}) @@ -182,13 +161,13 @@ def validate_input(self): .get("settings").get("netflowcollector"): if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("netflowcollector").get("ipAddress"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("netflowcollector").get("ipAddress")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("netflowcollector").get("port"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("netflowcollector").get("port")}) @@ -196,37 +175,37 @@ def validate_input(self): .get("settings").get("network_aaa"): if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("ipAddress"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("ipAddress")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("network"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("network")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("protocol"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("protocol")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("servers"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("servers")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("sharedSecret"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("sharedSecret")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("ntpServer"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("ntpServer")}) @@ -234,13 +213,13 @@ def validate_input(self): .get("settings").get("snmpServer"): if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("snmpServer").get("configureDnacIP"): - temp2.append({"secondaryIpAddress": self.config[0] + network_details.append({"secondaryIpAddress": self.config[0] .get("NetworkManagementDetails") \ .get("settings").get("snmpServer").get("configureDnacIP")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("snmpServer").get("ipAddresses"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("snmpServer").get("ipAddresses")}) @@ -248,46 +227,53 @@ def validate_input(self): .get("settings").get("syslogServer"): if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("syslogServer").get("configureDnacIP"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("syslogServer").get("configureDnacIP")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("syslogServer").get("ipAddresses"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("syslogServer").get("ipAddresses")}) if self.config[0].get("NetworkManagementDetails") \ .get("settings").get("timezone"): - temp2.append({"secondaryIpAddress": self.config[0] \ + network_details.append({"secondaryIpAddress": self.config[0] \ .get("NetworkManagementDetails") \ .get("settings").get("timezone")}) - temp = temp + temp1 + temp2 + self.log(str(network_details)) + self.log(str(ReservePool_details)) + self.log(str(network_details)) + playbook_details = globalPool_details + ReservePool_details + network_details valid_temp, invalid_params = validate_list_of_dicts( - temp, temp_spec + playbook_details, temp_spec ) if invalid_params: - msg = "Invalid parameters in playbook: {0}".format( + self.msg = "Invalid parameters in playbook: {0}".format( "\n".join(invalid_params) ) - self.module.fail_json(msg=msg) - log(str(invalid_params)) + self.status = "failed" + return self + self.log(str(invalid_params)) self.validated = valid_temp - if self.log: - log(str(valid_temp)) - log(str(self.result)) + self.log(str(valid_temp)) - log(str(self.validated)) + self.log(str(self.validated)) if self.params.get("config")[0].get("GlobalPoolDetails") is not None: for temp in self.validated: - log(str(temp)) + self.log(str(temp)) if self.params.get("config")[0].get("GlobalPoolDetails") \ .get("ipPoolName") is not None: - msg = "missing required arguments: ipPoolName" - self.module.fail_json(msg=msg) + self.msg = "missing required arguments: ipPoolName" + self.status = "failed" + return self + + self.msg = "Successfully validated input" + self.status = "success" + return self def get_dnac_params(self, params): dnac_params = dict( @@ -305,8 +291,8 @@ def get_dnac_params(self, params): def requires_update(self, have, want, obj_params): current_obj = have requested_obj = want - log(str(current_obj)) - log(str(requested_obj)) + self.log(str(current_obj)) + self.log(str(requested_obj)) return any(not dnac_compare_equality(current_obj.get(dnac_param), requested_obj.get(ansible_param)) @@ -326,9 +312,9 @@ def get_pool_id_from_name(self, pool_name): if isinstance(response, dict): if "response" in response: response = response.get("response") - log(str(response)) + self.log(str(response)) current_details = get_dict_result(response, "ipPoolName", pool_name) - log(str(current_details)) + self.log(str(current_details)) if current_details: pool_id = current_details.get("id") @@ -351,19 +337,19 @@ def get_res_id_from_name(self, res_name): if isinstance(response, dict): if "response" in response: response = response.get("response") - log(str(response)) + self.log(str(response)) current_details = get_dict_result(response, "groupName", res_name) - log(str(current_details)) + self.log(str(current_details)) _id = current_details.get("id") except: - log("except") + self.log("except") result = None return _id def get_current_pool(self, pool): - log(str(pool)) + self.log(str(pool)) pool_values = { "settings": { "ippool": [{ @@ -375,13 +361,13 @@ def get_current_pool(self, pool): }] } } - log(str(pool_values)) + self.log(str(pool_values)) if pool.get("ipv6") is False: pool_values.get("settings").get("ippool")[0].update({"IpAddressSpace": "IPv4"}) - log("ipv6 - false") + self.log("ipv6 - false") else: pool_values.get("settings").get("ippool")[0].update({"IpAddressSpace": "IPv6"}) - log("ipv6 - true") + self.log("ipv6 - true") if not pool["gateways"]: pool_values.get("settings").get("ippool")[0].update({"gateway": ""}) else: @@ -392,7 +378,7 @@ def get_current_pool(self, pool): def get_current_res(self, res): - log(str(res)) + self.log(str(res)) res_values = { "name": res.get("groupName"), "site_id": res.get("siteId"), @@ -431,12 +417,12 @@ def get_current_res(self, res): res_values.update({"ipv4GateWay": res.get("ipPools")[0].get("gateways")[0]}) else: res_values.update({"ipv4GateWay": ""}) - log(str(res_values)) + self.log(str(res_values)) return res_values def get_current_net(self, site_id): - log(str(site_id)) + self.log(str(site_id)) response = None try: @@ -448,7 +434,7 @@ def get_current_net(self, site_id): except: result = None - log(str(response)) + self.log(str(response)) if isinstance(response, dict): if "response" in response: @@ -464,22 +450,15 @@ def get_current_net(self, site_id): messageoftheday_details = get_dict_result(response, "key", "banner.setting") network_aaa = get_dict_result(response, "key", "aaa.network.server.1") network_aaa_pan = get_dict_result(response, "key", "aaa.server.pan.network") - log(str(syslog_details)) + self.log(str(syslog_details)) clientAndEndpoint_aaa = get_dict_result(response, "key", "aaa.endpoint.server.1") clientAndEndpoint_aaa_pan = get_dict_result(response, "key", "aaa.server.pan.endpoint") - log(str(network_aaa)) - log(str(clientAndEndpoint_aaa)) + self.log(str(network_aaa)) + self.log(str(clientAndEndpoint_aaa)) net_values = { "settings": { - # "dhcpServer": dhcp_details.get("value"), - - # "dnsServer": { - # "domainName": dns_details.get("value")[0].get("domainName"), - # "primaryIpAddress": dns_details.get("value")[0].get("primaryIpAddress"), - # "secondaryIpAddress": dns_details.get("value")[0].get("secondaryIpAddress") - # }, "snmpServer": { "configureDnacIP": snmp_details.get("value")[0].get("configureDnacIP"), @@ -497,29 +476,8 @@ def get_current_net(self, site_id): "configureDnacIP": netflow_details.get("value")[0].get("configureDnacIP"), }, - # "ntpServer": ntpserver_details.get("value"), - "timezone": timezone_details.get("value")[0], - # "messageOfTheday": { - # "bannerMessage": messageoftheday_details.get("value")[0].get("bannerMessage"), - # "retainExistingBanner": messageoftheday_details \ - # .get("value")[0].get("retainExistingBanner"), - # }, - - # "network_aaa": { - # "network": network_aaa.get("value")[0].get("ipAddress"), - # "protocol": network_aaa.get("value")[0].get("protocol"), - # "ipAddress": network_aaa_pan.get("value")[0] - # }, - - # "clientAndEndpoint_aaa": { - # "network": clientAndEndpoint_aaa.get("value")[0].get("ipAddress"), - # "protocol": clientAndEndpoint_aaa.get("value")[0].get("protocol"), - # "ipAddress": clientAndEndpoint_aaa_pan.get("value")[0], - # } - - } } if dhcp_details != None: @@ -534,36 +492,9 @@ def get_current_net(self, site_id): } }) - # if snmp_details != None: - # net_values.get("settings").update({"snmpServer": { - # "configureDnacIP": snmp_details \ - # .get("value")[0].get("configureDnacIP"), - # "ipAddresses": snmp_details.get("value")[0].get("ipAddresses"), - # } - # }) - - # if syslog_details != None: - # net_values.get("settings").update({"syslogServer": { - # "configureDnacIP": syslog_details \ - # .get("value")[0].get("configureDnacIP"), - # "ipAddresses": syslog_details.get("value")[0].get("ipAddresses"), - # } - # }) - - # if netflow_details != None: - # net_values.get("settings").update({"netflowcollector": { - # "ipAddress": netflow_details.get("value")[0].get("ipAddress"), - # "port": netflow_details.get("value")[0].get("port"), - # "configureDnacIP": netflow_details.get("value")[0].get("configureDnacIP"), - # } - # }) - if ntpserver_details != None: net_values.get("settings").update({"ntpServer": ntpserver_details.get("value")}) - # if timezone_details != None: - # net_values.get("settings").update({"timezone": timezone_details.get("value")[0]}) - if messageoftheday_details != None: net_values.get("settings").update({"messageOfTheday": { "bannerMessage": messageoftheday_details \ @@ -588,7 +519,7 @@ def get_current_net(self, site_id): "ipAddress": clientAndEndpoint_aaa_pan.get("value")[0], } }) - log(str(net_values)) + self.log(str(net_values)) return net_values def get_site_id(self, site_name): @@ -604,15 +535,15 @@ def get_site_id(self, site_name): except: result = None - log(str(response)) + self.log(str(response)) if not response: - log("Invalid site name or site not present") - self.result["response"] = [] - self.result["msg"] = "Invalid site name or site not present" - self.module.fail_json(**self.result) + self.log("Invalid site name or site not present") + self.msg = "Invalid site name or site not present" + self.status = "failed" + return self else: _id = response.get("response")[0].get("id") - log(str(_id)) + self.log(str(_id)) return _id @@ -634,14 +565,14 @@ def pool_exists(self): family = "network_settings", function = "get_global_pool", ) - log(str(response)) + self.log(str(response)) if isinstance(response, dict): if "response" in response: response = response.get("response") current_details = get_dict_result(response, "ipPoolName", name) - log(str(name)) - log(str(current_details)) + self.log(str(name)) + self.log(str(current_details)) if current_details: pool_exists = True pool_id = current_details.get("id") @@ -653,17 +584,18 @@ def pool_exists(self): get("GlobalPoolDetails").get("settings").get("ippool")[0].get("prev_name")) if pool_id is None: - msg = "Prev name doesn't exist\n" - self.module.fail_json(msg=msg) + self.msg = "Prev name doesn't exist\n" + self.status = "failed" + return self pool_exists = True current_details = get_dict_result(response, "id", pool_id) - log(str(current_details)) + self.log(str(current_details)) pool_details = self.get_current_pool(current_details) except Exception: result = None - log(str(pool_details)) - log(str(pool_id)) + self.log(str(pool_details)) + self.log(str(pool_id)) return (pool_exists, pool_details, pool_id) @@ -676,7 +608,7 @@ def res_exists(self): site_name = None _id = "" site_name = self.params.get("config")[0].get("ReservePoolDetails").get("siteName") - log(str(site_name)) + self.log(str(site_name)) if site_name is not None: site_id = self.get_site_id(site_name) @@ -687,11 +619,12 @@ def res_exists(self): if prev_name: if not self.params.get("config")[0].get("ReservePoolDetails").get("siteName"): - msg = "Mandatory Parameter siteName required\n" - self.module.fail_json(msg=msg) + self.msg = "Mandatory Parameter siteName required\n" + self.status = "failed" + return self _id = self.get_res_id_from_name(prev_name) - log(str(_id)) + self.log(str(_id)) try: response = self.dnac._exec( family="network_settings", @@ -701,14 +634,14 @@ def res_exists(self): if isinstance(response, dict): if "response" in response: response = response.get("response") - log(str(response)) + self.log(str(response)) if _id: current_details = get_dict_result(response, "id", _id) elif name: current_details = get_dict_result(response, "groupName", name) - log(str(current_details)) + self.log(str(current_details)) if current_details: res_exists = True @@ -717,8 +650,8 @@ def res_exists(self): except Exception: result = None - log(str(res_details)) - log(str(res_id)) + self.log(str(res_details)) + self.log(str(res_id)) return (res_exists, res_details, res_id) @@ -737,24 +670,22 @@ def get_have(self): have = {} (pool_exists, pool_details, pool_id) = self.pool_exists() - if self.log: - log("pool Exists: " + str(pool_exists) + "\n Current Site: " + str(pool_details)) + self.log("pool Exists: " + str(pool_exists) + "\n Current Site: " + str(pool_details)) if pool_exists: have["pool_id"] = pool_id have["pool_exists"] = pool_exists have["pool_details"] = pool_details - log(str(pool_details)) + self.log(str(pool_details)) self.have = have if self.params.get("config")[0].get("ReservePoolDetails") is not None: have_reserve = {} (res_exists, res_details, res_id) = self.res_exists() - log(str(res_exists)) - if self.log: - log("Reservation Exists: " + str(res_exists) \ - + "\n Reserved Pool: " + str(res_details)) + self.log(str(res_exists)) + self.log("Reservation Exists: " + str(res_exists) \ + + "\n Reserved Pool: " + str(res_details)) if res_exists: have_reserve["res_exists"] = res_exists @@ -774,17 +705,23 @@ def get_have(self): site_name = self.params.get("config")[0].get("NetworkManagementDetails").get("siteName") if site_name is None: - self.module.fail_json(msg="Mandatory Parameter siteName missing", response=[]) + self.msg = "Mandatory Parameter siteName missing" + self.status = "failed" + return self site_id = self.get_site_id(site_name) if site_id is None: - self.module.fail_json(msg="Invalid siteName", response=[]) + self.msg = "Invalid siteName" + self.status = "failed" + return self have_net["site_id"] = site_id have_net["net_details"] = self.get_current_net(site_id) - log(str(have_net)) + self.log(str(have_net)) self.have_net = have_net + self.status = "success" + return self def get_want(self): @@ -818,7 +755,7 @@ def get_want(self): }] } } - log(str(self.have)) + self.log(str(self.have)) if not self.have.get("pool_exists"): if want.get("settings").get("ippool")[0].get("dhcpServerIps") is None: want.get("settings").get("ippool")[0].update({"dhcpServerIps": []}) @@ -836,13 +773,13 @@ def get_want(self): .get("ippool")[0].get("IpAddressSpace") == "IPv4": want.get("settings").get("ippool")[0].update({"IpAddressSpace": "IPv4"}) - log("true") + self.log("true") elif self.have.get("pool_details").get("settings") \ .get("ippool")[0].get("IpAddressSpace") == "Ipv6": want.get("settings").get("ippool")[0].update({"IpAddressSpace": "IPv6"}) - log("false") + self.log("false") want.get("settings").get("ippool")[0].update({"type": self.have. \ get("pool_details").get("settings").get("ippool")[0].get("ipPoolType")}) @@ -873,7 +810,7 @@ def get_want(self): self.have.get("pool_details").get("settings") \ .get("ippool")[0].get("gateway")}) - log(str(want)) + self.log(str(want)) self.want = want if self.params.get("config")[0].get("ReservePoolDetails") is not None: @@ -917,7 +854,7 @@ def get_want(self): .get("ReservePoolDetails").get("ipv6TotalHost") } - log(str(self.have_reserve)) + self.log(str(self.have_reserve)) if not self.have_reserve: if want_reserve.get("type") is None: want_reserve.update({"type": "Generic"}) @@ -964,7 +901,7 @@ def get_want(self): self.want_reserve = want_reserve if self.params.get("config")[0].get("NetworkManagementDetails") is not None: - log(str(self.params)) + self.log(str(self.params)) want_net = { "settings": { "dhcpServer": { @@ -1145,9 +1082,9 @@ def get_want(self): else: if self.params.get("config")[0].get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("servers") == "ISE": - self.module.fail_json( - msg="missing parameter ipAddress", response=[] - ) + self.msg = "missing parameter ipAddress" + self.status = "failed" + return self if self.params.get("config")[0].get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("network"): want_net.get("settings").get("network_aaa").update({ @@ -1155,9 +1092,10 @@ def get_want(self): .get("settings").get("network_aaa").get("network") }) else: - self.module.fail_json( - msg="missing parameter network", response=[] - ) + self.msg="missing parameter network" + self.status = "failed" + return self + if self.params.get("config")[0].get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("protocol"): want_net.get("settings").get("network_aaa").update({ @@ -1165,9 +1103,10 @@ def get_want(self): .get("settings").get("network_aaa").get("protocol") }) else: - self.module.fail_json( - msg="missing parameter protocol", response=[] - ) + self.msg="missing parameter protocol" + self.status = "failed" + return self + if self.params.get("config")[0].get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("servers"): want_net.get("settings").get("network_aaa").update({ @@ -1175,9 +1114,10 @@ def get_want(self): .get("settings").get("network_aaa").get("servers") }) else: - self.module.fail_json( - msg="missing parameter servers", response=[] - ) + self.msg="missing parameter servers" + self.status = "failed" + return self + if self.params.get("config")[0].get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("sharedSecret"): want_net.get("settings").get("network_aaa").update({ @@ -1199,9 +1139,9 @@ def get_want(self): else: if self.params.get("config")[0].get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("servers") == "ISE": - self.module.fail_json( - msg="missing parameter ipAddress", response=[] - ) + self. msg="missing parameter ipAddress" + self.status = "failed" + return self if self.params.get("config")[0].get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("network"): want_net.get("settings").get("clientAndEndpoint_aaa").update({ @@ -1209,9 +1149,10 @@ def get_want(self): .get("settings").get("clientAndEndpoint_aaa").get("network") }) else: - self.module.fail_json( - msg="missing parameter network", response=[] - ) + self.msg="missing parameter network" + self.status = "failed" + return self + if self.params.get("config")[0].get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("protocol"): want_net.get("settings").get("clientAndEndpoint_aaa").update({ @@ -1219,9 +1160,9 @@ def get_want(self): .get("settings").get("clientAndEndpoint_aaa").get("protocol") }) else: - self.module.fail_json( - msg="missing parameter protocol", response=[] - ) + self.msg="missing parameter protocol" + self.status = "failed" + return self if self.params.get("config")[0].get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("servers"): want_net.get("settings").get("clientAndEndpoint_aaa").update({ @@ -1229,9 +1170,10 @@ def get_want(self): .get("settings").get("clientAndEndpoint_aaa").get("servers") }) else: - self.module.fail_json( - msg="missing parameter servers", response=[] - ) + self.msg="missing parameter servers" + self.status = "failed" + return self + if self.params.get("config")[0].get("NetworkManagementDetails") \ .get("settings").get("clientAndEndpoint_aaa").get("sharedSecret"): want_net.get("settings").get("clientAndEndpoint_aaa").update({ @@ -1241,50 +1183,51 @@ def get_want(self): }) else: del want_net.get("settings")["clientAndEndpoint_aaa"] - log(str(want_net)) + self.log(str(want_net)) self.want_net = want_net - + self.status = "success" + return self def get_execution_details(self, execid): response = None - log(str(execid)) + self.log(str(execid)) response = self.dnac._exec( family="task", function='get_business_api_execution_details', params={"execution_id": execid} ) - if self.log: - log(str(response)) + self.log(str(response)) return response - def get_diff_merge(self): + def get_diff_merged(self): if self.params.get("config")[0].get("GlobalPoolDetails") is not None: if not self.params.get("config")[0].get("GlobalPoolDetails") \ .get("settings").get("ippool")[0].get("ipPoolName"): - msg = "Mandatory Parameter ipPoolName required\n" - self.module.fail_json(msg=msg) + self.msg = "Mandatory Parameter ipPoolName required\n" + self.status = "failed" + return self pool_updated = False pool_created = False if self.have.get("pool_exists"): - log("entered") + self.log("entered") obj_params = [ ("settings", "settings"), ] if self.requires_update(self.have.get("pool_details"), self.want, obj_params): - log("Pool requires update") + self.log("Pool requires update") #Pool Exists pool_params = copy.deepcopy(self.want) pool_params.get("settings").get("ippool")[0] \ .update({"id": self.have.get("pool_id")}) - log(str(self.want)) - log(str(pool_params)) + self.log(str(self.want)) + self.log(str(pool_params)) del pool_params["settings"]["ippool"][0]["IpAddressSpace"] del pool_params["settings"]["ippool"][0]["ipPoolCidr"] del pool_params["settings"]["ippool"][0]["type"] @@ -1302,8 +1245,8 @@ def get_diff_merge(self): self.have.get("pool_details").get("settings") \ .get("ippool")[0].get("gateway")}) - log(str(pool_params)) - log(str(self.have)) + self.log(str(pool_params)) + self.log(str(self.have)) response = self.dnac._exec( family = "network_settings", function = "update_global_pool", @@ -1311,33 +1254,32 @@ def get_diff_merge(self): ) pool_updated = True - log(str(pool_updated)) + self.log(str(pool_updated)) else: - log("Pool doesn't requires an update") - log(str(self.have)) - log(str(self.result)) + self.log("Pool doesn't requires an update") + self.log(str(self.have)) + self.log(str(self.result)) self.result['response'] = self.have.get("settings") self.result['msg'] = "Pool doesn't requires an update" - # self.module.exit_json(**self.result) else: #creating New Pool pool_params = self.want - log(str(pool_params)) + self.log(str(pool_params)) response = self.dnac._exec( family="network_settings", function="create_global_pool", params = pool_params, ) - log("PoolCreated") - log(str(response)) + self.log("PoolCreated") + self.log(str(response)) pool_created = True if pool_created or pool_updated: if response and isinstance(response, dict): executionid = response.get("executionId") - log(str(executionid)) + self.log(str(executionid)) while True: execution_details = self.get_execution_details(executionid) if execution_details.get("status") == "SUCCESS": @@ -1346,26 +1288,26 @@ def get_diff_merge(self): break elif execution_details.get("bapiError"): - - self.module.fail_json(msg=execution_details.get("bapiError"), - response=execution_details) - break + + self.msg=execution_details.get("bapiError") + self.status = "failed" + return self if pool_updated: - log("Pool Updated Successfully") + self.log("Pool Updated Successfully") self.result['msg'] = "Pool Updated Successfully" self.result['response'].update({"Id": \ self.have.get("pool_details").get("id")}) elif pool_created: - log("Pool Created Successfully") + self.log("Pool Created Successfully") (pool_exists, pool_details, pool_id) = self.pool_exists() self.result['response'].update({"Id": pool_id}) self.result['response'].update({"Pool Exists": pool_exists}) self.result['response'].update({"Pool Details": pool_details}) self.result['msg'] = "Pool Created Successfully" else: - log("Pool doesn't need a update") + self.log("Pool doesn't need a update") self.result['msg'] = "Pool doesn't requires an update" self.result['response'].update({"Id": \ self.have.get("pool_details").get("id")}) @@ -1374,10 +1316,10 @@ def get_diff_merge(self): res_updated = False res_created = False - log(str(self.have_reserve.get("res_details"))) - log(str(self.want_reserve)) + self.log(str(self.have_reserve.get("res_details"))) + self.log(str(self.want_reserve)) if self.have_reserve: - log("entered") + self.log("entered") obj_params = [ ("name", "name"), ("type", "type"), @@ -1398,10 +1340,10 @@ def get_diff_merge(self): if self.requires_update(self.have_reserve.get("res_details"), \ self.want_reserve, obj_params): - log("Network requires update") + self.log("Network requires update") #Pool Exists - log(str(self.have_reserve)) - log(str(self.want_reserve)) + self.log(str(self.have_reserve)) + self.log(str(self.want_reserve)) res_params = copy.deepcopy(self.want_reserve) res_params.update({"site_id": self.site_id}) @@ -1412,36 +1354,37 @@ def get_diff_merge(self): params=res_params, ) - log("Reservation Updated") - log(str(response)) + self.log("Reservation Updated") + self.log(str(response)) res_updated = True else: - log("Reserved ip subpool doesn't requires an update") + self.log("Reserved ip subpool doesn't requires an update") self.result["response"] = self.have_reserve self.result["msg"] = "Reserved ip subpool doesn't requires an update" - # self.module.exit_json(**self.result) else: #creating New Reservation res_params = self.want_reserve - log(str(res_params)) + self.log(str(res_params)) if not self.want_reserve.get("name") or \ not self.want_reserve.get("ipv4GlobalPool") or \ not self.want_reserve.get("ipv4PrefixLength") or not self.site_id: - self.module.fail_json(msg="missing parameter name or \ - ipv4GlobalPool or ipv4PrefixLength or siteName", response=[]) + self.msg="missing parameter name or \ + ipv4GlobalPool or ipv4PrefixLength or siteName" + self.status = "failed" + return self res_params.update({"site_id": self.site_id}) - log(str(res_params)) + self.log(str(res_params)) response = self.dnac._exec( family="network_settings", function="reserve_ip_subpool", params=res_params, ) - log("Reservation Created") - log(str(response)) + self.log("Reservation Created") + self.log(str(response)) res_created = True if res_created or res_updated: @@ -1457,25 +1400,25 @@ def get_diff_merge(self): elif execution_details.get("bapiError"): - self.module.fail_json(msg=execution_details.get("bapiError"), - response=execution_details) - break + self.msg = execution_details.get("bapiError") + self.status = "failed" + return self if res_updated: - log("Reserved Ip Subpool Updated Successfully") + self.log("Reserved Ip Subpool Updated Successfully") self.result['msg'] = "Reserved Ip Subpool Updated Successfully" self.result['response'].update({"Reservation details": \ self.have.get("res_details")}) elif res_created: - log("Ip Subpool Reservation Created Successfully") + self.log("Ip Subpool Reservation Created Successfully") (res_exists, res_details, res_id) = self.res_exists() self.result['response'].update({"Reservation Id": res_id}) self.result['response'].update({"Reservation Exists": res_exists}) self.result['response'].update({"Reservation details": res_details}) self.result['msg'] = "Ip Subpool Reservation Created Successfully" else: - log("Ip Subpool Reservation doesn't need a update") + self.log("Ip Subpool Reservation doesn't need a update") self.result['msg'] = "Ip Subpool Reservation doesn't requires an update" self.result['response'].update({"Reservation details": \ self.have.get("res_details")}) @@ -1485,18 +1428,20 @@ def get_diff_merge(self): net_updated = False net_created = False if self.have_net: - log("entered") + self.log("entered") obj_params = [ ("settings", "settings"), ("siteName", "siteName") ] if self.want_net.get("settings").get("timezone") is None: - self.module.fail_json(msg="Missing required parameter timezone", response=[]) - # if len(self.have_net) == 10: + self.msg = "Missing required parameter timezone" + self.status = "failed" + return self + if self.requires_update(self.have_net.get("net_details"), self.want_net, obj_params): - log("Network update requires") - log(str(self.have_net)) - log(str(self.want_net)) + self.log("Network update requires") + self.log(str(self.have_net)) + self.log(str(self.want_net)) net_params = copy.deepcopy(self.want_net) net_params.update({"site_id": self.have_net.get("site_id")}) @@ -1506,40 +1451,21 @@ def get_diff_merge(self): params=net_params, ) - log("Network Updated") - log(str(response)) + self.log("Network Updated") + self.log(str(response)) net_updated = True else: - log("Network doesn't need an update") + self.log("Network doesn't need an update") self.result["response"] = self.have_net self.result["msg"] = "Network doesn't need an update" self.module.exit_json(**self.result) - # else: - # #creating New Pool - # net_params = self.want_reserve - # log(str(res_params)) - # if self.want_net.get("settings").get("timezone") is None: - # self.module.fail_json(msg="Missing required parameter timezone", response=[]) - - # net_params = copy.deepcopy(self.want_net) - # net_params.update({"site_id": self.have_net.get("site_id")}) - # response = self.dnac._exec( - # family="network_settings", - # function='create_network', - # params=net_params, - # ) - - # log("Network Updated") - # log(str(response)) - # net_updated = True - if net_updated: if response and isinstance(response, dict): executionid = response.get("executionId") - log(str(response)) - log(str(executionid)) + self.log(str(response)) + self.log(str(executionid)) while True: execution_details = self.get_execution_details(executionid) @@ -1550,20 +1476,17 @@ def get_diff_merge(self): elif execution_details.get("bapiError"): - self.module.fail_json(msg=execution_details.get("bapiError"), - response=execution_details) - break + self.msg = execution_details.get("bapiError") + self.status = "failed" + return self if net_updated: - log("Network has been changed Successfully") + self.log("Network has been changed Successfully") self.result['msg'] = "Network Updated successfully" self.result['response'] = self.want_net - # elif net_created: - # log("Network Created Successfully") - # self.result['msg'] = "Network Created successfully" - # self.result['response'] = self.want_net + else: - log("Pool doesn't need a update") + self.log("Pool doesn't need a update") self.result['msg'] = "Network doesn't requires an update" self.result['response'] = self.have_net @@ -1576,12 +1499,12 @@ def get_res_id_by_name(self, name): params={"siteId":self.site_id}, ) - log(str(response)) + self.log(str(response)) if isinstance(response, dict): if "response" in response: response = response.get("response") - log(str(response)) + self.log(str(response)) current_details = get_dict_result(response, "groupName", name) if current_details: @@ -1593,21 +1516,23 @@ def get_res_id_by_name(self, name): return _id - def get_diff_delete(self): + def get_diff_deleted(self): if self.params.get("config")[0].get("ReservePoolDetails") is not None: res_exists = self.have_reserve.get("res_exists") - log(str(res_exists)) + self.log(str(res_exists)) _id = None if self.want_reserve.get("name"): - log(str(self.want_reserve.get("name"))) + self.log(str(self.want_reserve.get("name"))) _id = self.get_res_id_by_name(self.want_reserve.get("name")) - log(str(_id)) + self.log(str(_id)) if res_exists: if not _id: - self.module.fail_json(msg="missing or \ - incorrect parameter reserved pool name", response=[]) - log(str(self.have_reserve.get("res_id"))) + self.msg = "missing or \ + incorrect parameter reserved pool name" + self.status = "failed" + return self + self.log(str(self.have_reserve.get("res_id"))) response = self.dnac._exec( family="network_settings", function="release_reserve_ip_subpool", @@ -1621,15 +1546,18 @@ def get_diff_delete(self): if task_details.get("status") == "SUCCESS": self.result['changed'] = True self.result['response'] = task_details - log(str(response)) + self.log(str(response)) self.result['msg'] = "Ip subpool reservation released successfully" elif task_details.get("bapiError"): - self.module.fail_json(msg=task_details.get("bapiError"), - response=task_details) + self.msg = task_details.get("bapiError") + self.status = "failed" + return self else: - self.module.fail_json(msg="Reserved Ip Subpool Not Found", response=[]) + self.msg = "Reserved Ip Subpool Not Found" + self.status = "failed" + return self if self.params.get("config")[0].get("GlobalPoolDetails") is not None: pool_exists = self.have.get("pool_exists") @@ -1648,19 +1576,23 @@ def get_diff_delete(self): if task_details.get("status") == "SUCCESS": self.result['changed'] = True self.result['response'] = task_details - log(str(response)) + self.log(str(response)) self.result['msg'] = "Pool deleted successfully" elif task_details.get("bapiError"): - self.module.fail_json(msg=task_details.get("bapiError"), - response=task_details) + self.msg = task_details.get("bapiError") + self.status = "failed" + return self else: - self.module.fail_json(msg="Pool Not Found", response=[]) + self.msg = "Pool Not Found" + self.status = "failed" + return self if self.params.get("config")[0].get("NetworkManagementDetails") is not None: - - self.module.fail_json(msg="No operation available for Delete Network", response=[]) + self.msg = "No operation available for Delete Network" + self.status = "failed" + return self def main(): @@ -1684,18 +1616,17 @@ def main(): dnac_network = DnacNetwork(module) - state = dnac_network.get_state() - dnac_network.validate_input() - - dnac_network.get_have() - dnac_network.get_want() + dnac_network.validate_input().check_return_status() + state = dnac_network.params.get("state") + if state not in dnac_network.supported_states: + dnac_network.status = "invalid" + dnac_network.msg = "State {0} is invalid".format(state) + dnac_network.check_return_status() - if state == "merged": - dnac_network.get_diff_merge() + dnac_network.get_have().check_return_status() + dnac_network.get_want().check_return_status() - elif state == "deleted": - dnac_network.get_diff_delete() - log(str(dnac_network.result)) + dnac_network.get_diff_state_apply[state]().check_return_status() module.exit_json(**dnac_network.result) From 4f4e063cf7c3810a25b1af1074ed31ff352a162a Mon Sep 17 00:00:00 2001 From: Muthu Rakesh <114211490+MUTHU-RAKESH-27@users.noreply.github.com> Date: Mon, 11 Sep 2023 11:08:25 +0530 Subject: [PATCH 5/9] Update network_intent.py --- plugins/modules/network_intent.py | 901 +++++++++++++----------------- 1 file changed, 384 insertions(+), 517 deletions(-) diff --git a/plugins/modules/network_intent.py b/plugins/modules/network_intent.py index 7db40ed6ea..bf97b07770 100644 --- a/plugins/modules/network_intent.py +++ b/plugins/modules/network_intent.py @@ -16,14 +16,12 @@ class DnacNetwork(DnacBase): """Class containing member attributes for network intent module""" def __init__(self, module): super().__init__(module) - self.have = {} - self.want = {} + self.have_global = {} + self.want_global = {} self.have_reserve = {} self.want_reserve = {} - self.have_net = {} - self.want_net = {} - self.have_dev = {} - self.want_dev = {} + self.have_network = {} + self.want_network = {} self.site_id = None @@ -87,163 +85,99 @@ def validate_input(self): ReservePool_details = [self.config[0].get("ReservePoolDetails")] if self.config[0].get("NetworkManagementDetails") is not None: - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dhcpServer"): + network_management_details = self.config[0] \ + .get("NetworkManagementDetails").get("settings") + if network_management_details.get("dhcpServer"): network_details.append({"dhcpServer": self.config[0].get("NetworkManagementDetails") \ .get("settings").get("dhcpServer")}) - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer"): - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("domainName"): - network_details.append({"domainName": self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("domainName")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("primaryIpAddress"): - network_details.append({"primaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("primaryIpAddress")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("secondaryIpAddress"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("secondaryIpAddress")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa"): - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("ipAddress"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("ipAddress")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("network"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("network")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("protocol"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("protocol")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("servers"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("servers")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("sharedSecret"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("sharedSecret")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday"): - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday").get("bannerMessage"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday").get("bannerMessage")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday").get("retainExistingBanner"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday").get("retainExistingBanner")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector"): - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("ipAddress"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("ipAddress")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("port"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("port")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa"): - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("ipAddress"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("ipAddress")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("network"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("network")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("protocol"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("protocol")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("servers"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("servers")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("sharedSecret"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("sharedSecret")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("ntpServer"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("ntpServer")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("snmpServer"): - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("snmpServer").get("configureDnacIP"): - network_details.append({"secondaryIpAddress": self.config[0] - .get("NetworkManagementDetails") \ - .get("settings").get("snmpServer").get("configureDnacIP")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("snmpServer").get("ipAddresses"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("snmpServer").get("ipAddresses")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("syslogServer"): - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("syslogServer").get("configureDnacIP"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("syslogServer").get("configureDnacIP")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("syslogServer").get("ipAddresses"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("syslogServer").get("ipAddresses")}) - - if self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("timezone"): - network_details.append({"secondaryIpAddress": self.config[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("timezone")}) - - self.log(str(network_details)) + if network_management_details.get("dnsServer"): + if network_management_details.get("dnsServer").get("domainName"): + network_details.append({"domainName": network_management_details \ + .get("dnsServer").get("domainName")}) + if network_management_details.get("dnsServer").get("primaryIpAddress"): + network_details.append({"primaryIpAddress": network_management_details \ + .get("dnsServer").get("primaryIpAddress")}) + if network_management_details.get("dnsServer").get("secondaryIpAddress"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("dnsServer").get("secondaryIpAddress")}) + + if network_management_details.get("clientAndEndpoint_aaa"): + if network_management_details.get("clientAndEndpoint_aaa").get("ipAddress"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("clientAndEndpoint_aaa").get("ipAddress")}) + if network_management_details.get("clientAndEndpoint_aaa").get("network"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("clientAndEndpoint_aaa").get("network")}) + if network_management_details.get("clientAndEndpoint_aaa").get("protocol"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("clientAndEndpoint_aaa").get("protocol")}) + if network_management_details.get("clientAndEndpoint_aaa").get("servers"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("clientAndEndpoint_aaa").get("servers")}) + if network_management_details.get("clientAndEndpoint_aaa").get("sharedSecret"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("clientAndEndpoint_aaa").get("sharedSecret")}) + + if network_management_details.get("messageOfTheday"): + if network_management_details.get("messageOfTheday").get("bannerMessage"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("messageOfTheday").get("bannerMessage")}) + + if network_management_details.get("messageOfTheday").get("retainExistingBanner"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("messageOfTheday").get("retainExistingBanner")}) + + if network_management_details.get("netflowcollector"): + if network_management_details.get("netflowcollector").get("ipAddress"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("netflowcollector").get("ipAddress")}) + if network_management_details.get("netflowcollector").get("port"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("netflowcollector").get("port")}) + + if network_management_details.get("network_aaa"): + if network_management_details.get("network_aaa").get("ipAddress"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("network_aaa").get("ipAddress")}) + if network_management_details.get("network_aaa").get("network"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("network_aaa").get("network")}) + if network_management_details.get("network_aaa").get("protocol"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("network_aaa").get("protocol")}) + if network_management_details.get("network_aaa").get("servers"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("network_aaa").get("servers")}) + if network_management_details.get("network_aaa").get("sharedSecret"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("network_aaa").get("sharedSecret")}) + + if network_management_details.get("ntpServer"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("ntpServer")}) + + if network_management_details.get("snmpServer"): + if network_management_details.get("snmpServer").get("configureDnacIP"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("snmpServer").get("configureDnacIP")}) + if network_management_details.get("snmpServer").get("ipAddresses"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("snmpServer").get("ipAddresses")}) + + if network_management_details.get("syslogServer"): + if network_management_details.get("syslogServer").get("configureDnacIP"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("syslogServer").get("configureDnacIP")}) + if network_management_details.get("syslogServer").get("ipAddresses"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("syslogServer").get("ipAddresses")}) + + if network_management_details.get("timezone"): + network_details.append({"secondaryIpAddress": network_management_details \ + .get("timezone")}) + + self.log(str(globalPool_details)) self.log(str(ReservePool_details)) self.log(str(network_details)) playbook_details = globalPool_details + ReservePool_details + network_details @@ -257,13 +191,13 @@ def validate_input(self): self.status = "failed" return self self.log(str(invalid_params)) - self.validated = valid_temp + self.validated_config = valid_temp - self.log(str(valid_temp)) + self.log(str(self.validated_config)) - self.log(str(self.validated)) + self.log(str(self.validated_config)) if self.params.get("config")[0].get("GlobalPoolDetails") is not None: - for temp in self.validated: + for temp in self.validated_config: self.log(str(temp)) if self.params.get("config")[0].get("GlobalPoolDetails") \ .get("ipPoolName") is not None: @@ -288,9 +222,9 @@ def get_dnac_params(self, params): return dnac_params - def requires_update(self, have, want, obj_params): - current_obj = have - requested_obj = want + def requires_update(self, have_global, want_global, obj_params): + current_obj = have_global + requested_obj = want_global self.log(str(current_obj)) self.log(str(requested_obj)) @@ -362,17 +296,17 @@ def get_current_pool(self, pool): } } self.log(str(pool_values)) + pool_ippool = pool_values.get("settings").get("ippool")[0] if pool.get("ipv6") is False: - pool_values.get("settings").get("ippool")[0].update({"IpAddressSpace": "IPv4"}) + pool_ippool.update({"IpAddressSpace": "IPv4"}) self.log("ipv6 - false") else: - pool_values.get("settings").get("ippool")[0].update({"IpAddressSpace": "IPv6"}) + pool_ippool.update({"IpAddressSpace": "IPv6"}) self.log("ipv6 - true") if not pool["gateways"]: - pool_values.get("settings").get("ippool")[0].update({"gateway": ""}) + pool_ippool.update({"gateway": ""}) else: - pool_values.get("settings").get("ippool")[0] \ - .update({"gateway": pool.get("gateways")[0]}) + pool_ippool.update({"gateway": pool.get("gateways")[0]}) return pool_values @@ -548,7 +482,7 @@ def get_site_id(self, site_name): return _id - def pool_exists(self): + def pool_exists(self, config): pool_exists = False pool_details = {} pool_id = None @@ -557,7 +491,7 @@ def pool_exists(self): #get it from validated - name = self.params.get("config")[0].get("GlobalPoolDetails") \ + name = config.get("GlobalPoolDetails") \ .get("settings").get("ippool")[0].get("ipPoolName") try: @@ -576,11 +510,11 @@ def pool_exists(self): if current_details: pool_exists = True pool_id = current_details.get("id") - elif self.config[0].get("GlobalPoolDetails").get("settings") \ + elif config.get("GlobalPoolDetails").get("settings") \ .get("ippool")[0].get("prev_name") is not None: pool_id = None - (pool_id, current_details) = self.get_pool_id_from_name(self.config[0]. \ + (pool_id, current_details) = self.get_pool_id_from_name(config. \ get("GlobalPoolDetails").get("settings").get("ippool")[0].get("prev_name")) if pool_id is None: @@ -599,7 +533,7 @@ def pool_exists(self): return (pool_exists, pool_details, pool_id) - def res_exists(self): + def res_exists(self, config): current_details = None res_exists = False res_details = None @@ -607,18 +541,18 @@ def res_exists(self): response = None site_name = None _id = "" - site_name = self.params.get("config")[0].get("ReservePoolDetails").get("siteName") + site_name = config.get("ReservePoolDetails").get("siteName") self.log(str(site_name)) if site_name is not None: site_id = self.get_site_id(site_name) self.site_id = site_id - name = self.config[0].get("ReservePoolDetails").get("name") - prev_name = self.config[0].get("ReservePoolDetails").get("prev_name") + name = config.get("ReservePoolDetails").get("name") + prev_name = config.get("ReservePoolDetails").get("prev_name") if prev_name: - if not self.params.get("config")[0].get("ReservePoolDetails").get("siteName"): + if not config.get("ReservePoolDetails").get("siteName"): self.msg = "Mandatory Parameter siteName required\n" self.status = "failed" return self @@ -655,7 +589,7 @@ def res_exists(self): return (res_exists, res_details, res_id) - def get_have(self): + def get_have(self, config): pool_exists = False pool_details = None pool_id = None @@ -666,23 +600,23 @@ def get_have(self): #checking if the pool is already exists or not - if self.params.get("config")[0].get("GlobalPoolDetails") is not None: - have = {} - (pool_exists, pool_details, pool_id) = self.pool_exists() + if config.get("GlobalPoolDetails") is not None: + have_global = {} + (pool_exists, pool_details, pool_id) = self.pool_exists(config) self.log("pool Exists: " + str(pool_exists) + "\n Current Site: " + str(pool_details)) if pool_exists: - have["pool_id"] = pool_id - have["pool_exists"] = pool_exists - have["pool_details"] = pool_details + have_global["pool_id"] = pool_id + have_global["pool_exists"] = pool_exists + have_global["pool_details"] = pool_details self.log(str(pool_details)) - self.have = have + self.have_global = have_global - if self.params.get("config")[0].get("ReservePoolDetails") is not None: + if config.get("ReservePoolDetails") is not None: have_reserve = {} - (res_exists, res_details, res_id) = self.res_exists() + (res_exists, res_details, res_id) = self.res_exists(config) self.log(str(res_exists)) self.log("Reservation Exists: " + str(res_exists) \ + "\n Reserved Pool: " + str(res_details)) @@ -699,10 +633,10 @@ def get_have(self): self.have_reserve = have_reserve - if self.params.get("config")[0].get("NetworkManagementDetails") is not None: + if config.get("NetworkManagementDetails") is not None: - have_net = {} - site_name = self.params.get("config")[0].get("NetworkManagementDetails").get("siteName") + have_network = {} + site_name = config.get("NetworkManagementDetails").get("siteName") if site_name is None: self.msg = "Mandatory Parameter siteName missing" @@ -716,142 +650,107 @@ def get_have(self): self.status = "failed" return self - have_net["site_id"] = site_id - have_net["net_details"] = self.get_current_net(site_id) - self.log(str(have_net)) - self.have_net = have_net + have_network["site_id"] = site_id + have_network["net_details"] = self.get_current_net(site_id) + self.log(str(have_network)) + self.have_network = have_network self.status = "success" return self - def get_want(self): - if self.params.get("config")[0].get("GlobalPoolDetails") is not None: - want = {} - IpAddressSpace = self.params.get("config")[0] \ - .get("GlobalPoolDetails").get("settings").get("ippool")[0] - dhcpServerIps = self.params.get("config")[0] \ - .get("GlobalPoolDetails").get("settings").get("ippool")[0] - dnsServerIps = self.params.get("config")[0] \ - .get("GlobalPoolDetails").get("settings").get("ippool")[0] - gateway = self.params.get("config")[0] \ - .get("GlobalPoolDetails").get("settings").get("ippool")[0] - ipPoolCidr = self.params.get("config")[0] \ - .get("GlobalPoolDetails").get("settings").get("ippool")[0] - ipPoolName = self.params.get("config")[0] \ - .get("GlobalPoolDetails").get("settings").get("ippool")[0] - _type = self.params.get("config")[0] \ + def get_want(self, config): + if config.get("GlobalPoolDetails") is not None: + want_global = {} + global_ippool = config \ .get("GlobalPoolDetails").get("settings").get("ippool")[0] - want = { + want_global = { "settings": { "ippool": [{ - "IpAddressSpace": IpAddressSpace.get("IpAddressSpace"), - "dhcpServerIps": dhcpServerIps.get("dhcpServerIps"), - "dnsServerIps": dnsServerIps.get("dnsServerIps"), - "gateway": gateway.get("gateway"), - "ipPoolCidr": ipPoolCidr.get("ipPoolCidr"), - "ipPoolName": ipPoolName.get("ipPoolName"), - "type": _type.get("type"), + "IpAddressSpace": global_ippool.get("IpAddressSpace"), + "dhcpServerIps": global_ippool.get("dhcpServerIps"), + "dnsServerIps": global_ippool.get("dnsServerIps"), + "gateway": global_ippool.get("gateway"), + "ipPoolCidr": global_ippool.get("ipPoolCidr"), + "ipPoolName": global_ippool.get("ipPoolName"), + "type": global_ippool.get("type"), }] } } - self.log(str(self.have)) - if not self.have.get("pool_exists"): - if want.get("settings").get("ippool")[0].get("dhcpServerIps") is None: - want.get("settings").get("ippool")[0].update({"dhcpServerIps": []}) - if want.get("settings").get("ippool")[0].get("dnsServerIps") is None: - want.get("settings").get("ippool")[0].update({"dnsServerIps": []}) - if want.get("settings").get("ippool")[0].get("IpAddressSpace") is None: - want.get("settings").get("ippool")[0].update({"IpAddressSpace": ""}) - if want.get("settings").get("ippool")[0].get("gateway") is None: - want.get("settings").get("ippool")[0].update({"gateway": ""}) - if want.get("settings").get("ippool")[0].get("type") is None: - want.get("settings").get("ippool")[0].update({"type": "Generic"}) + self.log(str(self.have_global)) + if not self.have_global.get("pool_exists"): + want_ippool = want_global.get("settings").get("ippool")[0] + + if want_ippool.get("dhcpServerIps") is None: + want_ippool.update({"dhcpServerIps": []}) + if want_ippool.get("dnsServerIps") is None: + want_ippool.update({"dnsServerIps": []}) + if want_ippool.get("IpAddressSpace") is None: + want_ippool.update({"IpAddressSpace": ""}) + if want_ippool.get("gateway") is None: + want_ippool.update({"gateway": ""}) + if want_ippool.get("type") is None: + want_ippool.update({"type": "Generic"}) else: - if self.have.get("pool_details").get("settings") \ - .get("ippool")[0].get("IpAddressSpace") == "IPv4": + have_ippool = self.have_global.get("pool_details") \ + .get("settings").get("ippool")[0] + want_ippool = want_global.get("settings").get("ippool")[0] - want.get("settings").get("ippool")[0].update({"IpAddressSpace": "IPv4"}) + if have_ippool.get("IpAddressSpace") == "IPv4": + + want_ippool.update({"IpAddressSpace": "IPv4"}) self.log("true") - elif self.have.get("pool_details").get("settings") \ - .get("ippool")[0].get("IpAddressSpace") == "Ipv6": + elif have_ippool.get("IpAddressSpace") == "Ipv6": - want.get("settings").get("ippool")[0].update({"IpAddressSpace": "IPv6"}) + want_ippool.update({"IpAddressSpace": "IPv6"}) self.log("false") - want.get("settings").get("ippool")[0].update({"type": self.have. \ - get("pool_details").get("settings").get("ippool")[0].get("ipPoolType")}) - want.get("settings").get("ippool")[0].update({"ipPoolCidr": \ - self.have.get("pool_details").get("settings") \ - .get("ippool")[0].get("ipPoolCidr")}) + want_ippool.update({"type": have_ippool.get("ipPoolType")}) + want_ippool.update({"ipPoolCidr": have_ippool.get("ipPoolCidr")}) + + if want_ippool.get("dhcpServerIps") is None and \ + have_ippool.get("dhcpServerIps") is not None: - if want.get("settings").get("ippool")[0].get("dhcpServerIps") is None and \ - self.have.get("pool_details").get("settings") \ - .get("ippool")[0].get("dhcpServerIps") is not None: - want.get("settings").get("ippool")[0].update({"dhcpServerIps": \ - self.have.get("pool_details").get("settings") \ - .get("ippool")[0].get("dhcpServerIps")}) + want_ippool.update({"dhcpServerIps": have_ippool.get("dhcpServerIps")}) - if want.get("settings").get("ippool")[0].get("dnsServerIps") is None and \ - self.have.get("pool_details").get("settings") \ - .get("ippool")[0].get("dnsServerIps") is not None: + if want_ippool.get("dnsServerIps") is None and \ + have_ippool.get("dnsServerIps") is not None: - want.get("settings").get("ippool")[0].update({"dnsServerIps": \ - self.have.get("pool_details").get("settings") \ - .get("ippool")[0].get("dnsServerIps")}) + want_ippool.update({"dnsServerIps": have_ippool.get("dnsServerIps")}) - if want.get("settings").get("ippool")[0].get("gateway") is None and \ - self.have.get("pool_details").get("settings") \ - .get("ippool")[0].get("gateway") is not None: + if want_ippool.get("gateway") is None and \ + have_ippool.get("gateway") is not None: - want.get("settings").get("ippool")[0].update({"gateway": \ - self.have.get("pool_details").get("settings") \ - .get("ippool")[0].get("gateway")}) + want_ippool.update({"gateway": have_ippool.get("gateway")}) - self.log(str(want)) - self.want = want + self.log(str(want_global)) + self.want_global = want_global - if self.params.get("config")[0].get("ReservePoolDetails") is not None: + if config.get("ReservePoolDetails") is not None: + res_pool = config.get("ReservePoolDetails") want_reserve = { - "name": self.params.get("config")[0].get("ReservePoolDetails").get("name"), - "type": self.params.get("config")[0].get("ReservePoolDetails").get("type"), - "ipv6AddressSpace": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv6AddressSpace"), - "ipv4GlobalPool": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv4GlobalPool"), - "ipv4Prefix": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv4Prefix"), - "ipv4PrefixLength": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv4PrefixLength"), - "ipv4GateWay": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv4GateWay"), - "ipv4DhcpServers": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv4DhcpServers"), - "ipv4DnsServers": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv4DnsServers"), - "ipv4Subnet": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv4Subnet"), - "ipv6GlobalPool": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv6GlobalPool"), - "ipv6Prefix": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv6Prefix"), - "ipv6PrefixLength": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv6PrefixLength"), - "ipv6GateWay": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv6GateWay"), - "ipv6DhcpServers": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv6DhcpServers"), - "ipv6Subnet": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv6Subnet"), - "ipv6DnsServers": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv6DnsServers"), - "ipv4TotalHost": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv4TotalHost"), - "ipv6TotalHost": self.params.get("config")[0] \ - .get("ReservePoolDetails").get("ipv6TotalHost") + "name": res_pool.get("name"), + "type": res_pool.get("type"), + "ipv6AddressSpace": res_pool.get("ipv6AddressSpace"), + "ipv4GlobalPool": res_pool.get("ipv4GlobalPool"), + "ipv4Prefix": res_pool.get("ipv4Prefix"), + "ipv4PrefixLength": res_pool.get("ipv4PrefixLength"), + "ipv4GateWay": res_pool.get("ipv4GateWay"), + "ipv4DhcpServers": res_pool.get("ipv4DhcpServers"), + "ipv4DnsServers": res_pool.get("ipv4DnsServers"), + "ipv4Subnet": res_pool.get("ipv4Subnet"), + "ipv6GlobalPool": res_pool.get("ipv6GlobalPool"), + "ipv6Prefix": res_pool.get("ipv6Prefix"), + "ipv6PrefixLength": res_pool.get("ipv6PrefixLength"), + "ipv6GateWay": res_pool.get("ipv6GateWay"), + "ipv6DhcpServers": res_pool.get("ipv6DhcpServers"), + "ipv6Subnet": res_pool.get("ipv6Subnet"), + "ipv6DnsServers": res_pool.get("ipv6DnsServers"), + "ipv4TotalHost": res_pool.get("ipv4TotalHost"), + "ipv6TotalHost": res_pool.get("ipv6TotalHost") } self.log(str(self.have_reserve)) @@ -900,9 +799,9 @@ def get_want(self): self.want_reserve = want_reserve - if self.params.get("config")[0].get("NetworkManagementDetails") is not None: + if config.get("NetworkManagementDetails") is not None: self.log(str(self.params)) - want_net = { + want_network = { "settings": { "dhcpServer": { @@ -935,256 +834,207 @@ def get_want(self): } } - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("dhcpServer"): - want_net.get("settings").update({"dhcpServer": - self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("dhcpServer") + network_management_details = config.get("NetworkManagementDetails") \ + .get("settings") + if network_management_details.get("dhcpServer"): + want_network.get("settings").update({"dhcpServer": + network_management_details.get("dhcpServer") }) else: - del want_net.get("settings")["dhcpServer"] + del want_network.get("settings")["dhcpServer"] - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("ntpServer"): - want_net.get("settings").update({"ntpServer": - self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("ntpServer") + if network_management_details.get("ntpServer"): + want_network.get("settings").update({"ntpServer": + network_management_details.get("ntpServer") }) else: - del want_net.get("settings")["ntpServer"] + del want_network.get("settings")["ntpServer"] - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("timezone"): - want_net.get("settings")["timezone"] = \ - self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("timezone") + if network_management_details.get("timezone"): + want_network.get("settings")["timezone"] = \ + network_management_details.get("timezone") else: - del want_net.get("settings")["timezone"] - - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer"): - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("domainName"): - want_net.get("settings").get("dnsServer").update({ - "domainName": self.params.get("config")[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("dnsServer").get("domainName") + del want_network.get("settings")["timezone"] + + if network_management_details.get("dnsServer"): + if network_management_details.get("dnsServer").get("domainName"): + want_network.get("settings").get("dnsServer").update({ + "domainName": network_management_details \ + .get("dnsServer").get("domainName") }) - if self.params.get("config")[0] \ - .get("NetworkManagementDetails").get("settings"): - want_net.get("settings").get("dnsServer").update({ - "primaryIpAddress": self.params.get("config")[0] \ - .get("NetworkManagementDetails").get("settings") \ + if network_management_details.get("dnsServer").get("primaryIpAddress"): + want_network.get("settings").get("dnsServer").update({ + "primaryIpAddress": network_management_details \ .get("dnsServer").get("primaryIpAddress") }) - if self.params.get("config")[0] \ - .get("NetworkManagementDetails").get("settings") \ - .get("dnsServer").get("secondaryIpAddress"): - want_net.get("settings").get("dnsServer").update({ - "secondaryIpAddress": self.params.get("config")[0] \ - .get("NetworkManagementDetails").get("settings") \ + if network_management_details.get("dnsServer").get("secondaryIpAddress"): + want_network.get("settings").get("dnsServer").update({ + "secondaryIpAddress": network_management_details \ .get("dnsServer").get("secondaryIpAddress") }) else: - del want_net.get("settings")["dnsServer"] - - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("snmpServer"): - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("snmpServer").get("configureDnacIP"): - want_net.get("settings").get("snmpServer").update({ - "configureDnacIP": self.params.get("config")[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("snmpServer").get("configureDnacIP") + del want_network.get("settings")["dnsServer"] + + if network_management_details.get("snmpServer"): + if network_management_details.get("snmpServer").get("configureDnacIP"): + want_network.get("settings").get("snmpServer").update({ + "configureDnacIP": network_management_details \ + .get("snmpServer").get("configureDnacIP") }) - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("snmpServer").get("ipAddresses"): - want_net.get("settings").get("snmpServer").update({ - "ipAddresses": self.params.get("config")[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("snmpServer").get("ipAddresses") + if network_management_details.get("snmpServer").get("ipAddresses"): + want_network.get("settings").get("snmpServer").update({ + "ipAddresses": network_management_details \ + .get("snmpServer").get("ipAddresses") }) else: - del want_net.get("settings")["snmpServer"] - - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("syslogServer"): - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("syslogServer").get("configureDnacIP"): - want_net.get("settings").get("syslogServer").update({ - "configureDnacIP": self.params.get("config")[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("syslogServer").get("configureDnacIP") + del want_network.get("settings")["snmpServer"] + + if network_management_details.get("syslogServer"): + if network_management_details.get("syslogServer").get("configureDnacIP"): + want_network.get("settings").get("syslogServer").update({ + "configureDnacIP": network_management_details \ + .get("syslogServer").get("configureDnacIP") }) - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("syslogServer").get("ipAddresses"): - want_net.get("settings").get("syslogServer").update({ - "ipAddresses": self.params.get("config")[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("syslogServer").get("ipAddresses") + if network_management_details.get("syslogServer").get("ipAddresses"): + want_network.get("settings").get("syslogServer").update({ + "ipAddresses": network_management_details \ + .get("syslogServer").get("ipAddresses") }) else: - del want_net.get("settings")["syslogServer"] - - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector"): - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("ipAddress"): - want_net.get("settings").get("netflowcollector").update({ - "ipAddress": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("ipAddress") + del want_network.get("settings")["syslogServer"] + + if network_management_details.get("netflowcollector"): + if network_management_details.get("netflowcollector").get("ipAddress"): + want_network.get("settings").get("netflowcollector").update({ + "ipAddress": network_management_details \ + .get("netflowcollector").get("ipAddress") }) - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("port"): - want_net.get("settings").get("netflowcollector").update({ - "port": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("port") + if network_management_details.get("netflowcollector").get("port"): + want_network.get("settings").get("netflowcollector").update({ + "port": network_management_details \ + .get("netflowcollector").get("port") }) - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("configureDnacIP"): - want_net.get("settings").get("netflowcollector").update({ - "configureDnacIP": self.params.get("config")[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("netflowcollector").get("configureDnacIP") + if network_management_details.get("netflowcollector").get("configureDnacIP"): + want_network.get("settings").get("netflowcollector").update({ + "configureDnacIP": network_management_details \ + .get("netflowcollector").get("configureDnacIP") }) else: - del want_net.get("settings")["netflowcollector"] - - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday"): - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday").get("bannerMessage"): - want_net.get("settings").get("messageOfTheday").update({ - "bannerMessage": self.params.get("config")[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday").get("bannerMessage") + del want_network.get("settings")["netflowcollector"] + + if network_management_details.get("messageOfTheday"): + if network_management_details.get("messageOfTheday").get("bannerMessage"): + want_network.get("settings").get("messageOfTheday").update({ + "bannerMessage": network_management_details.get("messageOfTheday").get("bannerMessage") }) - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday").get("retainExistingBanner"): - want_net.get("settings").get("messageOfTheday").update({ - "retainExistingBanner": self.params.get("config")[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("messageOfTheday").get("retainExistingBanner") + if network_management_details.get("messageOfTheday").get("retainExistingBanner"): + want_network.get("settings").get("messageOfTheday").update({ + "retainExistingBanner": network_management_details \ + .get("messageOfTheday").get("retainExistingBanner") }) else: - del want_net.get("settings")["messageOfTheday"] - - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa"): - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("ipAddress"): - want_net.get("settings").get("network_aaa").update({ - "ipAddress": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("ipAddress") + del want_network.get("settings")["messageOfTheday"] + + if network_management_details.get("network_aaa"): + if network_management_details.get("network_aaa").get("ipAddress"): + want_network.get("settings").get("network_aaa").update({ + "ipAddress": network_management_details \ + .get("network_aaa").get("ipAddress") }) else: - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("servers") == "ISE": + if network_management_details.get("network_aaa").get("servers") == "ISE": self.msg = "missing parameter ipAddress" self.status = "failed" return self - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("network"): - want_net.get("settings").get("network_aaa").update({ - "network": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("network") + if network_management_details.get("network_aaa").get("network"): + want_network.get("settings").get("network_aaa").update({ + "network": network_management_details.get("network_aaa").get("network") }) else: self.msg="missing parameter network" self.status = "failed" return self - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("protocol"): - want_net.get("settings").get("network_aaa").update({ - "protocol": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("protocol") + if network_management_details.get("network_aaa").get("protocol"): + want_network.get("settings").get("network_aaa").update({ + "protocol": network_management_details \ + .get("network_aaa").get("protocol") }) else: self.msg="missing parameter protocol" self.status = "failed" return self - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("servers"): - want_net.get("settings").get("network_aaa").update({ - "servers": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("servers") + if network_management_details.get("network_aaa").get("servers"): + want_network.get("settings").get("network_aaa").update({ + "servers": network_management_details \ + .get("network_aaa").get("servers") }) else: self.msg="missing parameter servers" self.status = "failed" return self - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("network_aaa").get("sharedSecret"): - want_net.get("settings").get("network_aaa").update({ - "sharedSecret": self.params.get("config")[0] \ + if network_management_details.get("network_aaa").get("sharedSecret"): + want_network.get("settings").get("network_aaa").update({ + "sharedSecret": config \ .get("NetworkManagementDetails") \ .get("settings").get("network_aaa").get("sharedSecret") }) else: - del want_net.get("settings")["network_aaa"] - - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa"): - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("ipAddress"): - want_net.get("settings").get("clientAndEndpoint_aaa").update({ - "ipAddress": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("ipAddress") + del want_network.get("settings")["network_aaa"] + + if network_management_details.get("clientAndEndpoint_aaa"): + if network_management_details.get("clientAndEndpoint_aaa").get("ipAddress"): + want_network.get("settings").get("clientAndEndpoint_aaa").update({ + "ipAddress": network_management_details \ + .get("clientAndEndpoint_aaa").get("ipAddress") }) else: - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("servers") == "ISE": + if network_management_details.get("clientAndEndpoint_aaa").get("servers") == "ISE": self. msg="missing parameter ipAddress" self.status = "failed" return self - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("network"): - want_net.get("settings").get("clientAndEndpoint_aaa").update({ - "network": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("network") + if network_management_details.get("clientAndEndpoint_aaa").get("network"): + want_network.get("settings").get("clientAndEndpoint_aaa").update({ + "network": network_management_details \ + .get("clientAndEndpoint_aaa").get("network") }) else: self.msg="missing parameter network" self.status = "failed" return self - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("protocol"): - want_net.get("settings").get("clientAndEndpoint_aaa").update({ - "protocol": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("protocol") + if network_management_details.get("clientAndEndpoint_aaa").get("protocol"): + want_network.get("settings").get("clientAndEndpoint_aaa").update({ + "protocol": network_management_details \ + .get("clientAndEndpoint_aaa").get("protocol") }) else: self.msg="missing parameter protocol" self.status = "failed" return self - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("servers"): - want_net.get("settings").get("clientAndEndpoint_aaa").update({ - "servers": self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("servers") + if network_management_details.get("clientAndEndpoint_aaa").get("servers"): + want_network.get("settings").get("clientAndEndpoint_aaa").update({ + "servers": network_management_details \ + .get("clientAndEndpoint_aaa").get("servers") }) else: self.msg="missing parameter servers" self.status = "failed" return self - if self.params.get("config")[0].get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("sharedSecret"): - want_net.get("settings").get("clientAndEndpoint_aaa").update({ - "sharedSecret": self.params.get("config")[0] \ - .get("NetworkManagementDetails") \ - .get("settings").get("clientAndEndpoint_aaa").get("sharedSecret") + if network_management_details.get("clientAndEndpoint_aaa").get("sharedSecret"): + want_network.get("settings").get("clientAndEndpoint_aaa").update({ + "sharedSecret": network_management_details \ + .get("clientAndEndpoint_aaa").get("sharedSecret") }) else: - del want_net.get("settings")["clientAndEndpoint_aaa"] - self.log(str(want_net)) - self.want_net = want_net + del want_network.get("settings")["clientAndEndpoint_aaa"] + self.log(str(want_network)) + self.want_network = want_network self.status = "success" return self @@ -1202,10 +1052,10 @@ def get_execution_details(self, execid): return response - def get_diff_merged(self): - if self.params.get("config")[0].get("GlobalPoolDetails") is not None: + def get_diff_merged(self, config): + if config.get("GlobalPoolDetails") is not None: - if not self.params.get("config")[0].get("GlobalPoolDetails") \ + if not config.get("GlobalPoolDetails") \ .get("settings").get("ippool")[0].get("ipPoolName"): self.msg = "Mandatory Parameter ipPoolName required\n" @@ -1215,18 +1065,18 @@ def get_diff_merged(self): pool_updated = False pool_created = False - if self.have.get("pool_exists"): + if self.have_global.get("pool_exists"): self.log("entered") obj_params = [ ("settings", "settings"), ] - if self.requires_update(self.have.get("pool_details"), self.want, obj_params): + if self.requires_update(self.have_global.get("pool_details"), self.want_global, obj_params): self.log("Pool requires update") #Pool Exists - pool_params = copy.deepcopy(self.want) + pool_params = copy.deepcopy(self.want_global) pool_params.get("settings").get("ippool")[0] \ - .update({"id": self.have.get("pool_id")}) - self.log(str(self.want)) + .update({"id": self.have_global.get("pool_id")}) + self.log(str(self.want_global)) self.log(str(pool_params)) del pool_params["settings"]["ippool"][0]["IpAddressSpace"] del pool_params["settings"]["ippool"][0]["ipPoolCidr"] @@ -1234,19 +1084,19 @@ def get_diff_merged(self): if pool_params.get("settings").get("ippool")[0].get("dhcpServerIps") is None: pool_params.get("settings").get("ippool")[0].update({"dhcpServerIps" : \ - self.have.get("pool_details").get("settings") \ + self.have_global.get("pool_details").get("settings") \ .get("ippool")[0].get("dhcpServerIps")}) if pool_params.get("settings").get("ippool")[0].get("dnsServerIps") is None: pool_params.get("settings").get("ippool")[0].update({"dnsServerIps" : \ - self.have.get("pool_details").get("settings") \ + self.have_global.get("pool_details").get("settings") \ .get("ippool")[0].get("dnsServerIps")}) if pool_params.get("settings").get("ippool")[0].get("gateway") is None: pool_params.get("settings").get("ippool")[0].update({"gateway" : \ - self.have.get("pool_details").get("settings") \ + self.have_global.get("pool_details").get("settings") \ .get("ippool")[0].get("gateway")}) self.log(str(pool_params)) - self.log(str(self.have)) + self.log(str(self.have_global)) response = self.dnac._exec( family = "network_settings", function = "update_global_pool", @@ -1258,14 +1108,14 @@ def get_diff_merged(self): else: self.log("Pool doesn't requires an update") - self.log(str(self.have)) + self.log(str(self.have_global)) self.log(str(self.result)) - self.result['response'] = self.have.get("settings") + self.result['response'] = self.have_global.get("settings") self.result['msg'] = "Pool doesn't requires an update" else: #creating New Pool - pool_params = self.want + pool_params = self.want_global self.log(str(pool_params)) response = self.dnac._exec( family="network_settings", @@ -1297,11 +1147,11 @@ def get_diff_merged(self): self.log("Pool Updated Successfully") self.result['msg'] = "Pool Updated Successfully" self.result['response'].update({"Id": \ - self.have.get("pool_details").get("id")}) + self.have_global.get("pool_details").get("id")}) elif pool_created: self.log("Pool Created Successfully") - (pool_exists, pool_details, pool_id) = self.pool_exists() + (pool_exists, pool_details, pool_id) = self.pool_exists(config) self.result['response'].update({"Id": pool_id}) self.result['response'].update({"Pool Exists": pool_exists}) self.result['response'].update({"Pool Details": pool_details}) @@ -1310,9 +1160,10 @@ def get_diff_merged(self): self.log("Pool doesn't need a update") self.result['msg'] = "Pool doesn't requires an update" self.result['response'].update({"Id": \ - self.have.get("pool_details").get("id")}) + self.have_global.get("pool_details").get("id")}) + return self - if self.params.get("config")[0].get("ReservePoolDetails") is not None: + if config.get("ReservePoolDetails") is not None: res_updated = False res_created = False @@ -1408,11 +1259,11 @@ def get_diff_merged(self): self.log("Reserved Ip Subpool Updated Successfully") self.result['msg'] = "Reserved Ip Subpool Updated Successfully" self.result['response'].update({"Reservation details": \ - self.have.get("res_details")}) + self.have_global.get("res_details")}) elif res_created: self.log("Ip Subpool Reservation Created Successfully") - (res_exists, res_details, res_id) = self.res_exists() + (res_exists, res_details, res_id) = self.res_exists(config) self.result['response'].update({"Reservation Id": res_id}) self.result['response'].update({"Reservation Exists": res_exists}) self.result['response'].update({"Reservation details": res_details}) @@ -1421,30 +1272,31 @@ def get_diff_merged(self): self.log("Ip Subpool Reservation doesn't need a update") self.result['msg'] = "Ip Subpool Reservation doesn't requires an update" self.result['response'].update({"Reservation details": \ - self.have.get("res_details")}) + self.have_global.get("res_details")}) + return self - if self.params.get("config")[0].get("NetworkManagementDetails") is not None: + if config.get("NetworkManagementDetails") is not None: net_updated = False net_created = False - if self.have_net: + if self.have_network: self.log("entered") obj_params = [ ("settings", "settings"), ("siteName", "siteName") ] - if self.want_net.get("settings").get("timezone") is None: + if self.want_network.get("settings").get("timezone") is None: self.msg = "Missing required parameter timezone" self.status = "failed" return self - if self.requires_update(self.have_net.get("net_details"), self.want_net, obj_params): + if self.requires_update(self.have_network.get("net_details"), self.want_network, obj_params): self.log("Network update requires") - self.log(str(self.have_net)) - self.log(str(self.want_net)) + self.log(str(self.have_network)) + self.log(str(self.want_network)) - net_params = copy.deepcopy(self.want_net) - net_params.update({"site_id": self.have_net.get("site_id")}) + net_params = copy.deepcopy(self.want_network) + net_params.update({"site_id": self.have_network.get("site_id")}) response = self.dnac._exec( family="network_settings", function='update_network', @@ -1457,7 +1309,7 @@ def get_diff_merged(self): else: self.log("Network doesn't need an update") - self.result["response"] = self.have_net + self.result["response"] = self.have_network self.result["msg"] = "Network doesn't need an update" self.module.exit_json(**self.result) @@ -1483,12 +1335,13 @@ def get_diff_merged(self): if net_updated: self.log("Network has been changed Successfully") self.result['msg'] = "Network Updated successfully" - self.result['response'] = self.want_net + self.result['response'] = self.want_network else: self.log("Pool doesn't need a update") self.result['msg'] = "Network doesn't requires an update" - self.result['response'] = self.have_net + self.result['response'] = self.have_network + return self def get_res_id_by_name(self, name): _id = None @@ -1516,9 +1369,9 @@ def get_res_id_by_name(self, name): return _id - def get_diff_deleted(self): + def get_diff_deleted(self,config): - if self.params.get("config")[0].get("ReservePoolDetails") is not None: + if config.get("ReservePoolDetails") is not None: res_exists = self.have_reserve.get("res_exists") self.log(str(res_exists)) _id = None @@ -1559,14 +1412,14 @@ def get_diff_deleted(self): self.status = "failed" return self - if self.params.get("config")[0].get("GlobalPoolDetails") is not None: - pool_exists = self.have.get("pool_exists") + if config.get("GlobalPoolDetails") is not None: + pool_exists = self.have_global.get("pool_exists") if pool_exists: response = self.dnac._exec( family="network_settings", function="delete_global_ip_pool", - params={"id": self.have.get("pool_id")}, + params={"id": self.have_global.get("pool_id")}, ) if response and isinstance(response, dict): @@ -1589,12 +1442,24 @@ def get_diff_deleted(self): self.status = "failed" return self - if self.params.get("config")[0].get("NetworkManagementDetails") is not None: + if config.get("NetworkManagementDetails") is not None: self.msg = "No operation available for Delete Network" self.status = "failed" return self + def reset_values(self): + """Reset all neccessary attributes to default values""" + + self.have_global.clear() + self.want_global.clear() + self.have_reserve.clear() + self.want_reserve.clear() + self.have_network.clear() + self.want_network.clear() + self.site_id = None + + def main(): """main entry point for module execution""" @@ -1623,10 +1488,12 @@ def main(): dnac_network.msg = "State {0} is invalid".format(state) dnac_network.check_return_status() - dnac_network.get_have().check_return_status() - dnac_network.get_want().check_return_status() + for config in dnac_network.config: + dnac_network.reset_values() + dnac_network.get_have(config).check_return_status() + dnac_network.get_want(config).check_return_status() - dnac_network.get_diff_state_apply[state]().check_return_status() + dnac_network.get_diff_state_apply[state](config).check_return_status() module.exit_json(**dnac_network.result) From c96d073113aed6864ae5be779768d13953b5fe9d Mon Sep 17 00:00:00 2001 From: Muthu Rakesh <114211490+MUTHU-RAKESH-27@users.noreply.github.com> Date: Mon, 11 Sep 2023 11:09:36 +0530 Subject: [PATCH 6/9] Update network_intent.yml --- playbooks/network_intent.yml | 59 ++++-------------------------------- 1 file changed, 6 insertions(+), 53 deletions(-) diff --git a/playbooks/network_intent.yml b/playbooks/network_intent.yml index 5c5da42b25..436e01eb6d 100644 --- a/playbooks/network_intent.yml +++ b/playbooks/network_intent.yml @@ -9,7 +9,7 @@ # - name: Create, update, delete global pool and reserve an ip pool - cisco.dnac.network_intent: + cisco.dnac.network_intent_sample: dnac_host: "{{ dnac_host }}" dnac_port: "{{ dnac_port }}" dnac_username: "{{ dnac_username }}" @@ -33,13 +33,13 @@ ReservePoolDetails: ipv6AddressSpace: false ipv4GlobalPool: 100.0.0.0/8 - ipv4Prefix: true + ipv4Prefix: True ipv4PrefixLength: 9 ipv4DnsServers: [] ipv4Subnet: 100.128.0.0 name: IP_Pool_3 siteName: Global/Chennai/Trill - slaacSupport: true + slaacSupport: True # prev_name: IP_Pool_4 type: LAN NetworkManagementDetails: @@ -58,7 +58,7 @@ # sharedSecret: string #ISE messageOfTheday: bannerMessage: hello - retainExistingBanner: true + retainExistingBanner: True netflowcollector: ipAddress: 10.0.0.4 port: 443 @@ -71,59 +71,12 @@ ntpServer: - 10.0.0.5 snmpServer: - configureDnacIP: true + configureDnacIP: True ipAddresses: - 10.0.0.6 syslogServer: - configureDnacIP: true + configureDnacIP: True ipAddresses: - 10.0.0.7 timezone: GMT siteName: Global/Chennai - # DeviceCredentialsDetails: - # cliCredential: - # description: CLI 1 - # username: user1 - # password: 12345 - # enablePassword: 12345 - # # credentialName: CLI #use this field for update - # snmpV2cRead: - # description: SNMP Read 1 - # readCommunity: user1 - # # credentialName: SNMP Read 1 #use this for update - # snmpV2cWrite: - # description: SNMP Write 1 - # writeCommunity: user1 - # # credentialName: SNMP Write 1 #use this for update - # snmpV3: - # authPassword: 12345678 - # authType: SHA - # snmpMode: AUTHPRIV - # privacyPassword: 12345678 - # privacyType: AES128 - # username: user1 - # description: SNMP v3 1 - # credentialName: SNMP v3 1 #use this for update - # httpsRead: - # name: HTTP Read 1 - # username: user1 - # password: 12345 - # port: 443 - # credentialName: HTTP Read 1 #use this field for update - # httpsWrite: - # name: HTTP Write 1 - # username: user1 - # password: 12345 - # port: 443 - # credentialName: HTTP Write 1 #use this field for update - # AssignDeviceCredential: - # cliName: CLI 1 - # snmpV2ReadName: SNMP Read 1 - # snmpV2WriteName: SNMP Write 1 - # snmpV3Name: SNMP v3 1 - # httpReadName: HTTP Read 1 - # httpWriteName: HTTP Write 1 - # siteName: Global/Chennai/Trill - # delCredentialName: CLI 1 #use this for delete - - From a84c321a24d80f8907215414f99b4366c3a60a6a Mon Sep 17 00:00:00 2001 From: Muthu Rakesh <114211490+MUTHU-RAKESH-27@users.noreply.github.com> Date: Mon, 11 Sep 2023 13:21:21 +0530 Subject: [PATCH 7/9] Update network_intent.py --- plugins/modules/network_intent.py | 285 ++++++++---------------------- 1 file changed, 78 insertions(+), 207 deletions(-) diff --git a/plugins/modules/network_intent.py b/plugins/modules/network_intent.py index bf97b07770..c329e559c6 100644 --- a/plugins/modules/network_intent.py +++ b/plugins/modules/network_intent.py @@ -28,7 +28,14 @@ def __init__(self, module): def validate_input(self): """Validate the fields provided in the playbook""" + if not self.config: + self.msg = "config not available in playbook for validattion" + self.status = "success" + return self + temp_spec = { + "settings": {"type": 'dict'}, + "ippool":{"type": 'list'}, "IpAddressSpace": {"type": 'string'}, "dhcpServerIps": {"type": 'list'}, "dnsServerIps": {"type": 'list'}, @@ -57,6 +64,13 @@ def validate_input(self): "slaacSupport": {"type": 'bool'}, "siteName": {"type": 'string'}, "dhcpServer": {"type": 'list'}, + "dnsServer": {"type": 'dict'}, + "syslogServer": {"type": 'dict'}, + "snmpServer": {"type": 'dict'}, + "netflowcollector": {"type": 'dict'}, + "messageOfTheday": {"type": 'dict'}, + "network_aaa": {"type": 'dict'}, + "clientAndEndpoint_aaa": {"type": 'dict'}, "domainName": {"type": 'string'}, "primaryIpAddress": {"type": 'string'}, "secondaryIpAddress": {"type": 'string'}, @@ -73,138 +87,19 @@ def validate_input(self): "ntpServer": {"type": 'list'} } - if self.config: - globalPool_details = [] - ReservePool_details = [] - network_details = [] - # Validate template params - if self.config[0].get("GlobalPoolDetails") is not None: - globalPool_details = self.config[0].get("GlobalPoolDetails").get("settings").get("ippool") - - if self.config[0].get("ReservePoolDetails") is not None: - ReservePool_details = [self.config[0].get("ReservePoolDetails")] - - if self.config[0].get("NetworkManagementDetails") is not None: - network_management_details = self.config[0] \ - .get("NetworkManagementDetails").get("settings") - if network_management_details.get("dhcpServer"): - network_details.append({"dhcpServer": self.config[0].get("NetworkManagementDetails") \ - .get("settings").get("dhcpServer")}) - - if network_management_details.get("dnsServer"): - if network_management_details.get("dnsServer").get("domainName"): - network_details.append({"domainName": network_management_details \ - .get("dnsServer").get("domainName")}) - if network_management_details.get("dnsServer").get("primaryIpAddress"): - network_details.append({"primaryIpAddress": network_management_details \ - .get("dnsServer").get("primaryIpAddress")}) - if network_management_details.get("dnsServer").get("secondaryIpAddress"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("dnsServer").get("secondaryIpAddress")}) - - if network_management_details.get("clientAndEndpoint_aaa"): - if network_management_details.get("clientAndEndpoint_aaa").get("ipAddress"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("clientAndEndpoint_aaa").get("ipAddress")}) - if network_management_details.get("clientAndEndpoint_aaa").get("network"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("clientAndEndpoint_aaa").get("network")}) - if network_management_details.get("clientAndEndpoint_aaa").get("protocol"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("clientAndEndpoint_aaa").get("protocol")}) - if network_management_details.get("clientAndEndpoint_aaa").get("servers"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("clientAndEndpoint_aaa").get("servers")}) - if network_management_details.get("clientAndEndpoint_aaa").get("sharedSecret"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("clientAndEndpoint_aaa").get("sharedSecret")}) - - if network_management_details.get("messageOfTheday"): - if network_management_details.get("messageOfTheday").get("bannerMessage"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("messageOfTheday").get("bannerMessage")}) - - if network_management_details.get("messageOfTheday").get("retainExistingBanner"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("messageOfTheday").get("retainExistingBanner")}) - - if network_management_details.get("netflowcollector"): - if network_management_details.get("netflowcollector").get("ipAddress"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("netflowcollector").get("ipAddress")}) - if network_management_details.get("netflowcollector").get("port"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("netflowcollector").get("port")}) - - if network_management_details.get("network_aaa"): - if network_management_details.get("network_aaa").get("ipAddress"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("network_aaa").get("ipAddress")}) - if network_management_details.get("network_aaa").get("network"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("network_aaa").get("network")}) - if network_management_details.get("network_aaa").get("protocol"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("network_aaa").get("protocol")}) - if network_management_details.get("network_aaa").get("servers"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("network_aaa").get("servers")}) - if network_management_details.get("network_aaa").get("sharedSecret"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("network_aaa").get("sharedSecret")}) - - if network_management_details.get("ntpServer"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("ntpServer")}) - - if network_management_details.get("snmpServer"): - if network_management_details.get("snmpServer").get("configureDnacIP"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("snmpServer").get("configureDnacIP")}) - if network_management_details.get("snmpServer").get("ipAddresses"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("snmpServer").get("ipAddresses")}) - - if network_management_details.get("syslogServer"): - if network_management_details.get("syslogServer").get("configureDnacIP"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("syslogServer").get("configureDnacIP")}) - if network_management_details.get("syslogServer").get("ipAddresses"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("syslogServer").get("ipAddresses")}) - - if network_management_details.get("timezone"): - network_details.append({"secondaryIpAddress": network_management_details \ - .get("timezone")}) - - self.log(str(globalPool_details)) - self.log(str(ReservePool_details)) - self.log(str(network_details)) - playbook_details = globalPool_details + ReservePool_details + network_details - valid_temp, invalid_params = validate_list_of_dicts( - playbook_details, temp_spec - ) - if invalid_params: - self.msg = "Invalid parameters in playbook: {0}".format( - "\n".join(invalid_params) - ) - self.status = "failed" - return self - self.log(str(invalid_params)) - self.validated_config = valid_temp - - self.log(str(self.validated_config)) - - self.log(str(self.validated_config)) - if self.params.get("config")[0].get("GlobalPoolDetails") is not None: - for temp in self.validated_config: - self.log(str(temp)) - if self.params.get("config")[0].get("GlobalPoolDetails") \ - .get("ipPoolName") is not None: - self.msg = "missing required arguments: ipPoolName" - self.status = "failed" - return self + # Validate template params + valid_temp, invalid_params = validate_list_of_dicts( + self.config, temp_spec + ) + if invalid_params: + self.msg = "Invalid parameters in playbook: {0}".format( + "\n".join(invalid_params)) + self.status = "failed" + return self + + self.validated_config = valid_temp + self.log(str(valid_temp)) self.msg = "Successfully validated input" self.status = "success" return self @@ -258,30 +153,59 @@ def get_pool_id_from_name(self, pool_name): return (pool_id, current_details) - def get_res_id_from_name(self, res_name): - _id = "" - current_details = None + def get_res_id_by_name(self, name): + _id = None try: response = self.dnac._exec( family="network_settings", function="get_reserve_ip_subpool", - params={"site_id": self.site_id} + params={"siteId":self.site_id}, ) + self.log(str(response)) + if isinstance(response, dict): if "response" in response: response = response.get("response") self.log(str(response)) - current_details = get_dict_result(response, "groupName", res_name) - self.log(str(current_details)) - _id = current_details.get("id") + current_details = get_dict_result(response, "groupName", name) + if current_details: + _id = current_details.get("id") + + except: + result = None + + return _id + + + def get_site_id(self, site_name): + + response = {} + _id = None + try: + response = self.dnac._exec( + family="sites", + function='get_site', + params={"name":site_name}, + ) + except: - self.log("except") result = None + self.log(str(response)) + if not response: + self.log("Invalid site name or site not present") + self.msg = "Invalid site name or site not present" + self.status = "failed" + return self + else: + _id = response.get("response")[0].get("id") + self.log(str(_id)) + return _id + def get_current_pool(self, pool): self.log(str(pool)) pool_values = { @@ -456,31 +380,6 @@ def get_current_net(self, site_id): self.log(str(net_values)) return net_values - def get_site_id(self, site_name): - - response = {} - _id = None - try: - response = self.dnac._exec( - family="sites", - function='get_site', - params={"name":site_name}, - ) - - except: - result = None - self.log(str(response)) - if not response: - self.log("Invalid site name or site not present") - self.msg = "Invalid site name or site not present" - self.status = "failed" - return self - else: - _id = response.get("response")[0].get("id") - self.log(str(_id)) - - return _id - def pool_exists(self, config): pool_exists = False @@ -556,7 +455,7 @@ def res_exists(self, config): self.msg = "Mandatory Parameter siteName required\n" self.status = "failed" return self - _id = self.get_res_id_from_name(prev_name) + _id = self.get_res_id_by_name(prev_name) self.log(str(_id)) try: @@ -1054,7 +953,7 @@ def get_execution_details(self, execid): def get_diff_merged(self, config): if config.get("GlobalPoolDetails") is not None: - + if not config.get("GlobalPoolDetails") \ .get("settings").get("ippool")[0].get("ipPoolName"): @@ -1161,7 +1060,6 @@ def get_diff_merged(self, config): self.result['msg'] = "Pool doesn't requires an update" self.result['response'].update({"Id": \ self.have_global.get("pool_details").get("id")}) - return self if config.get("ReservePoolDetails") is not None: @@ -1273,7 +1171,6 @@ def get_diff_merged(self, config): self.result['msg'] = "Ip Subpool Reservation doesn't requires an update" self.result['response'].update({"Reservation details": \ self.have_global.get("res_details")}) - return self if config.get("NetworkManagementDetails") is not None: @@ -1315,20 +1212,20 @@ def get_diff_merged(self, config): if net_updated: if response and isinstance(response, dict): - executionid = response.get("executionId") + task_id = response.get("response").get("taskId") self.log(str(response)) - self.log(str(executionid)) + self.log(str(task_id)) while True: - execution_details = self.get_execution_details(executionid) - if execution_details.get("status") == "SUCCESS": + task_details = self.get_task_details(task_id) + if task_details.get("status") == "SUCCESS": self.result['changed'] = True - self.result['response'] = execution_details + self.result['response'] = task_details break - elif execution_details.get("bapiError"): + elif task_details.get("bapiError"): - self.msg = execution_details.get("bapiError") + self.msg = task_details.get("bapiError") self.status = "failed" return self @@ -1341,32 +1238,8 @@ def get_diff_merged(self, config): self.log("Pool doesn't need a update") self.result['msg'] = "Network doesn't requires an update" self.result['response'] = self.have_network - return self - - def get_res_id_by_name(self, name): - _id = None - try: - response = self.dnac._exec( - family="network_settings", - function="get_reserve_ip_subpool", - params={"siteId":self.site_id}, - ) - self.log(str(response)) - - if isinstance(response, dict): - if "response" in response: - response = response.get("response") - self.log(str(response)) - - current_details = get_dict_result(response, "groupName", name) - if current_details: - _id = current_details.get("id") - - except: - result = None - - return _id + return self def get_diff_deleted(self,config): @@ -1395,12 +1268,13 @@ def get_diff_deleted(self,config): if response and isinstance(response, dict): executionid = response.get("executionId") while True: - task_details = self.get_task_details(executionid) + task_details = self.get_execution_details(executionid) if task_details.get("status") == "SUCCESS": self.result['changed'] = True self.result['response'] = task_details self.log(str(response)) self.result['msg'] = "Ip subpool reservation released successfully" + break elif task_details.get("bapiError"): self.msg = task_details.get("bapiError") @@ -1425,12 +1299,13 @@ def get_diff_deleted(self,config): if response and isinstance(response, dict): executionid = response.get("executionId") while True: - task_details = self.get_task_details(executionid) + task_details = self.get_execution_details(executionid) if task_details.get("status") == "SUCCESS": self.result['changed'] = True self.result['response'] = task_details self.log(str(response)) self.result['msg'] = "Pool deleted successfully" + break elif task_details.get("bapiError"): self.msg = task_details.get("bapiError") @@ -1441,11 +1316,7 @@ def get_diff_deleted(self,config): self.msg = "Pool Not Found" self.status = "failed" return self - - if config.get("NetworkManagementDetails") is not None: - self.msg = "No operation available for Delete Network" - self.status = "failed" - return self + return self def reset_values(self): From 12f76c8fa59967dc2492577562fc4ffe634b9e22 Mon Sep 17 00:00:00 2001 From: Muthu Rakesh <114211490+MUTHU-RAKESH-27@users.noreply.github.com> Date: Mon, 11 Sep 2023 13:37:10 +0530 Subject: [PATCH 8/9] Update network_intent.yml --- playbooks/network_intent.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/playbooks/network_intent.yml b/playbooks/network_intent.yml index 436e01eb6d..1f9ed2ac69 100644 --- a/playbooks/network_intent.yml +++ b/playbooks/network_intent.yml @@ -9,7 +9,7 @@ # - name: Create, update, delete global pool and reserve an ip pool - cisco.dnac.network_intent_sample: + cisco.dnac.network_intent: dnac_host: "{{ dnac_host }}" dnac_port: "{{ dnac_port }}" dnac_username: "{{ dnac_username }}" @@ -22,7 +22,7 @@ - GlobalPoolDetails: settings: ippool: - - ipPoolName: Global_Pool2 + - ipPoolName: Global_Pool1 gateway: "" #use this for updating IpAddressSpace: IPv4 #required when w2 are creating ipPoolCidr: 100.0.0.0/8 #required when we are creating From c2bfda87d7492765530f94240e02ae163756e09a Mon Sep 17 00:00:00 2001 From: Muthu Rakesh <114211490+MUTHU-RAKESH-27@users.noreply.github.com> Date: Mon, 11 Sep 2023 14:21:59 +0530 Subject: [PATCH 9/9] Update network_intent.py --- plugins/modules/network_intent.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/plugins/modules/network_intent.py b/plugins/modules/network_intent.py index c329e559c6..44c5f55d9d 100644 --- a/plugins/modules/network_intent.py +++ b/plugins/modules/network_intent.py @@ -167,7 +167,6 @@ def get_res_id_by_name(self, name): if isinstance(response, dict): if "response" in response: response = response.get("response") - self.log(str(response)) current_details = get_dict_result(response, "groupName", name) if current_details: @@ -308,13 +307,9 @@ def get_current_net(self, site_id): messageoftheday_details = get_dict_result(response, "key", "banner.setting") network_aaa = get_dict_result(response, "key", "aaa.network.server.1") network_aaa_pan = get_dict_result(response, "key", "aaa.server.pan.network") - self.log(str(syslog_details)) clientAndEndpoint_aaa = get_dict_result(response, "key", "aaa.endpoint.server.1") clientAndEndpoint_aaa_pan = get_dict_result(response, "key", "aaa.server.pan.endpoint") - self.log(str(network_aaa)) - self.log(str(clientAndEndpoint_aaa)) - net_values = { "settings": { @@ -509,14 +504,12 @@ def get_have(self, config): have_global["pool_id"] = pool_id have_global["pool_exists"] = pool_exists have_global["pool_details"] = pool_details - self.log(str(pool_details)) self.have_global = have_global if config.get("ReservePoolDetails") is not None: have_reserve = {} (res_exists, res_details, res_id) = self.res_exists(config) - self.log(str(res_exists)) self.log("Reservation Exists: " + str(res_exists) \ + "\n Reserved Pool: " + str(res_details)) @@ -699,7 +692,6 @@ def get_want(self, config): self.want_reserve = want_reserve if config.get("NetworkManagementDetails") is not None: - self.log(str(self.params)) want_network = { "settings": { "dhcpServer": { @@ -965,7 +957,6 @@ def get_diff_merged(self, config): pool_created = False if self.have_global.get("pool_exists"): - self.log("entered") obj_params = [ ("settings", "settings"), ] @@ -975,7 +966,6 @@ def get_diff_merged(self, config): pool_params = copy.deepcopy(self.want_global) pool_params.get("settings").get("ippool")[0] \ .update({"id": self.have_global.get("pool_id")}) - self.log(str(self.want_global)) self.log(str(pool_params)) del pool_params["settings"]["ippool"][0]["IpAddressSpace"] del pool_params["settings"]["ippool"][0]["ipPoolCidr"] @@ -1068,7 +1058,6 @@ def get_diff_merged(self, config): self.log(str(self.have_reserve.get("res_details"))) self.log(str(self.want_reserve)) if self.have_reserve: - self.log("entered") obj_params = [ ("name", "name"), ("type", "type"), @@ -1177,7 +1166,6 @@ def get_diff_merged(self, config): net_updated = False net_created = False if self.have_network: - self.log("entered") obj_params = [ ("settings", "settings"), ("siteName", "siteName")