forked from scotttyso/intersight_iac
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathezimm.py
executable file
·1145 lines (1082 loc) · 58 KB
/
ezimm.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
"""Intersight IAC -
Use This Wizard to Create Terraform HCL configuration from Question and Answer or the IMM Transition Tool.
It uses argparse to take in the following CLI arguments:
a or api-key: API client key id for the HTTP signature scheme
d or dir: Base Directory to use for creation of the HCL Configuration Files
i or ignore-tls: Ignores TLS server-side certificate verification
j or json_file: IMM Transition JSON export to convert to HCL
l or api-key-legacy: Use legacy API client (v2) key
s or api-key-file: Name of file containing secret key for the HTTP signature scheme
u or url: The intersight root URL for the api endpoint. (The default is https://intersight.com)
"""
from class_imm_transition import imm_transition
from class_pools import pools
from class_policies_lan import policies_lan
from class_policies_p1 import policies_p1
from class_policies_p2 import policies_p2
from class_policies_p3 import policies_p3
from class_policies_san import policies_san
from class_policies_vxan import policies_vxan
from class_profiles import profiles
from class_quick_start import quick_start
from class_terraform import terraform_cloud
from easy_functions import api_key, api_secret, policies_parse
from easy_functions import merge_easy_imm_repository, sensitive_var_value
from easy_functions import tfc_sensitive_variables, varBoolLoop
from easy_functions import variablesFromAPI, varStringLoop
from io import StringIO
from intersight.api import organization_api
from intersight.api import resource_api
from intersight.model.organization_organization_relationship import OrganizationOrganizationRelationship
from lxml import etree
from pathlib import Path
import argparse
import credentials
import json
import os
import platform
import re
import requests
import sys
import validating
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
home = Path.home()
def create_terraform_workspaces(jsonData, easy_jsonData, org):
opSystem = platform.system()
tfcb_config = []
templateVars = {}
templateVars["Description"] = f'Terraform Cloud Workspaces for Organization {org}'
templateVars["varInput"] = f'Do you want to Proceed with creating Workspaces in Terraform Cloud or Enterprise?'
templateVars["varDefault"] = 'Y'
templateVars["varName"] = 'Terraform Cloud Workspaces'
runTFCB = varBoolLoop(**templateVars)
if runTFCB == True:
kwargs = {}
kwargs["multi_select"] = True
kwargs["var_description"] = f'Select the Terraform Target.'
kwargs["jsonVars"] = ['Terraform Cloud', 'Terraform Enterprise']
kwargs["defaultVar"] = 'Terraform Cloud'
kwargs["varType"] = 'Target'
terraform_target = variablesFromAPI(**kwargs)
if terraform_target[0] == 'Terraform Enterprise':
templateVars["Description"] = f'Hostname of the Terraform Enterprise Instance'
templateVars["varDefault"] = f'app.terraform.io'
templateVars["varInput"] = f'What is the Hostname of the TFE Instance? [app.terraform.io]: '
templateVars["varName"] = f'Terraform Target Name'
templateVars["varRegex"] = '^[a-zA-Z0-9\\-\\.\\:]+$'
templateVars["minLength"] = 1
templateVars["maxLength"] = 90
templateVars["tfc_host"] = varStringLoop(**templateVars)
if re.search(r"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+", templateVars["tfc_host"]):
validating.ip_address('Terraform Target', templateVars["tfc_host"])
elif ':' in templateVars["tfc_host"]:
validating.ip_address('Terraform Target', templateVars["tfc_host"])
else:
validating.dns_name('Terraform Target', templateVars["tfc_host"])
else:
templateVars['tfc_host'] = 'app.terraform.io'
#templateVars = {}
templateVars["terraform_cloud_token"] = terraform_cloud().terraform_token()
# Obtain Terraform Cloud Organization
if os.environ.get('tfc_organization') is None:
templateVars["tfc_organization"] = terraform_cloud().tfc_organization(**templateVars)
os.environ['tfc_organization'] = templateVars["tfc_organization"]
else:
templateVars["tfc_organization"] = os.environ.get('tfc_organization')
tfcb_config.append({'tfc_organization':templateVars["tfc_organization"]})
# Obtain Version Control Provider
if os.environ.get('tfc_vcs_provider') is None:
tfc_vcs_provider,templateVars["tfc_oath_token"] = terraform_cloud().tfc_vcs_providers(**templateVars)
templateVars["tfc_vcs_provider"] = tfc_vcs_provider
os.environ['tfc_vcs_provider'] = tfc_vcs_provider
os.environ['tfc_oath_token'] = templateVars["tfc_oath_token"]
else:
templateVars["tfc_vcs_provider"] = os.environ.get('tfc_vcs_provider')
templateVars["tfc_oath_token"] = os.environ['tfc_oath_token']
# Obtain Version Control Base Repo
if os.environ.get('vcsBaseRepo') is None:
templateVars["vcsBaseRepo"] = terraform_cloud().tfc_vcs_repository(**templateVars)
os.environ['vcsBaseRepo'] = templateVars["vcsBaseRepo"]
else:
templateVars["vcsBaseRepo"] = os.environ.get('vcsBaseRepo')
templateVars["agentPoolId"] = ''
templateVars["allowDestroyPlan"] = False
templateVars["executionMode"] = 'remote'
templateVars["queueAllRuns"] = False
templateVars["speculativeEnabled"] = True
templateVars["triggerPrefixes"] = []
# Query the Terraform Versions from the Release URL
terraform_versions = []
url = f'https://releases.hashicorp.com/terraform/'
r = requests.get(url)
html = r.content.decode("utf-8")
parser = etree.HTMLParser()
tree = etree.parse(StringIO(html), parser=parser)
# This will get the anchor tags <a href...>
refs = tree.xpath("//a")
links = [link.get('href', '') for link in refs]
for i in links:
if re.search(r'/terraform/[1-2]\.[0-9]+\.[0-9]+/', i):
tf_version = re.search(r'/terraform/([1-2]\.[0-9]+\.[0-9]+)', i).group(1)
terraform_versions.append(tf_version)
# Removing Deprecated Versions from the List
deprecatedVersions = ["1.1.0", "1.1.1"]
for depver in deprecatedVersions:
for Version in terraform_versions:
if str(depver) == str(Version):
terraform_versions.remove(depver)
# Assign the Terraform Version from the Terraform Release URL Above
templateVars["multi_select"] = False
templateVars["var_description"] = "Terraform Version for Workspaces:"
templateVars["jsonVars"] = terraform_versions
templateVars["varType"] = 'Terraform Version'
templateVars["defaultVar"] = terraform_versions[0]
# Obtain Terraform Workspace Version
if os.environ.get('terraformVersion') is None:
templateVars["terraformVersion"] = variablesFromAPI(**templateVars)
os.environ['terraformVersion'] = templateVars["terraformVersion"]
else:
templateVars["terraformVersion"] = os.environ.get('terraformVersion')
repoFoldercheck = False
while repoFoldercheck == False:
if not os.environ.get('tfWorkDir') is None:
tfDir = os.environ.get('tfWorkDir')
else:
if os.environ.get('TF_DEST_DIR') is None:
tfDir = 'Intersight'
os.environ['tfWorkDir'] = 'Intersight'
else:
tfDir = os.environ.get('TF_DEST_DIR')
if opSystem == 'Windows' and re.search(r'(^\\|^\.\\)', tfDir):
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Within Terraform Cloud, the Workspace will be configured with the directory where the ')
print(f' configuration files are stored in the repo: {templateVars["vcsBaseRepo"]}.')
print(f' For Example if the shortpath was "Intersight", The Repo URL would end up like:\n')
print(f' - {templateVars["vcsBaseRepo"]}\\Intersight\\policies')
print(f' - {templateVars["vcsBaseRepo"]}\\Intersight\\pools')
print(f' - {templateVars["vcsBaseRepo"]}\\Intersight\\profiles')
print(f' - {templateVars["vcsBaseRepo"]}\\Intersight\\ucs_domain_profiles\n')
print(f' The Destination Directory has been entered as:\n')
print(f' {tfDir}\n')
print(f' Which looks to be a system path instead of a Repository Directory.')
print(f' Please confirm the Path Below is the short Path to the Repository Directory.')
print(f'\n-------------------------------------------------------------------------------------------\n')
dirLength = len(tfDir.split('\\'))
if re.search(r'\\$', tfDir):
dirSplit = tfDir.split("\\")[dirLength -2]
question = input(f'Press Enter to Confirm or Make Corrections: [{dirSplit}]: ')
else:
dirSplit = tfDir.split("\\")[dirLength -1]
question = input(f'Press Enter to Confirm or Make Corrections: [{dirSplit}]: ')
if question == '':
if re.search(r'\\$', tfDir):
tfDir = tfDir.split("\\")[dirLength -2]
else:
tfDir = tfDir.split("\\")[dirLength -1]
os.environ['tfWorkDir'] = tfDir
repoFoldercheck = True
else:
tfDir = question
os.environ['tfWorkDir'] = tfDir
repoFoldercheck = True
elif re.search(r'(^\/|^\.\.)', tfDir):
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Within Terraform Cloud, the Workspace will be configured with the directory where the ')
print(f' configuration files are stored in the repo: {templateVars["vcsBaseRepo"]}.')
print(f' For Example if the shortpath was "Intersight", The Repo URL end up like:\n')
print(f' - {templateVars["vcsBaseRepo"]}/Intersight/policies')
print(f' - {templateVars["vcsBaseRepo"]}/Intersight/pools')
print(f' - {templateVars["vcsBaseRepo"]}/Intersight/profiles')
print(f' - {templateVars["vcsBaseRepo"]}/Intersight/ucs_domain_profiles\n')
print(f' The Destination Directory has been entered as:\n')
print(f' {tfDir}\n')
print(f' Which looks to be a system path instead of a Repository Directory.')
print(f' Please confirm the Path Below is the short Path to the Repository Directory.')
print(f'\n-------------------------------------------------------------------------------------------\n')
dirLength = len(tfDir.split('/'))
question = input(f'Enter Value to Make Corrections: [Press Enter to Leave Base Path Empty]: ')
if question == '':
tfDir = ''
os.environ['tfWorkDir'] = tfDir
repoFoldercheck = True
else:
tfDir = question
os.environ['tfWorkDir'] = tfDir
repoFoldercheck = True
else:
repoFoldercheck = True
if opSystem == 'Windows':
folder_list = [
f'{tfDir}\\{org}\\policies',
f'{tfDir}\\{org}\\pools',
f'{tfDir}\\{org}\\profiles',
f'{tfDir}\\{org}\\ucs_domain_profiles'
]
else:
folder_list = [
f'{tfDir}/{org}/policies',
f'{tfDir}/{org}/pools',
f'{tfDir}/{org}/profiles',
f'{tfDir}/{org}/ucs_domain_profiles'
]
for folder in folder_list:
if opSystem == 'Windows':
folder_length = len(folder.split('\\'))
else:
folder_length = len(folder.split('/'))
templateVars["autoApply"] = True
if opSystem == 'Windows':
templateVars["Description"] = f'Intersight Organization {org} - %s' % (folder.split('\\')[folder_length -2])
else:
templateVars["Description"] = f'Intersight Organization {org} - %s' % (folder.split('/')[folder_length -2])
if opSystem == 'Windows':
fSplit = folder.split('\\')[folder_length -1]
else:
fSplit = folder.split('/')[folder_length -1]
if re.search('(pools|policies|ucs_domain_profiles)', fSplit):
templateVars["globalRemoteState"] = True
else:
templateVars["globalRemoteState"] = False
templateVars["workingDirectory"] = folder
if opSystem == 'Windows':
fSplit = folder.split("\\")[folder_length -1]
else:
fSplit = folder.split("/")[folder_length -1]
templateVars["Description"] = f'Name of the {fSplit} Workspace to Create in Terraform Cloud'
templateVars["varDefault"] = f'{org}_{fSplit}'
templateVars["varInput"] = f'Terraform Cloud Workspace Name. [{org}_{fSplit}]: '
templateVars["varName"] = f'Workspace Name'
templateVars["varRegex"] = '^[a-zA-Z0-9\\-\\_]+$'
templateVars["minLength"] = 1
templateVars["maxLength"] = 90
templateVars["workspaceName"] = varStringLoop(**templateVars)
if opSystem == 'Windows':
tfcb_config.append({folder.split('\\')[folder_length -1]:templateVars["workspaceName"]})
else:
tfcb_config.append({folder.split('/')[folder_length -1]:templateVars["workspaceName"]})
# templateVars["vcsBranch"] = ''
templateVars['workspace_id'] = terraform_cloud().tfcWorkspace(**templateVars)
vars = [
'apikey.Intersight API Key',
'secretkey.Intersight Secret Key'
]
for var in vars:
print(f'* Adding {var.split(".")[1]} to {templateVars["workspaceName"]}')
templateVars["Variable"] = var.split('.')[0]
if 'secret' in var:
templateVars["Multi_Line_Input"] = True
templateVars["Description"] = var.split('.')[1]
templateVars["varId"] = var.split('.')[0]
templateVars["varKey"] = var.split('.')[0]
templateVars["varValue"] = sensitive_var_value(jsonData, **templateVars)
templateVars["Sensitive"] = True
if 'secret' in var and opSystem == 'Windows':
if os.path.isfile(templateVars["varValue"]):
f = open(templateVars["varValue"])
templateVars["varValue"] = f.read().replace('\n', '\\n')
terraform_cloud().tfcVariables(**templateVars)
if opSystem == 'Windows':
folderSplit = folder.split("\\")[folder_length -1]
else:
folderSplit = folder.split("/")[folder_length -1]
if folderSplit == 'policies':
templateVars["Multi_Line_Input"] = False
vars = [
'ipmi_over_lan_policies.ipmi_key',
'iscsi_boot_policies.password',
'ldap_policies.binding_password',
'local_user_policies.local_user_password',
'persistent_memory_policies.secure_passphrase',
'snmp_policies.access_community_string',
'snmp_policies.password',
'snmp_policies.trap_community_string',
'virtual_media_policies.vmedia_password'
]
for var in vars:
policy_type = 'policies'
policy = '%s' % (var.split('.')[0])
policies,json_data = policies_parse(org, policy_type, policy)
y = var.split('.')[0]
z = var.split('.')[1]
if y == 'persistent_memory_policies':
if len(policies) > 0:
varValue = z
tfc_sensitive_variables(varValue, jsonData, **templateVars)
else:
for keys, values in json_data.items():
for key, value in values.items():
for k, v in value.items():
if 'local_user' in keys and k == 'enforce_strong_password':
templateVars['enforce_strong_password'] = v
if k == z:
if not v == 0:
if y == 'iscsi_boot_policies':
varValue = 'iscsi_boot_password'
else:
varValue = '%s_%s' % (k, v)
templateVars = tfc_sensitive_variables(varValue, jsonData, templateVars)
terraform_cloud().tfcVariables(**templateVars)
elif k == 'binding_parameters':
for ka, va in v.items():
if ka == 'bind_method':
if va == 'ConfiguredCredentials':
varValue = 'binding_parameters_password'
templateVars = tfc_sensitive_variables(varValue, jsonData, templateVars)
terraform_cloud().tfcVariables(**templateVars)
elif k == 'users' or k == 'vmedia_mappings':
for ka, va in v.items():
for kb, vb in va.items():
if kb == 'password':
varValue = '%s_%s' % (z, vb)
templateVars = tfc_sensitive_variables(varValue, jsonData, templateVars)
terraform_cloud().tfcVariables(**templateVars)
elif k == 'snmp_users' and z == 'password':
for ka, va in v.items():
for kb, vb in va.items():
if kb == 'auth_password':
varValue = 'snmp_auth_%s_%s' % (z, vb)
templateVars = tfc_sensitive_variables(varValue, jsonData, templateVars)
terraform_cloud().tfcVariables(**templateVars)
elif kb == 'privacy_password':
varValue = 'snmp_privacy_%s_%s' % (z, vb)
templateVars = tfc_sensitive_variables(varValue, jsonData, templateVars)
terraform_cloud().tfcVariables(**templateVars)
tfcb_config.append({'backend':'remote','org':org})
name_prefix = 'dummy'
type = 'pools'
policies_p1(name_prefix, org, type).intersight(easy_jsonData, tfcb_config)
type = 'policies'
policies_p1(name_prefix, org, type).intersight(easy_jsonData, tfcb_config)
type = 'profiles'
policies_p1(name_prefix, org, type).intersight(easy_jsonData, tfcb_config)
type = 'ucs_domain_profiles'
policies_p1(name_prefix, org, type).intersight(easy_jsonData, tfcb_config)
else:
valid = False
while valid == False:
templateVars = {}
templateVars["Description"] = f'Will You be utilizing Local or Terraform Cloud'
templateVars["varInput"] = f'Will you be utilizing Terraform Cloud?'
templateVars["varDefault"] = 'Y'
templateVars["varName"] = 'Terraform Type'
runTFCB = varBoolLoop(**templateVars)
if runTFCB == False:
tfcb_config.append({'backend':'local','org':org,'tfc_organization':'default'})
tfcb_config.append({'policies':'','pools':'','ucs_domain_profiles':''})
name_prefix = 'dummy'
type = 'pools'
policies_p1(name_prefix, org, type).intersight(easy_jsonData, tfcb_config)
type = 'policies'
policies_p1(name_prefix, org, type).intersight(easy_jsonData, tfcb_config)
type = 'profiles'
policies_p1(name_prefix, org, type).intersight(easy_jsonData, tfcb_config)
type = 'ucs_domain_profiles'
policies_p1(name_prefix, org, type).intersight(easy_jsonData, tfcb_config)
valid = True
else:
valid = True
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Skipping Step to Create Terraform Cloud Workspaces.')
print(f' Moving to last step to Confirm the Intersight Organization Exists.')
print(f'\n-------------------------------------------------------------------------------------------\n')
def intersight_org_check(home, org, args):
check_org = True
while check_org == True:
print(f'\n-------------------------------------------------------------------------------------------\n')
question = input(f'Do You Want to Check Intersight for the Organization {org}? Enter "Y" or "N" [Y]: ')
if question == 'Y' or question == '':
# Login to Intersight API
api_client = credentials.config_credentials(home, args)
#========================================================================
# Create Intersight API instance and Verify if the Resource Group Exists
#========================================================================
api_handle = resource_api.ResourceApi(api_client)
query_filter = f"Name eq '{org}_rg'"
kwargs = dict(filter=query_filter)
rg_list = api_handle.get_resource_group_list(**kwargs)
resourceGroup = f'{org}_rg'
if not rg_list.results:
api_body = {
"ClassId":"resource.Group",
"Name":resourceGroup,
"ObjectType":"resource.Group"
}
resource_group = api_handle.create_resource_group(api_body)
rg_2nd_list = api_handle.get_resource_group_list(**kwargs)
if rg_2nd_list.results:
rg_moid = rg_2nd_list.results[0].moid
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Resource Group {org}_rg has the Moid of {rg_moid},')
print(f' which was just Created.')
print(f'\n-------------------------------------------------------------------------------------------\n')
elif rg_list.results:
rg_moid = rg_list.results[0].moid
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Resource Group {org}_rg has the Moid of {rg_moid},')
print(f' which already exists.')
print(f'\n-------------------------------------------------------------------------------------------\n')
#=============================================================
# Create Intersight API instance and Verify if the Org Exists
#=============================================================
api_handle = organization_api.OrganizationApi(api_client)
query_filter = f"Name eq '{org}'"
kwargs = dict(filter=query_filter)
org_list = api_handle.get_organization_organization_list(**kwargs)
if not org_list.results:
api_body = {
"ClassId":"organization.Organization",
"Name":org,
"ObjectType":"organization.Organization",
"ResourceGroups":[{
"ClassId":"mo.MoRef",
"Moid": rg_moid,
"ObjectType":"resource.Group"
}]
}
organization = api_handle.create_organization_organization(api_body)
org_2nd_list = api_handle.get_organization_organization_list(**kwargs)
if org_2nd_list.results:
org_moid = org_2nd_list.results[0].moid
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Organization {org} has the Moid of {org_moid},')
print(f' which was just Created.')
print(f'\n-------------------------------------------------------------------------------------------\n')
elif org_list.results:
org_moid = org_list.results[0].moid
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Organization {org} has the Moid of {org_moid},')
print(f' which already exists.')
print(f'\n-------------------------------------------------------------------------------------------\n')
check_org = False
elif question == 'N':
check_org = False
else:
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Error!! Invalid Value. Please enter "Y" or "N".')
print(f'\n-------------------------------------------------------------------------------------------\n')
def process_imm_transition(json_data):
print(f'\n---------------------------------------------------------------------------------------\n')
print(f' Starting the Easy IMM Transition Wizard!')
print(f'\n---------------------------------------------------------------------------------------\n')
type = 'pools'
orgs = imm_transition(json_data, type).return_orgs()
imm_transition(json_data, type).ip_pools()
imm_transition(json_data, type).iqn_pools()
imm_transition(json_data, type).mac_pools()
imm_transition(json_data, type).uuid_pools()
imm_transition(json_data, type).wwnn_pools()
imm_transition(json_data, type).wwpn_pools()
type = 'policies'
imm_transition(json_data, type).bios_policies()
imm_transition(json_data, type).boot_order_policies()
imm_transition(json_data, type).ethernet_adapter_policies()
imm_transition(json_data, type).ethernet_network_control_policies()
imm_transition(json_data, type).ethernet_network_group_policies()
imm_transition(json_data, type).ethernet_network_policies()
imm_transition(json_data, type).ethernet_qos_policies()
imm_transition(json_data, type).fibre_channel_adapter_policies()
imm_transition(json_data, type).fibre_channel_network_policies()
imm_transition(json_data, type).fibre_channel_qos_policies()
imm_transition(json_data, type).flow_control_policies()
imm_transition(json_data, type).imc_access_policies()
imm_transition(json_data, type).ipmi_over_lan_policies()
imm_transition(json_data, type).iscsi_adapter_policies()
imm_transition(json_data, type).iscsi_boot_policies()
imm_transition(json_data, type).iscsi_static_target_policies()
imm_transition(json_data, type).lan_connectivity_policies()
imm_transition(json_data, type).link_aggregation_policies()
imm_transition(json_data, type).link_control_policies()
imm_transition(json_data, type).multicast_policies()
imm_transition(json_data, type).network_connectivity_policies()
imm_transition(json_data, type).ntp_policies()
imm_transition(json_data, type).port_policies()
imm_transition(json_data, type).power_policies()
imm_transition(json_data, type).san_connectivity_policies()
imm_transition(json_data, type).sd_card_policies()
imm_transition(json_data, type).serial_over_lan_policies()
imm_transition(json_data, type).snmp_policies()
imm_transition(json_data, type).storage_policies()
imm_transition(json_data, type).switch_control_policies()
imm_transition(json_data, type).syslog_policies()
imm_transition(json_data, type).system_qos_policies()
imm_transition(json_data, type).thermal_policies()
imm_transition(json_data, type).virtual_kvm_policies()
imm_transition(json_data, type).virtual_media_policies()
imm_transition(json_data, type).vlan_policies()
imm_transition(json_data, type).vsan_policies()
type = 'profiles'
imm_transition(json_data, type).ucs_chassis_profiles()
type = 'ucs_domain_profiles'
imm_transition(json_data, type).ucs_domain_profiles()
type = 'profiles'
imm_transition(json_data, type).ucs_server_profile_templates()
imm_transition(json_data, type).ucs_server_profiles()
# Return Organizations found in jsonData
return orgs
def process_wizard(easy_jsonData, jsonData):
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' Starting the Easy IMM Initial Configuration Wizard!')
print(f'\n-------------------------------------------------------------------------------------------\n')
templateVars = {}
templateVars["multi_select"] = False
jsonVars = easy_jsonData['wizard']
templateVars["var_description"] = jsonVars['mainMenu']['description']
templateVars["jsonVars"] = jsonVars['mainMenu']['enum']
templateVars["defaultVar"] = jsonVars['mainMenu']['default']
templateVars["varType"] = 'Main Menu'
main_menu = variablesFromAPI(**templateVars)
main_menu = main_menu.replace(' ', '_')
main_menu = main_menu.lower()
policy_list = []
if main_menu == 'deploy_domain_wizard':
policy_list = [
# Pools
'ip_pools',
'iqn_pools',
'mac_pools',
'resource_pools',
'uuid_pools',
'wwnn_pools',
'wwpn_pools',
# UCS Domain Policies and Profiles
'multicast_policies',
'vlan_policies',
'vsan_policies',
'flow_control_policies',
'link_aggregation_policies',
'link_control_policies',
'port_policies',
'network_connectivity_policies',
'ntp_policies',
'syslog_policies',
'snmp_policies',
'system_qos_policies',
'switch_control_policies',
'ucs_domain_profiles',
# UCS Chassis Policies and Profiles
'imc_access_policies',
'power_policies',
'thermal_policies',
'ucs_chassis_profiles',
# UCS Server Policies and Profiles
'bios_policies',
'boot_order_policies',
'virtual_media_policies',
'certificate_management_policies',
'ipmi_over_lan_policies',
'local_user_policies',
'serial_over_lan_policies',
'virtual_kvm_policies',
'sd_card_policies',
'storage_policies',
'ethernet_adapter_policies',
'ethernet_network_control_policies',
'ethernet_network_group_policies',
'ethernet_qos_policies',
'iscsi_adapter_policies',
'iscsi_boot_policies',
'iscsi_static_target_policies',
'fibre_channel_adapter_policies',
'fibre_channel_network_policies',
'fibre_channel_qos_policies',
'san_connectivity_policies',
'lan_connectivity_policies',
'ucs_server_template_profiles',
'ucs_server_profiles',
]
elif main_menu == 'deploy_domain_chassis_wizard':
policy_list = [
# UCS Chassis Policies and Profiles
'power_policies',
'thermal_policies',
'ucs_chassis_profiles'
]
elif main_menu == 'deploy_domain_fabric_interconnect_wizard':
policy_list = [
# UCS Domain Policies and Profiles
'multicast_policies',
'vlan_policies',
'vsan_policies',
'flow_control_policies',
'link_aggregation_policies',
'link_control_policies',
'port_policies',
'ntp_policies',
'network_connectivity_policies',
'system_qos_policies',
'switch_control_policies',
'ucs_domain_profiles',
]
elif main_menu == 'deploy_domain_servers_wizard':
policy_list = [
# Pools
'ip_pools',
'iqn_pools',
'mac_pools',
'resource_pools',
'uuid_pools',
'wwnn_pools',
'wwpn_pools',
# UCS Server Policies and Profiles
'bios_policies',
'boot_order_policies',
'virtual_media_policies',
'certificate_management_policies',
'ipmi_over_lan_policies',
'local_user_policies',
'serial_over_lan_policies',
'virtual_kvm_policies',
'sd_card_policies',
'storage_policies',
'ethernet_adapter_policies',
'ethernet_network_control_policies',
'ethernet_network_group_policies',
'ethernet_qos_policies',
'iscsi_adapter_policies',
'iscsi_boot_policies',
'iscsi_static_target_policies',
'fibre_channel_adapter_policies',
'fibre_channel_network_policies',
'fibre_channel_qos_policies',
'san_connectivity_policies',
'lan_connectivity_policies',
'ucs_server_template_profiles',
'ucs_server_profiles',
]
elif main_menu == 'deploy_standalone_servers_wizard':
policy_list = [
# Pools
'ip_pools',
'resource_pools',
# UCS Server Policies and Profiles
'bios_policies',
'boot_order_policies',
'persistent_memory_policies',
'virtual_media_policies',
'device_connector_policies',
'ipmi_over_lan_policies',
'ldap_policies',
'local_user_policies',
'network_connectivity_policies',
'ntp_policies',
'serial_over_lan_policies',
'smtp_policies',
'snmp_policies',
'ssh_policies',
'syslog_policies',
'virtual_kvm_policies',
'sd_card_policies',
'storage_policies',
'adapter_configuration_policies',
'ethernet_adapter_policies',
'ethernet_network_control_policies',
'ethernet_network_policies',
'ethernet_qos_policies',
'iscsi_adapter_policies',
'iscsi_boot_policies',
'iscsi_static_target_policies',
'fibre_channel_adapter_policies',
'fibre_channel_network_policies',
'fibre_channel_qos_policies',
'san_connectivity_policies',
'lan_connectivity_policies',
'ucs_server_template_profiles',
'ucs_server_profiles',
]
# Quick Start
elif '-_domain_-' in main_menu:
policy_list = [
'quick_start_pools',
'quick_start_domain_policies',
'quick_start_lan_san_policies',
'quick_start_ucs_chassis',
'quick_start_ucs_servers',
]
if 'm2' in main_menu:
policy_list.append('quick_start_vmware_m2')
elif 'raid' in main_menu:
policy_list.append('quick_start_vmware_raid1')
elif 'stateless' in main_menu:
policy_list.append('quick_start_vmware_stateless')
policy_list.append('quick_start_server_profile')
if main_menu == 'deploy_individual_policies':
templateVars["var_description"] = jsonVars['Individual']['description']
templateVars["jsonVars"] = jsonVars['Individual']['enum']
templateVars["defaultVar"] = jsonVars['Individual']['default']
templateVars["varType"] = 'Configuration Type'
type_menu = variablesFromAPI(**templateVars)
multi_select_descr = '\n - Single policy: 1 or 5\n'\
' - List of Policies: 1,2,3\n'\
' - Range of Policies: 1-3,5-6\n'
templateVars["multi_select"] = True
if type_menu == 'Policies':
templateVars["var_description"] = jsonVars['Policies']['description'] + multi_select_descr
templateVars["jsonVars"] = jsonVars['Policies']['enum']
templateVars["defaultVar"] = jsonVars['Policies']['default']
templateVars["varType"] = 'Policies'
policies_list = variablesFromAPI(**templateVars)
for line in policies_list:
line = line.replace(' ', '_')
line = line.replace('-', '_')
line = line.lower()
policy_list.append(line)
elif type_menu == 'Pools':
templateVars["var_description"] = jsonVars['Pools']['description'] + multi_select_descr
templateVars["jsonVars"] = jsonVars['Pools']['enum']
templateVars["defaultVar"] = jsonVars['Pools']['default']
templateVars["varType"] = 'Pools'
policies_list = variablesFromAPI(**templateVars)
for line in policies_list:
line = line.replace(' ', '_')
line = line.replace('-', '_')
line = line.lower()
policy_list.append(line)
elif type_menu == 'Profiles':
templateVars["var_description"] = jsonVars['Profiles']['description'] + multi_select_descr
templateVars["jsonVars"] = sorted(jsonVars['Profiles']['enum'])
templateVars["defaultVar"] = jsonVars['Profiles']['default']
templateVars["varType"] = 'Profiles'
policies_list = variablesFromAPI(**templateVars)
for line in policies_list:
line = line.replace(' ', '_')
line = line.replace('-', '_')
line = line.lower()
policy_list.append(line)
valid = False
while valid == False:
org = input('What is your Intersight Organization Name? [default]: ')
if org == '':
org = 'default'
valid = validating.org_rule('Intersight Organization', org, 1, 62)
if not main_menu == 'skip_policy_deployment':
print(f'\n-------------------------------------------------------------------------------------------\n')
print(f' By Default, the Intersight Organization will be used as the Name Prefix for Pools ')
print(f' and Policies. To Assign a different Prefix to the Pools and Policies use the prefix ')
print(f' options below. As Options, a different prefix for UCS domain policies and a prefix')
print(f' for Pools and Server Policies can be entered to override the default behavior.')
print(f'\n-------------------------------------------------------------------------------------------\n')
if not 'quick_start' in main_menu:
valid = False
while valid == False:
domain_prefix = input('Enter a Name Prefix for Domain Profile Policies. [press enter to skip]: ')
if domain_prefix == '':
valid = True
else:
valid = validating.name_rule(f"Name Prefix", domain_prefix, 1, 62)
valid = False
while valid == False:
name_prefix = input('Enter a Name Prefix for Pools and Server Policies. [press enter to skip]: ')
if name_prefix == '':
valid = True
else:
valid = validating.name_rule(f"Name Prefix", name_prefix, 1, 62)
else:
domain_prefix = 'default'
name_prefix = 'default'
kwargs = {}
for policy in policy_list:
opSystem = platform.system()
if os.environ.get('TF_DEST_DIR') is None:
tfDir = 'Intersight'
else:
tfDir = os.environ.get('TF_DEST_DIR')
if tfDir[-1] == '\\' or tfDir[-1] == '/':
tfDir = tfDir[:-1]
kwargs.update({'opSystem':opSystem,'tfDir':tfDir})
#==============================================
# Intersight Pools
#==============================================
type = 'pools'
if policy == 'ip_pools':
pools(name_prefix, org, type).ip_pools(jsonData, easy_jsonData, **kwargs)
elif policy == 'iqn_pools':
pools(name_prefix, org, type).iqn_pools(jsonData, easy_jsonData, **kwargs)
elif policy == 'mac_pools':
pools(name_prefix, org, type).mac_pools(jsonData, easy_jsonData, **kwargs)
elif policy == 'wwnn_pools':
pools(name_prefix, org, type).wwnn_pools(jsonData, easy_jsonData, **kwargs)
elif policy == 'wwpn_pools':
pools(name_prefix, org, type).wwpn_pools(jsonData, easy_jsonData, **kwargs)
elif policy == 'uuid_pools':
pools(name_prefix, org, type).uuid_pools(jsonData, easy_jsonData, **kwargs)
#==============================================
# Intersight Policies
#==============================================
type = 'policies'
if policy == 'adapter_configuration_policies':
policies_p1(name_prefix, org, type).adapter_configuration_policies(jsonData, easy_jsonData, **kwargs)
if policy == 'bios_policies':
policies_p1(name_prefix, org, type).bios_policies(jsonData, easy_jsonData, **kwargs)
if policy == 'boot_order_policies':
policies_p1(name_prefix, org, type).boot_order_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'certificate_management_policies':
policies_p1(name_prefix, org, type).certificate_management_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'device_connector_policies':
policies_p1(name_prefix, org, type).device_connector_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'ethernet_adapter_policies':
policies_lan(name_prefix, org, type).ethernet_adapter_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'ethernet_network_control_policies':
policies_lan(name_prefix, org, type).ethernet_network_control_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'ethernet_network_group_policies':
policies_lan(name_prefix, org, type).ethernet_network_group_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'ethernet_network_policies':
policies_lan(name_prefix, org, type).ethernet_network_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'ethernet_qos_policies':
policies_lan(name_prefix, org, type).ethernet_qos_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'fibre_channel_adapter_policies':
policies_san(name_prefix, org, type).fibre_channel_adapter_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'fibre_channel_network_policies':
policies_san(name_prefix, org, type).fibre_channel_network_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'fibre_channel_qos_policies':
policies_san(name_prefix, org, type).fibre_channel_qos_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'flow_control_policies':
policies_p1(domain_prefix, org, type).flow_control_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'imc_access_policies':
policies_p1(name_prefix, org, type).imc_access_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'ipmi_over_lan_policies':
policies_p1(name_prefix, org, type).ipmi_over_lan_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'iscsi_adapter_policies':
policies_lan(name_prefix, org, type).iscsi_adapter_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'iscsi_boot_policies':
policies_lan(name_prefix, org, type).iscsi_boot_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'iscsi_static_target_policies':
policies_lan(name_prefix, org, type).iscsi_static_target_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'ldap_policies':
policies_p1(name_prefix, org, type).ldap_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'link_aggregation_policies':
policies_p1(domain_prefix, org, type).link_aggregation_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'link_control_policies':
policies_p1(domain_prefix, org, type).link_control_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'local_user_policies':
policies_p2(name_prefix, org, type).local_user_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'multicast_policies':
policies_vxan(domain_prefix, org, type).multicast_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'network_connectivity_policies':
policies_p2(name_prefix, org, type).network_connectivity_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'ntp_policies':
policies_p2(name_prefix, org, type).ntp_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'persistent_memory_policies':
policies_p2(name_prefix, org, type).persistent_memory_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'port_policies':
policies_p2(domain_prefix, org, type).port_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'power_policies':
policies_p3(name_prefix, org, type).power_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'san_connectivity_policies':
policies_san(name_prefix, org, type).san_connectivity_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'lan_connectivity_policies':
policies_lan(name_prefix, org, type).lan_connectivity_policies(jsonData, easy_jsonData, **kwargs)
# elif policy == 'sd_card_policies':
# policies(name_prefix, org, type).sd_card_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'serial_over_lan_policies':
policies_p3(name_prefix, org, type).serial_over_lan_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'smtp_policies':
policies_p3(name_prefix, org, type).smtp_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'snmp_policies':
policies_p3(name_prefix, org, type).snmp_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'ssh_policies':
policies_p3(name_prefix, org, type).ssh_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'storage_policies':
policies_p3(name_prefix, org, type).storage_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'switch_control_policies':
policies_p3(domain_prefix, org, type).switch_control_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'syslog_policies':
policies_p3(name_prefix, org, type).syslog_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'system_qos_policies':
policies_p3(domain_prefix, org, type).system_qos_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'thermal_policies':
policies_p3(name_prefix, org, type).thermal_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'virtual_kvm_policies':
policies_p3(name_prefix, org, type).virtual_kvm_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'virtual_media_policies':
policies_p3(name_prefix, org, type).virtual_media_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'vlan_policies':
policies_vxan(domain_prefix, org, type).vlan_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'vsan_policies':
policies_vxan(domain_prefix, org, type).vsan_policies(jsonData, easy_jsonData, **kwargs)
#==============================================
# Intersight Profiles
#==============================================
type = 'profiles'
if policy == 'ucs_chassis_profiles':
profiles(domain_prefix, org, type).ucs_chassis_profiles(jsonData, easy_jsonData, **kwargs)
elif policy == 'ucs_server_profile_templates':
profiles(name_prefix, org, type).ucs_server_profile_templates(jsonData, easy_jsonData, **kwargs)
elif policy == 'ucs_server_profiles':
profiles(name_prefix, org, type).ucs_server_profiles(jsonData, easy_jsonData, **kwargs)
type = 'ucs_domain_profiles'
if policy == 'ucs_domain_profiles':
profiles(domain_prefix, org, type).ucs_domain_profiles(jsonData, easy_jsonData, name_prefix, **kwargs)
#==============================================
# Quick Start - Pools
#==============================================
type = 'pools'
if 'quick_start_pools' in policy:
primary_dns,secondary_dns = quick_start(domain_prefix, org, type).pools(jsonData, easy_jsonData, **kwargs)
kwargs.update({'primary_dns':primary_dns,'secondary_dns':secondary_dns})
#==============================================
# Quick Start - Policies
#==============================================
type = 'policies'
if 'quick_start_domain_policies' in policy or 'quick_start_rack_policies' in policy:
Config = True
if 'domain' in policy:
# kwargs = {'primary_dns': '208.67.220.220', 'secondary_dns': ''}
kwargs.update({'server_type':'FIAttached'})
Config,vlan_policy,vsan_a,vsan_b,fc_ports,mtu = quick_start(
name_prefix, org, type
).domain_policies(
jsonData, easy_jsonData, **kwargs
)
if Config == True:
kwargs.update({'vlan_policy':vlan_policy["vlan_policy"],'vlans':vlan_policy["vlans"],'native_vlan':vlan_policy["native_vlan"]})
kwargs.update({'vsan_a':vsan_a,'vsan_b':vsan_b,'fc_ports':fc_ports})
kwargs.update({'mtu':mtu})
else:
kwargs.update({'server_type':'Standalone'})
kwargs.update({'fc_ports':[]})
type = 'policies'
Config = quick_start(name_prefix, org, type).standalone_policies(jsonData, easy_jsonData, **kwargs)
if not Config == False:
# kwargs = {'primary_dns': '208.67.220.220', 'secondary_dns': '', 'server_type': 'FIAttached', 'vlan_policy': 'asgard-ucs', 'vlans': '1-99', 'native_vlan': '1', 'vsan_a': 100, 'vsan_b': 200, 'fc_ports': [1, 2, 3, 4], 'mtu': 9216}
quick_start(name_prefix, org, type).server_policies(jsonData, easy_jsonData, **kwargs)
elif 'quick_start_lan_san_policies' in policy:
type = 'policies'
if not Config == False:
quick_start(domain_prefix, org, type).lan_san_policies(jsonData, easy_jsonData, **kwargs)
elif policy == 'quick_start_vmware_m2':
if not Config == False:
quick_start(name_prefix, org, type).vmware_m2(**kwargs)