|
| 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() |
0 commit comments