Skip to content

Commit f717051

Browse files
author
Kunal Singh
authored
Merge pull request #206 from kunal-pmj/master
Samples for VMC M9 release
2 parents 0f9cd16 + 1807e87 commit f717051

File tree

5 files changed

+385
-0
lines changed

5 files changed

+385
-0
lines changed

samples/vmc/draas/activate_srm_ops.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
__author__ = 'VMware, Inc.'
16+
17+
import time
18+
from samples.vmc.helpers.sample_cli import parser, optional_args
19+
20+
from vmware.vapi.vmc.vmc_draas_client import create_vmc_draas_client
21+
from vmware.vapi.vmc.client import create_vmc_client
22+
23+
24+
class SrmActivationOperations(object):
25+
"""
26+
Demonstrates VMware Cloud Disaster Recovery As a Service (DRaaS)
27+
Site Recovery Manager (SRM) Activation Operations
28+
29+
Sample Prerequisites:
30+
- An organization associated with the calling user.
31+
- A SDDC in the organization with SRM Addon activated.
32+
- Refresh Token
33+
"""
34+
35+
def __init__(self):
36+
optional_args.add_argument('-c', '--cleardata',
37+
action='store_true',
38+
help='Clean up after sample run')
39+
40+
args = parser.parse_args()
41+
self.org_id = args.org_id
42+
self.sddc_id = args.sddc_id
43+
self.query_wait_time = 100
44+
self.max_wait_time = 900
45+
46+
self.cleanup = args.cleardata
47+
self.vmc_client = create_vmc_client(refresh_token=args.refresh_token)
48+
self.draas_client = create_vmc_draas_client(refresh_token=args.refresh_token)
49+
50+
def setup(self):
51+
# Check if the organization exists
52+
orgs = self.vmc_client.Orgs.list()
53+
if self.org_id not in [org.id for org in orgs]:
54+
raise ValueError("Org with ID {} doesn't exist".format(self.org_id))
55+
56+
# Check if the SDDC exists
57+
sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id)
58+
if self.sddc_id not in [sddc.id for sddc in sddcs]:
59+
raise ValueError("SDDC with ID {} doesn't exist in org {}".
60+
format(self.sddc_id, self.org_id))
61+
62+
# Activate SRM Addon in a SDDC
63+
def activate_srm(self):
64+
if self.draas_client.SiteRecovery.get(self.org_id, self.sddc_id).site_recovery_state != "ACTIVATED":
65+
srm_activation = self.draas_client.SiteRecovery.post(self.org_id,
66+
self.sddc_id,
67+
activate_site_recovery_config=None)
68+
print("Activation of SRM {} : {}".format(srm_activation.status,
69+
srm_activation.start_time))
70+
self.query_activation_status()
71+
else:
72+
print("SRM already activated in {}".format(self.sddc_id))
73+
74+
'''
75+
Note: There is no Task API to query activation status, though there is a task structure
76+
Hence querying the SRM activation status with resource_id & state for the status.
77+
'''
78+
def query_activation_status(self):
79+
timeout = time.time() + self.max_wait_time
80+
while time.time() < timeout:
81+
time.sleep(self.query_wait_time)
82+
status = self.draas_client.SiteRecovery.get(self.org_id, self.sddc_id)
83+
if status.site_recovery_state in ['ACTIVATED', 'DEACTIVATED', 'CANCELLED', 'FAILED']:
84+
print("Site Recovery (DRaaS) Activation Status in {} : {}"
85+
.format(status.updated, status.site_recovery_state))
86+
break
87+
else:
88+
print("Site Recovery (DRaaS) Activation Status in {} : {}"
89+
.format(status.updated, status.site_recovery_state))
90+
continue
91+
else:
92+
raise Exception("Max time out reached {}".format(self.max_wait_time))
93+
94+
# De-activate SRM Addon in a SDDC. This is a forceful operation as force=True
95+
def deactivate_srm(self):
96+
if self.cleanup:
97+
print("Deactivating SRM")
98+
self.draas_client.SiteRecovery.delete(self.org_id,
99+
self.sddc_id,
100+
force=True)
101+
self.query_activation_status()
102+
103+
104+
def main():
105+
srm_activation_ops = SrmActivationOperations()
106+
srm_activation_ops.activate_srm()
107+
srm_activation_ops.deactivate_srm()
108+
109+
110+
if __name__ == '__main__':
111+
main()
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#!/usr/bin/env python
2+
3+
"""
4+
* *******************************************************
5+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
6+
* SPDX-License-Identifier: MIT
7+
* *******************************************************
8+
*
9+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
10+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
11+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
12+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
13+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
14+
"""
15+
__author__ = 'VMware, Inc.'
16+
17+
import time
18+
from samples.vmc.helpers.sample_cli import parser, optional_args
19+
20+
from vmware.vapi.vmc.vmc_draas_client import create_vmc_draas_client
21+
from vmware.vapi.vmc.client import create_vmc_client
22+
from com.vmware.vmc.draas.model_client import ProvisionSrmConfig
23+
24+
25+
class DeployAdditionalNode(object):
26+
"""
27+
Demonstrates VMware Cloud Disaster Recovery As a Service (DRaaS)
28+
Additional Site Recovery Manager (SRM) Node Deployment operations
29+
30+
Sample Prerequisites:
31+
- An organization associated with the calling user.
32+
- A SDDC in the organization with SRM Addon activated
33+
"""
34+
35+
def __init__(self):
36+
optional_args.add_argument('-c', '--cleardata',
37+
action='store_true',
38+
help='Clean up after sample run')
39+
40+
args = parser.parse_args()
41+
self.org_id = args.org_id
42+
self.sddc_id = args.sddc_id
43+
self.wait_time = 100
44+
self.max_wait_time = 900
45+
self.node_extension_id = 'com.vcDr1'
46+
47+
self.cleanup = args.cleardata
48+
self.vmc_client = create_vmc_client(refresh_token=args.refresh_token)
49+
self.draas_client = create_vmc_draas_client(refresh_token=args.refresh_token)
50+
51+
def setup(self):
52+
# Check if the organization exists
53+
orgs = self.vmc_client.Orgs.list()
54+
if self.org_id not in [org.id for org in orgs]:
55+
raise ValueError("Org with ID {} doesn't exist".format(self.org_id))
56+
57+
# Check if the SDDC exists
58+
sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id)
59+
if self.sddc_id not in [sddc.id for sddc in sddcs]:
60+
raise ValueError("SDDC with ID {} doesn't exist in org {}".
61+
format(self.sddc_id, self.org_id))
62+
63+
# Check if the SRM Add-on is activated in VMC
64+
if "ACTIVATED" != self.draas_client.SiteRecovery.get(self.org_id, self.sddc_id).site_recovery_state:
65+
raise ValueError("DRaaS is not activated in SDDC with ID {} & org with ID {}".
66+
format(self.sddc_id, self.org_id))
67+
68+
# Deploy Additional SRM Node
69+
def deploy_srm(self):
70+
deploy_srm = self.draas_client.SiteRecoverySrmNodes.post(
71+
self.org_id,
72+
self.sddc_id,
73+
ProvisionSrmConfig(srm_extension_key_suffix=self.node_extension_id))
74+
print('Srm Additional Node Deployment Started {}'.format(deploy_srm.start_time))
75+
return deploy_srm.resource_id
76+
77+
'''
78+
Note: There is no Task API to query activation status, though there is a task structure.
79+
Hence querying the SRM activation status with resource_id and state for the status.
80+
'''
81+
def query_deployment(self, deployed_node_id):
82+
srm_node_details = self.draas_client.SiteRecovery.get(self.org_id, self.sddc_id).srm_nodes
83+
for node_index in range(len(srm_node_details)):
84+
if deployed_node_id == srm_node_details[node_index].id:
85+
timeout = time.time() + self.max_wait_time
86+
while time.time() < timeout:
87+
node_details = self.draas_client.SiteRecovery.get(self.org_id, self.sddc_id)
88+
time.sleep(self.wait_time)
89+
if node_details.srm_nodes[node_index].state in ['READY', 'DELETING', 'CANCELLED', 'FAILED']:
90+
print("Site Recovery (DRaaS) Additonal Node Deployment Status {} : {}"
91+
.format(node_details.updated,
92+
node_details.srm_nodes[node_index].state))
93+
break
94+
else:
95+
print("Site Recovery (DRaaS) Additonal Node Deployment Status {} : {}"
96+
.format(node_details.updated,
97+
node_details.srm_nodes[node_index].state))
98+
continue
99+
else:
100+
raise Exception("Max time out reached {}".format(self.max_wait_time))
101+
node_index += 1
102+
103+
# Deleting the additional node if with --cleardata flag
104+
def delete_node(self, node_id):
105+
if self.cleanup:
106+
print("Removing the Additional Node")
107+
self.draas_client.SiteRecoverySrmNodes.delete(
108+
self.org_id,
109+
self.sddc_id,
110+
node_id)
111+
self.query_deployment(node_id)
112+
113+
114+
def main():
115+
deploy_addtional_nodes = DeployAdditionalNode()
116+
deploy_addtional_nodes.setup()
117+
srm_node_id = deploy_addtional_nodes.deploy_srm()
118+
deploy_addtional_nodes.query_deployment(srm_node_id)
119+
deploy_addtional_nodes.delete_node(srm_node_id)
120+
121+
122+
if __name__ == '__main__':
123+
main()

samples/vmc/draas/get_srm_info.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import argparse
2+
import requests
3+
4+
from vmware.vapi.vmc.vmc_draas_client import create_vmc_draas_client
5+
6+
7+
parser = argparse.ArgumentParser()
8+
parser.add_argument(
9+
'--refresh_token',
10+
required=True,
11+
help='VMware Cloud API refresh token')
12+
13+
parser.add_argument(
14+
'--org_id',
15+
required=True,
16+
help='Organization identifier.')
17+
18+
parser.add_argument(
19+
'--sddc_id',
20+
required=True,
21+
help='Sddc Identifier.')
22+
23+
args = parser.parse_args()
24+
refresh_token = args.refresh_token
25+
org_id = args.org_id
26+
sddc_id = args.sddc_id
27+
28+
client = create_vmc_draas_client(refresh_token)
29+
site_recovery_activation_task = client.SiteRecovery.get(org_id, sddc_id)
30+
print(site_recovery_activation_task)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""
2+
* *******************************************************
3+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
4+
* SPDX-License-Identifier: MIT
5+
* *******************************************************
6+
*
7+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
8+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
9+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
10+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
11+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
12+
"""
13+
14+
__author__ = 'VMware, Inc.'
15+
__vcenter_version__ = '6.7+'
16+
17+
import sys
18+
from vmware.vapi.vsphere.client import create_vsphere_client
19+
20+
from samples.vsphere.common import (sample_cli, sample_util)
21+
from samples.vsphere.common.ssl_helper import get_unverified_session
22+
23+
24+
"""
25+
Description: Demonstrates services api workflow
26+
1.Stop a running service
27+
2.Get details of stopped service
28+
3.Start the service stopped in step 2
29+
4.Get details of service
30+
5.Restart the service
31+
6.Get details of service
32+
33+
"""
34+
35+
36+
parser = sample_cli.build_arg_parser()
37+
parser.add_argument(
38+
'--service_name',
39+
action='store',
40+
required=True,
41+
help='Specify servicename for all stop/start/restart operations')
42+
args = sample_util.process_cli_args(parser.parse_args())
43+
service_name = args.service_name
44+
session = get_unverified_session() if args.skipverification else None
45+
client = create_vsphere_client(server=args.server,
46+
username=args.username,
47+
password=args.password,
48+
session=session)
49+
50+
appliance_service = client.appliance.Services
51+
service_list = appliance_service.list()
52+
53+
54+
def ouput_display(info, service_name):
55+
print("Service : {}".format(service_name))
56+
print("Description : {}".format(info.description))
57+
print("state : {}".format(info.state))
58+
print("-----------------------------------")
59+
60+
61+
if service_name not in service_list:
62+
raise ValueError('Service with service name {} does not exists'.format(service_name))
63+
64+
print("Example: Stopping service : {}\n".format(service_name))
65+
appliance_service.stop(service_name)
66+
service_state = appliance_service.get(service_name)
67+
ouput_display(service_state, service_name)
68+
print("Example: Starting service : {}\n".format(service_name))
69+
appliance_service.start(service_name)
70+
service_state = appliance_service.get(service_name)
71+
ouput_display(service_state, service_name)
72+
print("Example: Restarting service : {}\n" .format(service_name))
73+
appliance_service.restart(service_name)
74+
print("Example: Getting service : {}\n".format(service_name))
75+
service_state = appliance_service.get(service_name)
76+
ouput_display(service_state, service_name)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
"""
3+
* *******************************************************
4+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
5+
* SPDX-License-Identifier: MIT
6+
* *******************************************************
7+
*
8+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
9+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
10+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
11+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
12+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
13+
"""
14+
15+
__author__ = 'VMware, Inc.'
16+
__vcenter_version__ = '6.7+'
17+
18+
from vmware.vapi.vsphere.client import create_vsphere_client
19+
20+
from samples.vsphere.common import (sample_cli, sample_util)
21+
from samples.vsphere.common.ssl_helper import get_unverified_session
22+
23+
24+
"""
25+
Description: Demonstrates services api workflow
26+
1.List all services
27+
28+
"""
29+
30+
parser = sample_cli.build_arg_parser()
31+
args = sample_util.process_cli_args(parser.parse_args())
32+
session = get_unverified_session() if args.skipverification else None
33+
client = create_vsphere_client(server=args.server,
34+
username=args.username,
35+
password=args.password,
36+
session=session)
37+
38+
service_list = client.appliance.Services.list()
39+
40+
print("Example: List Appliance Services:")
41+
print("-------------------\n")
42+
for key, values in service_list.items():
43+
print("Service Name : {} ".format(key))
44+
print("value : {}".format(values.description))
45+
print("State: {} \n".format(values.state))

0 commit comments

Comments
 (0)