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-client
This commit is contained in:
M V P Nitesh 2017-09-21 15:38:55 +05:30 committed by Daniel Abad
parent 05ccd7f5fc
commit e6856282a3
8 changed files with 736 additions and 0 deletions

View File

@ -30,6 +30,7 @@ class AmbiguousAuthSystem(ClientException):
"""Could not obtain token and endpoint using provided credentials."""
pass
# Alias for backwards compatibility
AmbigiousAuthSystem = AmbiguousAuthSystem

View File

@ -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)

View File

@ -51,6 +51,17 @@ class FakeStatsModelManager(object):
pass
class FakeQuotasModelManager(object):
def get(self, id, resource):
pass
def create(self, **kwargs):
pass
def delete(self, id):
pass
class MagnumFakeContainerInfra(object):
def __init__(self):
self.cluster_templates = FakeBaseModelManager()
@ -58,6 +69,7 @@ class MagnumFakeContainerInfra(object):
self.mservices = FakeBaseModelManager()
self.certificates = FakeBaseModelManager()
self.stats = FakeStatsModelManager()
self.quotas = FakeQuotasModelManager()
class MagnumFakeClientManager(osc_fakes.FakeClientManager):
@ -223,3 +235,34 @@ class FakeCluster(object):
cluster = osc_fakes.FakeResource(info=copy.deepcopy(cluster_info),
loaded=True)
return cluster
class FakeQuota(object):
"""Fake one or more Quota"""
@staticmethod
def create_one_quota(attrs=None):
"""Create a fake quota
:param Dictionary attrs:
A dictionary with all attributes
:return:
A FakeResource object, with project_id, resource and so on
"""
attrs = attrs or {}
quota_info = {
'resource': 'Cluster',
'created_at': '2017-09-15T05:40:34+00:00',
'updated_at': '2017-09-15T05:40:34+00:00',
'hard_limit': 1,
'project_id': 'be24b6fba2ed4476b2bd01fa8f0ba74e',
'id': 10,
'name': 'fake-quota',
}
quota_info.update(attrs)
quota = osc_fakes.FakeResource(info=copy.deepcopy(quota_info),
loaded=True)
return quota

View File

@ -15,8 +15,18 @@
import copy
import mock
import os
import sys
import tempfile
from contextlib import contextmanager
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from mock import call
from magnumclient import exceptions
from magnumclient.osc.v1 import clusters as osc_clusters
from magnumclient.tests.osc.unit.v1 import fakes as magnum_fakes
@ -327,3 +337,139 @@ class TestClusterShow(TestCluster):
self.assertRaises(magnum_fakes.MagnumParseException,
self.check_parser, self.cmd, arglist, verifylist)
@contextmanager
def capture(command, *args, **kwargs):
out, sys.stdout = sys.stdout, StringIO()
try:
command(*args, **kwargs)
sys.stdout.seek(0)
yield sys.stdout.read()
finally:
sys.stdout = out
class TestClusterConfig(TestCluster):
def setUp(self):
super(TestClusterConfig, self).setUp()
attr = dict()
attr['name'] = 'fake-cluster-1'
attr['status'] = 'CREATE_COMPLETE'
self._cluster = magnum_fakes.FakeCluster.create_one_cluster(attr)
self.clusters_mock.get = mock.Mock()
self.clusters_mock.get.return_value = self._cluster
# Fake the cluster_template
attr = dict()
attr['name'] = 'fake-ct'
attr['tls_disabled'] = True
self._cluster_template = \
magnum_fakes.FakeClusterTemplate.create_one_cluster_template(attr)
self.cluster_templates_mock = \
self.app.client_manager.container_infra.cluster_templates
self.cluster_templates_mock.get = mock.Mock()
self.cluster_templates_mock.get.return_value = self._cluster_template
# Get the command object to test
self.cmd = osc_clusters.ConfigCluster(self.app, None)
def test_cluster_config_no_cluster_fail(self):
arglist = []
verifylist = []
self.assertRaises(magnum_fakes.MagnumParseException,
self.check_parser, self.cmd, arglist, verifylist)
@mock.patch.dict(os.environ, {'SHELL': '/bin/bash'})
def test_cluster_config_custom_dir_with_config_only_works_if_force(self):
tmp_dir = tempfile.mkdtemp()
open(os.path.join(tmp_dir, 'config'), 'a').close() # touch config
arglist = ['fake-cluster', '--dir', tmp_dir]
verifylist = [
('cluster', 'fake-cluster'),
('force', False),
('dir', tmp_dir),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.assertRaises(exceptions.CommandError,
self.cmd.take_action, parsed_args)
self.clusters_mock.get.assert_called_with('fake-cluster')
arglist = ['fake-cluster', '--force', '--dir', tmp_dir]
verifylist = [
('cluster', 'fake-cluster'),
('force', True),
('dir', tmp_dir),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
expected_value = '''\
export KUBECONFIG={}/config
'''.format(tmp_dir)
with capture(self.cmd.take_action, parsed_args) as output:
self.assertEqual(expected_value, output)
self.clusters_mock.get.assert_called_with('fake-cluster')
@mock.patch.dict(os.environ, {'SHELL': '/bin/bash'})
def test_cluster_config_with_custom_dir(self):
tmp_dir = tempfile.mkdtemp()
arglist = ['fake-cluster', '--dir', tmp_dir]
verifylist = [
('cluster', 'fake-cluster'),
('dir', tmp_dir),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
expected_value = '''\
export KUBECONFIG={}/config
'''.format(tmp_dir)
with capture(self.cmd.take_action, parsed_args) as output:
self.assertEqual(expected_value, output)
self.clusters_mock.get.assert_called_with('fake-cluster')
@mock.patch.dict(os.environ, {'SHELL': '/bin/bash'})
def test_cluster_config_creates_config_in_cwd_if_not_dir_specified(self):
tmp_dir = tempfile.mkdtemp()
os.chdir(tmp_dir)
arglist = ['fake-cluster']
verifylist = [
('cluster', 'fake-cluster'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
expected_value = '''\
export KUBECONFIG={}/config
'''.format(os.getcwd())
with capture(self.cmd.take_action, parsed_args) as output:
self.assertEqual(expected_value, output)
self.clusters_mock.get.assert_called_with('fake-cluster')
def test_cluster_config_with_in_progress_status(self):
self._cluster.status = 'CREATE_IN_PROGRESS'
arglist = ['fake-cluster-1']
verifylist = [
('cluster', 'fake-cluster-1')
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.assertRaises(exceptions.CommandError,
self.cmd.take_action, parsed_args)

View File

@ -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)

View File

@ -39,6 +39,7 @@ def _show_quota(quota):
action='store_true',
default=False,
help=_('Flag to indicate list all tenant quotas.'))
@utils.deprecated(utils.MAGNUM_CLIENT_DEPRECATION_WARNING)
def do_quotas_list(cs, args):
"""Print a list of available quotas."""
quotas = cs.quotas.list(marker=args.marker,
@ -65,6 +66,7 @@ def do_quotas_list(cs, args):
type=int,
default=1,
help=_('Max resource limit.'))
@utils.deprecated(utils.MAGNUM_CLIENT_DEPRECATION_WARNING)
def do_quotas_create(cs, args):
"""Create a quota."""
@ -91,6 +93,7 @@ def do_quotas_create(cs, args):
required=True,
metavar='<resource>',
help=_('Resource name'))
@utils.deprecated(utils.MAGNUM_CLIENT_DEPRECATION_WARNING)
def do_quotas_delete(cs, args):
"""Delete specified resource quota."""
try:
@ -113,6 +116,7 @@ def do_quotas_delete(cs, args):
required=True,
metavar='<resource>',
help=_('Resource name'))
@utils.deprecated(utils.MAGNUM_CLIENT_DEPRECATION_WARNING)
def do_quotas_show(cs, args):
"""Show details about the given project resource quota."""
quota = cs.quotas.get(args.project_id, args.resource)
@ -132,6 +136,7 @@ def do_quotas_show(cs, args):
type=int,
default=1,
help=_('Max resource limit.'))
@utils.deprecated(utils.MAGNUM_CLIENT_DEPRECATION_WARNING)
def do_quotas_update(cs, args):
"""Update information about the given project resource quota."""
patch = dict()

View File

@ -0,0 +1,6 @@
---
features:
- |
Implemented Openstack command for quotas-create,
quotas-update, quotas-delete, quotas-show and
quotas-list

View File

@ -46,6 +46,11 @@ openstack.container_infra.v1 =
coe_ca_show = magnumclient.osc.v1.certificates:ShowCa
coe_ca_sign = magnumclient.osc.v1.certificates:SignCa
coe_stats_list = magnumclient.osc.v1.stats:ListStats
coe_quotas_create = magclient.osc.v1.quotas:CreateQuotas
coe_quotas_delete = magclient.osc.v1.quotas:DeleteQuotas
coe_quotas_update = magclient.osc.v1.quotas:UpdateQuotas
coe_quotas_show = magclient.osc.v1.quotas:ShowQuotas
coe_quotas_list = magclient.osc.v1.quotas:ListQuotas
coe_service_list = magnumclient.osc.v1.mservices:ListService