Skip to content

Commit c0d26bf

Browse files
author
René Moser
authored
Fix progress output for identifier name (#36)
1 parent a6b692d commit c0d26bf

3 files changed

Lines changed: 25 additions & 19 deletions

File tree

cloudscale_cli/commands/__init__.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ def __init__(self, cloud_resource_name=None, api_token=None, profile=None, debug
3939
# Alternate key to look for the resource as 'name'
4040
self.resource_name_key = 'name'
4141

42+
# Some resource have a different identifier name
43+
self.resource_uuid_name = 'UUID'
44+
4245
# Usually we want to sort the table by the key used as name
4346
self.resource_table_sort_key = None
4447

@@ -129,16 +132,16 @@ def cmd_get_by_name(self, name):
129132

130133
def cmd_show(self, uuid):
131134
try:
132-
with Spinner(text=f"Querying by UUID {uuid}"):
135+
with Spinner(text=f"Querying by {self.resource_uuid_name} {uuid}"):
133136
response = self.get_client_resource().get_by_uuid(uuid)
134137
click.echo(self._format_output(response))
135138
except CloudscaleApiException as e:
136139
results = self.cmd_get_by_name(name=uuid)
137140
if not results:
138141
if self.resource_name_key:
139-
msg = f"No resource found for {self.cloud_resource_name} having UUID or {self.resource_name_key}: {uuid}"
142+
msg = f"No resource found for {self.cloud_resource_name} having {self.resource_uuid_name} or {self.resource_name_key}: {uuid}"
140143
else:
141-
msg = f"No resource found for {self.cloud_resource_name} having UUID: {uuid}"
144+
msg = f"No resource found for {self.cloud_resource_name} having {self.resource_uuid_name}: {uuid}"
142145
click.echo(msg, err=True)
143146
sys.exit(1)
144147
click.echo(self._format_output(results))
@@ -168,24 +171,24 @@ def cmd_create(self, silent=False, **kwargs):
168171

169172
def cmd_update(self, uuid, tags=None, clear_tags=None, clear_all_tags=False, wait=False, **kwargs):
170173
try:
171-
with Spinner(text=f"Querying by UUID {uuid}"):
174+
with Spinner(text=f"Querying by {self.resource_uuid_name} {uuid}"):
172175
self.get_client_resource().get_by_uuid(uuid)
173176

174177
except CloudscaleApiException as e:
175178
results = self.cmd_get_by_name(name=uuid)
176179
if not results:
177180
if self.resource_name_key:
178-
msg = f"No resource found for {self.cloud_resource_name} having UUID or {self.resource_name_key}: {uuid}"
181+
msg = f"No resource found for {self.cloud_resource_name} having {self.resource_uuid_name} or {self.resource_name_key}: {uuid}"
179182
else:
180-
msg = f"No resource found for {self.cloud_resource_name} having UUID: {uuid}"
183+
msg = f"No resource found for {self.cloud_resource_name} having {self.resource_uuid_name}: {uuid}"
181184
click.echo(msg, err=True)
182185
sys.exit(1)
183186

184187
if len(results) > 1:
185-
click.echo(f"Error: More than one resource found for {self.cloud_resource_name} having name: {uuid}. Please use UUID to select the resource.", err=True)
188+
click.echo(f"Error: More than one resource found for {self.cloud_resource_name} having name: {uuid}. Please use {self.resource_uuid_name} to select the resource.", err=True)
186189
sys.exit(1)
187190

188-
# Single resource found, remember UUID
191+
# Single resource found, remember identifier
189192
uuid = results[0]['href'].split('/')[-1]
190193

191194
try:
@@ -208,7 +211,7 @@ def cmd_update(self, uuid, tags=None, clear_tags=None, clear_all_tags=False, wai
208211
_tags = None
209212

210213

211-
with Spinner(text=f"Updating by UUID {uuid}"):
214+
with Spinner(text=f"Updating by {self.resource_uuid_name} {uuid}"):
212215
self.get_client_resource().update(
213216
uuid=uuid,
214217
tags=_tags,
@@ -230,31 +233,31 @@ def cmd_delete(self, uuid, force=False, skip_query=False):
230233
try:
231234
if not skip_query:
232235
try:
233-
with Spinner(text=f"Querying by UUID {uuid}"):
236+
with Spinner(text=f"Querying by {self.resource_uuid_name} {uuid}"):
234237
response = self.get_client_resource().get_by_uuid(uuid)
235238
except CloudscaleApiException as e:
236239
results = self.cmd_get_by_name(name=uuid)
237240
if not results:
238241
if self.resource_name_key:
239-
msg = f"No resource found for {self.cloud_resource_name} having UUID or {self.resource_name_key}: {uuid}"
242+
msg = f"No resource found for {self.cloud_resource_name} having {self.resource_uuid_name} or {self.resource_name_key}: {uuid}"
240243
else:
241-
msg = f"No resource found for {self.cloud_resource_name} having UUID: {uuid}"
244+
msg = f"No resource found for {self.cloud_resource_name} having {self.resource_uuid_name}: {uuid}"
242245
click.echo(msg, err=True)
243246
sys.exit(1)
244247

245248
if len(results) > 1:
246249
click.echo(f"Error: More than one resource found for {self.cloud_resource_name} having name: {uuid}", err=True)
247250
sys.exit(1)
248251

249-
# Single resource found, remember UUID
252+
# Single resource found, remember identifier
250253
response = results[0]
251254
uuid = results[0]['href'].split('/')[-1]
252255

253256
click.echo(self._format_output(response))
254257

255258
if not force:
256259
click.confirm('Do you want to delete?', abort=True)
257-
with Spinner(text=f"Deleting by UUID {uuid}"):
260+
with Spinner(text=f"Deleting by {self.resource_uuid_name} {uuid}"):
258261
self.get_client_resource().delete(uuid)
259262
click.echo(f"{uuid} deleted!")
260263
except Exception as e:
@@ -264,24 +267,24 @@ def cmd_delete(self, uuid, force=False, skip_query=False):
264267
def cmd_act(self, action, uuid, wait=False):
265268
with Spinner(text=f"Processing"):
266269
try:
267-
with Spinner(text=f"Querying by UUID {uuid}"):
270+
with Spinner(text=f"Querying by {self.resource_uuid_name} {uuid}"):
268271
self.get_client_resource().get_by_uuid(uuid)
269272

270273
except CloudscaleApiException as e:
271274
results = self.cmd_get_by_name(name=uuid)
272275
if not results:
273276
if self.resource_name_key:
274-
msg = f"No resource found for {self.cloud_resource_name} having UUID or {self.resource_name_key}: {uuid}"
277+
msg = f"No resource found for {self.cloud_resource_name} having {self.resource_uuid_name} or {self.resource_name_key}: {uuid}"
275278
else:
276-
msg = f"No resource found for {self.cloud_resource_name} having UUID: {uuid}"
279+
msg = f"No resource found for {self.cloud_resource_name} having {self.resource_uuid_name}: {uuid}"
277280
click.echo(msg, err=True)
278281
sys.exit(1)
279282

280283
if len(results) > 1:
281-
click.echo(f"Error: More than one resource found for {self.cloud_resource_name} having name: {uuid}. Please use UUID to select the resource.", err=True)
284+
click.echo(f"Error: More than one resource found for {self.cloud_resource_name} having name: {uuid}. Please use {self.resource_uuid_name} to select the resource.", err=True)
282285
sys.exit(1)
283286

284-
# Single resource found, remember UUID
287+
# Single resource found, remember identifier
285288
uuid = results[0]['href'].split('/')[-1]
286289

287290
try:

cloudscale_cli/commands/floating_ip.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def floating_ip(ctx):
1414
'tags',
1515
]
1616
ctx.obj.resource_name_key = None
17+
ctx.obj.resource_uuid_name = "network"
1718

1819
@click.option('--filter-tag')
1920
@click.option('--filter-json')

cloudscale_cli/commands/objects_user.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def objects_user(ctx):
1212
'id',
1313
]
1414
ctx.obj.resource_name_key = 'display_name'
15+
ctx.obj.resource_uuid_name = "id"
1516
ctx.obj.response_transform_json = '''
1617
[].{
1718
"display_name": display_name,
@@ -21,6 +22,7 @@ def objects_user(ctx):
2122
"id": id
2223
}
2324
'''
25+
2426
@click.option('--filter-tag')
2527
@click.option('--filter-json')
2628
@click.option('--delete', is_flag=True)

0 commit comments

Comments
 (0)