diff --git a/samples/vmc/draas/__init__.py b/samples/vmc/draas/__init__.py new file mode 100644 index 00000000..240699dd --- /dev/null +++ b/samples/vmc/draas/__init__.py @@ -0,0 +1,25 @@ +""" +* ******************************************************* +* Copyright VMware, Inc. 2019. All Rights Reserved. +* SPDX-License-Identifier: MIT +* ******************************************************* +* +* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT +* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, +* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED +* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, +* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. +""" + +__author__ = 'VMware, Inc.' + +# Required to distribute different parts of this +# package as multiple distribution +try: + import pkg_resources + + pkg_resources.declare_namespace(__name__) +except ImportError: + from pkgutil import extend_path + + __path__ = extend_path(__path__, __name__) # @ReservedAssignment diff --git a/samples/vmc/draas/deploy_additional_instance.py b/samples/vmc/draas/deploy_additional_instance.py new file mode 100644 index 00000000..32e75dc8 --- /dev/null +++ b/samples/vmc/draas/deploy_additional_instance.py @@ -0,0 +1,123 @@ +#!/usr/bin/env python + +""" +* ******************************************************* +* Copyright (c) VMware, Inc. 2019. All Rights Reserved. +* SPDX-License-Identifier: MIT +* ******************************************************* +* +* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT +* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, +* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED +* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, +* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. +""" +__author__ = 'VMware, Inc.' + +from samples.vmc.helpers.sample_cli import parser, optional_args + +from vmware.vapi.vmc.client import create_vmc_client +from com.vmware.vmc.draas.model_client import ProvisionSrmConfig +from com.vmware.vmc.draas.model_client import ErrorResponse + +from com.vmware.vapi.std.errors_client import InvalidRequest +from samples.vmc.draas.helpers.draas_task_helper import wait_for_task + + +class DeployAdditionalInstance(object): + """ + Demonstrates VMware Cloud Disaster Recovery As a Service (DRaaS) + Additional Site Recovery instance deployment operations + + Sample Prerequisites: + - An organization associated with the calling user. + - A SDDC in the organization with Site Recovery activated + """ + + def __init__(self): + optional_args.add_argument('-c', '--cleardata', + action='store_true', + help='Clean up after sample run') + + optional_args.add_argument('--interval_sec', + default=60, + help='Task pulling interval in sec') + args = parser.parse_args() + self.org_id = args.org_id + self.sddc_id = args.sddc_id + self.interval_sec = int(args.interval_sec) + + ''' + SRM extension key suffix.This must be fewer than 13 characters + and can include alphanumeric characters, hyphen, or period, + but cannot start or end with a sequence of hyphen, or period characters + ''' + self.extension_key = 'TestNode' + + self.cleanup = args.cleardata + self.vmc_client = create_vmc_client(refresh_token=args.refresh_token) + + def setup(self): + # Check if the organization exists + orgs = self.vmc_client.Orgs.list() + if self.org_id not in [org.id for org in orgs]: + raise ValueError("Org with ID {} doesn't exist".format(self.org_id)) + + # Check if the SDDC exists + sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id) + if self.sddc_id not in [sddc.id for sddc in sddcs]: + raise ValueError("SDDC with ID {} doesn't exist in org {}". + format(self.sddc_id, self.org_id)) + + # Check if Site Recovery is activated in VMC + if "ACTIVATED" != self.vmc_client.draas.SiteRecovery.get(self.org_id, self.sddc_id).site_recovery_state: + raise ValueError("DRaaS is not activated in SDDC with ID {} & org with ID {}". + format(self.sddc_id, self.org_id)) + + # Deploy Additional Site Recovery Instance + def deploy_srm(self): + try: + print("Deploying Additional Site Recovery Instance") + deployment_task = self.vmc_client.draas.SiteRecoverySrmNodes.post( + self.org_id, + self.sddc_id, + ProvisionSrmConfig(srm_extension_key_suffix=self.extension_key)) + except InvalidRequest as e: + # Convert InvalidRequest to ErrorResponse to get error message + error_response = e.data.convert_to(ErrorResponse) + raise Exception(error_response.error_messages) + + wait_for_task(task_client=self.vmc_client.draas.Task, + org_id=self.org_id, + task_id=deployment_task.id, + interval_sec=self.interval_sec) + return deployment_task.resource_id + + # Deleting the additional Site Recovery instance, if with --cleardata flag + def delete_node(self, node_id): + if self.cleanup: + print("\nRemoving Additional Site Recovery Instance") + try: + srm_deactivation_task = self.vmc_client.draas.SiteRecoverySrmNodes.delete(self.org_id, + self.sddc_id, + node_id) + except InvalidRequest as e: + # Convert InvalidRequest to ErrorResponse to get error message + error_response = e.data.convert_to(ErrorResponse) + raise Exception(error_response.error_messages) + + wait_for_task(task_client=self.vmc_client.draas.Task, + org_id=self.org_id, + task_id=srm_deactivation_task.id, + interval_sec=self.interval_sec) + + +def main(): + deploy_addtional_instance = DeployAdditionalInstance() + deploy_addtional_instance.setup() + node_id = deploy_addtional_instance.deploy_srm() + deploy_addtional_instance.delete_node(node_id) + + +if __name__ == '__main__': + main() diff --git a/samples/vmc/draas/helpers/__init__.py b/samples/vmc/draas/helpers/__init__.py new file mode 100644 index 00000000..240699dd --- /dev/null +++ b/samples/vmc/draas/helpers/__init__.py @@ -0,0 +1,25 @@ +""" +* ******************************************************* +* Copyright VMware, Inc. 2019. All Rights Reserved. +* SPDX-License-Identifier: MIT +* ******************************************************* +* +* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT +* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, +* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED +* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, +* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. +""" + +__author__ = 'VMware, Inc.' + +# Required to distribute different parts of this +# package as multiple distribution +try: + import pkg_resources + + pkg_resources.declare_namespace(__name__) +except ImportError: + from pkgutil import extend_path + + __path__ = extend_path(__path__, __name__) # @ReservedAssignment diff --git a/samples/vmc/draas/helpers/draas_task_helper.py b/samples/vmc/draas/helpers/draas_task_helper.py new file mode 100644 index 00000000..6044a90f --- /dev/null +++ b/samples/vmc/draas/helpers/draas_task_helper.py @@ -0,0 +1,48 @@ +""" +* ******************************************************* +* Copyright (c) VMware, Inc. 2019. All Rights Reserved. +* SPDX-License-Identifier: MIT +* ******************************************************* +* +* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT +* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, +* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED +* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, +* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. +""" + +__author__ = 'VMware, Inc.' + +from time import sleep + +from com.vmware.vmc.draas.model_client import Task + + +def wait_for_task(task_client, org_id, task_id, interval_sec=60): + """ + Helper method to wait for a task to finish + :param task_client: task client to query the task object + :param org_id: organization id + :param task_id: task id + :param interval_sec: task pulling interval_sec in sec + :return: True if task finished successfully, False otherwise. + """ + print('Wait for task {} to finish'.format(task_id)) + print('Checking task status every {} seconds'.format(interval_sec)) + + while True: + task = task_client.get(org_id, task_id) + + if task.status == Task.STATUS_FINISHED: + print('\nTask {} finished successfully'.format(task_id)) + return True + elif task.status == Task.STATUS_FAILED: + print('\nTask {} failed'.format(task_id)) + return False + elif task.status == Task.STATUS_CANCELED: + print('\nTask {} cancelled'.format(task_id)) + return False + else: + print("Estimated time remaining: {} minutes".format( + task.estimated_remaining_minutes)) + sleep(interval_sec) diff --git a/samples/vmc/draas/site_recovery_activation_ops.py b/samples/vmc/draas/site_recovery_activation_ops.py new file mode 100644 index 00000000..9d5a4565 --- /dev/null +++ b/samples/vmc/draas/site_recovery_activation_ops.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python + +""" +* ******************************************************* +* Copyright (c) VMware, Inc. 2019. All Rights Reserved. +* SPDX-License-Identifier: MIT +* ******************************************************* +* +* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT +* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, +* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED +* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, +* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. +""" +__author__ = 'VMware, Inc.' + +from vmware.vapi.vmc.client import create_vmc_client +from com.vmware.vmc.draas.model_client import ErrorResponse +from com.vmware.vapi.std.errors_client import InvalidRequest + +from samples.vmc.helpers.sample_cli import parser, optional_args +from samples.vmc.draas.helpers.draas_task_helper import wait_for_task + + +class SiteRecoveryActivationOperations(object): + """ + Demonstrates VMware Cloud Disaster Recovery As a Service (DRaaS) + Site Recovery Activation Operations + + Sample Prerequisites: + - An organization associated with the calling user. + - A SDDC in the organization. + - Refresh Token + """ + + def __init__(self): + optional_args.add_argument('-c', '--cleardata', + action='store_true', + help='Clean up after sample run') + + optional_args.add_argument('--interval_sec', + default=60, + help='Task pulling interval in sec') + + args = parser.parse_args() + self.org_id = args.org_id + self.sddc_id = args.sddc_id + self.interval_sec = int(args.interval_sec) + + self.cleanup = args.cleardata + self.vmc_client = create_vmc_client(refresh_token=args.refresh_token) + + def setup(self): + # Check if the organization exists + orgs = self.vmc_client.Orgs.list() + if self.org_id not in [org.id for org in orgs]: + raise ValueError("Org with ID {} doesn't exist".format(self.org_id)) + + # Check if the SDDC exists + sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id) + if self.sddc_id not in [sddc.id for sddc in sddcs]: + raise ValueError("SDDC with ID {} doesn't exist in org {}". + format(self.sddc_id, self.org_id)) + + # Activate Site Recovery in a SDDC + def activate_srm(self): + try: + srm_activation_task = self.vmc_client.draas.SiteRecovery.post(self.org_id, + self.sddc_id) + except InvalidRequest as e: + # Convert InvalidRequest to ErrorResponse to get error message + error_response = e.data.convert_to(ErrorResponse) + raise Exception(error_response.error_messages) + + wait_for_task(task_client=self.vmc_client.draas.Task, + org_id=self.org_id, + task_id=srm_activation_task.id, + interval_sec=self.interval_sec) + + # De-activate Site Recovery Instance in a SDDC. This is a forceful operation as force=True + def deactivate_srm(self): + if self.cleanup: + try: + srm_deactivation_task = self.vmc_client.draas.SiteRecovery.delete(self.org_id, + self.sddc_id, + force=True) + except InvalidRequest as e: + # Convert InvalidRequest to ErrorResponse to get error message + error_response = e.data.convert_to(ErrorResponse) + raise Exception(error_response.error_messages) + + wait_for_task(task_client=self.vmc_client.draas.Task, + org_id=self.org_id, + task_id=srm_deactivation_task.id, + interval_sec=self.interval_sec) + + +def main(): + srm_activation_ops = SiteRecoveryActivationOperations() + srm_activation_ops.setup() + srm_activation_ops.activate_srm() + srm_activation_ops.deactivate_srm() + + +if __name__ == '__main__': + main() diff --git a/samples/vmc/draas/site_recovery_info.py b/samples/vmc/draas/site_recovery_info.py new file mode 100644 index 00000000..81a3f096 --- /dev/null +++ b/samples/vmc/draas/site_recovery_info.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python + +""" +* ******************************************************* +* Copyright (c) VMware, Inc. 2019. All Rights Reserved. +* SPDX-License-Identifier: MIT +* ******************************************************* +* +* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT +* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN, +* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED +* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, +* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. +""" + +from samples.vmc.helpers.sample_cli import parser + +from vmware.vapi.vmc.client import create_vmc_client + + +class VmcSiteRecoveryInfo(object): + """ + Retrieves details of site recovery from the + VMware Cloud Disaster Recovery As a Service (VMC DRaaS) + + Sample Prerequisites: + - Site Recovery Add-on should be activated in the SDDC + - An organization associated with the calling user. + - A SDDC in the organization with SRM Addon activated. + - Refresh Token + """ + def __init__(self): + args = parser.parse_args() + self.org_id = args.org_id + self.sddc_id = args.sddc_id + self.client = create_vmc_client(refresh_token=args.refresh_token) + + def get_draas_info(self): + dr_status = self.client.draas.SiteRecovery.get(self.org_id, self.sddc_id) + print("Vmware Cloud Site Recovery Status {}". + format(dr_status.site_recovery_state)) + print("Vmware Cloud DRaaS H5 URL {}".format(dr_status.draas_h5_url)) + print("*** Vmware Cloud DRaaS Srm Node Details ***") + srm_nodes_list = dr_status.srm_nodes + for node in srm_nodes_list: + print("\nSRM Node Id {} , status {}".format(node.id, node.state)) + print("SRM IP Address {}".format(node.ip_address)) + print("SRM IP Address {}".format(node.hostname)) + + print("\n*** Vmware Cloud DRaaS vSphere Replication (VR) Node Details ***") + print("VSphere Replication (VR) Node Status: {} , Id: {}" + .format(dr_status.vr_node.state, dr_status.vr_node.id)) + print("VR IP address {}".format(dr_status.vr_node.ip_address)) + print("VR HostName {}".format(dr_status.vr_node.hostname)) + + +def main(): + vmc_site_recovery_info = VmcSiteRecoveryInfo() + vmc_site_recovery_info.get_draas_info() + + +if __name__ == '__main__': + main()