Skip to content

Commit e41d08d

Browse files
committed
Support project name in quota CLI
Change-Id: I9886792f346bbe8d4887539ca69588b4957547e1
1 parent 1d53377 commit e41d08d

5 files changed

Lines changed: 47 additions & 22 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
---
2+
features:
3+
- Support both project name and ID in quota CLI.

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# of appearance. Changing the order has an impact on the overall integration
33
# process, which may cause wedges in the gate later.
44
pbr!=2.1.0,>=2.0.0 # Apache-2.0
5-
PrettyTable<0.8,>=0.7.2 # BSD
5+
PrettyTable>=0.7.2 # BSD
66
requests>=2.14.2 # Apache-2.0
77
simplejson>=3.5.1 # MIT
88
oslo.i18n>=3.15.3 # Apache-2.0
@@ -11,3 +11,4 @@ keystoneauth1>=3.4.0 # Apache-2.0
1111
python-swiftclient>=3.2.0 # Apache-2.0
1212
python-mistralclient!=3.2.0,>=3.1.0 # Apache-2.0
1313
osc-lib>=1.8.0 # Apache-2.0
14+
python-openstackclient>=3.12.0 # Apache-2.0

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ commands = sphinx-build -a -E -W -d releasenotes/build/doctrees -b html releasen
6161

6262
[flake8]
6363
enable-extensions = H106,H203,H904
64-
ignore = H202,H405,H501,W504
64+
ignore = H202,H405,H501,W504,H306
6565
show-source = True
6666
exclude=.venv,.git,.tox,dist,doc,*lib/python*,*egg,build,releasenotes
6767

troveclient/osc/v1/database_quota.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,48 +12,49 @@
1212

1313
"""Database v1 Quota action implementations"""
1414

15-
from osc_lib.command import command
1615
from osc_lib import utils as osc_utils
16+
from osc_lib.command import command
1717

1818
from troveclient.i18n import _
19+
from troveclient import utils
1920

2021

2122
class ShowDatabaseQuota(command.Lister):
22-
23-
_description = _("Show quotas for a tenant.")
23+
_description = _("Show quotas for a project.")
2424
columns = ['Resource', 'In Use', 'Reserved', 'Limit']
2525

2626
def get_parser(self, prog_name):
2727
parser = super(ShowDatabaseQuota, self).get_parser(prog_name)
2828
parser.add_argument(
29-
'tenant_id',
30-
metavar='<tenant_id>',
31-
help=_('Id of tenant for which to show quotas.'),
29+
'project',
30+
help=_('Id or name of the project.'),
3231
)
3332
return parser
3433

3534
def take_action(self, parsed_args):
3635
db_quota = self.app.client_manager.database.quota
36+
project_id = utils.get_project_id(
37+
self.app.client_manager.identity,
38+
parsed_args.project
39+
)
3740
quota = [osc_utils.get_item_properties(q, self.columns)
38-
for q in db_quota.show(parsed_args.tenant_id)]
41+
for q in db_quota.show(project_id)]
3942
return self.columns, quota
4043

4144

4245
class UpdateDatabaseQuota(command.ShowOne):
43-
44-
_description = _("Update quotas for a tenant.")
46+
_description = _("Update quotas for a project.")
4547

4648
def get_parser(self, prog_name):
4749
parser = super(UpdateDatabaseQuota, self).get_parser(prog_name)
4850
parser.add_argument(
49-
'tenant_id',
50-
metavar='<tenant_id>',
51-
help=_('Id of tenant for which to update quotas.'),
51+
'project',
52+
help=_('Id or name of the project.'),
5253
)
5354
parser.add_argument(
5455
'resource',
5556
metavar='<resource>',
56-
help=_('Id of resource to change.'),
57+
help=_('Resource name.'),
5758
)
5859
parser.add_argument(
5960
'limit',
@@ -65,9 +66,12 @@ def get_parser(self, prog_name):
6566

6667
def take_action(self, parsed_args):
6768
db_quota = self.app.client_manager.database.quota
69+
project_id = utils.get_project_id(
70+
self.app.client_manager.identity,
71+
parsed_args.project
72+
)
6873
update_params = {
6974
parsed_args.resource: parsed_args.limit
7075
}
71-
updated_quota = db_quota.update(parsed_args.tenant_id,
72-
update_params)
76+
updated_quota = db_quota.update(project_id, update_params)
7377
return zip(*sorted(updated_quota.items()))

troveclient/utils.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,16 @@
1414
# License for the specific language governing permissions and limitations
1515
# under the License.
1616

17-
import base64
18-
import os
19-
import simplejson as json
20-
import sys
2117
import uuid
2218

19+
import base64
20+
from openstackclient.identity import common as identity_common
21+
import os
2322
from oslo_utils import encodeutils
2423
from oslo_utils import uuidutils
2524
import prettytable
25+
import simplejson as json
26+
import sys
2627

2728
from troveclient.apiclient import exceptions
2829

@@ -225,8 +226,24 @@ def get_resource_id_by_name(manager, name):
225226
return resource.id
226227

227228

229+
def get_project_id(manager, id_or_name):
230+
if not uuidutils.is_uuid_like(id_or_name):
231+
try:
232+
project = identity_common.find_project(manager, id_or_name)
233+
id_or_name = project.id
234+
except Exception as e:
235+
msg = ("Failed to get project ID for %s, error: %s" %
236+
(id_or_name, str(e)))
237+
raise exceptions.CommandError(msg)
238+
239+
return id_or_name
240+
241+
228242
def find_resource(manager, name_or_id):
229-
"""Helper for the _find_* methods."""
243+
"""Helper for the _find_* methods.
244+
245+
This method should be replaced with osc_utils.find_resource()
246+
"""
230247
# first try to get entity as integer id
231248

232249
# When the 'name_or_id' is int, covert it to string.

0 commit comments

Comments
 (0)