From 08028b54dde5ce0b58c48b9c1974a922a9401864 Mon Sep 17 00:00:00 2001 From: Tianhao He Date: Tue, 13 Mar 2018 17:35:12 -0700 Subject: [PATCH] Add exception handling for sddc samples to get detailed error message Signed-off-by: Tianhao He --- samples/vmc/sddc/add_remove_hosts.py | 31 +++++++++++++++++++------- samples/vmc/sddc/create_delete_sddc.py | 25 ++++++++++++++++----- 2 files changed, 43 insertions(+), 13 deletions(-) diff --git a/samples/vmc/sddc/add_remove_hosts.py b/samples/vmc/sddc/add_remove_hosts.py index 068845cf..3d9cdcb7 100644 --- a/samples/vmc/sddc/add_remove_hosts.py +++ b/samples/vmc/sddc/add_remove_hosts.py @@ -19,7 +19,8 @@ import argparse import atexit import requests -from com.vmware.vmc.model_client import EsxConfig +from com.vmware.vmc.model_client import EsxConfig, ErrorResponse +from com.vmware.vapi.std.errors_client import InvalidRequest from vmware.vapi.vmc.client import create_vmc_client from samples.vmc.helpers.vmc_task_helper import wait_for_task @@ -89,9 +90,16 @@ class AddRemoveHosts(object): def add_host(self): print('\n# Example: Add 1 ESX hosts to SDDC {}:'.format(self.sddc_id)) esx_config = EsxConfig(1) - task = self.vmc_client.orgs.sddcs.Esxs.create(org=self.org_id, - sddc=self.sddc_id, - esx_config=esx_config) + + try: + task = self.vmc_client.orgs.sddcs.Esxs.create(org=self.org_id, + sddc=self.sddc_id, + esx_config=esx_config) + 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.orgs.Tasks, org_id=self.org_id, task_id=task.id, @@ -101,10 +109,17 @@ class AddRemoveHosts(object): print('\n# Example: Remove 1 ESX host from SDDC {}:'. format(self.sddc_id)) esx_config = EsxConfig(1) - task = self.vmc_client.orgs.sddcs.Esxs.create(org=self.org_id, - sddc=self.sddc_id, - esx_config=esx_config, - action='remove') + + try: + task = self.vmc_client.orgs.sddcs.Esxs.create(org=self.org_id, + sddc=self.sddc_id, + esx_config=esx_config, + action='remove') + 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.orgs.Tasks, org_id=self.org_id, task_id=task.id, diff --git a/samples/vmc/sddc/create_delete_sddc.py b/samples/vmc/sddc/create_delete_sddc.py index f2fed5b1..dc05999d 100644 --- a/samples/vmc/sddc/create_delete_sddc.py +++ b/samples/vmc/sddc/create_delete_sddc.py @@ -22,7 +22,8 @@ import requests from random import randrange from tabulate import tabulate -from com.vmware.vmc.model_client import AwsSddcConfig +from com.vmware.vmc.model_client import AwsSddcConfig, ErrorResponse +from com.vmware.vapi.std.errors_client import InvalidRequest from vmware.vapi.vmc.client import create_vmc_client from samples.vmc.helpers.vmc_task_helper import wait_for_task @@ -98,8 +99,15 @@ class CreateDeleteSDDC(object): sddc_config = AwsSddcConfig( region='US_WEST_2', num_hosts=1, name=self.sddc_name, provider=provider) - task = self.vmc_client.orgs.Sddcs.create(org=self.org_id, - sddc_config=sddc_config) + + try: + task = self.vmc_client.orgs.Sddcs.create(org=self.org_id, + sddc_config=sddc_config) + 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.orgs.Tasks, org_id=self.org_id, task_id=task.id, @@ -113,8 +121,15 @@ class CreateDeleteSDDC(object): def delete_sddc(self): print('\n# Example: Delete SDDC {} from org {}'.format(self.sddc_id, self.org_id)) - task = self.vmc_client.orgs.Sddcs.delete(org=self.org_id, - sddc=self.sddc_id) + + try: + task = self.vmc_client.orgs.Sddcs.delete(org=self.org_id, + sddc=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.orgs.Tasks, org_id=self.org_id, task_id=task.id,