Skip to content

Commit 8d7a12e

Browse files
authored
Merge pull request #189 from jobingeo/master
DRaaS Samples
2 parents 9274dd7 + 1011aee commit 8d7a12e

File tree

6 files changed

+389
-0
lines changed

6 files changed

+389
-0
lines changed

samples/vmc/draas/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
* *******************************************************
3+
* Copyright 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+
16+
# Required to distribute different parts of this
17+
# package as multiple distribution
18+
try:
19+
import pkg_resources
20+
21+
pkg_resources.declare_namespace(__name__)
22+
except ImportError:
23+
from pkgutil import extend_path
24+
25+
__path__ = extend_path(__path__, __name__) # @ReservedAssignment
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
from com.vmware.vapi.std.errors_client import InvalidRequest
18+
from com.vmware.vmc.draas.model_client import ErrorResponse
19+
from com.vmware.vmc.draas.model_client import ProvisionSrmConfig
20+
from vmware.vapi.vmc.client import create_vmc_client
21+
22+
from samples.vmc.draas.helpers.draas_task_helper import wait_for_task
23+
from samples.vmc.helpers.sample_cli import parser, optional_args
24+
25+
26+
class DeployAdditionalInstance(object):
27+
"""
28+
Demonstrates VMware Cloud Disaster Recovery As a Service (DRaaS)
29+
Additional Site Recovery instance deployment operations
30+
31+
Sample Prerequisites:
32+
- An organization associated with the calling user.
33+
- A SDDC in the organization with Site Recovery activated
34+
"""
35+
36+
def __init__(self):
37+
optional_args.add_argument('-c', '--cleardata',
38+
action='store_true',
39+
help='Clean up after sample run')
40+
41+
optional_args.add_argument('--interval_sec',
42+
default=60,
43+
help='Task pulling interval in sec')
44+
args = parser.parse_args()
45+
self.org_id = args.org_id
46+
self.sddc_id = args.sddc_id
47+
self.interval_sec = int(args.interval_sec)
48+
49+
'''
50+
SRM extension key suffix.This must be fewer than 13 characters
51+
and can include alphanumeric characters, hyphen, or period,
52+
but cannot start or end with a sequence of hyphen, or period characters
53+
'''
54+
self.extension_key = 'TestNode'
55+
56+
self.cleanup = args.cleardata
57+
self.vmc_client = create_vmc_client(refresh_token=args.refresh_token)
58+
59+
def setup(self):
60+
# Check if the organization exists
61+
orgs = self.vmc_client.Orgs.list()
62+
if self.org_id not in [org.id for org in orgs]:
63+
raise ValueError("Org with ID {} doesn't exist".format(self.org_id))
64+
65+
# Check if the SDDC exists
66+
sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id)
67+
if self.sddc_id not in [sddc.id for sddc in sddcs]:
68+
raise ValueError("SDDC with ID {} doesn't exist in org {}".
69+
format(self.sddc_id, self.org_id))
70+
71+
# Check if Site Recovery is activated in VMC
72+
if "ACTIVATED" != self.vmc_client.draas.SiteRecovery.get(self.org_id, self.sddc_id).site_recovery_state:
73+
raise ValueError("DRaaS is not activated in SDDC with ID {} & org with ID {}".
74+
format(self.sddc_id, self.org_id))
75+
76+
# Deploy Additional Site Recovery Instance
77+
def deploy_srm(self):
78+
try:
79+
print("Deploying Additional Site Recovery Instance")
80+
deployment_task = self.vmc_client.draas.SiteRecoverySrmNodes.post(
81+
self.org_id,
82+
self.sddc_id,
83+
ProvisionSrmConfig(srm_extension_key_suffix=self.extension_key))
84+
except InvalidRequest as e:
85+
# Convert InvalidRequest to ErrorResponse to get error message
86+
error_response = e.data.convert_to(ErrorResponse)
87+
raise Exception(error_response.error_messages)
88+
89+
wait_for_task(task_client=self.vmc_client.draas.Task,
90+
org_id=self.org_id,
91+
task_id=deployment_task.id,
92+
interval_sec=self.interval_sec)
93+
return deployment_task.resource_id
94+
95+
# Deleting the additional Site Recovery instance, if with --cleardata flag
96+
def delete_node(self, node_id):
97+
if self.cleanup:
98+
print("\nRemoving Additional Site Recovery Instance")
99+
try:
100+
srm_deactivation_task = self.vmc_client.draas.SiteRecoverySrmNodes.delete(self.org_id,
101+
self.sddc_id,
102+
node_id)
103+
except InvalidRequest as e:
104+
# Convert InvalidRequest to ErrorResponse to get error message
105+
error_response = e.data.convert_to(ErrorResponse)
106+
raise Exception(error_response.error_messages)
107+
108+
wait_for_task(task_client=self.vmc_client.draas.Task,
109+
org_id=self.org_id,
110+
task_id=srm_deactivation_task.id,
111+
interval_sec=self.interval_sec)
112+
113+
114+
def main():
115+
deploy_addtional_instance = DeployAdditionalInstance()
116+
deploy_addtional_instance.setup()
117+
node_id = deploy_addtional_instance.deploy_srm()
118+
deploy_addtional_instance.delete_node(node_id)
119+
120+
121+
if __name__ == '__main__':
122+
main()

samples/vmc/draas/helpers/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
* *******************************************************
3+
* Copyright 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+
16+
# Required to distribute different parts of this
17+
# package as multiple distribution
18+
try:
19+
import pkg_resources
20+
21+
pkg_resources.declare_namespace(__name__)
22+
except ImportError:
23+
from pkgutil import extend_path
24+
25+
__path__ = extend_path(__path__, __name__) # @ReservedAssignment
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
16+
from time import sleep
17+
18+
from com.vmware.vmc.draas.model_client import Task
19+
20+
21+
def wait_for_task(task_client, org_id, task_id, interval_sec=60):
22+
"""
23+
Helper method to wait for a task to finish
24+
:param task_client: task client to query the task object
25+
:param org_id: organization id
26+
:param task_id: task id
27+
:param interval_sec: task pulling interval_sec in sec
28+
:return: True if task finished successfully, False otherwise.
29+
"""
30+
print('Wait for task {} to finish'.format(task_id))
31+
print('Checking task status every {} seconds'.format(interval_sec))
32+
33+
while True:
34+
task = task_client.get(org_id, task_id)
35+
36+
if task.status == Task.STATUS_FINISHED:
37+
print('\nTask {} finished successfully'.format(task_id))
38+
return True
39+
elif task.status == Task.STATUS_FAILED:
40+
print('\nTask {} failed'.format(task_id))
41+
return False
42+
elif task.status == Task.STATUS_CANCELED:
43+
print('\nTask {} cancelled'.format(task_id))
44+
return False
45+
else:
46+
print("Estimated time remaining: {} minutes".format(
47+
task.estimated_remaining_minutes))
48+
sleep(interval_sec)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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+
from com.vmware.vapi.std.errors_client import InvalidRequest
18+
from com.vmware.vmc.draas.model_client import ErrorResponse
19+
from vmware.vapi.vmc.client import create_vmc_client
20+
21+
from samples.vmc.draas.helpers.draas_task_helper import wait_for_task
22+
from samples.vmc.helpers.sample_cli import parser, optional_args
23+
24+
25+
class SiteRecoveryActivationOperations(object):
26+
"""
27+
Demonstrates VMware Cloud Disaster Recovery As a Service (DRaaS)
28+
Site Recovery Activation Operations
29+
30+
Sample Prerequisites:
31+
- An organization associated with the calling user.
32+
- A SDDC in the organization.
33+
- Refresh Token
34+
"""
35+
36+
def __init__(self):
37+
optional_args.add_argument('-c', '--cleardata',
38+
action='store_true',
39+
help='Clean up after sample run')
40+
41+
optional_args.add_argument('--interval_sec',
42+
default=60,
43+
help='Task pulling interval in sec')
44+
45+
args = parser.parse_args()
46+
self.org_id = args.org_id
47+
self.sddc_id = args.sddc_id
48+
self.interval_sec = int(args.interval_sec)
49+
50+
self.cleanup = args.cleardata
51+
self.vmc_client = create_vmc_client(refresh_token=args.refresh_token)
52+
53+
def setup(self):
54+
# Check if the organization exists
55+
orgs = self.vmc_client.Orgs.list()
56+
if self.org_id not in [org.id for org in orgs]:
57+
raise ValueError("Org with ID {} doesn't exist".format(self.org_id))
58+
59+
# Check if the SDDC exists
60+
sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id)
61+
if self.sddc_id not in [sddc.id for sddc in sddcs]:
62+
raise ValueError("SDDC with ID {} doesn't exist in org {}".
63+
format(self.sddc_id, self.org_id))
64+
65+
# Activate Site Recovery in a SDDC
66+
def activate_srm(self):
67+
try:
68+
srm_activation_task = self.vmc_client.draas.SiteRecovery.post(self.org_id,
69+
self.sddc_id)
70+
except InvalidRequest as e:
71+
# Convert InvalidRequest to ErrorResponse to get error message
72+
error_response = e.data.convert_to(ErrorResponse)
73+
raise Exception(error_response.error_messages)
74+
75+
wait_for_task(task_client=self.vmc_client.draas.Task,
76+
org_id=self.org_id,
77+
task_id=srm_activation_task.id,
78+
interval_sec=self.interval_sec)
79+
80+
# De-activate Site Recovery Instance in a SDDC. This is a forceful operation as force=True
81+
def deactivate_srm(self):
82+
if self.cleanup:
83+
try:
84+
srm_deactivation_task = self.vmc_client.draas.SiteRecovery.delete(self.org_id,
85+
self.sddc_id,
86+
force=True)
87+
except InvalidRequest as e:
88+
# Convert InvalidRequest to ErrorResponse to get error message
89+
error_response = e.data.convert_to(ErrorResponse)
90+
raise Exception(error_response.error_messages)
91+
92+
wait_for_task(task_client=self.vmc_client.draas.Task,
93+
org_id=self.org_id,
94+
task_id=srm_deactivation_task.id,
95+
interval_sec=self.interval_sec)
96+
97+
98+
def main():
99+
srm_activation_ops = SiteRecoveryActivationOperations()
100+
srm_activation_ops.setup()
101+
srm_activation_ops.activate_srm()
102+
srm_activation_ops.deactivate_srm()
103+
104+
105+
if __name__ == '__main__':
106+
main()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
16+
from vmware.vapi.vmc.client import create_vmc_client
17+
18+
from samples.vmc.helpers.sample_cli import parser
19+
20+
21+
class VmcSiteRecoveryInfo(object):
22+
"""
23+
Retrieves details of site recovery from the
24+
VMware Cloud Disaster Recovery As a Service (VMC DRaaS)
25+
26+
Sample Prerequisites:
27+
- Site Recovery Add-on should be activated in the SDDC
28+
- An organization associated with the calling user.
29+
- A SDDC in the organization with SRM Addon activated.
30+
- Refresh Token
31+
"""
32+
def __init__(self):
33+
args = parser.parse_args()
34+
self.org_id = args.org_id
35+
self.sddc_id = args.sddc_id
36+
self.client = create_vmc_client(refresh_token=args.refresh_token)
37+
38+
def get_draas_info(self):
39+
dr_status = self.client.draas.SiteRecovery.get(self.org_id, self.sddc_id)
40+
print("Vmware Cloud Site Recovery Status {}".
41+
format(dr_status.site_recovery_state))
42+
print("Vmware Cloud DRaaS H5 URL {}".format(dr_status.draas_h5_url))
43+
print("*** Vmware Cloud DRaaS Srm Node Details ***")
44+
srm_nodes_list = dr_status.srm_nodes
45+
for node in srm_nodes_list:
46+
print("\nSRM Node Id {} , status {}".format(node.id, node.state))
47+
print("SRM IP Address {}".format(node.ip_address))
48+
print("SRM IP Address {}".format(node.hostname))
49+
50+
print("\n*** Vmware Cloud DRaaS vSphere Replication (VR) Node Details ***")
51+
print("VSphere Replication (VR) Node Status: {} , Id: {}"
52+
.format(dr_status.vr_node.state, dr_status.vr_node.id))
53+
print("VR IP address {}".format(dr_status.vr_node.ip_address))
54+
print("VR HostName {}".format(dr_status.vr_node.hostname))
55+
56+
57+
def main():
58+
vmc_site_recovery_info = VmcSiteRecoveryInfo()
59+
vmc_site_recovery_info.get_draas_info()
60+
61+
62+
if __name__ == '__main__':
63+
main()

0 commit comments

Comments
 (0)