Skip to content

Commit 70fbf68

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "quota: Migrate 'quota *' to SDK"
2 parents 2d59c67 + 6127b44 commit 70fbf68

File tree

7 files changed

+562
-890
lines changed

7 files changed

+562
-890
lines changed

openstackclient/common/quota.py

Lines changed: 116 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import logging
2020
import sys
2121

22+
from openstack import exceptions as sdk_exceptions
2223
from osc_lib.command import command
2324
from osc_lib import exceptions
2425
from osc_lib import utils
@@ -104,11 +105,8 @@ def _xform_get_quota(data, value, keys):
104105

105106
def get_project(app, project):
106107
if project is not None:
107-
identity_client = app.client_manager.identity
108-
project = utils.find_resource(
109-
identity_client.projects,
110-
project,
111-
)
108+
identity_client = app.client_manager.sdk_connection.identity
109+
project = identity_client.find_project(project, ignore_missing=False)
112110
project_id = project.id
113111
project_name = project.name
114112
elif app.client_manager.auth_ref:
@@ -134,16 +132,18 @@ def get_compute_quotas(
134132
default=False,
135133
):
136134
try:
137-
client = app.client_manager.compute
135+
client = app.client_manager.sdk_connection.compute
138136
if default:
139-
quota = client.quotas.defaults(project_id)
137+
quota = client.get_quota_set_defaults(project_id)
140138
else:
141-
quota = client.quotas.get(project_id, detail=detail)
142-
except Exception as e:
143-
if type(e).__name__ == 'EndpointNotFound':
144-
return {}
145-
raise
146-
return quota._info
139+
quota = client.get_quota_set(project_id, usage=detail)
140+
except sdk_exceptions.EndpointNotFound:
141+
return {}
142+
data = quota.to_dict()
143+
if not detail:
144+
del data['usage']
145+
del data['reservation']
146+
return data
147147

148148

149149
def get_volume_quotas(
@@ -154,17 +154,18 @@ def get_volume_quotas(
154154
default=False,
155155
):
156156
try:
157-
client = app.client_manager.volume
157+
client = app.client_manager.sdk_connection.volume
158158
if default:
159-
quota = client.quotas.defaults(project_id)
159+
quota = client.get_quota_set_defaults(project_id)
160160
else:
161-
quota = client.quotas.get(project_id, usage=detail)
162-
except Exception as e:
163-
if type(e).__name__ == 'EndpointNotFound':
164-
return {}
165-
else:
166-
raise
167-
return quota._info
161+
quota = client.get_quota_set(project_id, usage=detail)
162+
except sdk_exceptions.EndpointNotFound:
163+
return {}
164+
data = quota.to_dict()
165+
if not detail:
166+
del data['usage']
167+
del data['reservation']
168+
return data
168169

169170

170171
def get_network_quotas(
@@ -242,37 +243,35 @@ def get_parser(self, prog_name):
242243
return parser
243244

244245
def _list_quota_compute(self, parsed_args, project_ids):
245-
compute_client = self.app.client_manager.compute
246+
compute_client = self.app.client_manager.sdk_connection.compute
246247
result = []
247248

248-
for p in project_ids:
249+
for project_id in project_ids:
249250
try:
250-
data = compute_client.quotas.get(p)
251-
except Exception as ex:
252-
if (
253-
type(ex).__name__ == 'NotFound'
254-
or ex.http_status >= 400
255-
and ex.http_status <= 499
256-
):
257-
# Project not found, move on to next one
258-
LOG.warning(f"Project {p} not found: {ex}")
259-
continue
260-
else:
261-
raise
262-
263-
result_data = _xform_get_quota(
264-
data,
265-
p,
251+
project_data = compute_client.get_quota_set(project_id)
252+
except (
253+
sdk_exceptions.NotFoundException,
254+
sdk_exceptions.ForbiddenException,
255+
) as exc:
256+
# Project not found, move on to next one
257+
LOG.warning(f"Project {project_id} not found: {exc}")
258+
continue
259+
260+
project_result = _xform_get_quota(
261+
project_data,
262+
project_id,
266263
COMPUTE_QUOTAS.keys(),
267264
)
268-
default_data = compute_client.quotas.defaults(p)
269-
result_default = _xform_get_quota(
265+
266+
default_data = compute_client.get_quota_set_defaults(project_id)
267+
default_result = _xform_get_quota(
270268
default_data,
271-
p,
269+
project_id,
272270
COMPUTE_QUOTAS.keys(),
273271
)
274-
if result_default != result_data:
275-
result += result_data
272+
273+
if default_result != project_result:
274+
result += project_result
276275

277276
columns = (
278277
'id',
@@ -306,33 +305,35 @@ def _list_quota_compute(self, parsed_args, project_ids):
306305
)
307306

308307
def _list_quota_volume(self, parsed_args, project_ids):
309-
volume_client = self.app.client_manager.volume
308+
volume_client = self.app.client_manager.sdk_connection.volume
310309
result = []
311310

312-
for p in project_ids:
311+
for project_id in project_ids:
313312
try:
314-
data = volume_client.quotas.get(p)
315-
except Exception as ex:
316-
if type(ex).__name__ == 'NotFound':
317-
# Project not found, move on to next one
318-
LOG.warning(f"Project {p} not found: {ex}")
319-
continue
320-
else:
321-
raise
322-
323-
result_data = _xform_get_quota(
324-
data,
325-
p,
313+
project_data = volume_client.get_quota_set(project_id)
314+
except (
315+
sdk_exceptions.NotFoundException,
316+
sdk_exceptions.ForbiddenException,
317+
) as exc:
318+
# Project not found, move on to next one
319+
LOG.warning(f"Project {project_id} not found: {exc}")
320+
continue
321+
322+
project_result = _xform_get_quota(
323+
project_data,
324+
project_id,
326325
VOLUME_QUOTAS.keys(),
327326
)
328-
default_data = volume_client.quotas.defaults(p)
329-
result_default = _xform_get_quota(
327+
328+
default_data = volume_client.get_quota_set_defaults(project_id)
329+
default_result = _xform_get_quota(
330330
default_data,
331-
p,
331+
project_id,
332332
VOLUME_QUOTAS.keys(),
333333
)
334-
if result_default != result_data:
335-
result += result_data
334+
335+
if default_result != project_result:
336+
result += project_result
336337

337338
columns = (
338339
'id',
@@ -359,33 +360,35 @@ def _list_quota_volume(self, parsed_args, project_ids):
359360
)
360361

361362
def _list_quota_network(self, parsed_args, project_ids):
362-
client = self.app.client_manager.network
363+
network_client = self.app.client_manager.network
363364
result = []
364365

365-
for p in project_ids:
366+
for project_id in project_ids:
366367
try:
367-
data = client.get_quota(p)
368-
except Exception as ex:
369-
if type(ex).__name__ == 'NotFound':
370-
# Project not found, move on to next one
371-
LOG.warning(f"Project {p} not found: {ex}")
372-
continue
373-
else:
374-
raise
375-
376-
result_data = _xform_get_quota(
377-
data,
378-
p,
368+
project_data = network_client.get_quota(project_id)
369+
except (
370+
sdk_exceptions.NotFoundException,
371+
sdk_exceptions.ForbiddenException,
372+
) as exc:
373+
# Project not found, move on to next one
374+
LOG.warning(f"Project {project_id} not found: {exc}")
375+
continue
376+
377+
project_result = _xform_get_quota(
378+
project_data,
379+
project_id,
379380
NETWORK_KEYS,
380381
)
381-
default_data = client.get_quota_default(p)
382-
result_default = _xform_get_quota(
382+
383+
default_data = network_client.get_quota_default(project_id)
384+
default_result = _xform_get_quota(
383385
default_data,
384-
p,
386+
project_id,
385387
NETWORK_KEYS,
386388
)
387-
if result_default != result_data:
388-
result += result_data
389+
390+
if default_result != project_result:
391+
result += project_result
389392

390393
columns = (
391394
'id',
@@ -419,7 +422,8 @@ def _list_quota_network(self, parsed_args, project_ids):
419422

420423
def take_action(self, parsed_args):
421424
project_ids = [
422-
p.id for p in self.app.client_manager.identity.projects.list()
425+
p.id
426+
for p in self.app.client_manager.sdk_connection.identity.projects()
423427
]
424428
if parsed_args.compute:
425429
return self._list_quota_compute(parsed_args, project_ids)
@@ -561,16 +565,16 @@ def take_action(self, parsed_args):
561565
msg = _('--force cannot be used with --class or --default')
562566
raise exceptions.CommandError(msg)
563567

564-
compute_client = self.app.client_manager.compute
565-
volume_client = self.app.client_manager.volume
568+
compute_client = self.app.client_manager.sdk_connection.compute
569+
volume_client = self.app.client_manager.sdk_connection.volume
566570

567571
compute_kwargs = {}
568572
for k, v in COMPUTE_QUOTAS.items():
569573
value = getattr(parsed_args, k, None)
570574
if value is not None:
571575
compute_kwargs[k] = value
572576

573-
if parsed_args.force is True:
577+
if compute_kwargs and parsed_args.force is True:
574578
compute_kwargs['force'] = parsed_args.force
575579

576580
volume_kwargs = {}
@@ -604,12 +608,12 @@ def take_action(self, parsed_args):
604608

605609
if parsed_args.quota_class or parsed_args.default:
606610
if compute_kwargs:
607-
compute_client.quota_classes.update(
611+
compute_client.update_quota_class_set(
608612
parsed_args.project or 'default',
609613
**compute_kwargs,
610614
)
611615
if volume_kwargs:
612-
volume_client.quota_classes.update(
616+
volume_client.update_quota_class_set(
613617
parsed_args.project or 'default',
614618
**volume_kwargs,
615619
)
@@ -625,9 +629,9 @@ def take_action(self, parsed_args):
625629
project = project_info['id']
626630

627631
if compute_kwargs:
628-
compute_client.quotas.update(project, **compute_kwargs)
632+
compute_client.update_quota_set(project, **compute_kwargs)
629633
if volume_kwargs:
630-
volume_client.quotas.update(project, **volume_kwargs)
634+
volume_client.update_quota_set(project, **volume_kwargs)
631635
if (
632636
network_kwargs
633637
and self.app.client_manager.is_network_endpoint_enabled()
@@ -765,14 +769,24 @@ def take_action(self, parsed_args):
765769
if 'id' in info:
766770
del info['id']
767771

768-
# Remove the 'location' field for resources from openstacksdk
769-
if 'location' in info:
770-
del info['location']
772+
# Remove the sdk-derived fields
773+
for field in ('location', 'name', 'force'):
774+
if field in info:
775+
del info[field]
771776

772777
if not parsed_args.usage:
773778
result = [{'resource': k, 'limit': v} for k, v in info.items()]
774779
else:
775-
result = [{'resource': k, **v} for k, v in info.items()]
780+
result = [
781+
{
782+
'resource': k,
783+
'limit': v or 0,
784+
'in_use': info['usage'].get(k, 0),
785+
'reserved': info['reservation'].get(k, 0),
786+
}
787+
for k, v in info.items()
788+
if k not in ('usage', 'reservation')
789+
]
776790

777791
columns = (
778792
'resource',
@@ -850,21 +864,20 @@ def get_parser(self, prog_name):
850864
return parser
851865

852866
def take_action(self, parsed_args):
853-
identity_client = self.app.client_manager.identity
854-
project = utils.find_resource(
855-
identity_client.projects,
856-
parsed_args.project,
867+
identity_client = self.app.client_manager.sdk_connection.identity
868+
project = identity_client.find_project(
869+
parsed_args.project, ignore_missing=False
857870
)
858871

859872
# compute quotas
860873
if parsed_args.service in {'all', 'compute'}:
861-
compute_client = self.app.client_manager.compute
862-
compute_client.quotas.delete(project.id)
874+
compute_client = self.app.client_manager.sdk_connection.compute
875+
compute_client.revert_quota_set(project.id)
863876

864877
# volume quotas
865878
if parsed_args.service in {'all', 'volume'}:
866-
volume_client = self.app.client_manager.volume
867-
volume_client.quotas.delete(project.id)
879+
volume_client = self.app.client_manager.sdk_connection.volume
880+
volume_client.revert_quota_set(project.id)
868881

869882
# network quotas (but only if we're not using nova-network, otherwise
870883
# we already deleted the quotas in the compute step)

0 commit comments

Comments
 (0)