From b8554584c2c0deefcca3694a699acbc1b9c9f070 Mon Sep 17 00:00:00 2001 From: Kyle Ruddy Date: Wed, 21 Mar 2018 17:34:28 -0400 Subject: [PATCH 1/3] Remove DHCP Column from Output Not every logical switch contains DHCP IP Pool information. Removing the DHCP column from being output. Resolving the Following Error: (VMWonAWSDemo) kruddy-m02:vsphere-automation-sdk-python kyleruddy$ python3 ./samples/vmc/networks/logical_network_crud.py -r $refreshtoken -o $orgid -s $sddcid --network-name 'Python-SDK-LogicalNetwork' -c # Example: Add a logical network to the compute gateway Compute Gateway ID: edge-2 # New logical network "Python-SDK-LogicalNetwork" is added # Example: List all logical networks Traceback (most recent call last): File "./samples/vmc/networks/logical_network_crud.py", line 208, in main() File "./samples/vmc/networks/logical_network_crud.py", line 201, in main logical_network_crud.get_logical_network() File "./samples/vmc/networks/logical_network_crud.py", line 134, in get_logical_network self.print_output(networks) File "./samples/vmc/networks/logical_network_crud.py", line 192, in print_output network.dhcp_configs.ip_pools[0].ip_range]) AttributeError: 'NoneType' object has no attribute 'ip_pools' --- samples/vmc/networks/logical_network_crud.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/samples/vmc/networks/logical_network_crud.py b/samples/vmc/networks/logical_network_crud.py index 69a53481..bf8ffe96 100644 --- a/samples/vmc/networks/logical_network_crud.py +++ b/samples/vmc/networks/logical_network_crud.py @@ -188,10 +188,9 @@ class LogicalNetworkCrud(object): table.append([network.cgw_name, network.id, network.name, '{}/{}'.format( network.subnets.address_groups[0].primary_address, - network.subnets.address_groups[0].prefix_length), - network.dhcp_configs.ip_pools[0].ip_range]) + network.subnets.address_groups[0].prefix_length)]) print(tabulate(table, ['Gateway', 'Network ID', 'Network Name', - 'Subnets', 'DHCP'])) + 'Subnets'])) def main(): From 0a62db4e0f093fb9233be0f88443abca0da59eb1 Mon Sep 17 00:00:00 2001 From: Kyle Ruddy Date: Wed, 21 Mar 2018 17:41:40 -0400 Subject: [PATCH 2/3] Added List_SDDC Function & Added Standard Output Function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adding a standalone list_sddc function, parameters to handle the calling of whether or not the intended action be a list, create, and/or delete method, added a standard output function to match what’s available as part of the logical network example. --- samples/vmc/sddc/create_delete_sddc.py | 57 ++++++++++++++++++++------ 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/samples/vmc/sddc/create_delete_sddc.py b/samples/vmc/sddc/create_delete_sddc.py index dc05999d..7de14c53 100644 --- a/samples/vmc/sddc/create_delete_sddc.py +++ b/samples/vmc/sddc/create_delete_sddc.py @@ -42,6 +42,9 @@ class CreateDeleteSDDC(object): self.vmc_client = None self.sddc_id = None self.sddc_name = None + self.listsddc = None + self.createsddc = None + self.deletesddc = None self.cleanup = None self.refresh_token = None self.interval_sec = None @@ -65,6 +68,18 @@ class CreateDeleteSDDC(object): default=60, help='Task pulling interval in sec') + parser.add_argument('-ls', '--listsddc', + action='store_true', + help='List SDDCs in the specified Org') + + parser.add_argument('-cs', '--createsddc', + action='store_true', + help='Create an SDDC in the specified Org') + + parser.add_argument('-ds', '--deletesddc', + action='store_true', + help='Deletes the SDDC in the specified Org ') + parser.add_argument('-c', '--cleardata', action='store_true', help='Clean up after sample run') @@ -73,6 +88,9 @@ class CreateDeleteSDDC(object): self.refresh_token = args.refresh_token self.org_id = args.org_id + self.listsddc = args.listsddc + self.createsddc = args.createsddc + self.deletesddc = args.deletesddc self.cleanup = args.cleardata self.sddc_name = args.sddc_name or \ @@ -116,7 +134,7 @@ class CreateDeleteSDDC(object): print('\n# Example: SDDC created:') self.sddc_id = task.resource_id sddc = self.vmc_client.orgs.Sddcs.get(self.org_id, self.sddc_id) - print(tabulate([[sddc.id, sddc.name]], ["ID", "Name"])) + self.print_output([sddc]) def delete_sddc(self): print('\n# Example: Delete SDDC {} from org {}'.format(self.sddc_id, @@ -137,22 +155,35 @@ class CreateDeleteSDDC(object): print('\n# Example: Remaining SDDCs:'. format(self.org_id)) - sddcs_list = self.vmc_client.orgs.Sddcs.list(self.org_id) + sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id) + self.print_output(sddcs) - headers = ["ID", "Name"] + 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): table = [] - for sddc in sddcs_list: - table.append([sddc.id, sddc.name]) - print(tabulate(table, headers)) - + for sddc in sddcs: + table.append([sddc.id, sddc.name, sddc.resource_config.region]) + print(tabulate(table, ['ID', 'Name', 'AWS Region'])) def main(): - esx_operations = CreateDeleteSDDC() - esx_operations.option() - esx_operations.setup() - esx_operations.create_sddc() - if esx_operations.cleanup: - esx_operations.delete_sddc() + sddc_operations = CreateDeleteSDDC() + sddc_operations.option() + 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() if __name__ == '__main__': From 49cc25979397ed00c109e18f20a6c99f6e574605 Mon Sep 17 00:00:00 2001 From: Kyle Ruddy Date: Fri, 23 Mar 2018 08:28:24 -0400 Subject: [PATCH 3/3] Update Sample Name - sddc_crud.py Since the sample has been updated to do more than just creating and deleting an SDDC, the file name has been updated as well. --- samples/vmc/sddc/{create_delete_sddc.py => sddc_crud.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename samples/vmc/sddc/{create_delete_sddc.py => sddc_crud.py} (100%) diff --git a/samples/vmc/sddc/create_delete_sddc.py b/samples/vmc/sddc/sddc_crud.py similarity index 100% rename from samples/vmc/sddc/create_delete_sddc.py rename to samples/vmc/sddc/sddc_crud.py