Skip to content

Commit 15eae0a

Browse files
authored
Merge pull request #170 from tianhao64/master
Bug fixes for sddc_crud.py
2 parents c60d430 + 1884e64 commit 15eae0a

File tree

2 files changed

+98
-17
lines changed

2 files changed

+98
-17
lines changed

samples/vmc/sddc/sddc_crud.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
"""
33
* *******************************************************
4-
* Copyright (c) VMware, Inc. 2017. All Rights Reserved.
4+
* Copyright (c) VMware, Inc. 2017-2019. All Rights Reserved.
55
* SPDX-License-Identifier: MIT
66
* *******************************************************
77
*
@@ -112,27 +112,33 @@ def create_sddc(self):
112112
print('\n# Example: Create a SDDC ({}) in org {}:'.format(
113113
self.sddc_name, self.org_id))
114114

115-
account_id = self.vmc_client.orgs.account_link.ConnectedAccounts.get(
116-
self.org_id)[0].id
115+
account_linking_config = None
117116

118-
vpc_map = self.vmc_client.orgs.account_link.CompatibleSubnets.get(
119-
org=self.org_id, linked_account_id=account_id).vpc_map
117+
# Get connected accounts if any
118+
account_ids = self.vmc_client.orgs.account_link.ConnectedAccounts.get(
119+
self.org_id)
120120

121-
customer_subnet_id = self.get_subnet_id(vpc_map)
122-
if not customer_subnet_id:
123-
raise ValueError('No available subnet for region {}'.format(
124-
self.region))
121+
if len(account_ids) > 0:
122+
account_id = account_ids[0].id
123+
124+
vpc_map = self.vmc_client.orgs.account_link.CompatibleSubnets.get(
125+
org=self.org_id, linked_account_id=account_id).vpc_map
126+
127+
customer_subnet_id = self.get_subnet_id(vpc_map)
128+
if not customer_subnet_id:
129+
raise ValueError('No available subnet for region {}'.format(
130+
self.region))
131+
132+
account_linking_config = AccountLinkSddcConfig(
133+
customer_subnet_ids=[customer_subnet_id],
134+
connected_account_id=account_id)
125135

126136
sddc_config = AwsSddcConfig(
127137
region=self.region,
128138
name=self.sddc_name,
129-
account_link_sddc_config=[
130-
AccountLinkSddcConfig(
131-
customer_subnet_ids=[customer_subnet_id],
132-
connected_account_id=account_id)
133-
],
139+
account_link_sddc_config=[account_linking_config] if account_linking_config else None,
134140
provider=os.environ.get('VMC_PROVIDER', SddcConfig.PROVIDER_AWS),
135-
num_hosts=1,
141+
num_hosts=4,
136142
deployment_type=SddcConfig.DEPLOYMENT_TYPE_SINGLEAZ)
137143

138144
try:
@@ -155,6 +161,15 @@ def create_sddc(self):
155161
self.print_output([sddc])
156162

157163
def delete_sddc(self):
164+
# Get SDDC ID by name
165+
sddcs = self.vmc_client.orgs.Sddcs.list(self.org_id)
166+
for sddc in sddcs:
167+
if sddc.name == self.sddc_name:
168+
self.sddc_id = sddc.id
169+
break
170+
else:
171+
raise ValueError('Cannot find sddc "{}"'.format(sddc_name))
172+
158173
print('\n# Example: Delete SDDC {} from org {}'.format(
159174
self.sddc_id, self.org_id))
160175

@@ -186,8 +201,7 @@ def list_sddc(self):
186201

187202
def print_output(self, sddcs):
188203
for sddc in sddcs:
189-
print('ID: {}, Name: {}, AWS Region: {}'.format(
190-
sddc.id, sddc.name, sddc.resource_config.region))
204+
print('ID: {}, Name: {}'.format(sddc.id, sddc.name))
191205

192206
def get_subnet_id(self, vpc_map):
193207
for v in vpc_map.values():

samples/vmc/tasks/list_tasks_stg.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
* *******************************************************
3+
* Copyright (c) VMware, Inc. 2019. All Rights Reserved.
4+
* SPDX-License-Identifier: MIT
5+
* *******************************************************
6+
*
7+
* DISCLAIMER. THIS PROGRAM IS PROVIDED TO YOU "AS IS" WITHOUT
8+
* WARRANTIES OR CONDITIONS OF ANY KIND, WHETHER ORAL OR WRITTEN,
9+
* EXPRESS OR IMPLIED. THE AUTHOR SPECIFICALLY DISCLAIMS ANY IMPLIED
10+
* WARRANTIES OR CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY,
11+
* NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
12+
"""
13+
14+
__author__ = 'VMware, Inc.'
15+
16+
import argparse
17+
18+
from com.vmware.vmc.model_client import Task
19+
from vmware.vapi.vmc.client import create_vmc_client
20+
21+
"""
22+
Demonstrates how to list tasks with given status
23+
24+
Sample Prerequisites:
25+
- VMware Cloud on AWS console API access
26+
"""
27+
28+
accepted = [Task.STATUS_STARTED, Task.STATUS_CANCELING, Task.STATUS_FINISHED,
29+
Task.STATUS_FAILED, Task.STATUS_CANCELED]
30+
31+
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
32+
33+
parser.add_argument('--refresh-token',
34+
required=True,
35+
help='VMware Cloud API refresh token')
36+
37+
parser.add_argument('--org-id',
38+
required=True,
39+
help='Organization identifier.')
40+
41+
parser.add_argument('--task-status',
42+
help='Task status to filter. Possible values are: {} \
43+
Show all tasks if no value is passed'.format(accepted))
44+
45+
args = parser.parse_args()
46+
47+
vmc_client = create_vmc_client(args.refresh_token)
48+
49+
tasks = []
50+
51+
if args.task_status:
52+
status = args.task_status.upper()
53+
54+
if status not in accepted:
55+
raise ValueError('Status "{}" is invalid, accept values are {}'.
56+
format(args.task_status, accepted))
57+
58+
tasks = vmc_client.orgs.Tasks.list(
59+
org=args.org_id, filter="(status eq '{}')".format(status))
60+
61+
print('# List all "{}" tasks:\n'.format(status))
62+
else:
63+
tasks = vmc_client.orgs.Tasks.list(org=args.org_id)
64+
print('# List all tasks:\n')
65+
66+
for task in tasks:
67+
print('{}\n'.format(task))

0 commit comments

Comments
 (0)