-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwcpctl.py
executable file
·711 lines (635 loc) · 35.5 KB
/
wcpctl.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
#!/usr/bin/env python3
#
# “Copyright 2020 VMware, Inc.”
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software
# and associated documentation files (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import requests
import json
import time
import yaml
import sys
import uuid
import argparse
import getpass
import os
parser = argparse.ArgumentParser(description='wcpctl controls for managing Supervisor Clusters in vSphere 7 with K8s. Uses YAML configuration files to setup and manage the Supervisor Cluster. Find additional information at: https://github.io/papivot/wcpctl')
parser.add_argument('--version', action='version',version='%(prog)s v0.3')
subparsers = parser.add_subparsers(help='Commands',dest='verb')
# A create command
create_parser = subparsers.add_parser('create', help='Create WCP object(s)')
create_parser.add_argument('filename', action='store', help='YAML file with WCP object configuration. See examples for help')
create_parser.add_argument('-u', action="store", dest="userid", help='VCenter userid. If not provided, will default to administrator@vsphere.local')
# A apply command
apply_parser = subparsers.add_parser('apply', help='Apply configuration changes to WCP object(s)')
apply_parser.add_argument('filename', action='store', help='YAML file with WCP object configuration. See examples for help')
apply_parser.add_argument('-u', action="store", dest="userid", help='VCenter userid. If not provided, will default to administrator@vsphere.local')
# A delete command
delete_parser = subparsers.add_parser('delete', help='Delete WCP object(s)')
delete_parser.add_argument('filename', action='store', help='YAML file with WCP object configuration. See examples for help')
delete_parser.add_argument('-u', action="store", dest="userid", help='VCenter userid. If not provided, will default to administrator@vsphere.local')
# A delete command
describe_parser = subparsers.add_parser('describe', help='Describe a WCP object(s)')
describe_parser.add_argument('filename', action='store', help='YAML file with WCP metadata info. See examples for help')
describe_parser.add_argument('-u', action="store", dest="userid", help='VCenter userid. If not provided, will default to administrator@vsphere.local')
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
headers = {'content-type': 'application/json'}
cmd = parser.parse_args()
verb = cmd.verb
filename = cmd.filename
if cmd.userid:
userid = cmd.userid
else:
if not os.environ.get('WCP_USERNAME'):
userid = "administrator@vsphere.local"
else:
userid = os.environ.get('WCP_USERNAME')
if not os.environ.get('WCP_PASSWORD'):
password = getpass.getpass(prompt='Password: ')
else:
password = os.environ.get('WCP_PASSWORD')
if not os.environ.get('SKIP_COMPAT_CHECK'):
skip_compat = False
else:
skip_compat = True
def generate_random_uuid():
return str(uuid.uuid4())
def get_storage_id(storage_name,dc):
json_response = s.get('https://'+vcip+'/rest/vcenter/datastore?filter.datacenters='+dc+'&filter.names='+storage_name)
if json_response.ok:
results = json.loads(json_response.text)["value"]
for result in results:
if result["name"] == storage_name:
return result["datastore"]
else:
return 0
return 0
def get_storage_policy(sp_name):
json_response = s.get('https://' + vcip + '/rest/vcenter/storage/policies')
if json_response.ok:
results = json.loads(json_response.text)["value"]
for result in results:
if result["name"] == sp_name:
return result["policy"]
else:
return 0
else:
return 0
def get_content_library(cl_name):
json_response = s.get('https://' + vcip + '/rest/com/vmware/content/library')
if json_response.ok:
results = json.loads(json_response.text)["value"]
for result in results:
json_response = s.get('https://' + vcip + '/rest/com/vmware/content/library/id:' + result)
if json_response.ok:
cl_library = json.loads(json_response.text)["value"]
if cl_library["name"] == cl_name:
return cl_library["id"]
else:
return 0
return 0
def get_nsx_switch(cluster):
json_response = s.get('https://'+vcip+'/api/vcenter/namespace-management/distributed-switch-compatibility?cluster='+cluster+'&compatible=true')
if json_response.ok:
results = json.loads(json_response.text)
# Making assumption that there is only 1 distributed switch.
nsx_sw_id = results[0]['distributed_switch']
return nsx_sw_id
else:
return 0
def get_nsx_edge_cluster(cluster,dvs):
json_response = s.get('https://'+vcip+'/api/vcenter/namespace-management/edge-cluster-compatibility?cluster='+cluster+'&compatible=true&distributed_switch='+dvs)
if json_response.ok:
results = json.loads(json_response.text)
edge_id = results[0]['edge_cluster']
return edge_id
else:
return 0
def get_mgmt_network(mgmt_nw_name, dc):
json_response = s.get('https://' + vcip + '/rest/vcenter/network?filter.datacenters=' + dc)
if json_response.ok:
results = json.loads(json_response.text)["value"]
for result in results:
if result["name"] == mgmt_nw_name:
return result["network"]
else:
return 0
return 0
def check_wcp_cluster_compatibility(cluster,net_p,skip_compat):
if skip_compat:
return 1
else:
json_response = s.get('https://' + vcip + '/api/vcenter/namespace-management/cluster-compatibility?network_provider='+ net_p)
if json_response.ok:
results = json.loads(json_response.text)
res = next((sub for sub in results if sub['cluster'] == cluster), None)
if res['compatible'] == True:
return 1
else:
return 0
else:
return 0
def check_wcp_cluster_status(cluster):
json_response = s.get('https://' + vcip + '/api/vcenter/namespace-management/clusters/' + cluster)
if json_response.ok:
result = json.loads(json_response.text)
if result["config_status"] == "RUNNING":
if result["kubernetes_status"] == "READY":
return result["api_server_cluster_endpoint"]
else:
return 0
else:
return 0
def check_wcp_harbor_status(cluster):
json_response = s.get('https://' + vcip + '/rest/vcenter/content/registries/harbor')
if json_response.ok:
results = json.loads(json_response.text)["value"]
for result in results:
if result["cluster"] == cluster:
return result["registry"]
else:
return 0
else:
return 0
def check_wcp_harbor_ui_url_status(cluster):
json_response = s.get('https://' + vcip + '/rest/vcenter/content/registries/harbor')
if json_response.ok:
results = json.loads(json_response.text)["value"]
for result in results:
if result["cluster"] == cluster:
return result["ui_access_url"]
else:
return 0
else:
return 0
def check_wcp_ns_status(ns_name):
json_response = s.get('https://' + vcip + '/api/vcenter/namespaces/instances/' + ns_name)
if json_response.ok:
result = json.loads(json_response.text)
if result["config_status"] == "RUNNING":
return 1
else:
return 0
else:
return 0
with open(filename, ) as f:
yamldocs = yaml.load_all(f, Loader=yaml.FullLoader)
for yamldoc in yamldocs:
objtype = yamldoc["kind"]
vcip = yamldoc["metadata"]["vcenter"]
datacenter = yamldoc["metadata"]["datacenter"]
cluster = yamldoc["metadata"]["cluster"]
spec = yamldoc["spec"]
s = "Global"
s = requests.Session()
s.verify = False
# Connect to VCenter and start a session
vcsession = s.post('https://' + vcip + '/rest/com/vmware/cis/session', auth=(userid, password))
if not vcsession.ok:
print("Session creation is failed, please check vcenter connection")
sys.exit()
token = json.loads(vcsession.text)["value"]
token_header = {'vmware-api-session-id': token}
# Based on the datacenter get all datacenters
datacenter_object = s.get('https://' + vcip + '/rest/vcenter/datacenter?filter.names=' + datacenter)
if len(json.loads(datacenter_object.text)["value"]) == 0:
print("No datacenter found, please enter valid datacenter name")
sys.exit()
datacenter_id = json.loads(datacenter_object.text)["value"][0].get("datacenter")
# Based on the cluster name get the cluster_id
cluster_object = s.get(
'https://' + vcip + '/rest/vcenter/cluster?filter.names=' + cluster + '&filter.datacenters=' + datacenter_id)
if len(json.loads(cluster_object.text)["value"]) == 0:
print("No cluster found, please enter valid cluster name")
sys.exit()
cluster_id = json.loads(cluster_object.text)["value"][0].get("cluster")
if verb == "create":
# create wcpCluster
if objtype == "wcpCluster":
del yamldoc["kind"]
del yamldoc["metadata"]
if check_wcp_cluster_compatibility(cluster_id, yamldoc["spec"]["network_provider"],skip_compat):
if not check_wcp_cluster_status(cluster_id):
temp1 = get_storage_policy(yamldoc["spec"].get("ephemeral_storage_policy"))
if not temp1:
print("wcpCluster/" + cluster + " check value for ephemeral_storage_policy")
sys.exit()
temp2 = get_storage_policy(yamldoc["spec"].get("master_storage_policy"))
if not temp2:
print("wcpCluster/" + cluster + " check value for master_storage_policy")
sys.exit()
temp3 = get_storage_policy(yamldoc["spec"]["image_storage"].get("storage_policy"))
if not temp3:
print("wcpCluster/" + cluster + " check value for storage_policy")
sys.exit()
temp4 = get_content_library(yamldoc["spec"].get("default_kubernetes_service_content_library"))
if not temp4:
print ("wcpCluster/"+cluster+" check value for default_kubernetes_service_content_library")
sys.exit()
temp5 = get_mgmt_network(yamldoc["spec"]["master_management_network"].get("network"), datacenter_id)
if not temp5:
print("wcpCluster/" + cluster + " check value for master_management_network - network")
sys.exit()
#Process for NSX config
if yamldoc["spec"]["network_provider"] == "NSXT_CONTAINER_PLUGIN":
temp6 = get_nsx_switch(cluster_id)
if not temp6:
print("wcpCluster/" + cluster + " no compatiable NSX switch for cluster")
sys.exit()
temp7 = get_nsx_edge_cluster(cluster_id, temp6)
if not temp7:
print("wcpCluster/" + cluster + " no compatiable NSX edge cluster")
sys.exit()
yamldoc["spec"]["ncp_cluster_network_spec"].update({"cluster_distributed_switch": temp6})
yamldoc["spec"]["ncp_cluster_network_spec"].update({"nsx_edge_cluster": temp7})
# Process BYO LB
else:
temp6 = get_mgmt_network(yamldoc["spec"]["workload_networks_spec"]["supervisor_primary_workload_network"]["vsphere_network"].get("portgroup"), datacenter_id)
if not temp6:
print("wcpCluster/" + cluster + " check value for master_management_network - network")
sys.exit()
yamldoc["spec"]["workload_networks_spec"]["supervisor_primary_workload_network"]["vsphere_network"].update({"portgroup": temp6})
# Update ...if any of the above is 0 then quit
yamldoc["spec"].update({"ephemeral_storage_policy": temp1})
yamldoc["spec"].update({"master_storage_policy": temp2})
yamldoc["spec"]["image_storage"].update({"storage_policy": temp3})
yamldoc["spec"].update({"default_kubernetes_service_content_library": temp4})
yamldoc["spec"]["master_management_network"].update({"network": temp5})
json_payload = json.loads(json.dumps(yamldoc["spec"]))
json_response = s.post('https://'+vcip+'/api/vcenter/namespace-management/clusters/'+cluster_id+'?action=enable', headers=headers,json=json_payload)
if json_response.ok:
print("wcpCluster/" + cluster + " creation started")
else:
print("wcpCluster/" + cluster + " creation failed")
print(json_response.text)
else:
print ("wcpCluster/"+cluster+" already operational at https://"+check_wcp_cluster_status(cluster_id))
else:
print("wcpCluster/" + cluster + " not compatiable")
# create wcpRegistry
if objtype == "wcpRegistry":
if not check_wcp_harbor_status(cluster_id):
del yamldoc["kind"]
del yamldoc["metadata"]
client_token = generate_random_uuid()
yamldoc.update({"client_token": client_token})
yamldoc["spec"].update({"cluster": cluster_id})
reg_creation_started = 0
i = 0
# Update variables and proceed
for sp in yamldoc["spec"]["storage"]:
temp1 = get_storage_policy(sp["policy"])
if temp1:
yamldoc["spec"]["storage"][i].update({"policy": temp1})
else:
print("wcpNamespace/" + spec["namespace"] + " invalid storage policy specified")
sys.exit()
i = i + 1
json_payload = json.loads(json.dumps(yamldoc))
headers.update({'vmware-api-session-id': token})
json_response = s.post('https://'+vcip+'/rest/vcenter/content/registries/harbor',headers=headers,json=json_payload)
if json_response.ok:
print("wcpRegistry/Harbor creation started")
reg_creation_started = 1
else:
print("wcpRegistry/Harbor failed")
print(json_response.text)
if reg_creation_started == 1:
while not check_wcp_harbor_ui_url_status(cluster_id):
print("Sleeping for 20 sec...")
time.sleep(20)
print("wcpRegistry/Harbor access URL " + check_wcp_harbor_ui_url_status(cluster_id))
else:
print("wcpRegistry/Harbor already running")
# create wcpNamespace
if objtype == "wcpNamespace":
if not check_wcp_ns_status(spec["namespace"]):
spec.update({"cluster": cluster_id})
i = 0
# Update variables and proceed
for sp in spec["storage_specs"]:
temp1 = get_storage_policy(sp["policy"])
if temp1:
spec["storage_specs"][i].update({"policy": temp1})
else:
print("wcpNamespace/" + spec["namespace"] + " invalid storage policy specified")
sys.exit()
i = i + 1
json_payload = json.loads(json.dumps(spec))
json_response = s.post('https://'+vcip+'/api/vcenter/namespaces/instances',headers=headers,json=json_payload)
if json_response.ok:
print("wcpNamespace/" + spec["namespace"] + " created")
else:
print("wcpNamespace/" + spec["namespace"] + " creation failed")
print(json_response.text)
else:
print("wcpNamespace/" + spec["namespace"] + " already exists. No changes made")
# create wcpContentLibrary
if objtype == "wcpContentLibrary":
if not get_content_library(yamldoc["spec"].get("name")):
del yamldoc["kind"]
del yamldoc["metadata"]
client_token = generate_random_uuid()
yamldoc.update({"client_token": client_token})
i = 0
for sb in yamldoc["spec"]["storage_backings"]:
temp1 = get_storage_id(sb["datastore_id"], datacenter_id)
if temp1:
yamldoc["spec"]["storage_backings"][i].update({"datastore_id": temp1})
else:
print("wcpContentLibrary invalid storage name specified")
sys.exit()
i = i + 1
yamldoc["create_spec"] = yamldoc.pop("spec")
json_payload = json.loads(json.dumps(yamldoc))
headers.update({'vmware-api-session-id': token})
json_response = s.post('https://'+vcip+'/rest/com/vmware/content/subscribed-library',headers=headers,json=json_payload)
if json_response.ok:
print("wcpContentLibrary/Subscribed_library created")
else:
print("wcpContentLibrary/Subscribed_library created")
print(json_response.text)
else:
print("wcpContentLibrary/Subscribed_library already running")
elif verb == "delete":
# delete wcpCluster
if objtype == "wcpCluster":
if check_wcp_cluster_status(cluster_id):
# Check if you want to delete ???
json_response = s.post('https://'+vcip+'/api/vcenter/namespace-management/clusters/'+cluster_id+'?action=disable')
if json_response.ok:
print("wcpCluster/" + cluster + " delete initiated")
else:
print("wcpCluster/" + cluster + " delete failed")
print(json_response.text)
else:
print("wcpCluster/" + cluster + " not operational")
# delete wcpRegistry
if objtype == "wcpRegistry":
reg_destruction_started = 0
harbor_id = check_wcp_harbor_status(cluster_id)
if harbor_id:
json_response = s.delete('https://'+vcip+'/rest/vcenter/content/registries/harbor/'+harbor_id, headers=token_header)
if (json_response.ok):
print("wcpRegistry/Harbor deletion started")
reg_destruction_started = 1
else:
print("wcpRegistry/Harbor deletion failed")
print(json_response.text)
if reg_destruction_started == 1:
while check_wcp_harbor_status(cluster_id):
print("Sleeping for 20 sec...")
time.sleep(20)
else:
print("wcpRegistry/Harbor not found")
# delete wcpNamespace
if objtype == "wcpNamespace":
json_response = s.delete('https://' + vcip + '/api/vcenter/namespaces/instances/' + spec["namespace"])
if (json_response.ok):
print("wcpNamespace/" + spec["namespace"] + " deleted")
else:
print("wcpNamespace/" + spec["namespace"] + " deletion failed")
print(json_response.text)
# delete wcpContentLibrary
if objtype == "wcpContentLibrary":
contentlibrary_id = get_content_library(yamldoc["spec"].get("name"))
if contentlibrary_id:
json_response = s.delete('https://'+vcip+'/rest/com/vmware/content/subscribed-library/id:'+contentlibrary_id)
if (json_response.ok):
print("wcpContentLibrary/Subscribed_library successfully deleted")
else:
print("wcpContentLibrary/Subscribed_library deletion failed")
result = json.loads(json_response.text)
print(json.dumps(result, indent=2, sort_keys=True))
else:
print("wcpContentLibrary/Subscribed library not found")
elif verb == "apply":
# apply wcpCluster
if objtype == "wcpCluster":
del yamldoc["kind"]
del yamldoc["metadata"]
if check_wcp_cluster_compatibility(cluster_id, yamldoc["spec"]["network_provider"],skip_compat):
if not check_wcp_cluster_status(cluster_id):
temp1 = get_storage_policy(yamldoc["spec"].get("ephemeral_storage_policy"))
temp2 = get_storage_policy(yamldoc["spec"].get("master_storage_policy"))
temp3 = get_storage_policy(yamldoc["spec"]["image_storage"].get("storage_policy"))
temp4 = get_content_library(yamldoc["spec"].get("default_kubernetes_service_content_library"))
temp5 = get_mgmt_network(yamldoc["spec"]["master_management_network"].get("network"), datacenter_id)
if not temp5:
print("wcpCluster/" + cluster + " check value for master_management_network - network")
sys.exit()
#Process for NSX config
if yamldoc["spec"]["network_provider"] == "NSXT_CONTAINER_PLUGIN":
temp6 = get_nsx_switch(cluster_id)
if not temp6:
print("wcpCluster/" + cluster + " no compatiable NSX switch for cluster")
sys.exit()
temp7 = get_nsx_edge_cluster(cluster_id, temp6)
if not temp7:
print("wcpCluster/" + cluster + " no compatiable NSX edge cluster")
sys.exit()
yamldoc["spec"]["ncp_cluster_network_spec"].update({"cluster_distributed_switch": temp6})
yamldoc["spec"]["ncp_cluster_network_spec"].update({"nsx_edge_cluster": temp7})
# Process BYO LB
else:
temp6 = get_mgmt_network(yamldoc["spec"]["workload_networks_spec"]["supervisor_primary_workload_network"]["vsphere_network"].get("portgroup"), datacenter_id)
if not temp6:
print("wcpCluster/" + cluster + " check value for master_management_network - network")
sys.exit()
yamldoc["spec"]["workload_networks_spec"]["supervisor_primary_workload_network"]["vsphere_network"].update({"portgroup": temp6})
# Update any of the above is 0 then quit
yamldoc["spec"].update({"ephemeral_storage_policy": temp1})
yamldoc["spec"].update({"master_storage_policy": temp2})
yamldoc["spec"]["image_storage"].update({"storage_policy": temp3})
yamldoc["spec"].update({"default_kubernetes_service_content_library": temp4})
yamldoc["spec"]["master_management_network"].update({"network": temp5})
json_payload = json.loads(json.dumps(yamldoc["spec"]))
json_response = s.post('https://'+vcip+'/api/vcenter/namespace-management/clusters/'+cluster_id+'?action=enable', headers=headers,json=json_payload)
if json_response.ok:
print("wcpCluster/" + cluster + " creation started")
else:
print("wcpCluster/" + cluster + " creation failed")
print(json_response.text)
else:
print ("wcpCluster/"+cluster+" already operational at https://"+check_wcp_cluster_status(cluster_id))
else:
print("wcpCluster/" + cluster + " not compatiable")
# TO DO : stub to patch server
# json_response = s.patch('https://'+vcip+'/api/vcenter/namespace-management/clusters/'+cluster_id, headers=headers,json=json_payload)
# if json_response.ok:
# print ("wcpCluster/"+cluster+" updated")
# else:
# print ("wcpCluster/"+cluster+" update failed")
# print (json_response.text)
# To rotate password
# json_response = s.post('https://'+vcip+'/api/vcenter/namespace-management/clusters/'+cluster_id+'?action=rotate_password')
# if json_response.ok:
# print ("wcpCluster/"+cluster+" password rotated")
# else:
# print ("wcpCluster/"+cluster+" password rotation failed")
# print (json_response.text)
# apply wcpRegistry
if objtype == "wcpRegistry":
# same as create as no modify option
if not check_wcp_harbor_status(cluster_id):
del yamldoc["kind"]
del yamldoc["metadata"]
client_token = generate_random_uuid()
yamldoc.update({"client_token": client_token})
yamldoc["spec"].update({"cluster": cluster_id})
i = 0
# Update variables and proceed
for sp in yamldoc["spec"]["storage"]:
temp1 = get_storage_policy(sp["policy"])
if temp1:
yamldoc["spec"]["storage"][i].update({"policy": temp1})
else:
print("wcpNamespace/" + spec["namespace"] + " invalid storage policy specified")
sys.exit()
i = i + 1
json_payload = json.loads(json.dumps(yamldoc))
headers.update({'vmware-api-session-id': token})
json_response = s.post('https://'+vcip+'/rest/vcenter/content/registries/harbor',headers=headers,json=json_payload)
if json_response.ok:
print("wcpRegistry/Harbor created")
reg_creation_started = 1
else:
print("wcpRegistry/Harbor failed")
print(json_response.text)
if reg_creation_started == 1:
while not check_wcp_harbor_ui_url_status(cluster_id):
print("Sleeping for 20 sec...")
time.sleep(20)
print("wcpRegistry/Harbor access URL " + check_wcp_harbor_ui_url_status(cluster_id))
else:
print("wcpRegistry/Harbor already running")
# apply wcpNamespace
if objtype == "wcpNamespace":
if check_wcp_ns_status(spec["namespace"]):
spec.update({"cluster": cluster_id})
i = 0
# Update variables and proceed
for sp in spec["storage_specs"]:
temp1 = get_storage_policy(sp["policy"])
if temp1:
spec["storage_specs"][i].update({"policy": temp1})
else:
print("wcpNamespace/" + spec["namespace"] + " invalid storage policy specified")
sys.exit()
i = i + 1
json_payload = json.loads(json.dumps(spec))
json_response = s.put('https://'+vcip+'/api/vcenter/namespaces/instances/'+spec["namespace"],headers=headers,json=json_payload)
if json_response.ok:
print("wcpNamespace/" + spec["namespace"] + " updated")
else:
print("wcpNamespace/" + spec["namespace"] + " update failed")
print(json_response.text)
else:
spec.update({"cluster": cluster_id})
i = 0
# Update variables and proceed
for sp in spec["storage_specs"]:
temp1 = get_storage_policy(sp["policy"])
if temp1:
spec["storage_specs"][i].update({"policy": temp1})
else:
print("wcpNamespace/" + spec["namespace"] + " invalid storage policy specified")
sys.exit()
i = i + 1
json_payload = json.loads(json.dumps(spec))
json_response = s.post('https://'+vcip+'/api/vcenter/namespaces/instances',headers=headers,json=json_payload)
if json_response.ok:
print("wcpNamespace/" + spec["namespace"] + " created")
else:
print("wcpNamespace/" + spec["namespace"] + " creation failed")
print(json_response.text)
# apply wcpContentLibrary
if objtype == "wcpContentLibrary":
if not get_content_library(yamldoc["spec"].get("name")):
del yamldoc["kind"]
del yamldoc["metadata"]
client_token = generate_random_uuid()
yamldoc.update({"client_token": client_token})
i = 0
for sb in yamldoc["spec"]["storage_backings"]:
temp1 = get_storage_id(sb["datastore_id"], datacenter_id)
if temp1:
yamldoc["spec"]["storage_backings"][i].update({"datastore_id": temp1})
else:
print("wcpContentLibrary invalid storage name specified")
sys.exit()
i = i + 1
yamldoc["create_spec"] = yamldoc.pop("spec")
json_payload = json.loads(json.dumps(yamldoc))
headers.update({'vmware-api-session-id': token})
json_response = s.post('https://'+vcip+'/rest/com/vmware/content/subscribed-library',headers=headers,json=json_payload)
if json_response.ok:
print("wcpContentLibrary/Subscribed_library created")
else:
print("wcpContentLibrary/Subscribed_library created")
print(json_response.text)
else:
print("wcpContentLibrary/Subscribed_library already running")
elif verb == 'describe':
# describe wcpCluster
if objtype == "wcpCluster":
if check_wcp_cluster_status(cluster_id):
json_response = s.get('https://'+vcip+'/api/vcenter/namespace-management/clusters/'+cluster_id)
if json_response.ok:
result = json.loads(json_response.text)
print(json.dumps(result, indent=2, sort_keys=True))
else:
print("wcpCluster/" + cluster + " error describing")
else:
print("wcpCluster/" + cluster + " not ready")
# describe wcpRegistry
if objtype == "wcpRegistry":
harbor_id = check_wcp_harbor_status(cluster_id)
if harbor_id:
json_response = s.get('https://' + vcip + '/rest/vcenter/content/registries/harbor/' + harbor_id)
if (json_response.ok):
result = json.loads(json_response.text)
print(json.dumps(result, indent=2, sort_keys=True))
else:
print("wcpRegistry/Harbor error describing")
else:
print("wcpRegistry/Harbor not found")
# describe wcpNamespace
if objtype == "wcpNamespace":
json_response = s.get('https://' + vcip + '/api/vcenter/namespaces/instances')
if (json_response.ok):
results = json.loads(json_response.text)
for result in results:
if result["cluster"] == cluster_id:
json_response = s.get('https://'+vcip+'/api/vcenter/namespaces/instances/'+result["namespace"])
if (json_response.ok):
nsresult = json.loads(json_response.text)
print(json.dumps(nsresult, indent=2, sort_keys=True))
else:
print("wcpNamespace" + result["namespace"] + " error describing")
else:
print("wcpNamespace error describing")
# describe wcpContentLibrary
if objtype == "wcpContentLibrary":
contentlibrary_id = get_content_library(yamldoc["spec"].get("name"))
if contentlibrary_id:
json_response = s.get('https://'+vcip+'/rest/com/vmware/content/subscribed-library/id:'+contentlibrary_id)
if (json_response.ok):
result = json.loads(json_response.text)
print(json.dumps(result, indent=2, sort_keys=True))
else:
print("wcpContentLibrary/Subscribed library error describing")
else:
print("wcpContentLibrary/Subscribed library not found")
# Clean up and exit...
session_delete = s.delete('https://' + vcip + '/rest/com/vmware/cis/session', auth=(userid, password))