1
0
mirror of https://github.com/vmware/vsphere-automation-sdk-python.git synced 2024-11-22 01:39:58 -05:00

Add exception handling for sddc samples to get detailed error message

Signed-off-by: Tianhao He <het@vmware.com>
This commit is contained in:
Tianhao He 2018-03-13 17:35:12 -07:00
parent aae89a9af0
commit 08028b54dd
2 changed files with 43 additions and 13 deletions

View File

@ -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,

View File

@ -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,