2017-12-05 18:20:37 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
* *******************************************************
|
2019-07-22 00:16:07 -04:00
|
|
|
* Copyright (c) VMware, Inc. 2017-2019. All Rights Reserved.
|
2017-12-05 18:20:37 -05:00
|
|
|
* 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.'
|
|
|
|
|
|
|
|
import os
|
|
|
|
from random import randrange
|
2019-08-20 02:54:24 -04:00
|
|
|
import argparse
|
2017-12-05 18:20:37 -05:00
|
|
|
|
2018-03-13 20:35:12 -04:00
|
|
|
from com.vmware.vapi.std.errors_client import InvalidRequest
|
2018-06-15 01:23:07 -04:00
|
|
|
from com.vmware.vmc.model_client import AwsSddcConfig, ErrorResponse, AccountLinkSddcConfig, SddcConfig
|
2017-12-05 18:20:37 -05:00
|
|
|
from vmware.vapi.vmc.client import create_vmc_client
|
|
|
|
from samples.vmc.helpers.vmc_task_helper import wait_for_task
|
|
|
|
|
|
|
|
|
|
|
|
class CreateDeleteSDDC(object):
|
|
|
|
"""
|
|
|
|
Demonstrates create and delete a SDDC
|
|
|
|
|
|
|
|
Sample Prerequisites:
|
|
|
|
- An organization associated with the calling user.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
|
2019-08-20 02:54:24 -04:00
|
|
|
required_args = parser.add_argument_group(
|
|
|
|
'required arguments')
|
|
|
|
optional_args = parser.add_argument_group(
|
|
|
|
'optional arguments')
|
|
|
|
|
|
|
|
required_args.add_argument(
|
|
|
|
'--refresh_token',
|
2018-11-22 02:51:21 -05:00
|
|
|
required=True,
|
2019-08-20 02:54:24 -04:00
|
|
|
help='Refresh token obtained from CSP')
|
2018-11-22 02:51:21 -05:00
|
|
|
|
2019-08-20 02:54:24 -04:00
|
|
|
required_args.add_argument(
|
|
|
|
'--org_id',
|
|
|
|
required=True,
|
|
|
|
help='Organization identifier.')
|
2018-11-22 02:51:21 -05:00
|
|
|
|
2019-08-20 02:54:24 -04:00
|
|
|
optional_args.add_argument(
|
2018-11-22 02:51:21 -05:00
|
|
|
'--sddc-name',
|
|
|
|
help="Name of the SDDC to be created. "
|
|
|
|
"Default is 'Sample SDDC xx'")
|
|
|
|
|
2019-08-20 02:54:24 -04:00
|
|
|
optional_args.add_argument('--region', default='US_WEST_2', help='AWS Region')
|
2018-11-22 02:51:21 -05:00
|
|
|
|
2019-08-20 02:54:24 -04:00
|
|
|
optional_args.add_argument(
|
2018-11-22 02:51:21 -05:00
|
|
|
'--interval-sec',
|
|
|
|
default=60,
|
|
|
|
help='Task pulling interval in sec')
|
|
|
|
|
2019-08-20 02:54:24 -04:00
|
|
|
optional_args.add_argument(
|
2018-11-22 02:51:21 -05:00
|
|
|
'--listsddc',
|
|
|
|
action='store_true',
|
|
|
|
help='List SDDCs in the specified Org')
|
|
|
|
|
2019-08-20 02:54:24 -04:00
|
|
|
optional_args.add_argument(
|
2018-11-22 02:51:21 -05:00
|
|
|
'--createsddc',
|
|
|
|
action='store_true',
|
|
|
|
help='Create an SDDC in the specified Org')
|
|
|
|
|
2019-08-20 02:54:24 -04:00
|
|
|
optional_args.add_argument(
|
2018-11-22 02:51:21 -05:00
|
|
|
'--deletesddc',
|
|
|
|
action='store_true',
|
|
|
|
help='Deletes the SDDC in the specified Org ')
|
|
|
|
|
2019-08-20 02:54:24 -04:00
|
|
|
optional_args.add_argument(
|
2018-11-22 02:51:21 -05:00
|
|
|
'--cleardata',
|
|
|
|
action='store_true',
|
|
|
|
help='Clean up after sample run')
|
2017-12-05 18:20:37 -05:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
self.refresh_token = args.refresh_token
|
|
|
|
self.org_id = args.org_id
|
2018-06-15 01:23:07 -04:00
|
|
|
self.region = args.region
|
2018-03-21 17:41:40 -04:00
|
|
|
self.listsddc = args.listsddc
|
|
|
|
self.createsddc = args.createsddc
|
|
|
|
self.deletesddc = args.deletesddc
|
2017-12-05 18:20:37 -05:00
|
|
|
self.cleanup = args.cleardata
|
2018-06-15 01:23:07 -04:00
|
|
|
self.sddc_id = None
|
2018-11-22 02:51:21 -05:00
|
|
|
self.sddc_name = args.sddc_name or 'Sample SDDC {}'.format(
|
|
|
|
randrange(100))
|
2017-12-05 18:20:37 -05:00
|
|
|
self.interval_sec = int(args.interval_sec)
|
|
|
|
|
|
|
|
# Login to VMware Cloud on AWS
|
2018-06-15 01:23:07 -04:00
|
|
|
self.vmc_client = create_vmc_client(self.refresh_token)
|
|
|
|
|
|
|
|
def setup(self):
|
2017-12-05 18:20:37 -05:00
|
|
|
|
|
|
|
# Check if the organization exists
|
|
|
|
orgs = self.vmc_client.Orgs.list()
|
|
|
|
if self.org_id not in [org.id for org in orgs]:
|
2018-11-22 02:51:21 -05:00
|
|
|
raise ValueError("Org with ID {} doesn't exist".format(
|
|
|
|
self.org_id))
|
2017-12-05 18:20:37 -05:00
|
|
|
|
|
|
|
def create_sddc(self):
|
2018-11-22 02:51:21 -05:00
|
|
|
print('\n# Example: Create a SDDC ({}) in org {}:'.format(
|
|
|
|
self.sddc_name, self.org_id))
|
2017-12-05 18:20:37 -05:00
|
|
|
|
2019-07-21 23:29:10 -04:00
|
|
|
account_linking_config = None
|
2018-06-15 01:23:07 -04:00
|
|
|
|
2019-07-21 23:29:10 -04:00
|
|
|
# Get connected accounts if any
|
|
|
|
account_ids = self.vmc_client.orgs.account_link.ConnectedAccounts.get(
|
|
|
|
self.org_id)
|
2018-06-15 01:23:07 -04:00
|
|
|
|
2019-07-22 00:16:07 -04:00
|
|
|
if len(account_ids) > 0:
|
2019-07-21 23:29:10 -04:00
|
|
|
account_id = account_ids[0].id
|
|
|
|
|
|
|
|
vpc_map = self.vmc_client.orgs.account_link.CompatibleSubnets.get(
|
|
|
|
org=self.org_id, linked_account_id=account_id).vpc_map
|
|
|
|
|
|
|
|
customer_subnet_id = self.get_subnet_id(vpc_map)
|
|
|
|
if not customer_subnet_id:
|
|
|
|
raise ValueError('No available subnet for region {}'.format(
|
|
|
|
self.region))
|
|
|
|
|
|
|
|
account_linking_config = AccountLinkSddcConfig(
|
|
|
|
customer_subnet_ids=[customer_subnet_id],
|
|
|
|
connected_account_id=account_id)
|
2018-06-15 01:23:07 -04:00
|
|
|
|
2017-12-05 18:20:37 -05:00
|
|
|
sddc_config = AwsSddcConfig(
|
2018-06-15 01:23:07 -04:00
|
|
|
region=self.region,
|
|
|
|
name=self.sddc_name,
|
2019-07-21 23:29:10 -04:00
|
|
|
account_link_sddc_config=[account_linking_config] if account_linking_config else None,
|
2018-06-15 01:23:07 -04:00
|
|
|
provider=os.environ.get('VMC_PROVIDER', SddcConfig.PROVIDER_AWS),
|
2019-07-21 23:29:10 -04:00
|
|
|
num_hosts=4,
|
2018-06-15 01:23:07 -04:00
|
|
|
deployment_type=SddcConfig.DEPLOYMENT_TYPE_SINGLEAZ)
|
2018-03-13 20:35:12 -04:00
|
|
|
|
|
|
|
try:
|
2018-11-22 02:51:21 -05:00
|
|
|
task = self.vmc_client.orgs.Sddcs.create(
|
|
|
|
org=self.org_id, sddc_config=sddc_config)
|
2018-03-13 20:35:12 -04:00
|
|
|
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)
|
|
|
|
|
2018-11-22 02:51:21 -05:00
|
|
|
wait_for_task(
|
|
|
|
task_client=self.vmc_client.orgs.Tasks,
|
|
|
|
org_id=self.org_id,
|
|
|
|
task_id=task.id,
|
|
|
|
interval_sec=self.interval_sec)
|
2017-12-05 18:20:37 -05:00
|
|
|
|
|
|
|
print('\n# Example: SDDC created:')
|
|
|
|
self.sddc_id = task.resource_id
|
|
|
|
sddc = self.vmc_client.orgs.Sddcs.get(self.org_id, self.sddc_id)
|
2018-03-21 17:41:40 -04:00
|
|
|
self.print_output([sddc])
|
2017-12-05 18:20:37 -05:00
|
|
|
|
|
|
|
def delete_sddc(self):
|
2019-07-22 00:16:07 -04:00
|
|
|
# Get SDDC ID by name
|
|
|
|
sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id)
|
|
|
|
for sddc in sddcs:
|
|
|
|
if sddc.name == self.sddc_name:
|
|
|
|
self.sddc_id = sddc.id
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
raise ValueError('Cannot find sddc "{}"'.format(sddc_name))
|
|
|
|
|
2018-11-22 02:51:21 -05:00
|
|
|
print('\n# Example: Delete SDDC {} from org {}'.format(
|
|
|
|
self.sddc_id, self.org_id))
|
2018-03-13 20:35:12 -04:00
|
|
|
|
|
|
|
try:
|
2018-11-22 02:51:21 -05:00
|
|
|
task = self.vmc_client.orgs.Sddcs.delete(
|
|
|
|
org=self.org_id, sddc=self.sddc_id)
|
2018-03-13 20:35:12 -04:00
|
|
|
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)
|
|
|
|
|
2018-11-22 02:51:21 -05:00
|
|
|
wait_for_task(
|
|
|
|
task_client=self.vmc_client.orgs.Tasks,
|
|
|
|
org_id=self.org_id,
|
|
|
|
task_id=task.id,
|
|
|
|
interval_sec=self.interval_sec)
|
2017-12-05 18:20:37 -05:00
|
|
|
|
2018-11-22 02:51:21 -05:00
|
|
|
print('\n# Example: Remaining SDDCs:')
|
2018-03-21 17:41:40 -04:00
|
|
|
sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id)
|
|
|
|
self.print_output(sddcs)
|
|
|
|
|
|
|
|
def list_sddc(self):
|
|
|
|
sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id)
|
|
|
|
if not sddcs:
|
|
|
|
raise ValueError('The sample requires at least one SDDC associated'
|
|
|
|
'with the calling user')
|
|
|
|
print("\n# Example: List SDDCs")
|
|
|
|
self.print_output(sddcs)
|
|
|
|
|
|
|
|
def print_output(self, sddcs):
|
|
|
|
for sddc in sddcs:
|
2019-07-22 00:16:07 -04:00
|
|
|
print('ID: {}, Name: {}'.format(sddc.id, sddc.name))
|
2018-06-15 01:23:07 -04:00
|
|
|
|
|
|
|
def get_subnet_id(self, vpc_map):
|
|
|
|
for v in vpc_map.values():
|
|
|
|
for subnet in v.subnets:
|
|
|
|
if subnet.region_name.lower() == self.region.lower():
|
|
|
|
return subnet.subnet_id
|
|
|
|
|
2017-12-05 18:20:37 -05:00
|
|
|
|
|
|
|
def main():
|
2018-03-21 17:41:40 -04:00
|
|
|
sddc_operations = CreateDeleteSDDC()
|
|
|
|
sddc_operations.setup()
|
|
|
|
if sddc_operations.listsddc:
|
|
|
|
sddc_operations.list_sddc()
|
|
|
|
if sddc_operations.createsddc:
|
|
|
|
sddc_operations.create_sddc()
|
|
|
|
if sddc_operations.deletesddc:
|
|
|
|
sddc_operations.delete_sddc()
|
|
|
|
if sddc_operations.cleanup:
|
|
|
|
sddc_operations.delete_sddc()
|
2017-12-05 18:20:37 -05:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|