mirror of
https://github.com/vmware/vsphere-automation-sdk-python.git
synced 2024-11-21 17:29:59 -05:00
Merge pull request #170 from tianhao64/master
Bug fixes for sddc_crud.py
This commit is contained in:
commit
15eae0ae02
@ -1,7 +1,7 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
"""
|
"""
|
||||||
* *******************************************************
|
* *******************************************************
|
||||||
* Copyright (c) VMware, Inc. 2017. All Rights Reserved.
|
* Copyright (c) VMware, Inc. 2017-2019. All Rights Reserved.
|
||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
* *******************************************************
|
* *******************************************************
|
||||||
*
|
*
|
||||||
@ -112,27 +112,33 @@ class CreateDeleteSDDC(object):
|
|||||||
print('\n# Example: Create a SDDC ({}) in org {}:'.format(
|
print('\n# Example: Create a SDDC ({}) in org {}:'.format(
|
||||||
self.sddc_name, self.org_id))
|
self.sddc_name, self.org_id))
|
||||||
|
|
||||||
account_id = self.vmc_client.orgs.account_link.ConnectedAccounts.get(
|
account_linking_config = None
|
||||||
self.org_id)[0].id
|
|
||||||
|
|
||||||
vpc_map = self.vmc_client.orgs.account_link.CompatibleSubnets.get(
|
# Get connected accounts if any
|
||||||
org=self.org_id, linked_account_id=account_id).vpc_map
|
account_ids = self.vmc_client.orgs.account_link.ConnectedAccounts.get(
|
||||||
|
self.org_id)
|
||||||
|
|
||||||
customer_subnet_id = self.get_subnet_id(vpc_map)
|
if len(account_ids) > 0:
|
||||||
if not customer_subnet_id:
|
account_id = account_ids[0].id
|
||||||
raise ValueError('No available subnet for region {}'.format(
|
|
||||||
self.region))
|
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)
|
||||||
|
|
||||||
sddc_config = AwsSddcConfig(
|
sddc_config = AwsSddcConfig(
|
||||||
region=self.region,
|
region=self.region,
|
||||||
name=self.sddc_name,
|
name=self.sddc_name,
|
||||||
account_link_sddc_config=[
|
account_link_sddc_config=[account_linking_config] if account_linking_config else None,
|
||||||
AccountLinkSddcConfig(
|
|
||||||
customer_subnet_ids=[customer_subnet_id],
|
|
||||||
connected_account_id=account_id)
|
|
||||||
],
|
|
||||||
provider=os.environ.get('VMC_PROVIDER', SddcConfig.PROVIDER_AWS),
|
provider=os.environ.get('VMC_PROVIDER', SddcConfig.PROVIDER_AWS),
|
||||||
num_hosts=1,
|
num_hosts=4,
|
||||||
deployment_type=SddcConfig.DEPLOYMENT_TYPE_SINGLEAZ)
|
deployment_type=SddcConfig.DEPLOYMENT_TYPE_SINGLEAZ)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -155,6 +161,15 @@ class CreateDeleteSDDC(object):
|
|||||||
self.print_output([sddc])
|
self.print_output([sddc])
|
||||||
|
|
||||||
def delete_sddc(self):
|
def delete_sddc(self):
|
||||||
|
# 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))
|
||||||
|
|
||||||
print('\n# Example: Delete SDDC {} from org {}'.format(
|
print('\n# Example: Delete SDDC {} from org {}'.format(
|
||||||
self.sddc_id, self.org_id))
|
self.sddc_id, self.org_id))
|
||||||
|
|
||||||
@ -186,8 +201,7 @@ class CreateDeleteSDDC(object):
|
|||||||
|
|
||||||
def print_output(self, sddcs):
|
def print_output(self, sddcs):
|
||||||
for sddc in sddcs:
|
for sddc in sddcs:
|
||||||
print('ID: {}, Name: {}, AWS Region: {}'.format(
|
print('ID: {}, Name: {}'.format(sddc.id, sddc.name))
|
||||||
sddc.id, sddc.name, sddc.resource_config.region))
|
|
||||||
|
|
||||||
def get_subnet_id(self, vpc_map):
|
def get_subnet_id(self, vpc_map):
|
||||||
for v in vpc_map.values():
|
for v in vpc_map.values():
|
||||||
|
67
samples/vmc/tasks/list_tasks_stg.py
Normal file
67
samples/vmc/tasks/list_tasks_stg.py
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
"""
|
||||||
|
* *******************************************************
|
||||||
|
* 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.'
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
from com.vmware.vmc.model_client import Task
|
||||||
|
from vmware.vapi.vmc.client import create_vmc_client
|
||||||
|
|
||||||
|
"""
|
||||||
|
Demonstrates how to list tasks with given status
|
||||||
|
|
||||||
|
Sample Prerequisites:
|
||||||
|
- VMware Cloud on AWS console API access
|
||||||
|
"""
|
||||||
|
|
||||||
|
accepted = [Task.STATUS_STARTED, Task.STATUS_CANCELING, Task.STATUS_FINISHED,
|
||||||
|
Task.STATUS_FAILED, Task.STATUS_CANCELED]
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||||
|
|
||||||
|
parser.add_argument('--refresh-token',
|
||||||
|
required=True,
|
||||||
|
help='VMware Cloud API refresh token')
|
||||||
|
|
||||||
|
parser.add_argument('--org-id',
|
||||||
|
required=True,
|
||||||
|
help='Organization identifier.')
|
||||||
|
|
||||||
|
parser.add_argument('--task-status',
|
||||||
|
help='Task status to filter. Possible values are: {} \
|
||||||
|
Show all tasks if no value is passed'.format(accepted))
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
vmc_client = create_vmc_client(args.refresh_token)
|
||||||
|
|
||||||
|
tasks = []
|
||||||
|
|
||||||
|
if args.task_status:
|
||||||
|
status = args.task_status.upper()
|
||||||
|
|
||||||
|
if status not in accepted:
|
||||||
|
raise ValueError('Status "{}" is invalid, accept values are {}'.
|
||||||
|
format(args.task_status, accepted))
|
||||||
|
|
||||||
|
tasks = vmc_client.orgs.Tasks.list(
|
||||||
|
org=args.org_id, filter="(status eq '{}')".format(status))
|
||||||
|
|
||||||
|
print('# List all "{}" tasks:\n'.format(status))
|
||||||
|
else:
|
||||||
|
tasks = vmc_client.orgs.Tasks.list(org=args.org_id)
|
||||||
|
print('# List all tasks:\n')
|
||||||
|
|
||||||
|
for task in tasks:
|
||||||
|
print('{}\n'.format(task))
|
Loading…
Reference in New Issue
Block a user