OSC command for magnum quota's
Implemented Openstack command for quotas-create, quotas-update, quotas-delete, quotas-show and quotas-list. Change-Id: I889c2384afdf4cf113345af646b865271784c40c Partially-Implements: blueprint deprecate-magnum-clientchanges/00/506100/11
parent
05ccd7f5fc
commit
e6856282a3
@ -0,0 +1,215 @@
|
||||
# Copyright 2015 NEC Corporation. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from magnumclient.common import cliutils as utils
|
||||
from magnumclient.common import utils as magnum_utils
|
||||
from magnumclient.i18n import _
|
||||
from osc_lib.command import command
|
||||
|
||||
QUOTA_ATTRIBUTES = [
|
||||
'resource',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'hard_limit',
|
||||
'project_id',
|
||||
'id'
|
||||
]
|
||||
|
||||
|
||||
def _show_quota(quota):
|
||||
utils.print_dict(quota._info)
|
||||
|
||||
|
||||
class CreateQuotas(command.Command):
|
||||
_description = _("Create a quota.")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(CreateQuotas, self).get_parser(prog_name)
|
||||
parser.add_argument('--project-id',
|
||||
required=True,
|
||||
metavar='<project-id>',
|
||||
help='Project ID')
|
||||
parser.add_argument('--resource',
|
||||
required=True,
|
||||
metavar='<resource>',
|
||||
help='Resource name.')
|
||||
parser.add_argument('--hard-limit',
|
||||
metavar='<hard-limit>',
|
||||
type=int,
|
||||
default=1,
|
||||
help='Max resource limit (default: hard-limit=1)')
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
self.log.debug("take_action(%s)", parsed_args)
|
||||
|
||||
mag_client = self.app.client_manager.container_infra
|
||||
|
||||
opts = {
|
||||
'project_id': parsed_args.project_id,
|
||||
'resource': parsed_args.resource,
|
||||
'hard_limit': parsed_args.hard_limit
|
||||
}
|
||||
try:
|
||||
quota = mag_client.quotas.create(**opts)
|
||||
_show_quota(quota)
|
||||
except Exception as e:
|
||||
print("Create quota for project_id %(id)s resource %(res)s failed:"
|
||||
" %(e)s" % {'id': parsed_args.project_id,
|
||||
'res': parsed_args.resource,
|
||||
'e': e})
|
||||
|
||||
|
||||
class DeleteQuotas(command.Command):
|
||||
_description = _("Delete specified resource quota.")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(DeleteQuotas, self).get_parser(prog_name)
|
||||
parser.add_argument('--project-id',
|
||||
required=True,
|
||||
metavar='<project-id>',
|
||||
help='Project ID')
|
||||
parser.add_argument('--resource',
|
||||
required=True,
|
||||
metavar='<resource>',
|
||||
help='Resource name.')
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
self.log.debug("take_action(%s)", parsed_args)
|
||||
|
||||
mag_client = self.app.client_manager.container_infra
|
||||
|
||||
try:
|
||||
mag_client.quotas.delete(parsed_args.project_id,
|
||||
parsed_args.resource)
|
||||
print("Request to delete quota for project id %(id)s and resource "
|
||||
"%(res)s has been accepted." % {'id': parsed_args.project_id,
|
||||
'res': parsed_args.resource})
|
||||
except Exception as e:
|
||||
print("Quota delete failed for project id %(id)s and resource "
|
||||
"%(res)s :%(e)s" % {'id': parsed_args.project_id,
|
||||
'res': parsed_args.resource,
|
||||
'e': e})
|
||||
|
||||
|
||||
class ShowQuotas(command.Command):
|
||||
_description = _("Show details about the given project resource quota.")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(ShowQuotas, self).get_parser(prog_name)
|
||||
parser.add_argument('--project-id',
|
||||
required=True,
|
||||
metavar='<project-id>',
|
||||
help='Project ID')
|
||||
parser.add_argument('--resource',
|
||||
required=True,
|
||||
metavar='<resource>',
|
||||
help='Resource name.')
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
self.log.debug("take_action(%s)", parsed_args)
|
||||
|
||||
mag_client = self.app.client_manager.container_infra
|
||||
project_id = parsed_args.project_id
|
||||
resource = parsed_args.resource
|
||||
quota = mag_client.quotas.get(project_id, resource)
|
||||
_show_quota(quota)
|
||||
|
||||
|
||||
class UpdateQuotas(command.Command):
|
||||
_description = _("Update information about the given "
|
||||
"project resource quota.")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(UpdateQuotas, self).get_parser(prog_name)
|
||||
parser.add_argument('--project-id',
|
||||
required=True,
|
||||
metavar='<project-id>',
|
||||
help='Project ID')
|
||||
parser.add_argument('--resource',
|
||||
required=True,
|
||||
metavar='<resource>',
|
||||
help='Resource name.')
|
||||
parser.add_argument('--hard-limit',
|
||||
metavar='<hard-limit>',
|
||||
type=int,
|
||||
default=1,
|
||||
help='Max resource limit (default: hard-limit=1)')
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
self.log.debug("take_action(%s)", parsed_args)
|
||||
|
||||
mag_client = self.app.client_manager.container_infra
|
||||
|
||||
opts = {
|
||||
'project_id': parsed_args.project_id,
|
||||
'resource': parsed_args.resource,
|
||||
'hard_limit': parsed_args.hard_limit
|
||||
}
|
||||
try:
|
||||
quota = mag_client.quotas.update(parsed_args.project_id,
|
||||
parsed_args.resource, opts)
|
||||
_show_quota(quota)
|
||||
except Exception as e:
|
||||
print("Update quota for project_id %(id)s resource %(res)s failed:"
|
||||
" %(e)s" % {'id': parsed_args.project_id,
|
||||
'res': parsed_args.resource,
|
||||
'e': e})
|
||||
|
||||
|
||||
class ListQuotas(command.Command):
|
||||
_description = _("Print a list of available quotas.")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(ListQuotas, self).get_parser(prog_name)
|
||||
parser.add_argument('--marker',
|
||||
metavar='<marker>',
|
||||
default=None,
|
||||
help=_('The last quota UUID of the previous page; '
|
||||
'displays list of quotas after "marker".'))
|
||||
parser.add_argument('--limit',
|
||||
metavar='<limit>',
|
||||
type=int,
|
||||
help='Maximum number of quotas to return.')
|
||||
parser.add_argument('--sort-key',
|
||||
metavar='<sort-key>',
|
||||
help='Column to sort results by.')
|
||||
parser.add_argument('--sort-dir',
|
||||
metavar='<sort-dir>',
|
||||
choices=['desc', 'asc'],
|
||||
help='Direction to sort. "asc" or "desc".')
|
||||
parser.add_argument('--all-tenants',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='Flag to indicate list all tenant quotas.')
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
self.log.debug("take_action(%s)", parsed_args)
|
||||
|
||||
mag_client = self.app.client_manager.container_infra
|
||||
|
||||
quotas = mag_client.quotas.list(marker=parsed_args.marker,
|
||||
limit=parsed_args.limit,
|
||||
sort_key=parsed_args.sort_key,
|
||||
sort_dir=parsed_args.sort_dir,
|
||||
all_tenants=parsed_args.all_tenants)
|
||||
columns = ['project_id', 'resource', 'hard_limit']
|
||||
utils.print_list(quotas, columns,
|
||||
{'versions':
|
||||
magnum_utils.print_list_field('versions')},
|
||||
sortby_index=None)
|
@ -0,0 +1,315 @@
|
||||
# Copyright 2015 NEC Corporation. All rights reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
import mock
|
||||
|
||||
from magnumclient.osc.v1 import quotas as osc_quotas
|
||||
from magnumclient.tests.osc.unit.v1 import fakes as magnum_fakes
|
||||
|
||||
|
||||
class TestQuotas(magnum_fakes.TestMagnumClientOSCV1):
|
||||
|
||||
def setUp(self):
|
||||
super(TestQuotas, self).setUp()
|
||||
|
||||
self.quotas_mock = self.app.client_manager.container_infra.quotas
|
||||
|
||||
|
||||
class TestQuotasCreate(TestQuotas):
|
||||
|
||||
def setUp(self):
|
||||
super(TestQuotasCreate, self).setUp()
|
||||
attr = dict()
|
||||
attr['name'] = 'fake-quota'
|
||||
attr['project_id'] = 'abc'
|
||||
attr['resource'] = 'Cluster'
|
||||
self._quota = magnum_fakes.FakeQuota.create_one_quota(attr)
|
||||
|
||||
self._default_args = {
|
||||
'project_id': 'abc',
|
||||
'resource': 'Cluster',
|
||||
'hard_limit': 1
|
||||
}
|
||||
|
||||
self.quotas_mock.create = mock.Mock()
|
||||
self.quotas_mock.create.return_value = self._quota
|
||||
|
||||
self.cmd = osc_quotas.CreateQuotas(self.app, None)
|
||||
|
||||
self.data = tuple(map(lambda x: getattr(self._quota, x),
|
||||
osc_quotas.QUOTA_ATTRIBUTES))
|
||||
|
||||
def test_quotas_create(self):
|
||||
arglist = [
|
||||
'--project-id', 'abc',
|
||||
'--resource', 'Cluster'
|
||||
]
|
||||
verifylist = [
|
||||
('project_id', 'abc'),
|
||||
('resource', 'Cluster')
|
||||
]
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
self.cmd.take_action(parsed_args)
|
||||
self.quotas_mock.create.assert_called_with(**self._default_args)
|
||||
|
||||
def test_quotas_create_with_hardlimit(self):
|
||||
arglist = [
|
||||
'--project-id', 'abc',
|
||||
'--resource', 'Cluster',
|
||||
'--hard-limit', '10'
|
||||
]
|
||||
verifylist = [
|
||||
('project_id', 'abc'),
|
||||
('resource', 'Cluster'),
|
||||
('hard_limit', 10)
|
||||
]
|
||||
self._default_args['hard_limit'] = 10
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
|
||||
self.cmd.take_action(parsed_args)
|
||||
self.quotas_mock.create.assert_called_with(**self._default_args)
|
||||
|
||||
def test_quotas_create_wrong_projectid(self):
|
||||
arglist = ['abcd']
|
||||
verifylist = [
|
||||
('project_id', 'abcd')
|
||||
]
|
||||
|
||||
self.assertRaises(magnum_fakes.MagnumParseException,
|
||||
self.check_parser, self.cmd, arglist, verifylist)
|
||||
|
||||
def test_quotas_create_missing_args(self):
|
||||
arglist = []
|
||||
verifylist = []
|
||||
self.assertRaises(magnum_fakes.MagnumParseException,
|
||||
self.check_parser, self.cmd, arglist, verifylist)
|
||||
|
||||
def test_quotas_create_with_wrong_args(self):
|
||||
arglist = [
|
||||
'--project-id', 'abc',
|
||||
'--resources', 'Cluster', # Misspelling 'resources'
|
||||
'--hard-limit', '10'
|
||||
]
|
||||
verifylist = []
|
||||
self.assertRaises(magnum_fakes.MagnumParseException,
|
||||
self.check_parser, self.cmd, arglist, verifylist)
|
||||
|
||||
|
||||
class TestQuotasDelete(TestQuotas):
|
||||
|
||||
def setUp(self):
|
||||
super(TestQuotasDelete, self).setUp()
|
||||
|
||||
self.quotas_mock.delete = mock.Mock()
|
||||
self.quotas_mock.delete.return_value = None
|
||||
|
||||
self.cmd = osc_quotas.DeleteQuotas(self.app, None)
|
||||
|
||||
def test_quotas_delete(self):
|
||||
arglist = [
|
||||
'--project-id', 'abc',
|
||||
'--resource', 'Cluster'
|
||||
]
|
||||
verifylist = [
|
||||
('project_id', 'abc'),
|
||||
('resource', 'Cluster')
|
||||
]
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
self.cmd.take_action(parsed_args)
|
||||
self.quotas_mock.delete.assert_called_with('abc', 'Cluster')
|
||||
|
||||
def test_quotas_delete_no_project_id(self):
|
||||
arglist = [
|
||||
'--resource', 'Cluster'
|
||||
]
|
||||
verifylist = [
|
||||
('resource', 'Cluster')
|
||||
]
|
||||
self.assertRaises(magnum_fakes.MagnumParseException,
|
||||
self.check_parser, self.cmd, arglist, verifylist)
|
||||
|
||||
def test_quotas_delete_no_resource(self):
|
||||
arglist = [
|
||||
'--project-id', 'abc',
|
||||
]
|
||||
verifylist = [
|
||||
('project_id', 'abc')
|
||||
]
|
||||
self.assertRaises(magnum_fakes.MagnumParseException,
|
||||
self.check_parser, self.cmd, arglist, verifylist)
|
||||
|
||||
def test_quotas_delete_missing_args(self):
|
||||
arglist = []
|
||||
verifylist = []
|
||||
self.assertRaises(magnum_fakes.MagnumParseException,
|
||||
self.check_parser, self.cmd, arglist, verifylist)
|
||||
|
||||
def test_quotas_delete_wrong_args(self):
|
||||
arglist = [
|
||||
'--project-ids', 'abc', # Misspelling 'ids' instead of 'id'
|
||||
'--resource', 'Cluster'
|
||||
]
|
||||
verifylist = []
|
||||
self.assertRaises(magnum_fakes.MagnumParseException,
|
||||
self.check_parser, self.cmd, arglist, verifylist)
|
||||
|
||||
|
||||
class TestQuotasShow(TestQuotas):
|
||||
|
||||
def setUp(self):
|
||||
super(TestQuotasShow, self).setUp()
|
||||
|
||||
attr = dict()
|
||||
attr['name'] = 'fake-quota'
|
||||
attr['project_id'] = 'abc'
|
||||
attr['resource'] = 'Cluster'
|
||||
self._quota = magnum_fakes.FakeQuota.create_one_quota(attr)
|
||||
|
||||
self._default_args = {
|
||||
'project_id': 'abc',
|
||||
'resource': 'Cluster',
|
||||
}
|
||||
|
||||
self.quotas_mock.get = mock.Mock()
|
||||
self.quotas_mock.get.return_value = self._quota
|
||||
|
||||
self.cmd = osc_quotas.ShowQuotas(self.app, None)
|
||||
|
||||
self.data = tuple(map(lambda x: getattr(self._quota, x),
|
||||
osc_quotas.QUOTA_ATTRIBUTES))
|
||||
|
||||
def test_quotas_show(self):
|
||||
arglist = [
|
||||
'--project-id', 'abc',
|
||||
'--resource', 'Cluster'
|
||||
]
|
||||
verifylist = [
|
||||
('project_id', 'abc'),
|
||||
('resource', 'Cluster')
|
||||
]
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
self.cmd.take_action(parsed_args)
|
||||
self.quotas_mock.get.assert_called_with('abc', 'Cluster')
|
||||
|
||||
def test_quotas_show_missing_args(self):
|
||||
arglist = []
|
||||
verifylist = []
|
||||
self.assertRaises(magnum_fakes.MagnumParseException,
|
||||
self.check_parser, self.cmd, arglist, verifylist)
|
||||
|
||||
|
||||
class TestQuotasUpdate(TestQuotas):
|
||||
|
||||
def setUp(self):
|
||||
super(TestQuotasUpdate, self).setUp()
|
||||
attr = dict()
|
||||
attr['name'] = 'fake-quota'
|
||||
attr['project_id'] = 'abc'
|
||||
attr['resource'] = 'Cluster'
|
||||
self._quota = magnum_fakes.FakeQuota.create_one_quota(attr)
|
||||
|
||||
self._default_args = {
|
||||
'project_id': 'abc',
|
||||
'resource': 'Cluster',
|
||||
'hard_limit': 10
|
||||
}
|
||||
|
||||
self.quotas_mock.update = mock.Mock()
|
||||
self.quotas_mock.update.return_value = self._quota
|
||||
|
||||
self.cmd = osc_quotas.UpdateQuotas(self.app, None)
|
||||
|
||||
def test_quotas_update(self):
|
||||
arglist = [
|
||||
'--project-id', 'abc',
|
||||
'--resource', 'Cluster',
|
||||
'--hard-limit', '10'
|
||||
]
|
||||
verifylist = [
|
||||
('project_id', 'abc'),
|
||||
('resource', 'Cluster'),
|
||||
('hard_limit', 10)
|
||||
]
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
self.cmd.take_action(parsed_args)
|
||||
self.quotas_mock.update.assert_called_with('abc', 'Cluster',
|
||||
self._default_args)
|
||||
|
||||
def test_quotas_update_missing_args(self):
|
||||
arglist = ['abcd']
|
||||
verifylist = [
|
||||
('project_id', 'abcd')
|
||||
]
|
||||
|
||||
self.assertRaises(magnum_fakes.MagnumParseException,
|
||||
self.check_parser, self.cmd, arglist, verifylist)
|
||||
|
||||
def test_quotas_update_wrong_args(self):
|
||||
arglist = [
|
||||
'--project-id', 'abc',
|
||||
'--resource', 'Cluster',
|
||||
'--hard-limits', '10' # Misspelling 'hard-limits'
|
||||
]
|
||||
verifylist = [
|
||||
('project_id', 'abc'),
|
||||
('resource', 'Cluster'),
|
||||
('hard_limit', 10)
|
||||
]
|
||||
|
||||
self.assertRaises(magnum_fakes.MagnumParseException,
|
||||
self.check_parser, self.cmd, arglist, verifylist)
|
||||
|
||||
|
||||
class TestQuotasList(TestQuotas):
|
||||
|
||||
def setUp(self):
|
||||
super(TestQuotasList, self).setUp()
|
||||
attr = dict()
|
||||
attr['name'] = 'fake-quota'
|
||||
attr['project_id'] = 'abc'
|
||||
attr['resource'] = 'Cluster'
|
||||
self._quota = magnum_fakes.FakeQuota.create_one_quota(attr)
|
||||
|
||||
self.quotas_mock.list = mock.Mock()
|
||||
self.quotas_mock.list.return_value = [self._quota]
|
||||
|
||||
self.cmd = osc_quotas.ListQuotas(self.app, None)
|
||||
|
||||
def test_quotas_list_with_no_options(self):
|
||||
arglist = [
|
||||
]
|
||||
verifylist = [
|
||||
('limit', None),
|
||||
('sort_key', None),
|
||||
('sort_dir', None),
|
||||
('marker', None),
|
||||
('all_tenants', False)
|
||||
]
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
self.cmd.take_action(parsed_args)
|
||||
self.quotas_mock.list.assert_called_with(
|
||||
limit=None,
|
||||
sort_dir=None,
|
||||
sort_key=None,
|
||||
marker=None,
|
||||
all_tenants=False
|
||||
)
|
||||
|
||||
def test_quotas_list_wrong_args(self):
|
||||
arglist = ['--wrong']
|
||||
verifylist = [
|
||||
('wrong')
|
||||
]
|
||||
self.assertRaises(magnum_fakes.MagnumParseException,
|
||||
self.check_parser, self.cmd, arglist, verifylist)
|
@ -0,0 +1,6 @@
|
||||
---
|
||||
features:
|
||||
- |
|
||||
Implemented Openstack command for quotas-create,
|
||||
quotas-update, quotas-delete, quotas-show and
|
||||
quotas-list
|
Loading…
Reference in New Issue