Skip to content

Commit dd6da07

Browse files
authored
Print Diff for DCNM_Inventory Results, Fix for Issue #90 (#465)
* Print Diff for DCNM_Inventory Results, Fix for Issue #90 * Pep8 Fix * Fix Erroneous Diff Append for Poap + Bootstrap --------- Co-authored-by: = <=>
1 parent 1ee3763 commit dd6da07

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

plugins/modules/dcnm_inventory.py

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,7 @@ def __init__(self, module):
509509
self.node_migration = False
510510
self.nd_prefix = "/appcenter/cisco/ndfc/api/v1/lan-fabric"
511511
self.switch_snos = []
512+
self.diff_input_format = []
512513

513514
self.result = dict(changed=False, diff=[], response=[])
514515

@@ -1811,6 +1812,120 @@ def failure(self, resp):
18111812

18121813
self.module.fail_json(msg=res)
18131814

1815+
def format_diff(self):
1816+
"""
1817+
Format the diff for inventory operations
1818+
"""
1819+
diff = []
1820+
create_list = []
1821+
preprovision_list = []
1822+
bootstrap_list = []
1823+
rma_list = []
1824+
delete_list = []
1825+
1826+
# Add created switches to the diff
1827+
for create in self.diff_create:
1828+
if create.get("switches") and len(create["switches"]) > 0:
1829+
switch = create["switches"][0]
1830+
item = {
1831+
"ip_address": switch.get("ipaddr"),
1832+
"serial_number": switch.get("serialNumber"),
1833+
"role": create.get("role"),
1834+
"platform": switch.get("platform"),
1835+
"version": switch.get("version"),
1836+
"hostname": switch.get("sysName"),
1837+
"preserve_config": create.get("preserveConfig", False)
1838+
}
1839+
create_list.append(item)
1840+
1841+
if create_list:
1842+
create_item = {
1843+
"action": "create",
1844+
"switches": create_list
1845+
}
1846+
diff.append(create_item)
1847+
1848+
# Add POAP switches to the diff
1849+
for poap in self.want_create_poap:
1850+
item = {
1851+
"ip_address": poap.get("ipAddress"),
1852+
"role": poap.get("role"),
1853+
"hostname": poap.get("hostname"),
1854+
"platform": poap.get("model"),
1855+
"version": poap.get("version")
1856+
}
1857+
if poap.get("serialNumber"):
1858+
item["serial_number"] = poap.get("serialNumber")
1859+
bootstrap_list.append(item)
1860+
if poap.get("preprovisionSerial"):
1861+
item["preprovision_serial"] = poap.get("preprovisionSerial")
1862+
preprovision_list.append(item)
1863+
1864+
if bootstrap_list:
1865+
diff.append({
1866+
"action": "bootstrap",
1867+
"bootstrap_switches": bootstrap_list
1868+
})
1869+
if preprovision_list:
1870+
diff.append({
1871+
"action": "preprovision",
1872+
"preprovision_switches": preprovision_list
1873+
})
1874+
1875+
# Add RMA switches to the diff
1876+
for rma in self.want_create_rma:
1877+
item = {
1878+
"ip_address": rma.get("ipAddress"),
1879+
"old_serial": rma.get("oldSerialNumber"),
1880+
"new_serial": rma.get("newSerialNumber"),
1881+
"platform": rma.get("model"),
1882+
"version": rma.get("version")
1883+
}
1884+
rma_list.append(item)
1885+
1886+
if rma_list:
1887+
diff.append({
1888+
"action": "rma",
1889+
"rma_switches": rma_list
1890+
})
1891+
1892+
# Add deleted switches to the diff
1893+
for serial in self.diff_delete:
1894+
ip_address = None
1895+
hostname = None
1896+
for have_c in self.have_create:
1897+
if have_c["switches"][0]["serialNumber"] == serial:
1898+
ip_address = have_c["switches"][0]["ipaddr"]
1899+
hostname = have_c["switches"][0]["sysName"]
1900+
role = have_c["switches"][0]["role"]
1901+
platform = have_c["switches"][0]["platform"]
1902+
version = have_c["switches"][0]["version"]
1903+
break
1904+
1905+
item = {
1906+
"serial_number": serial
1907+
}
1908+
if ip_address:
1909+
item["ip_address"] = ip_address
1910+
if hostname:
1911+
item["hostname"] = hostname
1912+
if role:
1913+
item["role"] = role
1914+
if platform:
1915+
item["platform"] = platform
1916+
if version:
1917+
item["version"] = version
1918+
1919+
delete_list.append(item)
1920+
1921+
if delete_list:
1922+
diff.append({
1923+
"action": "delete",
1924+
"switches": delete_list
1925+
})
1926+
1927+
self.diff_input_format = diff
1928+
18141929

18151930
def main():
18161931
"""main entry point for module execution"""
@@ -1943,6 +2058,8 @@ def main():
19432058
if module.params["deploy"]:
19442059
dcnm_inv.config_deploy()
19452060

2061+
dcnm_inv.format_diff()
2062+
dcnm_inv.result["diff"] = dcnm_inv.diff_input_format
19462063
module.exit_json(**dcnm_inv.result)
19472064

19482065

0 commit comments

Comments
 (0)