forked from scotttyso/intersight_iac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_policies_p2.py
1881 lines (1733 loc) · 116 KB
/
class_policies_p2.py
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
#!/usr/bin/env python3
import jinja2
import os
import pkg_resources
import platform
import re
import stdiomask
import validating
from class_pools import pools
from class_policies_lan import policies_lan
from class_policies_p1 import policies_p1
from class_policies_vxan import policies_vxan
from easy_functions import choose_policy
from easy_functions import exit_default_no
from easy_functions import local_users_function
from easy_functions import ntp_alternate, ntp_primary
from easy_functions import policies_parse
from easy_functions import policy_descr, policy_name
from easy_functions import varBoolLoop
from easy_functions import variablesFromAPI
from easy_functions import varNumberLoop
from easy_functions import vars_from_list
from easy_functions import vlan_list_full
from easy_functions import write_to_template
ucs_template_path = pkg_resources.resource_filename('class_policies_p2', 'Templates/')
class policies_p2(object):
def __init__(self, name_prefix, org, type):
self.templateLoader = jinja2.FileSystemLoader(
searchpath=(ucs_template_path + '%s/') % (type))
self.templateEnv = jinja2.Environment(loader=self.templateLoader)
self.name_prefix = name_prefix
self.org = org
self.type = type
#==============================================
# Local User Policy Module
#==============================================
def local_user_policies(self, jsonData, easy_jsonData, **kwargs):
name_prefix = self.name_prefix
name_suffix = 'local_users'
opSystem = kwargs['opSystem']
org = self.org
policy_type = 'Local User Policy'
templateVars = {}
templateVars["header"] = '%s Variables' % (policy_type)
templateVars["initial_write"] = True
templateVars["org"] = org
templateVars["policy_type"] = policy_type
templateVars["template_file"] = 'template_open.jinja2'
templateVars["template_type"] = 'local_user_policies'
tfDir = kwargs['tfDir']
# Open the Template file
write_to_template(self, **templateVars)
templateVars["initial_write"] = False
configure_loop = False
while configure_loop == False:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' A {policy_type} will configure servers with Local Users for KVM Access. This Policy ')
print(f' is not required to standup a server but is a good practice for day 2 support.\n')
print(f' This wizard will save the configuration for this section to the following file:')
if opSystem == 'Windows':
print(f' - {tfDir}\\{org}\\{self.type}\\{templateVars["template_type"]}.auto.tfvars')
else:
print(f' - {tfDir}/{org}/{self.type}/{templateVars["template_type"]}.auto.tfvars')
print(f'\n-------------------------------------------------------------------------------------------\n')
configure = input(f'Do You Want to Configure a {policy_type}? Enter "Y" or "N" [Y]: ')
if configure == 'Y' or configure == '':
loop_count = 1
policy_loop = False
while policy_loop == False:
if not name_prefix == '':
name = '%s_%s' % (name_prefix, name_suffix)
else:
name = '%s_%s' % (org, name_suffix)
templateVars["name"] = policy_name(name, policy_type)
templateVars["descr"] = policy_descr(templateVars["name"], policy_type)
# Obtain Information for iam.EndpointPasswordProperties
templateVars["multi_select"] = False
jsonVars = jsonData['components']['schemas']['iam.EndPointPasswordProperties']['allOf'][1]['properties']
# Local User Always Send Password
templateVars["Description"] = jsonVars['ForceSendPassword']['description']
templateVars["varInput"] = f'Do you want Intersight to Always send the user password with policy updates?'
templateVars["varDefault"] = 'N'
templateVars["varName"] = 'Force Send Password'
templateVars["always_send_user_password"] = varBoolLoop(**templateVars)
# Local User Enforce Strong Password
templateVars["Description"] = jsonVars['EnforceStrongPassword']['description']
templateVars["varInput"] = f'Do you want to Enforce Strong Passwords?'
templateVars["varDefault"] = 'Y'
templateVars["varName"] = 'Enforce Strong Password'
templateVars["enforce_strong_password"] = varBoolLoop(**templateVars)
# Local User Password Expiry
templateVars["Description"] = jsonVars['EnablePasswordExpiry']['description']
templateVars["varInput"] = f'Do you want to Enable password Expiry on the Endpoint?'
templateVars["varDefault"] = 'Y'
templateVars["varName"] = 'Enable Password Expiry'
templateVars["enable_password_expiry"] = varBoolLoop(**templateVars)
if templateVars["enable_password_expiry"] == True:
# Local User Grace Period
templateVars["Description"] = 'Grace Period, in days, after the password is expired '\
'that a user can continue to use their expired password.'\
'The allowed grace period is between 0 to 5 days. With 0 being no grace period.'
templateVars["varDefault"] = jsonVars['GracePeriod']['default']
templateVars["varInput"] = 'How many days would you like to set for the Grace Period?'
templateVars["varName"] = 'Grace Period'
templateVars["varRegex"] = '[0-9]+'
templateVars["minNum"] = jsonVars['GracePeriod']['minimum']
templateVars["maxNum"] = jsonVars['GracePeriod']['maximum']
templateVars["grace_period"] = varNumberLoop(**templateVars)
# Local User Notification Period
templateVars["Description"] = 'Notification Period - Number of days, between 0 to 15 '\
'(0 being disabled), that a user is notified to change their password before it expires.'
templateVars["varDefault"] = jsonVars['NotificationPeriod']['default']
templateVars["varInput"] = 'How many days would you like to set for the Notification Period?'
templateVars["varName"] = 'Notification Period'
templateVars["varRegex"] = '[0-9]+'
templateVars["minNum"] = jsonVars['NotificationPeriod']['minimum']
templateVars["maxNum"] = jsonVars['NotificationPeriod']['maximum']
templateVars["notification_period"] = varNumberLoop(**templateVars)
# Local User Password Expiry Duration
valid = False
while valid == False:
templateVars["Description"] = 'Note: When Password Expiry is Enabled, Password Expiry '\
'Duration sets the duration of time, (in days), a password may be valid. '\
'The password expiryduration must be greater than '\
'notification period + grace period. Range is 1-3650.'
templateVars["varDefault"] = jsonVars['PasswordExpiryDuration']['default']
templateVars["varInput"] = 'How many days would you like to set for the Password Expiry Duration?'
templateVars["varName"] = 'Password Expiry Duration'
templateVars["varRegex"] = '[0-9]+'
templateVars["minNum"] = jsonVars['PasswordExpiryDuration']['minimum']
templateVars["maxNum"] = jsonVars['PasswordExpiryDuration']['maximum']
templateVars["password_expiry_duration"] = varNumberLoop(**templateVars)
x = int(templateVars["grace_period"])
y = int(templateVars["notification_period"])
z = int(templateVars["password_expiry_duration"])
if z > (x + y):
valid = True
else:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Error!! The Value of Password Expiry Duration must be greater than Grace Period +')
print(f' Notification Period. {z} is not greater than [{x} + {y}]')
print(f'\n-------------------------------------------------------------------------------------------\n')
# Local User Notification Period
templateVars["Description"] = jsonVars['PasswordHistory']['description'] + \
' Range is 0 to 5.'
templateVars["varDefault"] = jsonVars['PasswordHistory']['default']
templateVars["varInput"] = 'How many passwords would you like to store for a user?'
templateVars["varName"] = 'Password History'
templateVars["varRegex"] = '[0-9]+'
templateVars["minNum"] = jsonVars['PasswordHistory']['minimum']
templateVars["maxNum"] = jsonVars['PasswordHistory']['maximum']
templateVars["password_history"] = varNumberLoop(**templateVars)
else:
templateVars["grace_period"] = 0
templateVars["notification_period"] = 15
templateVars["password_expiry_duration"] = 90
templateVars["password_history"] = 5
# Local Users
ilCount = 1
local_users = []
user_loop = False
while user_loop == False:
question = input(f'Would you like to configure a Local user? Enter "Y" or "N" [Y]: ')
if question == '' or question == 'Y':
local_users,user_loop = local_users_function(
jsonData, easy_jsonData, ilCount, **templateVars
)
elif question == 'N':
user_loop = True
else:
print(f'\n------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n------------------------------------------------------\n')
templateVars["local_users"] = local_users
templateVars["enabled"] = True
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' always_send_user_password = {templateVars["always_send_user_password"]}')
print(f' description = "{templateVars["descr"]}"')
print(f' enable_password_expiry = {templateVars["enable_password_expiry"]}')
print(f' enforce_strong_password = {templateVars["enforce_strong_password"]}')
print(f' grace_period = "{templateVars["grace_period"]}"')
print(f' name = "{templateVars["name"]}"')
print(f' password_expiry_duration = "{templateVars["password_expiry_duration"]}"')
print(f' password_history = "{templateVars["password_history"]}"')
if len(templateVars["local_users"]) > 0:
print(f' local_users = ''{')
for item in templateVars["local_users"]:
for k, v in item.items():
if k == 'username':
print(f' "{v}" = ''{')
for k, v in item.items():
if k == 'enabled':
print(f' enable = {v}')
elif k == 'password':
print(f' password = "Sensitive"')
elif k == 'role':
print(f' role = {v}')
print(f' ''}')
print(f' ''}')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid_confirm = False
while valid_confirm == False:
confirm_policy = input('Do you want to accept the configuration above? Enter "Y" or "N" [Y]: ')
if confirm_policy == 'Y' or confirm_policy == '':
confirm_policy = 'Y'
# Write Policies to Template File
templateVars["template_file"] = '%s.jinja2' % (templateVars["template_type"])
write_to_template(self, **templateVars)
configure_loop, policy_loop = exit_default_no(templateVars["policy_type"])
valid_confirm = True
elif confirm_policy == 'N':
print(f'\n------------------------------------------------------\n')
print(f' Starting {templateVars["policy_type"]} Section over.')
print(f'\n------------------------------------------------------\n')
valid_confirm = True
else:
print(f'\n------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n------------------------------------------------------\n')
elif configure == 'N':
configure_loop = True
else:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n-------------------------------------------------------------------------------------------\n')
# Close the Template file
templateVars["template_file"] = 'template_close.jinja2'
write_to_template(self, **templateVars)
#==============================================
# Network Connectivity Policy Module
#==============================================
def network_connectivity_policies(self, jsonData, easy_jsonData, **kwargs):
name_prefix = self.name_prefix
name_suffix = 'dns'
opSystem = kwargs['opSystem']
org = self.org
policy_type = 'Network Connectivity Policy'
templateVars = {}
templateVars["header"] = '%s Variables' % (policy_type)
templateVars["initial_write"] = True
templateVars["org"] = org
templateVars["policy_type"] = policy_type
templateVars["template_file"] = 'template_open.jinja2'
templateVars["template_type"] = 'network_connectivity_policies'
tfDir = kwargs['tfDir']
# Open the Template file
write_to_template(self, **templateVars)
templateVars["initial_write"] = False
configure_loop = False
while configure_loop == False:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' It is strongly recommended to have a Network Connectivity (DNS) Policy for the')
print(f' UCS Domain Profile. Without it, DNS resolution will fail.\n')
print(f' This wizard will save the configuration for this section to the following file:')
if opSystem == 'Windows':
print(f' - {tfDir}\\{org}\\{self.type}\\{templateVars["template_type"]}.auto.tfvars')
else:
print(f' - {tfDir}/{org}/{self.type}/{templateVars["template_type"]}.auto.tfvars')
print(f'\n-------------------------------------------------------------------------------------------\n')
policy_loop = False
while policy_loop == False:
if not name_prefix == '':
name = '%s_%s' % (name_prefix, name_suffix)
else:
name = '%s_%s' % (org, name_suffix)
templateVars["name"] = policy_name(name, policy_type)
templateVars["descr"] = policy_descr(templateVars["name"], policy_type)
valid = False
while valid == False:
templateVars["preferred_ipv4_dns_server"] = input('What is your Primary IPv4 DNS Server? [208.67.220.220]: ')
if templateVars["preferred_ipv4_dns_server"] == '':
templateVars["preferred_ipv4_dns_server"] = '208.67.220.220'
valid = validating.ip_address('Primary IPv4 DNS Server', templateVars["preferred_ipv4_dns_server"])
valid = False
while valid == False:
alternate_true = input('Do you want to Configure an Alternate IPv4 DNS Server? Enter "Y" or "N" [Y]: ')
if alternate_true == 'Y' or alternate_true == '':
templateVars["alternate_ipv4_dns_server"] = input('What is your Alternate IPv4 DNS Server? [208.67.222.222]: ')
if templateVars["alternate_ipv4_dns_server"] == '':
templateVars["alternate_ipv4_dns_server"] = '208.67.222.222'
valid = validating.ip_address('Alternate IPv4 DNS Server', templateVars["alternate_ipv4_dns_server"])
elif alternate_true == 'N':
templateVars["alternate_ipv4_dns_server"] = ''
valid = True
else:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid = False
while valid == False:
enable_ipv6 = input('Do you want to Configure IPv6 DNS? Enter "Y" or "N" [N]: ')
if enable_ipv6 == 'Y':
templateVars["enable_ipv6"] = True
templateVars["preferred_ipv6_dns_server"] = input('What is your Primary IPv6 DNS Server? [2620:119:35::35]: ')
if templateVars["preferred_ipv6_dns_server"] == '':
templateVars["preferred_ipv6_dns_server"] = '2620:119:35::35'
valid = validating.ip_address('Primary IPv6 DNS Server', templateVars["preferred_ipv6_dns_server"])
if enable_ipv6 == 'N' or enable_ipv6 == '':
templateVars["enable_ipv6"] = False
templateVars["preferred_ipv6_dns_server"] = ''
valid = True
valid = False
while valid == False:
if enable_ipv6 == 'Y':
alternate_true = input('Do you want to Configure an Alternate IPv6 DNS Server? Enter "Y" or "N" [Y]: ')
if alternate_true == 'Y' or alternate_true == '':
templateVars["alternate_ipv6_dns_server"] = input('What is your Alternate IPv6 DNS Server? [2620:119:53::53]: ')
if templateVars["alternate_ipv6_dns_server"] == '':
templateVars["alternate_ipv6_dns_server"] = '2620:119:53::53'
valid = validating.ip_address('Alternate IPv6 DNS Server', templateVars["alternate_ipv6_dns_server"])
elif alternate_true == 'N':
templateVars["alternate_ipv6_dns_server"] = ''
valid = True
else:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n-------------------------------------------------------------------------------------------\n')
else:
templateVars["alternate_ipv6_dns_server"] = ''
valid = True
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' description = "{templateVars["descr"]}"')
print(f' name = "{templateVars["name"]}"')
if not templateVars["preferred_ipv4_dns_server"] == '':
print(f' dns_servers_v4 = [')
print(f' {templateVars["preferred_ipv4_dns_server"]},')
if not templateVars["alternate_ipv4_dns_server"] == '':
print(f' {templateVars["alternate_ipv4_dns_server"]}')
print(f' ]')
if not templateVars["preferred_ipv6_dns_server"] == '':
print(f' dns_servers_v6 = [')
print(f' {templateVars["preferred_ipv6_dns_server"]},')
if not templateVars["alternate_ipv6_dns_server"] == '':
print(f' {templateVars["alternate_ipv6_dns_server"]}')
print(f' ]')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid_confirm = False
while valid_confirm == False:
confirm_policy = input('Do you want to accept the configuration above? Enter "Y" or "N" [Y]: ')
if confirm_policy == 'Y' or confirm_policy == '':
confirm_policy = 'Y'
# Write Policies to Template File
templateVars["template_file"] = '%s.jinja2' % (templateVars["template_type"])
write_to_template(self, **templateVars)
configure_loop, policy_loop = exit_default_no(templateVars["policy_type"])
valid_confirm = True
elif confirm_policy == 'N':
print(f'\n------------------------------------------------------\n')
print(f' Starting {templateVars["policy_type"]} Section over.')
print(f'\n------------------------------------------------------\n')
valid_confirm = True
else:
print(f'\n------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n------------------------------------------------------\n')
# Close the Template file
templateVars["template_file"] = 'template_close.jinja2'
write_to_template(self, **templateVars)
#==============================================
# NTP Policy Module
#==============================================
def ntp_policies(self, jsonData, easy_jsonData, **kwargs):
name_prefix = self.name_prefix
name_suffix = 'ntp'
opSystem = kwargs['opSystem']
org = self.org
policy_type = 'NTP Policy'
templateVars = {}
templateVars["header"] = '%s Variables' % (policy_type)
templateVars["initial_write"] = True
templateVars["org"] = org
templateVars["policy_type"] = policy_type
templateVars["template_file"] = 'template_open.jinja2'
templateVars["template_type"] = 'ntp_policies'
tfDir = kwargs['tfDir']
# Open the Template file
write_to_template(self, **templateVars)
templateVars["initial_write"] = False
configure_loop = False
while configure_loop == False:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' It is strongly recommended to configure an NTP Policy for the UCS Domain Profile.')
print(f' Without an NTP Policy Events can be incorrectly timestamped and Intersight ')
print(f' Communication, as an example, could be interrupted with Certificate Validation\n')
print(f' checks, as an example.\n')
print(f' This wizard will save the configuration for this section to the following file:')
if opSystem == 'Windows':
print(f' - {tfDir}\\{org}\\{self.type}\\{templateVars["template_type"]}.auto.tfvars')
else:
print(f' - {tfDir}/{org}/{self.type}/{templateVars["template_type"]}.auto.tfvars')
print(f'\n-------------------------------------------------------------------------------------------\n')
policy_loop = False
while policy_loop == False:
if not name_prefix == '':
name = '%s_%s' % (name_prefix, name_suffix)
else:
name = '%s_%s' % (org, name_suffix)
templateVars["name"] = policy_name(name, policy_type)
templateVars["descr"] = policy_descr(templateVars["name"], policy_type)
primary_ntp = ntp_primary()
alternate_ntp = ntp_alternate()
templateVars["enabled"] = True
templateVars["ntp_servers"] = []
templateVars["ntp_servers"].append(primary_ntp)
if not alternate_ntp == '':
templateVars["ntp_servers"].append(alternate_ntp)
templateVars["multi_select"] = False
jsonVars = jsonData['components']['schemas']['appliance.SystemInfo']['allOf'][1]['properties']['TimeZone']['enum']
tz_regions = []
for i in jsonVars:
tz_region = i.split('/')[0]
if not tz_region in tz_regions:
tz_regions.append(tz_region)
tz_regions = sorted(tz_regions)
templateVars["var_description"] = 'Timezone Regions...'
templateVars["jsonVars"] = tz_regions
templateVars["defaultVar"] = 'America'
templateVars["varType"] = 'Time Region'
time_region = variablesFromAPI(**templateVars)
region_tzs = []
for item in jsonVars:
if time_region in item:
region_tzs.append(item)
templateVars["var_description"] = 'Region Timezones...'
templateVars["jsonVars"] = sorted(region_tzs)
templateVars["defaultVar"] = ''
templateVars["varType"] = 'Region Timezones'
templateVars["timezone"] = variablesFromAPI(**templateVars)
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' description = "{templateVars["descr"]}"')
print(f' name = "{templateVars["name"]}"')
print(f' timezone = "{templateVars["timezone"]}"')
if len(templateVars["ntp_servers"]) > 0:
print(f' ntp_servers = [')
for server in templateVars["ntp_servers"]:
print(f' "{server}",')
print(f' ]')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid_confirm = False
while valid_confirm == False:
confirm_policy = input('Do you want to accept the configuration above? Enter "Y" or "N" [Y]: ')
if confirm_policy == 'Y' or confirm_policy == '':
confirm_policy = 'Y'
# Write Policies to Template File
templateVars["template_file"] = '%s.jinja2' % (templateVars["template_type"])
write_to_template(self, **templateVars)
configure_loop, policy_loop = exit_default_no(templateVars["policy_type"])
valid_confirm = True
elif confirm_policy == 'N':
print(f'\n------------------------------------------------------\n')
print(f' Starting {templateVars["policy_type"]} Section over.')
print(f'\n------------------------------------------------------\n')
valid_confirm = True
else:
print(f'\n------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n------------------------------------------------------\n')
# Close the Template file
templateVars["template_file"] = 'template_close.jinja2'
write_to_template(self, **templateVars)
#==============================================
# Persistent Memory Policy Module
#==============================================
def persistent_memory_policies(self, jsonData, easy_jsonData, **kwargs):
name_prefix = self.name_prefix
name_suffix = 'persistent_memory'
opSystem = kwargs['opSystem']
org = self.org
policy_type = 'Persistent Memory Policy'
templateVars = {}
templateVars["header"] = '%s Variables' % (policy_type)
templateVars["initial_write"] = True
templateVars["org"] = org
templateVars["policy_type"] = policy_type
templateVars["template_file"] = 'template_open.jinja2'
templateVars["template_type"] = 'persistent_memory_policies'
tfDir = kwargs['tfDir']
# Open the Template file
write_to_template(self, **templateVars)
templateVars["initial_write"] = False
configure_loop = False
while configure_loop == False:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' A {policy_type} allows the configuration of security, Goals, and ')
print(f' Namespaces of Persistent Memory Modules:')
print(f' - Goal - Used to configure volatile memory and regions in all the PMem Modules connected ')
print(f' to all the sockets of the server. Intersight supports only the creation and modification')
print(f' of a Goal as part of the Persistent Memory policy. Some data loss occurs when a Goal is')
print(f' modified during the creation or modification of a Persistent Memory Policy.')
print(f' - Namespaces - Used to partition a region mapped to a specific socket or a PMem Module on a')
print(f' socket. Intersight supports only the creation and deletion of Namespaces as part of the ')
print(f' Persistent Memory Policy. Modifying a Namespace is not supported. Some data loss occurs ')
print(f' when a Namespace is created or deleted during the creation of a Persistent Memory policy.')
print(f' It is important to consider the memory performance guidelines and population rules of ')
print(f' the Persistent Memory Modules before they are installed or replaced, and the policy is ')
print(f' deployed. The population guidelines for the PMem Modules can be divided into the ')
print(f' following categories, based on the number of CPU sockets:')
print(f' * Dual CPU for UCS B200 M6, C220 M6, C240 M6, and xC210 M6 servers')
print(f' * Dual CPU for UCS C220 M5, C240 M5, and B200 M5 servers')
print(f' * Dual CPU for UCS S3260 M5 servers')
print(f' * Quad CPU for UCS C480 M5 and B480 M5 servers')
print(f' - Security - Used to configure the secure passphrase for all the persistent memory modules.\n')
print(f' This wizard will save the configuration for this section to the following file:')
if opSystem == 'Windows':
print(f' - {tfDir}\\{org}\\{self.type}\\{templateVars["template_type"]}.auto.tfvars')
else:
print(f' - {tfDir}/{org}/{self.type}/{templateVars["template_type"]}.auto.tfvars')
print(f'\n-------------------------------------------------------------------------------------------\n')
configure = input(f'Do You Want to Configure a {policy_type}? Enter "Y" or "N" [Y]: ')
if configure == 'Y' or configure == '':
policy_loop = False
while policy_loop == False:
if not name_prefix == '':
name = '%s_%s' % (name_prefix, name_suffix)
else:
name = '%s_%s' % (org, name_suffix)
templateVars["name"] = policy_name(name, policy_type)
templateVars["descr"] = policy_descr(templateVars["name"], policy_type)
templateVars["multi_select"] = False
jsonVars = jsonData['components']['schemas']['memory.PersistentMemoryPolicy']['allOf'][1]['properties']
templateVars["var_description"] = jsonVars['ManagementMode']['description']
templateVars["jsonVars"] = sorted(jsonVars['ManagementMode']['enum'])
templateVars["defaultVar"] = jsonVars['ManagementMode']['default']
templateVars["varType"] = 'Management Mode'
templateVars["management_mode"] = variablesFromAPI(**templateVars)
if templateVars["management_mode"] == 'configured-from-intersight':
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' A Secure passphrase will enable the protection of data on the persistent memory modules. ')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid = False
while valid == False:
encrypt_memory = input('Do you want to enable a secure passphrase? Enter "Y" or "N" [Y]: ')
if encrypt_memory == 'Y' or encrypt_memory == '':
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' The Passphrase must be between 8 and 32 characters in length. The allowed characters are:')
print(f' - a-z, A-Z, 0-9 and special characters: \u0021, &, #, $, %, +, ^, @, _, *, -.')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid_passphrase = False
while valid_passphrase == False:
secure_passphrase = stdiomask.getpass(prompt='Enter the Secure Passphrase: ')
templateVars["minLength"] = 8
templateVars["maxLength"] = 32
templateVars["rePattern"] = '^[a-zA-Z0-9\\u0021\\&\\#\\$\\%\\+\\%\\@\\_\\*\\-\\.]+$'
templateVars["varName"] = 'Secure Passphrase'
varValue = secure_passphrase
valid_passphrase = validating.length_and_regex_sensitive(templateVars["rePattern"],
templateVars["varName"],
varValue,
templateVars["minLength"],
templateVars["maxLength"]
)
os.environ['TF_VAR_secure_passphrase'] = '%s' % (secure_passphrase)
valid = True
else:
valid = True
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' The percentage of volatile memory required for goal creation.')
print(f' The actual volatile and persistent memory size allocated to the region may differ with')
print(f' the given percentage.')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid = False
while valid == False:
templateVars["memory_mode_percentage"] = input('What is the Percentage of Valatile Memory to assign to this Policy? [0]: ')
if templateVars["memory_mode_percentage"] == '':
templateVars["memory_mode_percentage"] = 0
if re.search(r'[\d]+', str(templateVars["memory_mode_percentage"])):
valid = validating.number_in_range('Memory Mode Percentage', templateVars["memory_mode_percentage"], 1, 100)
else:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' "{templateVars["memory_mode_percentage"]}" is not a valid number.')
print(f'\n-------------------------------------------------------------------------------------------\n')
jsonVars = jsonData['components']['schemas']['memory.PersistentMemoryGoal']['allOf'][1]['properties']
templateVars["var_description"] = jsonVars['PersistentMemoryType']['description']
templateVars["jsonVars"] = sorted(jsonVars['PersistentMemoryType']['enum'])
templateVars["defaultVar"] = jsonVars['PersistentMemoryType']['default']
templateVars["varType"] = 'Persistent Memory Type'
templateVars["persistent_memory_type"] = variablesFromAPI(**templateVars)
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' This Flag will enable or Disable the retention of Namespaces between Server Profile')
print(f' association and dissassociation.')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid = False
while valid == False:
templateVars["retain_namespaces"] = input('Do you want to Retain Namespaces? Enter "Y" or "N" [Y]: ')
if templateVars["retain_namespaces"] == '' or templateVars["retain_namespaces"] == 'Y':
templateVars["retain_namespaces"] = True
valid = True
elif templateVars["retain_namespaces"] == 'N':
templateVars["retain_namespaces"] = False
valid = True
else:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n-------------------------------------------------------------------------------------------\n')
templateVars["namespaces"] = []
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Namespace is a partition made in one or more Persistent Memory Regions. You can create a')
print(f' namespace in Raw or Block mode.')
print(f'\n-------------------------------------------------------------------------------------------\n')
namespace_configure = input(f'Do You Want to Configure a namespace? Enter "Y" or "N" [Y]: ')
if namespace_configure == 'Y' or namespace_configure == '':
sub_loop = False
while sub_loop == False:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Name of this Namespace to be created on the server.')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid = False
while valid == False:
namespace_name = input('What is the Name for this Namespace? ')
templateVars["minLength"] = 1
templateVars["maxLength"] = 63
templateVars["rePattern"] = '^[a-zA-Z0-9\\#\\_\\-]+$'
templateVars["varName"] = 'Name for the Namespace'
varValue = namespace_name
valid = validating.length_and_regex(templateVars["rePattern"], templateVars["varName"], varValue, templateVars["minLength"], templateVars["maxLength"])
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Capacity of this Namespace in gibibytes (GiB). Range is 1-9223372036854775807')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid = False
while valid == False:
capacity = input('What is the Capacity to assign to this Namespace? ')
templateVars["minNum"] = 1
templateVars["maxNum"] = 9223372036854775807
templateVars["varName"] = 'Namespace Capacity'
varValue = int(capacity)
if re.search(r'[\d]+',str(varValue)):
valid = validating.number_in_range(templateVars["varName"], varValue, templateVars["minNum"], templateVars["maxNum"])
else:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' "{varValue}" is not a valid number.')
print(f'\n-------------------------------------------------------------------------------------------\n')
jsonVars = jsonData['components']['schemas']['memory.PersistentMemoryLogicalNamespace']['allOf'][1]['properties']
templateVars["var_description"] = jsonVars['Mode']['description']
templateVars["jsonVars"] = sorted(jsonVars['Mode']['enum'])
templateVars["defaultVar"] = jsonVars['Mode']['default']
templateVars["varType"] = 'Mode'
mode = variablesFromAPI(**templateVars)
templateVars["var_description"] = jsonVars['SocketId']['description']
templateVars["jsonVars"] = sorted(jsonVars['SocketId']['enum'])
templateVars["defaultVar"] = jsonVars['SocketId']['default']
templateVars["varType"] = 'Socket Id'
socket_id = variablesFromAPI(**templateVars)
if templateVars["persistent_memory_type"] == 'app-direct-non-interleaved':
templateVars["var_description"] = jsonVars['SocketMemoryId']['description']
templateVars["jsonVars"] = [x for x in jsonVars['SocketMemoryId']['enum']]
templateVars["defaultVar"] = '2'
templateVars["popList"] = ['Not Applicable']
templateVars["varType"] = 'Socket Memory Id'
socket_memory_id = variablesFromAPI(**templateVars)
else:
socket_memory_id = 'Not Applicable'
namespace = {
'capacity':capacity,
'mode':mode,
'name':namespace_name,
'socket_id':socket_id,
'socket_memory_id':socket_memory_id
}
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' capacity = "{capacity}"')
print(f' mode = "{mode}"')
print(f' name = "{namespace_name}"')
print(f' socket_id = "{socket_id}"')
print(f' socket_memory_id = "{socket_memory_id}"')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid_confirm = False
while valid_confirm == False:
confirm_namespace = input('Do you want to accept the configuration above? Enter "Y" or "N" [Y]: ')
if confirm_namespace == 'Y' or confirm_namespace == '':
templateVars["namespaces"].append(namespace)
valid_exit = False
while valid_exit == False:
sub_exit = input(f'Would You like to Configure another namespace? Enter "Y" or "N" [N]: ')
if sub_exit == 'Y':
valid_confirm = True
valid_exit = True
elif sub_exit == 'N' or sub_exit == '':
sub_loop = True
valid = True
valid_confirm = True
valid_exit = True
else:
print(f'\n------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n------------------------------------------------------\n')
elif confirm_namespace == 'N':
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Starting namespace Configuration Over.')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid_confirm = True
else:
print(f'\n------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n------------------------------------------------------\n')
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' description = "{templateVars["descr"]}"')
print(f' management_mode = "{templateVars["management_mode"]}"')
print(f' name = "{templateVars["name"]}"')
if templateVars["management_mode"] == 'configured-from-intersight':
print(f' # GOALS')
print(f' memory_mode_percentage = {templateVars["memory_mode_percentage"]}')
print(f' persistent_memory_type = {templateVars["persistent_memory_type"]}')
print(f' # NAMESPACES')
print(f' namespaces = ''{')
for item in templateVars["namespaces"]:
print(f' "{item["name"]}" = ''{')
print(f' capacity = {item["capacity"]}')
print(f' mode = {item["mode"]}')
print(f' socket_id = {item["socket_id"]}')
print(f' socket_memory_id = {item["socket_memory_id"]}')
print(f' ''}')
print(f' ''}')
print(f' retain_namespaces = "{templateVars["retain_namespaces"]}"')
print(f'\n-------------------------------------------------------------------------------------------\n')
valid_confirm = False
while valid_confirm == False:
confirm_policy = input('Do you want to accept the above configuration? Enter "Y" or "N" [Y]: ')
if confirm_policy == 'Y' or confirm_policy == '':
confirm_policy = 'Y'
# Write Policies to Template File
templateVars["template_file"] = '%s.jinja2' % (templateVars["template_type"])
write_to_template(self, **templateVars)
configure_loop, policy_loop = exit_default_no(templateVars["policy_type"])
valid_confirm = True
elif confirm_policy == 'N':
print(f'\n------------------------------------------------------\n')
print(f' Starting {templateVars["policy_type"]} Section over.')
print(f'\n------------------------------------------------------\n')
valid_confirm = True
else:
print(f'\n------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n------------------------------------------------------\n')
elif configure == 'N':
configure_loop = True
else:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n-------------------------------------------------------------------------------------------\n')
# Close the Template file
templateVars["template_file"] = 'template_close.jinja2'
write_to_template(self, **templateVars)
#==============================================
# Port Policy Module
#==============================================
def port_policies(self, jsonData, easy_jsonData, **kwargs):
name_prefix = self.name_prefix
opSystem = kwargs['opSystem']
org = self.org
policy_type = 'Port Policy'
templateVars = {}
templateVars["header"] = '%s Variables' % (policy_type)
templateVars["initial_write"] = True
templateVars["org"] = org
templateVars["policy_type"] = policy_type
templateVars["template_file"] = 'template_open.jinja2'
templateVars["template_type"] = 'port_policies'
tfDir = kwargs['tfDir']
# Open the Template file
write_to_template(self, **templateVars)
templateVars["initial_write"] = False
port_count = 0
configure_loop = False
while configure_loop == False:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' A {policy_type} is used to configure the ports for a UCS Domain Profile. This includes:')
print(f' - Unified Ports - Ports to convert to Fibre-Channel Mode.')
print(f' - Appliance Ports')
print(f' - Appliance Port-Channels')
print(f' - Ethernet Uplinks')
print(f' - Ethernet Uplink Port-Channels')
print(f' - FCoE Uplinks')
print(f' - FCoE Uplink Port-Channels')
print(f' - Fibre-Channel Storage')
print(f' - Fibre-Channel Uplinks')
print(f' - Fibre-Channel Uplink Port-Channels')
print(f' - Server Ports\n')
print(f' This wizard will save the configuration for this section to the following file:')
if opSystem == 'Windows':
print(f' - {tfDir}\\{org}\\{self.type}\\{templateVars["template_type"]}.auto.tfvars')
else:
print(f' - {tfDir}/{org}/{self.type}/{templateVars["template_type"]}.auto.tfvars')
print(f'\n-------------------------------------------------------------------------------------------\n')
policy_loop = False
while policy_loop == False:
print(f' IMPORTANT NOTE: The wizard will create a Port Policy for Fabric A and Fabric B')
print(f' automatically. The Policy Name will be appended with [name]_A for ')
print(f' Fabric A and [name]_B for Fabric B. You only need one Policy per')
print(f' Domain.')
print(f'\n-------------------------------------------------------------------------------------------\n')
if not name_prefix == '':
name = '%s' % (name_prefix)
else:
name = '%s' % (org)
templateVars["name"] = policy_name(name, policy_type)
templateVars["descr"] = policy_descr(templateVars["name"], policy_type)
templateVars["multi_select"] = False
jsonVars = jsonData['components']['schemas']['fabric.PortPolicy']['allOf'][1]['properties']
templateVars["var_description"] = jsonVars['DeviceModel']['description']
templateVars["jsonVars"] = sorted(jsonVars['DeviceModel']['enum'])
templateVars["defaultVar"] = jsonVars['DeviceModel']['default']
templateVars["varType"] = 'Device Model'
templateVars["device_model"] = variablesFromAPI(**templateVars)
fc_mode,ports_in_use,fc_converted_ports,port_modes = port_modes_fc(jsonData, easy_jsonData, name_prefix, **templateVars)
templateVars["fc_mode"] = fc_mode
templateVars["ports_in_use"] = ports_in_use
templateVars["fc_converted_ports"] = fc_converted_ports
templateVars["port_modes"] = port_modes
templateVars["fc_ports"] = templateVars["port_modes"]["port_list"]
# Appliance Port-Channel
templateVars['port_type'] = 'Appliance Port-Channel'
port_channel_appliances,templateVars['ports_in_use'] = port_list_eth(jsonData, easy_jsonData, name_prefix, **templateVars)
# Ethernet Uplink Port-Channel
templateVars['port_type'] = 'Ethernet Uplink Port-Channel'
port_channel_ethernet_uplinks,templateVars['ports_in_use'] = port_list_eth(jsonData, easy_jsonData, name_prefix, **templateVars)
# Fibre-Channel Port-Channel
templateVars["fc_ports_in_use"] = []
templateVars["port_type"] = 'Fibre-Channel Port-Channel'
Fab_A,Fab_B,fc_ports_in_use = port_list_fc(jsonData, easy_jsonData, name_prefix, **templateVars)
Fabric_A_fc_port_channels = Fab_A
Fabric_B_fc_port_channels = Fab_B
templateVars["fc_ports_in_use"] = fc_ports_in_use
# FCoE Uplink Port-Channel
templateVars['port_type'] = 'FCoE Uplink Port-Channel'
port_channel_fcoe_uplinks,templateVars['ports_in_use'] = port_list_eth(jsonData, easy_jsonData, name_prefix, **templateVars)
# Appliance Ports
templateVars['port_type'] = 'Appliance Ports'
port_role_appliances,templateVars['ports_in_use'] = port_list_eth(jsonData, easy_jsonData, name_prefix, **templateVars)
# Ethernet Uplink
templateVars['port_type'] = 'Ethernet Uplink'
port_role_ethernet_uplinks,templateVars['ports_in_use'] = port_list_eth(jsonData, easy_jsonData, name_prefix, **templateVars)
# Fibre-Channel Storage
templateVars["port_type"] = 'Fibre-Channel Storage'
Fab_A,Fab_B,fc_ports_in_use = port_list_fc(jsonData, easy_jsonData, name_prefix, **templateVars)
Fabric_A_port_role_fc_storage = Fab_A
Fabric_B_port_role_fc_storage = Fab_B
templateVars["fc_ports_in_use"] = fc_ports_in_use
# Fibre-Channel Uplink
templateVars["port_type"] = 'Fibre-Channel Uplink'
Fab_A,Fab_B,fc_ports_in_use = port_list_fc(jsonData, easy_jsonData, name_prefix, **templateVars)
Fabric_A_port_role_fc_uplink = Fab_A
Fabric_B_port_role_fc_uplink = Fab_B
templateVars["fc_ports_in_use"] = fc_ports_in_use
# FCoE Uplink
templateVars['port_type'] = 'FCoE Uplink'
port_role_fcoe_uplinks,templateVars['ports_in_use'] = port_list_eth(jsonData, easy_jsonData, name_prefix, **templateVars)
# Server Ports
templateVars['port_type'] = 'Server Ports'
port_role_servers,templateVars['ports_in_use'] = port_list_eth(jsonData, easy_jsonData, name_prefix, **templateVars)
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' description = "{templateVars["descr"]}"')
print(f' device_model = "{templateVars["device_model"]}"')
print(f' name = "{templateVars["name"]}"')
if len(port_channel_appliances) > 0:
print(f' port_channel_appliances = [')
for item in port_channel_appliances:
for k, v in item.items():
if k == 'pc_id':
print(f' {v} = ''{')
for k, v in item.items():
if k == 'admin_speed':
print(f' admin_speed = "{v}"')
elif k == 'ethernet_network_control_policy':
print(f' ethernet_network_control_policy = "{v}"')
elif k == 'ethernet_network_group_policy':
print(f' ethernet_network_group_policy = "{v}"')
elif k == 'interfaces':
print(f' interfaces = [')
for i in v:
print(' {')
for x, y in i.items():
print(f' {x} = {y}')
print(' }')
print(f' ]')
elif k == 'mode':
print(f' mode = "{v}"')
elif k == 'priority':
print(f' priority = "{v}"')
print(' }')
print(f' ]')
if len(port_channel_ethernet_uplinks) > 0:
print(f' port_channel_ethernet_uplinks = [')
for item in port_channel_ethernet_uplinks:
for k, v in item.items():
if k == 'pc_id':
print(f' {v} = ''{')
for k, v in item.items():
if k == 'admin_speed':
print(f' admin_speed = "{v}"')
elif k == 'flow_control_policy':
print(f' flow_control_policy = "{v}"')
elif k == 'interfaces':
print(f' interfaces = [')
for i in v:
print(' {')
for x, y in i.items():
print(f' {x} = {y}')
print(' }')
print(f' ]')