From 1884e64a37291ce3359aed23457d9c51e4742b3b Mon Sep 17 00:00:00 2001 From: Tianhao He Date: Mon, 22 Jul 2019 12:16:07 +0800 Subject: [PATCH] Add get sddc id by name when deleting sddc Don't print region as some testing sddcs don't have region info. --- samples/vmc/sddc/sddc_crud.py | 16 +++++-- samples/vmc/tasks/list_tasks_stg.py | 67 +++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 samples/vmc/tasks/list_tasks_stg.py diff --git a/samples/vmc/sddc/sddc_crud.py b/samples/vmc/sddc/sddc_crud.py index 89dff119..1ef118c0 100644 --- a/samples/vmc/sddc/sddc_crud.py +++ b/samples/vmc/sddc/sddc_crud.py @@ -1,7 +1,7 @@ #!/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 * ******************************************************* * @@ -118,7 +118,7 @@ class CreateDeleteSDDC(object): account_ids = self.vmc_client.orgs.account_link.ConnectedAccounts.get( self.org_id) - if len(account_ids) > 0 : + if len(account_ids) > 0: account_id = account_ids[0].id vpc_map = self.vmc_client.orgs.account_link.CompatibleSubnets.get( @@ -161,6 +161,15 @@ class CreateDeleteSDDC(object): self.print_output([sddc]) 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( self.sddc_id, self.org_id)) @@ -192,8 +201,7 @@ class CreateDeleteSDDC(object): def print_output(self, sddcs): for sddc in sddcs: - print('ID: {}, Name: {}, AWS Region: {}'.format( - sddc.id, sddc.name, sddc.resource_config.region)) + print('ID: {}, Name: {}'.format(sddc.id, sddc.name)) def get_subnet_id(self, vpc_map): for v in vpc_map.values(): diff --git a/samples/vmc/tasks/list_tasks_stg.py b/samples/vmc/tasks/list_tasks_stg.py new file mode 100644 index 00000000..9c9f6bc5 --- /dev/null +++ b/samples/vmc/tasks/list_tasks_stg.py @@ -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))