1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2023, Cisco Systems
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
"""Ansible module to perform operations on device credentials in Cisco DNA Center."""
from __future__ import absolute_import, division, print_function
__metaclass__ = type
__author__ = ['Muthu Rakesh, Madhan Sankaranarayanan']
DOCUMENTATION = r"""
---
module: device_credential_intent
short_description: Resource module for Global Device Credentials and Assigning Credentials to sites.
description:
- Manage operations on Global Device Credentials and Assigning Credentials to sites.
- API to create global device credentials.
- API to update global device credentials.
- API to delete global device credentials.
- API to assign the device credential to the site.
version_added: '6.7.0'
extends_documentation_fragment:
- cisco.dnac.intent_params
author: Muthu Rakesh (@MUTHU-RAKESH-27)
Madhan Sankaranarayanan (@madhansansel)
options:
config_verify:
description: Set to True to verify the Cisco DNA Center after applying the playbook config.
type: bool
default: False
state:
description: The state of Cisco DNA Center after module completion.
type: str
choices: [ merged, deleted ]
default: merged
config:
description:
- List of details of global device credentials and site names.
type: list
elements: dict
required: true
suboptions:
global_credential_details:
description: Manages global device credentials
type: dict
suboptions:
cli_credential:
description: Global Credential V2's cliCredential.
type: list
elements: dict
suboptions:
description:
description: Description. Required for creating the credential.
type: str
enable_password:
description:
- cli_credential credential Enable Password.
- Password cannot contain spaces or angle brackets (< >)
type: str
id:
description: Credential Id. Use this for updating the device credential.
type: str
password:
description:
- cli_credential credential Password.
- Required for creating/updating the credential.
- Password cannot contain spaces or angle brackets (< >).
type: str
username:
description:
- cli_credential credential Username.
- Username cannot contain spaces or angle brackets (< >).
type: str
old_description:
description: Old Description. Use this for updating the description/Username.
type: str
old_username:
description: Old Username. Use this for updating the description/Username.
type: str
https_read:
description: Global Credential V2's httpsRead.
type: list
elements: dict
suboptions:
id:
description: Credential Id. Use this for updating the device credential.
type: str
name:
description: Name. Required for creating the credential.
type: str
password:
description:
- https_read credential Password.
- Required for creating/updating the credential.
- Password cannot contain spaces or angle brackets (< >).
type: str
port:
description: Port. Default port is 443.
type: int
username:
description:
- https_read credential Username.
- Username cannot contain spaces or angle brackets (< >).
type: str
old_description:
description: Old Description. Use this for updating the description/Username.
type: str
old_username:
description: Old Username. Use this for updating the description/Username.
type: str
https_write:
description: Global Credential V2's httpsWrite.
type: list
elements: dict
suboptions:
id:
description: Credential Id. Use this for updating the device credential.
type: str
name:
description: Name. Required for creating the credential.
type: str
password:
description:
- https_write credential Password.
- Required for creating/updating the credential.
- Password cannot contain spaces or angle brackets (< >).
type: str
port:
description: Port. Default port is 443.
type: int
username:
description:
- https_write credential Username.
- Username cannot contain spaces or angle brackets (< >).
type: str
old_description:
description: Old Description. Use this for updating the description/Username.
type: str
old_username:
description: Old Username. Use this for updating the description/Username.
type: str
snmp_v2c_read:
description: Global Credential V2's snmpV2cRead.
type: list
elements: dict
suboptions:
description:
description: Description. Required for creating the credential.
type: str
id:
description: Credential Id. Use this for updating the device credential.
type: str
read_community:
description:
- snmp_v2c_read Read Community.
- Password cannot contain spaces or angle brackets (< >).
type: str
old_description:
description: Old Description. Use this for updating the description.
type: str
snmp_v2c_write:
description: Global Credential V2's snmpV2cWrite.
type: list
elements: dict
suboptions:
description:
description: Description. Required for creating the credential.
type: str
id:
description: Credential Id. Use this for updating the device credential.
type: str
write_community:
description:
- snmp_v2c_write Write Community.
- Password cannot contain spaces or angle brackets (< >).
type: str
old_description:
description: Old Description. Use this for updating the description.
type: str
snmp_v3:
description: Global Credential V2's snmpV3.
type: list
elements: dict
suboptions:
auth_password:
description:
- snmp_v3 Auth Password.
- Password must contain minimum 8 characters.
- Password cannot contain spaces or angle brackets (< >).
type: str
auth_type:
description: Auth Type. ["SHA", "MD5"].
type: str
description:
description:
- snmp_v3 Description.
- Should be unique from other snmp_v3 credentials.
type: str
id:
description: Credential Id. Use this for updating the device credential.
type: str
privacy_password:
description:
- snmp_v3 Privacy Password.
- Password must contain minimum 8 characters.
- Password cannot contain spaces or angle brackets (< >).
type: str
privacy_type:
description: Privacy Type. ["AES128", "AES192", "AES256"].
type: str
snmp_mode:
description: Snmp Mode. ["AUTHPRIV", "AUTHNOPRIV", "NOAUTHNOPRIV"].
type: str
username:
description:
- snmp_v3 credential Username.
- Username cannot contain spaces or angle brackets (< >).
type: str
old_description:
description: Old Description. Use this for updating the description.
type: str
assign_credentials_to_site:
description: Assign Device Credentials to Site.
type: dict
suboptions:
cli_credential:
description: CLI Credential.
type: dict
suboptions:
description:
description: CLI Credential Description.
type: str
username:
description: CLI Credential Username.
type: str
id:
description: CLI Credential Id. Use (Description, Username) or Id.
type: str
https_read:
description: HTTP(S) Read Credential
type: dict
suboptions:
description:
description: HTTP(S) Read Credential Description.
type: str
username:
description: HTTP(S) Read Credential Username.
type: str
id:
description: HTTP(S) Read Credential Id. Use (Description, Username) or Id.
type: str
https_write:
description: HTTP(S) Write Credential
type: dict
suboptions:
description:
description: HTTP(S) Write Credential Description.
type: str
username:
description: HTTP(S) Write Credential Username.
type: str
id:
description: HTTP(S) Write Credential Id. Use (Description, Username) or Id.
type: str
site_name:
description: Site Name to assign credential.
type: list
elements: str
snmp_v2c_read:
description: SNMPv2c Read Credential
type: dict
suboptions:
description:
description: SNMPv2c Read Credential Description.
type: str
id:
description: SNMPv2c Read Credential Id. Use Description or Id.
type: str
snmp_v2c_write:
description: SNMPv2c Write Credential
type: dict
suboptions:
description:
description: SNMPv2c Write Credential Description.
type: str
id:
description: SNMPv2c Write Credential Id. Use Description or Id.
type: str
snmp_v3:
description: snmp_v3 Credential
type: dict
suboptions:
description:
description: snmp_v3 Credential Description.
type: str
id:
description: snmp_v3 Credential Id. Use Description or Id.
type: str
requirements:
- dnacentersdk >= 2.7.1
- python >= 3.9
seealso:
- name: Cisco DNA Center documentation for Discovery CreateGlobalCredentialsV2
description: Complete reference of the CreateGlobalCredentialsV2 API.
link: https://developer.cisco.com/docs/dna-center/#!create-global-credentials-v-2
- name: Cisco DNA Center documentation for Discovery DeleteGlobalCredentialV2
description: Complete reference of the DeleteGlobalCredentialV2 API.
link: https://developer.cisco.com/docs/dna-center/#!delete-global-credential-v-2
- name: Cisco DNA Center documentation for Discovery UpdateGlobalCredentialsV2
description: Complete reference of the UpdateGlobalCredentialsV2 API.
link: https://developer.cisco.com/docs/dna-center/#!update-global-credentials-v-2
- name: Cisco DNA Center documentation for Network Settings AssignDeviceCredentialToSiteV2
description: Complete reference of the AssignDeviceCredentialToSiteV2 API.
link: https://developer.cisco.com/docs/dna-center/#!assign-device-credential-to-site-v-2
notes:
- SDK Method used are
discovery.Discovery.create_global_credentials_v2,
discovery.Discovery.delete_global_credential_v2,
discovery.Discovery.update_global_credentials_v2,
network_settings.NetworkSettings.assign_device_credential_to_site_v2,
- Paths used are
post /dna/intent/api/v2/global-credential,
delete /dna/intent/api/v2/global-credential/{id},
put /dna/intent/api/v2/global-credential,
post /dna/intent/api/v2/credential-to-site/{siteId},
"""
EXAMPLES = r"""
---
- name: Create Credentials and assign it to a site.
cisco.dnac.device_credential_intent:
dnac_host: "{{ dnac_host }}"
dnac_port: "{{ dnac_port }}"
dnac_username: "{{ dnac_username }}"
dnac_password: "{{ dnac_password }}"
dnac_verify: "{{ dnac_verify }}"
dnac_debug: "{{ dnac_debug }}"
dnac_log: True
dnac_log_level: "{{ dnac_log_level }}"
state: merged
config_verify: True
config:
- global_credential_details:
cli_credential:
- description: string
username: string
password: string
enable_password: string
snmp_v2c_read:
- description: string
read_community: string
snmp_v2c_write:
- description: string
write_community: string
snmp_v3:
- auth_password: string
auth_type: SHA
snmp_mode: AUTHPRIV
privacy_password: string
privacy_type: AES128
username: string
description: string
https_read:
- description: string
username: string
password: string
port: 443
https_write:
- description: string
username: string
password: string
port: 443
assign_credentials_to_site:
cli_credential:
id: string
snmp_v2c_read:
id: string
snmp_v2c_write:
id: string
snmp_v3:
id: string
https_read:
id: string
https_write:
id: string
site_name:
- string
- name: Create Multiple Credentials.
cisco.dnac.device_credential_intent:
dnac_host: "{{ dnac_host }}"
dnac_port: "{{ dnac_port }}"
dnac_username: "{{ dnac_username }}"
dnac_password: "{{ dnac_password }}"
dnac_verify: "{{ dnac_verify }}"
dnac_debug: "{{ dnac_debug }}"
dnac_log: True
dnac_log_level: "{{ dnac_log_level }}"
state: merged
config_verify: True
config:
- global_credential_details:
cli_credential:
- description: string
username: string
password: string
enable_password: string
- description: string
username: string
password: string
enable_password: string
snmp_v2c_read:
- description: string
read_community: string
- description: string
read_community: string
snmp_v2c_write:
- description: string
write_community: string
- description: string
write_community: string
snmp_v3:
- auth_password: string
auth_type: SHA
snmp_mode: AUTHPRIV
privacy_password: string
privacy_type: AES128
username: string
description: string
- auth_password: string
auth_type: SHA
snmp_mode: AUTHPRIV
privacy_password: string
privacy_type: AES128
username: string
description: string
https_read:
- description: string
username: string
password: string
port: 443
- description: string
username: string
password: string
port: 443
https_write:
- description: string
username: string
password: string
port: 443
- description: string
username: string
password: string
port: 443
- name: Update global device credentials using id
cisco.dnac.device_credential_intent:
dnac_host: "{{ dnac_host }}"
dnac_port: "{{ dnac_port }}"
dnac_username: "{{ dnac_username }}"
dnac_password: "{{ dnac_password }}"
dnac_verify: "{{ dnac_verify }}"
dnac_debug: "{{ dnac_debug }}"
dnac_log: True
dnac_log_level: "{{ dnac_log_level }}"
state: merged
config_verify: True
config:
- global_credential_details:
cli_credential:
- description: string
username: string
password: string
enable_password: string
id: string
snmp_v2c_read:
- description: string
read_community: string
id: string
snmp_v2c_write:
- description: string
write_community: string
id: string
snmp_v3:
- auth_password: string
auth_type: SHA
snmp_mode: AUTHPRIV
privacy_password: string
privacy_type: AES128
username: string
description: string
id: string
https_read:
- description: string
username: string
password: string
port: 443
id: string
https_write:
- description: string
username: string
password: string
port: 443
id: string
- name: Update multiple global device credentials using id
cisco.dnac.device_credential_intent:
dnac_host: "{{ dnac_host }}"
dnac_port: "{{ dnac_port }}"
dnac_username: "{{ dnac_username }}"
dnac_password: "{{ dnac_password }}"
dnac_verify: "{{ dnac_verify }}"
dnac_debug: "{{ dnac_debug }}"
dnac_log: True
dnac_log_level: "{{ dnac_log_level }}"
state: merged
config_verify: True
config:
- global_credential_details:
cli_credential:
- description: string
username: string
password: string
enable_password: string
id: string
- description: string
username: string
password: string
enable_password: string
id: string
snmp_v2c_read:
- description: string
read_community: string
id: string
- description: string
read_community: string
id: string
snmp_v2c_write:
- description: string
write_community: string
id: string
- description: string
write_community: string
id: string
snmp_v3:
- auth_password: string
auth_type: SHA
snmp_mode: AUTHPRIV
privacy_password: string
privacy_type: AES128
username: string
description: string
id: string
- auth_password: string
auth_type: SHA
snmp_mode: AUTHPRIV
privacy_password: string
privacy_type: AES128
username: string
description: string
id: string
https_read:
- description: string
username: string
password: string
port: 443
id: string
- description: string
username: string
password: string
port: 443
id: string
https_write:
- description: string
username: string
password: string
port: 443
id: string
- description: string
username: string
password: string
port: 443
id: string
- name: Update global device credential name/description using old name and description.
cisco.dnac.device_credential_intent:
dnac_host: "{{ dnac_host }}"
dnac_port: "{{ dnac_port }}"
dnac_username: "{{ dnac_username }}"
dnac_password: "{{ dnac_password }}"
dnac_verify: "{{ dnac_verify }}"
dnac_debug: "{{ dnac_debug }}"
dnac_log: True
dnac_log_level: "{{ dnac_log_level }}"
state: merged
config_verify: True
config:
- global_credential_details:
cli_credential:
- description: string
username: string
password: string
enable_password: string
old_description: string
old_username: string
snmp_v2c_read:
- description: string
read_community: string
old_description: string
snmp_v2c_write:
- description: string
write_community: string
old_description: string
snmp_v3:
- auth_password: string
auth_type: string
snmp_mode: string
privacy_password: string
privacy_type: string
username: string
description: string
https_read:
- description: string
username: string
password: string
port: string
old_description: string
old_username: string
https_write:
- description: string
username: string
password: string
port: string
old_description: string
old_username: string
- name: Assign Credentials to sites using old description and username.
cisco.dnac.device_credential_intent:
dnac_host: "{{ dnac_host }}"
dnac_port: "{{ dnac_port }}"
dnac_username: "{{ dnac_username }}"
dnac_password: "{{ dnac_password }}"
dnac_verify: "{{ dnac_verify }}"
dnac_debug: "{{ dnac_debug }}"
dnac_log: True
dnac_log_level: "{{ dnac_log_level }}"
state: merged
config_verify: True
config:
- assign_credentials_to_site:
cli_credential:
description: string
username: string
snmp_v2c_read:
description: string
snmp_v2c_write:
description: string
snmp_v3:
description: string
https_read:
description: string
username: string
https_write:
description: string
username: string
site_name:
- string
- string
"""
RETURN = r"""
# Case_1: Successful creation/updation/deletion of global device credentials
dnac_response1:
description: A dictionary or list with the response returned by the Cisco DNAC Python SDK
returned: always
type: dict
sample: >
{
"response": {
"taskId": "string",
"url": "string"
},
"version": "string"
}
# Case_2: Successful assignment of global device credentials to a site.
dnac_response2:
description: A dictionary or list with the response returned by the Cisco DNAC Python SDK
returned: always
type: dict
sample: >
{
"response": {
"taskId": "string",
"url": "string"
},
"version": "string"
}
"""
import copy
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.cisco.dnac.plugins.module_utils.dnac import (
DnacBase,
validate_list_of_dicts,
get_dict_result,
)
class DnacCredential(DnacBase):
"""Class containing member attributes for device credential intent module"""
def __init__(self, module):
super().__init__(module)
self.result["response"] = [
{
"globalCredential": {},
"assignCredential": {}
}
]
def validate_input(self):
"""
Validate the fields provided in the playbook.
Checks the configuration provided in the playbook against a predefined specification
to ensure it adheres to the expected structure and data types.
Parameters:
self: The instance of the class containing the 'config' attribute to be validated.
Returns:
The method returns an instance of the class with updated attributes:
- self.msg: A message describing the validation result.
- self.status: The status of the validation (either 'success' or 'failed').
- self.validated_config: If successful, a validated version of 'config' parameter.
Example:
To use this method, create an instance of the class and call 'validate_input' on it.
If the validation succeeds, 'self.status' will be 'success' and 'self.validated_config'
will contain the validated configuration. If it fails, 'self.status' will be 'failed',
'self.msg' will describe the validation issues.
"""
if not self.config:
self.msg = "config not available in playbook for validation"
self.status = "success"
return self
# temp_spec is the specification for the expected structure of configuration parameters
temp_spec = {
"global_credential_details": {
"type": 'dict',
"cli_credential": {
"type": 'list',
"description": {"type": 'string'},
"username": {"type": 'string'},
"password": {"type": 'string'},
"enable_password": {"type": 'string'},
"old_description": {"type": 'string'},
"old_username": {"type": 'string'},
"id": {"type": 'string'},
},
"snmp_v2c_read": {
"type": 'list',
"description": {"type": 'string'},
"read_community": {"type": 'string'},
"old_description": {"type": 'string'},
"id": {"type": 'string'},
},
"snmp_v2c_write": {
"type": 'list',
"description": {"type": 'string'},
"write_community": {"type": 'string'},
"old_description": {"type": 'string'},
"id": {"type": 'string'},
},
"snmp_v3": {
"type": 'list',
"description": {"type": 'string'},
"username": {"type": 'string'},
"snmp_mode": {"type": 'string'},
"auth_type": {"type": 'string'},
"auth_password": {"type": 'string'},
"privacy_type": {"type": 'string'},
"privacy_password": {"type": 'string'},
"old_description": {"type": 'string'},
"id": {"type": 'string'},
},
"https_read": {
"type": 'list',
"description": {"type": 'string'},
"username": {"type": 'string'},
"password": {"type": 'string'},
"port": {"type": 'integer'},
"old_description": {"type": 'string'},
"old_username": {"type": 'string'},
"id": {"type": 'string'},
},
"https_write": {
"type": 'list',
"description": {"type": 'string'},
"username": {"type": 'string'},
"password": {"type": 'string'},
"port": {"type": 'integer'},
"old_description": {"type": 'string'},
"old_username": {"type": 'string'},
"id": {"type": 'string'},
}
},
"assign_credentials_to_site": {
"type": 'dict',
"cli_credential": {
"type": 'dict',
"description": {"type: 'string'"},
"username": {"type": 'string'},
"id": {"type": 'string'},
},
"snmp_v2c_read": {
"type": 'dict',
"description": {"type: 'string'"},
"username": {"type": 'string'},
"id": {"type": 'string'},
},
"snmp_v2c_write": {
"type": 'dict',
"description": {"type: 'string'"},
"id": {"type": 'string'},
},
"snmp_v3": {
"type": 'dict',
"description": {"type: 'string'"},
"id": {"type": 'string'},
},
"https_read": {
"type": 'dict',
"description": {"type: 'string'"},
"username": {"type": 'string'},
"id": {"type": 'string'},
},
"https_write": {
"type": 'dict',
"description": {"type: 'string'"},
"username": {"type": 'string'},
"id": {"type": 'string'},
},
"site_name": {
"type": 'list',
"elements": 'string'
}
}
}
# Validate playbook params against the specification (temp_spec)
self.config = self.camel_to_snake_case(self.config)
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("Successfully validated playbook config params: {0}".format(valid_temp), "INFO")
self.msg = "Successfully validated input from the playbook"
self.status = "success"
return self
def get_site_id(self, site_name):
"""
Get the site id from the site name.
Use check_return_status() to check for failure
Parameters:
site_name (str) - Site name
Returns:
str or None - The Site Id if found, or None if not found or error
"""
try:
response = self.dnac._exec(
family="sites",
function='get_site',
op_modifies=True,
params={"name": site_name},
)
self.log("Received API response from 'get_site': {0}".format(response), "DEBUG")
if not response:
self.log("Failed to retrieve the site ID for the site name: {0}"
.format(site_name), "ERROR")
return None
_id = response.get("response")[0].get("id")
self.log("Site ID for the site name {0}: {1}".format(site_name, _id), "INFO")
except Exception as e:
self.log("Exception occurred while getting site_id from the site_name: {0}"
.format(e), "CRITICAL")
return None
return _id
def get_global_credentials_params(self):
"""
Get the current Global Device Credentials from Cisco DNA Center.
Parameters:
self - The current object details.
Returns:
global_credentials (dict) - All global device credentials details.
"""
try:
global_credentials = self.dnac._exec(
family="discovery",
function='get_all_global_credentials_v2',
)
global_credentials = global_credentials.get("response")
self.log("All global device credentials details: {0}"
.format(global_credentials), "DEBUG")
except Exception as e:
self.log("Exception occurred while getting global device credentials: {0}"
.format(e), "CRITICAL")
return None
return global_credentials
def get_cli_params(self, cliDetails):
"""
Format the CLI parameters for the CLI credential configuration in Cisco DNA Center.
Parameters:
cliDetails (list of dict) - Cisco DNA Center details containing CLI Credentials.
Returns:
cliCredential (list of dict) - Processed CLI credential data
in the format suitable for the Cisco DNA Center config.
"""
cliCredential = []
for item in cliDetails:
if item is None:
cliCredential.append(None)
else:
value = {
"username": item.get("username"),
"description": item.get("description"),
"id": item.get("id")
}
cliCredential.append(value)
return cliCredential
def get_snmpV2cRead_params(self, snmpV2cReadDetails):
"""
Format the snmpV2cRead parameters for the snmpV2cRead
credential configuration in Cisco DNA Center.
Parameters:
snmpV2cReadDetails (list of dict) - Cisco DNA Center
Details containing snmpV2cRead Credentials.
Returns:
snmpV2cRead (list of dict) - Processed snmpV2cRead credential
data in the format suitable for the Cisco DNA Center config.
"""
snmpV2cRead = []
for item in snmpV2cReadDetails:
if item is None:
snmpV2cRead.append(None)
else:
value = {
"description": item.get("description"),
"id": item.get("id")
}
snmpV2cRead.append(value)
return snmpV2cRead
def get_snmpV2cWrite_params(self, snmpV2cWriteDetails):
"""
Format the snmpV2cWrite parameters for the snmpV2cWrite
credential configuration in Cisco DNA Center.
Parameters:
snmpV2cWriteDetails (list of dict) - Cisco DNA Center
Details containing snmpV2cWrite Credentials.
Returns:
snmpV2cWrite (list of dict) - Processed snmpV2cWrite credential
data in the format suitable for the Cisco DNA Center config.
"""
snmpV2cWrite = []
for item in snmpV2cWriteDetails:
if item is None:
snmpV2cWrite.append(None)
else:
value = {
"description": item.get("description"),
"id": item.get("id")
}
snmpV2cWrite.append(value)
return snmpV2cWrite
def get_httpsRead_params(self, httpsReadDetails):
"""
Format the httpsRead parameters for the httpsRead
credential configuration in Cisco DNA Center.
Parameters:
httpsReadDetails (list of dict) - Cisco DNA Center
Details containing httpsRead Credentials.
Returns:
httpsRead (list of dict) - Processed httpsRead credential
data in the format suitable for the Cisco DNA Center config.
"""
httpsRead = []
for item in httpsReadDetails:
if item is None:
httpsRead.append(None)
else:
value = {
"description": item.get("description"),
"username": item.get("username"),
"port": item.get("port"),
"id": item.get("id")
}
httpsRead.append(value)
return httpsRead
def get_httpsWrite_params(self, httpsWriteDetails):
"""
Format the httpsWrite parameters for the httpsWrite
credential configuration in Cisco DNA Center.
Parameters:
httpsWriteDetails (list of dict) - Cisco DNA Center
Details containing httpsWrite Credentials.
Returns:
httpsWrite (list of dict) - Processed httpsWrite credential
data in the format suitable for the Cisco DNA Center config.
"""
httpsWrite = []
for item in httpsWriteDetails:
if item is None:
httpsWrite.append(None)
else:
value = {
"description": item.get("description"),
"username": item.get("username"),
"port": item.get("port"),
"id": item.get("id")
}
httpsWrite.append(value)
return httpsWrite
def get_snmpV3_params(self, snmpV3Details):
"""
Format the snmpV3 parameters for the snmpV3 credential configuration in Cisco DNA Center.
Parameters:
snmpV3Details (list of dict) - Cisco DNA Center details containing snmpV3 Credentials.
Returns:
snmpV3 (list of dict) - Processed snmpV3 credential
data in the format suitable for the Cisco DNA Center config.
"""
snmpV3 = []
for item in snmpV3Details:
if item is None:
snmpV3.append(None)
else:
value = {
"username": item.get("username"),
"description": item.get("description"),
"snmpMode": item.get("snmpMode"),
"id": item.get("id"),
}
if value.get("snmpMode") == "AUTHNOPRIV":
value["authType"] = item.get("authType")
elif value.get("snmpMode") == "AUTHPRIV":
value.update({
"authType": item.get("authType"),
"privacyType": item.get("privacyType")
})
snmpV3.append(value)
return snmpV3
def get_cli_credentials(self, CredentialDetails, global_credentials):
"""
Get the current CLI Credential from
Cisco DNA Center based on the provided playbook details.
Check this API using the check_return_status.
Parameters:
CredentialDetails (dict) - Playbook details containing Global Device Credentials.
global_credentials (dict) - All global device credentials details.
Returns:
cliDetails (List) - The current CLI credentials.
"""
# playbook CLI Credential details
all_CLI = CredentialDetails.get("cli_credential")
# All CLI details from Cisco DNA Center
cli_details = global_credentials.get("cliCredential")
# Cisco DNA Center details for the CLI Credential given in the playbook
cliDetails = []
if all_CLI and cli_details:
for cliCredential in all_CLI:
cliDetail = None
cliId = cliCredential.get("id")
if cliId:
cliDetail = get_dict_result(cli_details, "id", cliId)
if not cliDetail:
self.msg = "CLI credential ID is invalid"
self.status = "failed"
return self
cliOldDescription = cliCredential.get("old_description")
cliOldUsername = cliCredential.get("old_username")
if cliOldDescription and cliOldUsername and (not cliDetail):
for item in cli_details:
if item.get("description") == cliOldDescription \
and item.get("username") == cliOldUsername:
if cliDetail:
self.msg = "More than one CLI credential with same \
old_description and old_username. Pass ID."
self.status = "failed"
return self
cliDetail = item
if not cliDetail:
self.msg = "CLI credential old_description or old_username is invalid"
self.status = "failed"
return self
cliDescription = cliCredential.get("description")
cliUsername = cliCredential.get("username")
if cliDescription and cliUsername and (not cliDetail):
for item in cli_details:
if item.get("description") == cliDescription \
and item.get("username") == cliUsername:
if cliDetail:
self.msg = "More than one CLI Credential with same \
description and username. Pass ID."
self.status = "failed"
return self
cliDetail = item
cliDetails.append(cliDetail)
return cliDetails
def get_snmpV2cRead_credentials(self, CredentialDetails, global_credentials):
"""
Get the current snmpV2cRead Credential from
Cisco DNA Center based on the provided playbook details.
Check this API using the check_return_status.
Parameters:
CredentialDetails (dict) - Playbook details containing Global Device Credentials.
global_credentials (dict) - All global device credentials details.
Returns:
snmpV2cReadDetails (List) - The current snmpV2cRead.
"""
# Playbook snmpV2cRead Credential details
all_snmpV2cRead = CredentialDetails.get("snmp_v2c_read")
# All snmpV2cRead details from the Cisco DNA Center
snmpV2cRead_details = global_credentials.get("snmpV2cRead")
# Cisco DNA Center details for the snmpV2cRead Credential given in the playbook
snmpV2cReadDetails = []
if all_snmpV2cRead and snmpV2cRead_details:
for snmpV2cReadCredential in all_snmpV2cRead:
snmpV2cReadDetail = None
snmpV2cReadId = snmpV2cReadCredential.get("id")
if snmpV2cReadId:
snmpV2cReadDetail = get_dict_result(snmpV2cRead_details, "id", snmpV2cReadId)
if not snmpV2cReadDetail:
self.msg = "snmpV2cRead credential ID is invalid"
self.status = "failed"
return self
snmpV2cReadOldDescription = snmpV2cReadCredential.get("old_description")
if snmpV2cReadOldDescription and (not snmpV2cReadDetail):
snmpV2cReadDetail = get_dict_result(
snmpV2cRead_details,
"description",
snmpV2cReadOldDescription
)
if not snmpV2cReadDetail:
self.msg = "snmpV2cRead credential old_description is invalid"
self.status = "failed"
return self
snmpV2cReadDescription = snmpV2cReadCredential.get("description")
if snmpV2cReadDescription and (not snmpV2cReadDetail):
snmpV2cReadDetail = get_dict_result(
snmpV2cRead_details,
"description",
snmpV2cReadDescription
)
snmpV2cReadDetails.append(snmpV2cReadDetail)
return snmpV2cReadDetails
def get_snmpV2cWrite_credentials(self, CredentialDetails, global_credentials):
"""
Get the current snmpV2cWrite Credential from
Cisco DNA Center based on the provided playbook details.
Check this API using the check_return_status.
Parameters:
CredentialDetails (dict) - Playbook details containing Global Device Credentials.
global_credentials (dict) - All global device credentials details.
Returns:
snmpV2cWriteDetails (List) - The current snmpV2cWrite.
"""
# Playbook snmpV2cWrite Credential details
all_snmpV2cWrite = CredentialDetails.get("snmp_v2c_write")
# All snmpV2cWrite details from the Cisco DNA Center
snmpV2cWrite_details = global_credentials.get("snmpV2cWrite")
# Cisco DNA Center details for the snmpV2cWrite Credential given in the playbook
snmpV2cWriteDetails = []
if all_snmpV2cWrite and snmpV2cWrite_details:
for snmpV2cWriteCredential in all_snmpV2cWrite:
snmpV2cWriteDetail = None
snmpV2cWriteId = snmpV2cWriteCredential.get("id")
if snmpV2cWriteId:
snmpV2cWriteDetail = get_dict_result(snmpV2cWrite_details, "id", snmpV2cWriteId)
if not snmpV2cWriteDetail:
self.msg = "snmpV2cWrite credential ID is invalid"
self.status = "failed"
return self
snmpV2cWriteOldDescription = snmpV2cWriteCredential.get("old_description")
if snmpV2cWriteOldDescription and (not snmpV2cWriteDetail):
snmpV2cWriteDetail = get_dict_result(
snmpV2cWrite_details,
"description",
snmpV2cWriteOldDescription
)
if not snmpV2cWriteDetail:
self.msg = "snmpV2cWrite credential old_description is invalid "
self.status = "failed"
return self
snmpV2cWriteDescription = snmpV2cWriteCredential.get("description")
if snmpV2cWriteDescription and (not snmpV2cWriteDetail):
snmpV2cWriteDetail = get_dict_result(
snmpV2cWrite_details,
"description",
snmpV2cWriteDescription
)
snmpV2cWriteDetails.append(snmpV2cWriteDetail)
return snmpV2cWriteDetails
def get_httpsRead_credentials(self, CredentialDetails, global_credentials):
"""
Get the current httpsRead Credential from
Cisco DNA Center based on the provided playbook details.
Check this API using the check_return_status.
Parameters:
CredentialDetails (dict) - Playbook details containing Global Device Credentials.
global_credentials (dict) - All global device credentials details.
Returns:
httpsReadDetails (List) - The current httpsRead.
"""
# Playbook httpsRead Credential details
all_httpsRead = CredentialDetails.get("https_read")
# All httpsRead details from the Cisco DNA Center
httpsRead_details = global_credentials.get("httpsRead")
# Cisco DNA Center details for the httpsRead Credential given in the playbook
httpsReadDetails = []
if all_httpsRead and httpsRead_details:
for httpsReadCredential in all_httpsRead:
httpsReadDetail = None
httpsReadId = httpsReadCredential.get("id")
if httpsReadId:
httpsReadDetail = get_dict_result(httpsRead_details, "id", httpsReadId)
if not httpsReadDetail:
self.msg = "httpsRead credential Id is invalid"
self.status = "failed"
return self
httpsReadOldDescription = httpsReadCredential.get("old_description")
httpsReadOldUsername = httpsReadCredential.get("old_username")
if httpsReadOldDescription and httpsReadOldUsername and (not httpsReadDetail):
for item in httpsRead_details:
if item.get("description") == httpsReadOldDescription \
and item.get("username") == httpsReadOldUsername:
if httpsReadDetail:
self.msg = "More than one httpsRead credential with same \
old_description and old_username. Pass ID."
self.status = "failed"
return self
httpsReadDetail = item
if not httpsReadDetail:
self.msg = "httpsRead credential old_description or old_username is invalid"
self.status = "failed"
return self
httpsReadDescription = httpsReadCredential.get("description")
httpsReadUsername = httpsReadCredential.get("username")
if httpsReadDescription and httpsReadUsername and (not httpsReadDetail):
for item in httpsRead_details:
if item.get("description") == httpsReadDescription \
and item.get("username") == httpsReadUsername:
if httpsReadDetail:
self.msg = "More than one httpsRead credential with same \
description and username. Pass ID."
self.status = "failed"
return self
httpsReadDetail = item
httpsReadDetails.append(httpsReadDetail)
return httpsReadDetails
def get_httpsWrite_credentials(self, CredentialDetails, global_credentials):
"""
Get the current httpsWrite Credential from
Cisco DNA Center based on the provided playbook details.
Check this API using the check_return_status.
Parameters:
CredentialDetails (dict) - Playbook details containing Global Device Credentials.
global_credentials (dict) - All global device credentials details.
Returns:
httpsWriteDetails (List) - The current httpsWrite.
"""
# Playbook httpsWrite Credential details
all_httpsWrite = CredentialDetails.get("https_write")
# All httpsWrite details from the Cisco DNA Center
httpsWrite_details = global_credentials.get("httpsWrite")
# Cisco DNA Center details for the httpsWrite Credential given in the playbook
httpsWriteDetails = []
if all_httpsWrite and httpsWrite_details:
for httpsWriteCredential in all_httpsWrite:
httpsWriteDetail = None
httpsWriteId = httpsWriteCredential.get("id")
if httpsWriteId:
httpsWriteDetail = get_dict_result(httpsWrite_details, "id", httpsWriteId)
if not httpsWriteDetail:
self.msg = "httpsWrite credential Id is invalid"
self.status = "failed"
return self
httpsWriteOldDescription = httpsWriteCredential.get("old_description")
httpsWriteOldUsername = httpsWriteCredential.get("old_username")
if httpsWriteOldDescription and httpsWriteOldUsername and (not httpsWriteDetail):
for item in httpsWrite_details:
if item.get("description") == httpsWriteOldDescription \
and item.get("username") == httpsWriteOldUsername:
if httpsWriteDetail:
self.msg = "More than one httpsWrite credential with same \
old_description and old_username. Pass ID"
self.status = "failed"
return self
httpsWriteDetail = item
if not httpsWriteDetail:
self.msg = "httpsWrite credential old_description or \
old_username is invalid"
self.status = "failed"
return self
httpsWriteDescription = httpsWriteCredential.get("description")
httpsWriteUsername = httpsWriteCredential.get("username")
if httpsWriteDescription and httpsWriteUsername and (not httpsWriteDetail):
for item in httpsWrite_details:
if item.get("description") == httpsWriteDescription \
and item.get("username") == httpsWriteUsername:
httpsWriteDetail = item
httpsWriteDetails.append(httpsWriteDetail)
return httpsWriteDetails
def get_snmpV3_credentials(self, CredentialDetails, global_credentials):
"""
Get the current snmpV3 Credential from
Cisco DNA Center based on the provided playbook details.
Check this API using the check_return_status.
Parameters:
CredentialDetails (dict) - Playbook details containing Global Device Credentials.
global_credentials (dict) - All global device credentials details.
Returns:
snmpV3Details (List) - The current snmpV3.
"""
# Playbook snmpV3 Credential details
all_snmpV3 = CredentialDetails.get("snmp_v3")
# All snmpV3 details from the Cisco DNA Center
snmpV3_details = global_credentials.get("snmpV3")
# Cisco DNA Center details for the snmpV3 Credential given in the playbook
snmpV3Details = []
if all_snmpV3 and snmpV3_details:
for snmpV3Credential in all_snmpV3:
snmpV3Detail = None
snmpV3Id = snmpV3Credential.get("id")
if snmpV3Id:
snmpV3Detail = get_dict_result(snmpV3_details, "id", snmpV3Id)
if not snmpV3Detail:
self.msg = "snmpV3 credential id is invalid"
self.status = "failed"
return self
snmpV3OldDescription = snmpV3Credential.get("old_description")
if snmpV3OldDescription and (not snmpV3Detail):
snmpV3Detail = get_dict_result(snmpV3_details,
"description", snmpV3OldDescription)
if not snmpV3Detail:
self.msg = "snmpV3 credential old_description is invalid"
self.status = "failed"
return self
snmpV3Description = snmpV3Credential.get("description")
if snmpV3Description and (not snmpV3Detail):
snmpV3Detail = get_dict_result(snmpV3_details, "description", snmpV3Description)
snmpV3Details.append(snmpV3Detail)
return snmpV3Details
def get_have_device_credentials(self, CredentialDetails):
"""
Get the current Global Device Credentials from
Cisco DNA Center based on the provided playbook details.
Check this API using the check_return_status.
Parameters:
CredentialDetails (dict) - Playbook details containing Global Device Credentials.
Returns:
self - The current object with updated information.
"""
global_credentials = self.get_global_credentials_params()
cliDetails = self.get_cli_credentials(CredentialDetails, global_credentials)
snmpV2cReadDetails = self.get_snmpV2cRead_credentials(CredentialDetails, global_credentials)
snmpV2cWriteDetails = self.get_snmpV2cWrite_credentials(CredentialDetails,
global_credentials)
httpsReadDetails = self.get_httpsRead_credentials(CredentialDetails, global_credentials)
httpsWriteDetails = self.get_httpsWrite_credentials(CredentialDetails, global_credentials)
snmpV3Details = self.get_snmpV3_credentials(CredentialDetails, global_credentials)
self.have.update({"globalCredential": {}})
if cliDetails:
cliCredential = self.get_cli_params(cliDetails)
self.have.get("globalCredential").update({"cliCredential": cliCredential})
if snmpV2cReadDetails:
snmpV2cRead = self.get_snmpV2cRead_params(snmpV2cReadDetails)
self.have.get("globalCredential").update({"snmpV2cRead": snmpV2cRead})
if snmpV2cWriteDetails:
snmpV2cWrite = self.get_snmpV2cWrite_params(snmpV2cWriteDetails)
self.have.get("globalCredential").update({"snmpV2cWrite": snmpV2cWrite})
if httpsReadDetails:
httpsRead = self.get_httpsRead_params(httpsReadDetails)
self.have.get("globalCredential").update({"httpsRead": httpsRead})
if httpsWriteDetails:
httpsWrite = self.get_httpsWrite_params(httpsWriteDetails)
self.have.get("globalCredential").update({"httpsWrite": httpsWrite})
if snmpV3Details:
snmpV3 = self.get_snmpV3_params(snmpV3Details)
self.have.get("globalCredential").update({"snmpV3": snmpV3})
self.log("Global device credential details: {0}"
.format(self.have.get("globalCredential")), "DEBUG")
self.msg = "Collected the Global Device Credential Details from the Cisco DNA Center"
self.status = "success"
return self
def get_have(self, config):
"""
Get the current Global Device Credentials and
Device Credentials assigned to a site in Cisco DNA Center.
Parameters:
config (dict) - Playbook details containing Global Device
Credentials configurations and Device Credentials should
be assigned to a site.
Returns:
self - The current object with updated information of Global
Device Credentials and Device Credentials assigned to a site.
"""
if config.get("global_credential_details") is not None:
CredentialDetails = config.get("global_credential_details")
self.get_have_device_credentials(CredentialDetails).check_return_status()
self.log("Current State (have): {0}".format(self.have), "INFO")
self.msg = "Successfully retrieved the details from the Cisco DNA Center"
self.status = "success"
return self
def get_want_device_credentials(self, CredentialDetails):
"""
Get the Global Device Credentials from the playbook.
Check this API using the check_return_status.
Parameters:
CredentialDetails (dict) - Playbook details containing Global Device Credentials.
Returns:
self - The current object with updated information of
Global Device Credentials from the playbook.
"""
want = {
"want_create": {},
"want_update": {}
}
if CredentialDetails.get("cli_credential"):
cli = CredentialDetails.get("cli_credential")
have_cli_ptr = 0
create_cli_ptr = 0
update_cli_ptr = 0
values = ["password", "description", "username", "id"]
have_cliCredential = self.have.get("globalCredential").get("cliCredential")
for item in cli:
if not have_cliCredential or have_cliCredential[have_cli_ptr] is None:
if want.get("want_create").get("cliCredential") is None:
want.get("want_create").update({"cliCredential": []})
create_credential = want.get("want_create").get("cliCredential")
create_credential.append({})
for i in range(0, 3):
if item.get(values[i]):
create_credential[create_cli_ptr] \
.update({values[i]: item.get(values[i])})
else:
self.msg = values[i] + " is mandatory for creating \
cliCredential " + str(have_cli_ptr)
self.status = "failed"
return self
if item.get("enable_password"):
create_credential[create_cli_ptr] \
.update({"enablePassword": item.get("enable_password")})
create_cli_ptr = create_cli_ptr + 1
else:
if want.get("want_update").get("cliCredential") is None:
want.get("want_update").update({"cliCredential": []})
update_credential = want.get("want_update").get("cliCredential")
update_credential.append({})
if item.get("password"):
update_credential[update_cli_ptr] \
.update({"password": item.get("password")})
else:
self.msg = "password is mandatory for udpating \
cliCredential " + str(have_cli_ptr)
self.status = "failed"
return self
for i in range(1, 4):
if item.get(values[i]):
update_credential[update_cli_ptr] \
.update({values[i]: item.get(values[i])})
else:
update_credential[update_cli_ptr].update({
values[i]: self.have.get("globalCredential")
.get("cliCredential")[have_cli_ptr].get(values[i])
})
if item.get("enable_password"):
update_credential[update_cli_ptr].update({
"enablePassword": item.get("enable_password")
})
update_cli_ptr = update_cli_ptr + 1
have_cli_ptr = have_cli_ptr + 1
if CredentialDetails.get("snmp_v2c_read"):
snmpV2cRead = CredentialDetails.get("snmp_v2c_read")
have_snmpv2cread_ptr = 0
create_snmpv2cread_ptr = 0
update_snmpv2cread_ptr = 0
values = ["read_community", "description", "id"]
keys = ["readCommunity", "description", "id"]
have_snmpV2cRead = self.have.get("globalCredential").get("snmpV2cRead")
for item in snmpV2cRead:
if not have_snmpV2cRead or have_snmpV2cRead[have_snmpv2cread_ptr] is None:
if want.get("want_create").get("snmpV2cRead") is None:
want.get("want_create").update({"snmpV2cRead": []})
create_credential = want.get("want_create").get("snmpV2cRead")
create_credential.append({})
for i in range(0, 2):
if item.get(values[i]):
create_credential[create_snmpv2cread_ptr] \
.update({keys[i]: item.get(values[i])})
else:
self.msg = values[i] + " is mandatory for creating \
snmpV2cRead " + str(have_snmpv2cread_ptr)
self.status = "failed"
return self
create_snmpv2cread_ptr = create_snmpv2cread_ptr + 1
else:
if want.get("want_update").get("snmpV2cRead") is None:
want.get("want_update").update({"snmpV2cRead": []})
update_credential = want.get("want_update").get("snmpV2cRead")
update_credential.append({})
if item.get("read_community"):
update_credential[update_snmpv2cread_ptr] \
.update({"readCommunity": item.get("read_community")})
else:
self.msg = "read_community is mandatory for updating \
snmpV2cRead " + str(have_snmpv2cread_ptr)
self.status = "failed"
return self
for i in range(1, 3):
if item.get(values[i]):
update_credential[update_snmpv2cread_ptr] \
.update({values[i]: item.get(values[i])})
else:
update_credential[update_snmpv2cread_ptr].update({
values[i]: self.have.get("globalCredential")
.get("snmpV2cRead")[have_snmpv2cread_ptr].get(values[i])
})
update_snmpv2cread_ptr = update_snmpv2cread_ptr + 1
have_snmpv2cread_ptr = have_snmpv2cread_ptr + 1
if CredentialDetails.get("snmp_v2c_write"):
snmpV2cWrite = CredentialDetails.get("snmp_v2c_write")
have_snmpv2cwrite_ptr = 0
create_snmpv2cwrite_ptr = 0
update_snmpv2cwrite_ptr = 0
values = ["write_community", "description", "id"]
keys = ["writeCommunity", "description", "id"]
have_snmpV2cWrite = self.have.get("globalCredential").get("snmpV2cWrite")
for item in snmpV2cWrite:
if not have_snmpV2cWrite or have_snmpV2cWrite[have_snmpv2cwrite_ptr] is None:
if want.get("want_create").get("snmpV2cWrite") is None:
want.get("want_create").update({"snmpV2cWrite": []})
create_credential = want.get("want_create").get("snmpV2cWrite")
create_credential.append({})
for i in range(0, 2):
if item.get(values[i]):
create_credential[create_snmpv2cwrite_ptr] \
.update({keys[i]: item.get(values[i])})
else:
self.msg = values[i] + " is mandatory for creating \
snmpV2cWrite " + str(have_snmpv2cwrite_ptr)
self.status = "failed"
return self
create_snmpv2cwrite_ptr = create_snmpv2cwrite_ptr + 1
else:
if want.get("want_update").get("snmpV2cWrite") is None:
want.get("want_update").update({"snmpV2cWrite": []})
update_credential = want.get("want_update").get("snmpV2cWrite")
update_credential.append({})
if item.get("write_community"):
update_credential[update_snmpv2cwrite_ptr] \
.update({"writeCommunity": item.get("write_community")})
else:
self.msg = "write_community is mandatory for updating \
snmpV2cWrite " + str(have_snmpv2cwrite_ptr)
self.status = "failed"
return self
for i in range(1, 3):
if item.get(values[i]):
update_credential[update_snmpv2cwrite_ptr] \
.update({values[i]: item.get(values[i])})
else:
update_credential[update_snmpv2cwrite_ptr].update({
values[i]: self.have.get("globalCredential")
.get("snmpV2cWrite")[have_snmpv2cwrite_ptr].get(values[i])
})
update_snmpv2cwrite_ptr = update_snmpv2cwrite_ptr + 1
have_snmpv2cwrite_ptr = have_snmpv2cwrite_ptr + 1
if CredentialDetails.get("https_read"):
httpsRead = CredentialDetails.get("https_read")
have_httpsread_ptr = 0
create_httpsread_ptr = 0
update_httpsread_ptr = 0
values = ["password", "description", "username", "id", "port"]
have_httpsRead = self.have.get("globalCredential").get("httpsRead")
for item in httpsRead:
self.log("Global credentials details: {0}"
.format(self.have.get("globalCredential")), "DEBUG")
if not have_httpsRead or have_httpsRead[have_httpsread_ptr] is None:
if want.get("want_create").get("httpsRead") is None:
want.get("want_create").update({"httpsRead": []})
create_credential = want.get("want_create").get("httpsRead")
create_credential.append({})
for i in range(0, 3):
if item.get(values[i]):
create_credential[create_httpsread_ptr] \
.update({values[i]: item.get(values[i])})
else:
self.msg = values[i] + " is mandatory for creating \
httpsRead " + str(have_httpsread_ptr)
self.status = "failed"
return self
if item.get("port"):
create_credential[create_httpsread_ptr] \
.update({"port": item.get("port")})
else:
create_credential[create_httpsread_ptr] \
.update({"port": "443"})
create_httpsread_ptr = create_httpsread_ptr + 1
else:
if want.get("want_update").get("httpsRead") is None:
want.get("want_update").update({"httpsRead": []})
update_credential = want.get("want_update").get("httpsRead")
update_credential.append({})
if item.get("password"):
update_credential[update_httpsread_ptr] \
.update({"password": item.get("password")})
else:
self.msg = "password is mandatory for updating \
httpsRead " + str(have_httpsread_ptr)
self.status = "failed"
return self
for i in range(1, 5):
if item.get(values[i]):
update_credential[update_httpsread_ptr] \
.update({values[i]: item.get(values[i])})
else:
update_credential[update_httpsread_ptr].update({
values[i]: self.have.get("globalCredential")
.get("httpsRead")[have_httpsread_ptr].get(values[i])
})
update_httpsread_ptr = update_httpsread_ptr + 1
have_httpsread_ptr = have_httpsread_ptr + 1
if CredentialDetails.get("https_write"):
httpsWrite = CredentialDetails.get("https_write")
have_httpswrite_ptr = 0
create_httpswrite_ptr = 0
update_httpswrite_ptr = 0
values = ["password", "description", "username", "id", "port"]
have_httpsWrite = self.have.get("globalCredential").get("httpsWrite")
for item in httpsWrite:
if not have_httpsWrite or have_httpsWrite[have_httpswrite_ptr] is None:
if want.get("want_create").get("httpsWrite") is None:
want.get("want_create").update({"httpsWrite": []})
create_credential = want.get("want_create").get("httpsWrite")
create_credential.append({})
for i in range(0, 3):
if item.get(values[i]):
create_credential[create_httpswrite_ptr] \
.update({values[i]: item.get(values[i])})
else:
self.msg = values[i] + " is mandatory for creating \
httpsWrite " + str(have_httpswrite_ptr)
self.status = "failed"
return self
if item.get("port"):
create_credential[create_httpswrite_ptr] \
.update({"port": item.get("port")})
else:
create_credential[create_httpswrite_ptr] \
.update({"port": "443"})
create_httpswrite_ptr = create_httpswrite_ptr + 1
else:
if want.get("want_update").get("httpsWrite") is None:
want.get("want_update").update({"httpsWrite": []})
update_credential = want.get("want_update").get("httpsWrite")
update_credential.append({})
if item.get("password"):
update_credential[update_httpswrite_ptr] \
.update({"password": item.get("password")})
else:
self.msg = "password is mandatory for updating \
httpsRead " + str(have_httpswrite_ptr)
self.status = "failed"
return self
for i in range(1, 5):
if item.get(values[i]):
update_credential[update_httpswrite_ptr] \
.update({values[i]: item.get(values[i])})
else:
update_credential[update_httpswrite_ptr].update({
values[i]: self.have.get("globalCredential")
.get("httpsWrite")[have_httpswrite_ptr].get(values[i])
})
update_httpswrite_ptr = update_httpswrite_ptr + 1
have_httpswrite_ptr = have_httpswrite_ptr + 1
if CredentialDetails.get("snmp_v3"):
snmpV3 = CredentialDetails.get("snmp_v3")
have_snmpv3_ptr = 0
create_snmpv3_ptr = 0
update_snmpv3_ptr = 0
values = ["description", "username", "id"]
have_snmpV3 = self.have.get("globalCredential").get("snmpV3")
for item in snmpV3:
if not have_snmpV3 or have_snmpV3[have_snmpv3_ptr] is None:
if want.get("want_create").get("snmpV3") is None:
want.get("want_create").update({"snmpV3": []})
create_credential = want.get("want_create").get("snmpV3")
create_credential.append({})
for i in range(0, 2):
if item.get(values[i]):
create_credential[create_snmpv3_ptr] \
.update({values[i]: item.get(values[i])})
else:
self.msg = values[i] + " is mandatory for creating \
snmpV3 " + str(have_snmpv3_ptr)
self.status = "failed"
return self
if item.get("snmp_mode"):
create_credential[create_snmpv3_ptr] \
.update({"snmpMode": item.get("snmp_mode")})
else:
create_credential[create_snmpv3_ptr] \
.update({"snmpMode": "AUTHPRIV"})
if create_credential[create_snmpv3_ptr].get("snmpMode") == "AUTHNOPRIV" or \
create_credential[create_snmpv3_ptr].get("snmpMode") == "AUTHPRIV":
auths = ["auth_password", "auth_type"]
keys = {
"auth_password": "authPassword",
"auth_type": "authType"
}
for auth in auths:
if item.get(auth):
create_credential[create_snmpv3_ptr] \
.update({keys[auth]: item.get(auth)})
else:
self.msg = auth + " is mandatory for creating \
snmpV3 " + str(have_snmpv3_ptr)
self.status = "failed"
return self
if len(item.get("auth_password")) < 8:
self.msg = "auth_password length should be greater than 8"
self.status = "failed"
return self
self.log("snmp_mode: {0}".format(create_credential[create_snmpv3_ptr]
.get("snmpMode")), "DEBUG")
if create_credential[create_snmpv3_ptr].get("snmpMode") == "AUTHPRIV":
privs = ["privacy_password", "privacy_type"]
key = {
"privacy_password": "privacyPassword",
"privacy_type": "privacyType"
}
for priv in privs:
if item.get(priv):
create_credential[create_snmpv3_ptr] \
.update({key[priv]: item.get(priv)})
else:
self.msg = priv + " is mandatory for creating \
snmpV3 " + str(have_snmpv3_ptr)
self.status = "failed"
return self
if len(item.get("privacy_password")) < 8:
self.msg = "privacy_password should be greater than 8"
self.status = "failed"
return self
elif create_credential[create_snmpv3_ptr].get("snmpMode") != "NOAUTHNOPRIV":
self.msg = "snmp_mode in snmpV3 is not \
['AUTHPRIV', 'AUTHNOPRIV', 'NOAUTHNOPRIV']"
self.status = "failed"
return self
create_snmpv3_ptr = create_snmpv3_ptr + 1
else:
if want.get("want_update").get("snmpV3") is None:
want.get("want_update").update({"snmpV3": []})
update_credential = want.get("want_update").get("snmpV3")
update_credential.append({})
for value in values:
if item.get(value):
update_credential[update_snmpv3_ptr] \
.update({value: item.get(value)})
else:
update_credential[update_snmpv3_ptr].update({
value: self.have.get("globalCredential")
.get("snmpV3")[have_snmpv3_ptr].get(value)
})
if item.get("snmp_mode"):
update_credential[update_snmpv3_ptr] \
.update({"snmpMode": item.get("snmp_mode")})
if update_credential[update_snmpv3_ptr].get("snmpMode") == "AUTHNOPRIV" or \
update_credential[update_snmpv3_ptr].get("snmpMode") == "AUTHPRIV":
if item.get("auth_type"):
update_credential[update_snmpv3_ptr] \
.update({"authType": item.get("auth_type")})
elif self.have.get("globalCredential") \
.get("snmpMode")[have_snmpv3_ptr].get("authType"):
update_credential[update_snmpv3_ptr].update({
"authType": self.have.get("globalCredential")
.get("snmpMode")[have_snmpv3_ptr].get("authType")
})
else:
self.msg = "auth_type is required for updating snmpV3 " + \
str(have_snmpv3_ptr)
self.status = "failed"
return self
if item.get("auth_password"):
update_credential[update_snmpv3_ptr] \
.update({"authPassword": item.get("auth_password")})
else:
self.msg = "auth_password is required for updating snmpV3 " + \
str(have_snmpv3_ptr)
self.status = "failed"
return self
if len(item.get("auth_password")) < 8:
self.msg = "auth_password length should be greater than 8"
self.status = "failed"
return self
elif update_credential[update_snmpv3_ptr].get("snmpMode") == "AUTHPRIV":
if item.get("privacy_type"):
update_credential[update_snmpv3_ptr] \
.update({"privacyType": item.get("privacy_type")})
elif self.have.get("globalCredential") \
.get("snmpMode")[have_snmpv3_ptr].get("privacyType"):
update_credential[update_snmpv3_ptr].update({
"privacyType": self.have.get("globalCredential")
.get("snmpMode")[have_snmpv3_ptr].get("privacyType")
})
else:
self.msg = "privacy_type is required for updating snmpV3 " + \
str(have_snmpv3_ptr)
self.status = "failed"
return self
if item.get("privacy_password"):
update_credential[update_snmpv3_ptr] \
.update({"privacyPassword": item.get("privacy_password")})
else:
self.msg = "privacy_password is required for updating snmpV3 " + \
str(have_snmpv3_ptr)
self.status = "failed"
return self
if len(item.get("privacy_password")) < 8:
self.msg = "privacy_password length should be greater than 8"
self.status = "failed"
return self
update_snmpv3_ptr = update_snmpv3_ptr + 1
have_snmpv3_ptr = have_snmpv3_ptr + 1
self.want.update(want)
self.msg = "Collected the Global Credentials from the Cisco DNA Center"
self.status = "success"
return self
def get_want_assign_credentials(self, AssignCredentials):
"""
Get the Credentials to be assigned to a site from the playbook.
Check this API using the check_return_status.
Parameters:
AssignCredentials (dict) - Playbook details containing
credentials that need to be assigned to a site.
Returns:
self - The current object with updated information of credentials
that need to be assigned to a site from the playbook.
"""
want = {
"assign_credentials": {}
}
site_name = AssignCredentials.get("site_name")
if not site_name:
self.msg = "site_name is required for AssignCredentials"
self.status = "failed"
return self
site_id = []
for site_name in site_name:
siteId = self.get_site_id(site_name)
if not site_name:
self.msg = "site_name is invalid in AssignCredentials"
self.status = "failed"
return self
site_id.append(siteId)
want.update({"site_id": site_id})
global_credentials = self.get_global_credentials_params()
cli_credential = AssignCredentials.get("cli_credential")
if cli_credential:
cliId = cli_credential.get("id")
cliDescription = cli_credential.get("description")
cliUsername = cli_credential.get("username")
if cliId or cliDescription and cliUsername:
# All CLI details from the Cisco DNA Center
cli_details = global_credentials.get("cliCredential")
if not cli_details:
self.msg = "Global CLI credential is not available"
self.status = "failed"
return self
cliDetail = None
if cliId:
cliDetail = get_dict_result(cli_details, "id", cliId)
if not cliDetail:
self.msg = "The ID for the CLI credential is not valid."
self.status = "failed"
return self
elif cliDescription and cliUsername:
for item in cli_details:
if item.get("description") == cliDescription and \
item.get("username") == cliUsername:
cliDetail = item
if not cliDetail:
self.msg = "The username and description of the CLI credential are invalid"
self.status = "failed"
return self
want.get("assign_credentials").update({"cliId": cliDetail.get("id")})
snmp_v2c_read = AssignCredentials.get("snmp_v2c_read")
if snmp_v2c_read:
snmpV2cReadId = snmp_v2c_read.get("id")
snmpV2cReadDescription = snmp_v2c_read.get("description")
if snmpV2cReadId or snmpV2cReadDescription:
# All snmpV2cRead details from the Cisco DNA Center
snmpV2cRead_details = global_credentials.get("snmpV2cRead")
if not snmpV2cRead_details:
self.msg = "Global snmpV2cRead credential is not available"
self.status = "failed"
return self
snmpV2cReadDetail = None
if snmpV2cReadId:
snmpV2cReadDetail = get_dict_result(snmpV2cRead_details, "id", snmpV2cReadId)
if not snmpV2cReadDetail:
self.msg = "The ID of the snmpV2cRead credential is not valid."
self.status = "failed"
return self
elif snmpV2cReadDescription:
for item in snmpV2cRead_details:
if item.get("description") == snmpV2cReadDescription:
snmpV2cReadDetail = item
if not snmpV2cReadDetail:
self.msg = "The username and description for the snmpV2cRead credential are invalid."
self.status = "failed"
return self
want.get("assign_credentials").update({"snmpV2ReadId": snmpV2cReadDetail.get("id")})
snmp_v2c_write = AssignCredentials.get("snmp_v2c_write")
if snmp_v2c_write:
snmpV2cWriteId = snmp_v2c_write.get("id")
snmpV2cWriteDescription = snmp_v2c_write.get("description")
if snmpV2cWriteId or snmpV2cWriteDescription:
# All snmpV2cWrite details from the Cisco DNA Center
snmpV2cWrite_details = global_credentials.get("snmpV2cWrite")
if not snmpV2cWrite_details:
self.msg = "Global snmpV2cWrite Credential is not available"
self.status = "failed"
return self
snmpV2cWriteDetail = None
if snmpV2cWriteId:
snmpV2cWriteDetail = get_dict_result(snmpV2cWrite_details, "id", snmpV2cWriteId)
if not snmpV2cWriteDetail:
self.msg = "The ID of the snmpV2cWrite credential is invalid."
self.status = "failed"
return self
elif snmpV2cWriteDescription:
for item in snmpV2cWrite_details:
if item.get("description") == snmpV2cWriteDescription:
snmpV2cWriteDetail = item
if not snmpV2cWriteDetail:
self.msg = "The username and description of the snmpV2cWrite credential are invalid."
self.status = "failed"
return self
want.get("assign_credentials").update({"snmpV2WriteId": snmpV2cWriteDetail.get("id")})
https_read = AssignCredentials.get("https_read")
if https_read:
httpReadId = https_read.get("id")
httpReadDescription = https_read.get("description")
httpReadUsername = https_read.get("username")
if httpReadId or httpReadDescription and httpReadUsername:
# All httpRead details from the Cisco DNA Center
httpRead_details = global_credentials.get("httpsRead")
if not httpRead_details:
self.msg = "Global httpRead Credential is not available."
self.status = "failed"
return self
httpReadDetail = None
if httpReadId:
httpReadDetail = get_dict_result(httpRead_details, "id", httpReadId)
if not httpReadDetail:
self.msg = "The ID of the httpRead credential is not valid."
self.status = "failed"
return self
elif httpReadDescription and httpReadUsername:
for item in httpRead_details:
if item.get("description") == httpReadDescription and \
item.get("username") == httpReadUsername:
httpReadDetail = item
if not httpReadDetail:
self.msg = "The description and username for the httpRead credential are invalid."
self.status = "failed"
return self
want.get("assign_credentials").update({"httpRead": httpReadDetail.get("id")})
https_write = AssignCredentials.get("https_write")
if https_write:
httpWriteId = https_write.get("id")
httpWriteDescription = https_write.get("description")
httpWriteUsername = https_write.get("username")
if httpWriteId or httpWriteDescription and httpWriteUsername:
# All httpWrite details from the Cisco DNA Center
httpWrite_details = global_credentials.get("httpsWrite")
if not httpWrite_details:
self.msg = "Global httpWrite credential is not available."
self.status = "failed"
return self
httpWriteDetail = None
if httpWriteId:
httpWriteDetail = get_dict_result(httpWrite_details, "id", httpWriteId)
if not httpWriteDetail:
self.msg = "The ID of the httpWrite credential is not valid."
self.status = "failed"
return self
elif httpWriteDescription and httpWriteUsername:
for item in httpWrite_details:
if item.get("description") == httpWriteDescription and \
item.get("username") == httpWriteUsername:
httpWriteDetail = item
if not httpWriteDetail:
self.msg = "The description and username for the httpWrite credential are invalid."
self.status = "failed"
return self
want.get("assign_credentials").update({"httpWrite": httpWriteDetail.get("id")})
snmp_v3 = AssignCredentials.get("snmp_v3")
if snmp_v3:
snmpV3Id = snmp_v3.get("id")
snmpV3Description = snmp_v3.get("description")
if snmpV3Id or snmpV3Description:
# All snmpV3 details from the Cisco DNA Center
snmpV3_details = global_credentials.get("snmpV3")
if not snmpV3_details:
self.msg = "Global snmpV3 Credential is not available."
self.status = "failed"
return self
snmpV3Detail = None
if snmpV3Id:
snmpV3Detail = get_dict_result(snmpV3_details, "id", snmpV3Id)
if not snmpV3Detail:
self.msg = "The ID of the snmpV3 credential is not valid."
self.status = "failed"
return self
elif snmpV3Description:
for item in snmpV3_details:
if item.get("description") == snmpV3Description:
snmpV3Detail = item
if not snmpV3Detail:
self.msg = "The username and description for the snmpV2cWrite credential are invalid."
self.status = "failed"
return self
want.get("assign_credentials").update({"snmpV3Id": snmpV3Detail.get("id")})
self.log("Desired State (want): {0}".format(want), "INFO")
self.want.update(want)
self.msg = "Collected the Credentials needed to be assigned from the Cisco DNA Center"
self.status = "success"
return self
def get_want(self, config):
"""
Get the current Global Device Credentials and Device
Credentials assigned to a site form the playbook.
Parameters:
config (dict) - Playbook details containing Global Device
Credentials configurations and Device Credentials should
be assigned to a site.
Returns:
self - The current object with updated information of Global
Device Credentials and Device Credentials assigned to a site.
"""
if config.get("global_credential_details"):
CredentialDetails = config.get("global_credential_details")
self.get_want_device_credentials(CredentialDetails).check_return_status()
if config.get("assign_credentials_to_site"):
AssignCredentials = config.get("assign_credentials_to_site")
self.get_want_assign_credentials(AssignCredentials).check_return_status()
self.log("Desired State (want): {0}".format(self.want), "INFO")
self.msg = "Successfully retrieved details from the playbook"
self.status = "success"
return self
def create_device_credentials(self):
"""
Create Global Device Credential to the Cisco DNA
Center based on the provided playbook details.
Check the return value of the API with check_return_status().
Parameters:
self
Returns:
self
"""
result_global_credential = self.result.get("response")[0].get("globalCredential")
want_create = self.want.get("want_create")
if not want_create:
result_global_credential.update({
"No Creation": {
"response": "No Response",
"msg": "No Creation is available"
}
})
return self
credential_params = want_create
self.log("Creating global credential API input parameters: {0}"
.format(credential_params), "DEBUG")
response = self.dnac._exec(
family="discovery",
function='create_global_credentials_v2',
op_modifies=True,
params=credential_params,
)
self.log("Received API response from 'create_global_credentials_v2': {0}"
.format(response), "DEBUG")
validation_string = "global credential addition performed"
self.check_task_response_status(response, validation_string).check_return_status()
self.log("Global credential created successfully", "INFO")
result_global_credential.update({
"Creation": {
"response": credential_params,
"msg": "Global Credential Created Successfully"
}
})
self.msg = "Global Device Credential Created Successfully"
self.status = "success"
return self
def update_device_credentials(self):
"""
Update Device Credential to the Cisco DNA Center based on the provided playbook details.
Check the return value of the API with check_return_status().
Parameters:
self
Returns:
self
"""
result_global_credential = self.result.get("response")[0].get("globalCredential")
# Get the result global credential and want_update from the current object
want_update = self.want.get("want_update")
# If no credentials to update, update the result and return
if not want_update:
result_global_credential.update({
"No Updation": {
"response": "No Response",
"msg": "No Updation is available"
}
})
self.msg = "No Updation is available"
self.status = "success"
return self
i = 0
flag = True
values = ["cliCredential", "snmpV2cRead", "snmpV2cWrite",
"httpsRead", "httpsWrite", "snmpV3"]
final_response = []
self.log("Desired State for global device credentials updation: {0}"
.format(want_update), "DEBUG")
while flag:
flag = False
credential_params = {}
for value in values:
if want_update.get(value) and i < len(want_update.get(value)):
flag = True
credential_params.update({value: want_update.get(value)[i]})
i = i + 1
if credential_params:
final_response.append(credential_params)
response = self.dnac._exec(
family="discovery",
function='update_global_credentials_v2',
op_modifies=True,
params=credential_params,
)
self.log("Received API response for 'update_global_credentials_v2': {0}"
.format(response), "DEBUG")
validation_string = "global credential update performed"
self.check_task_response_status(response, validation_string).check_return_status()
self.log("Updating device credential API input parameters: {0}"
.format(final_response), "DEBUG")
self.log("Global device credential updated successfully", "INFO")
result_global_credential.update({
"Updation": {
"response": final_response,
"msg": "Global Device Credential Updated Successfully"
}
})
self.msg = "Global Device Credential Updated Successfully"
self.status = "success"
return self
def assign_credentials_to_site(self):
"""
Assign Global Device Credential to the Cisco DNA
Center based on the provided playbook details.
Check the return value of the API with check_return_status().
Parameters:
self
Returns:
self
"""
result_assign_credential = self.result.get("response")[0].get("assignCredential")
credential_params = self.want.get("assign_credentials")
final_response = []
self.log("Assigning device credential to site API input parameters: {0}"
.format(credential_params), "DEBUG")
if not credential_params:
result_assign_credential.update({
"No Assign Credentials": {
"response": "No Response",
"msg": "No Assignment is available"
}
})
self.msg = "No Assignment is available"
self.status = "success"
return self
site_ids = self.want.get("site_id")
for site_id in site_ids:
credential_params.update({"site_id": site_id})
final_response.append(copy.deepcopy(credential_params))
response = self.dnac._exec(
family="network_settings",
function='assign_device_credential_to_site_v2',
op_modifies=True,
params=credential_params,
)
self.log("Received API response for 'assign_device_credential_to_site_v2': {0}"
.format(response), "DEBUG")
validation_string = "desired common settings operation successful"
self.check_task_response_status(response, validation_string).check_return_status()
self.log("Device credential assigned to site {0} is successfully."
.format(site_ids), "INFO")
self.log("Desired State for assign credentials to a site: {0}"
.format(final_response), "DEBUG")
result_assign_credential.update({
"Assign Credentials": {
"response": final_response,
"msg": "Device Credential Assigned to a site is Successfully"
}
})
self.msg = "Global Credential is assigned Successfully"
self.status = "success"
return self
def get_diff_merged(self, config):
"""
Update or Create Global Device Credential and assign device
credential to a site in Cisco DNA Center based on the playbook provided.
Parameters:
config (list of dict) - Playbook details containing Global
Device Credential and assign credentials to a site information.
Returns:
self
"""
if config.get("global_credential_details") is not None:
self.create_device_credentials().check_return_status()
if config.get("global_credential_details") is not None:
self.update_device_credentials().check_return_status()
if config.get("assign_credentials_to_site") is not None:
self.assign_credentials_to_site().check_return_status()
return self
def delete_device_credential(self, config):
"""
Delete Global Device Credential in Cisco DNA Center based on the playbook details.
Check the return value of the API with check_return_status().
Parameters:
config (dict) - Playbook details containing Global Device Credential information.
self - The current object details.
Returns:
self
"""
result_global_credential = self.result.get("response")[0].get("globalCredential")
have_values = self.have.get("globalCredential")
final_response = {}
self.log("Global device credentials to be deleted: {0}".format(have_values), "DEBUG")
credential_mapping = {
"cliCredential": "cli_credential",
"snmpV2cRead": "snmp_v2c_read",
"snmpV2cWrite": "snmp_v2c_write",
"snmpV3": "snmp_v3",
"httpsRead": "https_read",
"httpsWrite": "https_write"
}
for item in have_values:
config_itr = 0
final_response.update({item: []})
for value in have_values.get(item):
if value is None:
self.log("Credential Name: {0}".format(item), "DEBUG")
self.log("Credential Item: {0}".format(config.get("global_credential_details")
.get(credential_mapping.get(item))), "DEBUG")
final_response.get(item).append(
str(config.get("global_credential_details")
.get(credential_mapping.get(item))[config_itr]) + " is not found."
)
continue
_id = have_values.get(item)[config_itr].get("id")
response = self.dnac._exec(
family="discovery",
function="delete_global_credential_v2",
op_modifies=True,
params={"id": _id},
)
self.log("Received API response for 'delete_global_credential_v2': {0}"
.format(response), "DEBUG")
validation_string = "global credential deleted successfully"
self.check_task_response_status(response, validation_string).check_return_status()
final_response.get(item).append(_id)
config_itr = config_itr + 1
self.log("Deleting device credential API input parameters: {0}"
.format(final_response), "DEBUG")
self.log("Successfully deleted global device credential.", "INFO")
result_global_credential.update({
"Deletion": {
"response": final_response,
"msg": "Global Device Credentials Deleted Successfully"
}
})
self.msg = "Global Device Credentials Updated Successfully"
self.status = "success"
return self
def get_diff_deleted(self, config):
"""
Delete Global Device Credential in Cisco DNA Center based on the playbook details.
Parameters:
config (dict) - Playbook details containing Global Device Credential information.
self - The current object details.
Returns:
self
"""
if config.get("global_credential_details") is not None:
self.delete_device_credential(config).check_return_status()
return self
def verify_diff_merged(self, config):
"""
Validating the DNAC configuration with the playbook details
when state is merged (Create/Update).
Parameters:
config (dict) - Playbook details containing Global Pool,
Reserved Pool, and Network Management configuration.
Returns:
self
"""
self.log(str("Entered the verify function."), "DEBUG")
self.get_have(config)
self.get_want(config)
self.log("Current State (have): {0}".format(self.have), "INFO")
self.log("Desired State (want): {0}".format(self.want), "INFO")
if config.get("global_credential_details") is not None:
if self.want.get("want_create"):
self.msg = "Global Device Credentials config is not applied to the DNAC"
self.status = "failed"
return self
if self.want.get("want_update"):
credential_types = ["cliCredential", "snmpV2cRead", "snmpV2cWrite",
"httpsRead", "httpsWrite", "snmpV3"]
value_mapping = {
"cliCredential": ["username", "description", "id"],
"snmpV2cRead": ["description", "id"],
"snmpV2cWrite": ["description", "id"],
"httpsRead": ["description", "username", "port", "id"],
"httpsWrite": ["description", "username", "port", "id"],
"snmpV3": ["username", "description", "snmpMode", "id"]
}
for credential_type in credential_types:
if self.want.get(credential_type):
want_credential = self.want.get(credential_type)
if self.have.get(credential_type):
have_credential = self.have.get(credential_type)
values = value_mapping.get(credential_type)
for value in values:
equality = have_credential.get(value) is want_credential.get(value)
if not have_credential or not equality:
self.msg = "{0} config is not applied ot the DNAC".format(credential_type)
self.status = "failed"
return self
self.log("Successfully validated global device credential", "INFO")
self.result.get("response")[0].get("globalCredential").update({"Validation": "Success"})
if config.get("assign_credentials_to_site") is not None:
self.log("Successfully validated the assign device credential to site", "INFO")
self.result.get("response")[0].get("assignCredential").update({"Validation": "Success"})
self.msg = "Successfully validated the Global Device Credential and \
Assign Device Credential to Site."
self.status = "success"
return self
def verify_diff_deleted(self, config):
"""
Validating the DNAC configuration with the playbook details
when state is deleted (delete).
Parameters:
config (dict) - Playbook details containing Global Pool,
Reserved Pool, and Network Management configuration.
Returns:
self
"""
self.get_have(config)
self.log("Current State (have): {0}".format(self.have), "INFO")
self.log("Desired State (want): {0}".format(self.want), "INFO")
if config.get("global_credential_details") is not None:
have_global_credential = self.have.get("globalCredential")
credential_types = ["cliCredential", "snmpV2cRead", "snmpV2cWrite",
"httpsRead", "httpsWrite", "snmpV3"]
for credential_type in credential_types:
for item in have_global_credential.get(credential_type):
if item is not None:
self.msg = "Delete Global Device Credentials config \
is not applied to the config"
self.status = "failed"
return self
self.log("Successfully validated absence of global device credential.", "INFO")
self.result.get("response")[0].get("globalCredential").update({"Validation": "Success"})
self.msg = "Successfully validated the absence of Global Device Credential."
self.status = "success"
return self
def reset_values(self):
"""
Reset all neccessary attributes to default values
Parameters:
self
Returns:
self
"""
self.have.clear()
self.want.clear()
return self
def main():
"""main entry point for module execution"""
# Define the specification for module arguments
element_spec = {
"dnac_host": {"type": 'str', "required": True},
"dnac_port": {"type": 'str', "default": '443'},
"dnac_username": {"type": 'str', "default": 'admin', "aliases": ['user']},
"dnac_password": {"type": 'str', "no_log": True},
"dnac_verify": {"type": 'bool', "default": 'True'},
"dnac_version": {"type": 'str', "default": '2.2.3.3'},
"dnac_debug": {"type": 'bool', "default": False},
"dnac_log": {"type": 'bool', "default": False},
"dnac_log_level": {"type": 'str', "default": 'WARNING'},
"dnac_log_file_path": {"type": 'str', "default": 'dnac.log'},
"dnac_log_append": {"type": 'bool', "default": True},
"config_verify": {"type": 'bool', "default": False},
'dnac_api_task_timeout': {'type': 'int', "default": 1200},
'dnac_task_poll_interval': {'type': 'int', "default": 2},
"config": {"type": 'list', "required": True, "elements": 'dict'},
"state": {"default": 'merged', "choices": ['merged', 'deleted']},
"validate_response_schema": {"type": 'bool', "default": True},
}
# Create an AnsibleModule object with argument specifications
module = AnsibleModule(argument_spec=element_spec, supports_check_mode=False)
dnac_credential = DnacCredential(module)
state = dnac_credential.params.get("state")
config_verify = dnac_credential.params.get("config_verify")
if state not in dnac_credential.supported_states:
dnac_credential.status = "invalid"
dnac_credential.msg = "State {0} is invalid".format(state)
dnac_credential.check_return_status()
dnac_credential.validate_input().check_return_status()
for config in dnac_credential.config:
dnac_credential.reset_values()
dnac_credential.get_have(config).check_return_status()
if state != "deleted":
dnac_credential.get_want(config).check_return_status()
dnac_credential.get_diff_state_apply[state](config).check_return_status()
if config_verify:
dnac_credential.verify_diff_state_apply[state](config).check_return_status()
module.exit_json(**dnac_credential.result)
if __name__ == "__main__":
main()
|