Skip to content

Commit

Permalink
[SDK] [CLI] kms quota (#259)
Browse files Browse the repository at this point in the history
[SDK] [CLI] kms quota

Closes #17

Reviewed-by: None <None>
Reviewed-by: Irina Pereiaslavskaia <[email protected]>
  • Loading branch information
anton-sidelnikov authored Jan 12, 2022
1 parent 11c342f commit 9dead1c
Show file tree
Hide file tree
Showing 8 changed files with 223 additions and 0 deletions.
44 changes: 44 additions & 0 deletions otcextensions/osclient/kms/v1/quota.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 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.
#

"""KMS Quota v1 action implementations"""
import logging

from osc_lib import utils
from osc_lib.command import command

from otcextensions.i18n import _

LOG = logging.getLogger(__name__)


class ListKMSQuota(command.Lister):
_description = _('List KMS Quotas')
columns = ('quota', 'used', 'type')

def get_parser(self, prog_name):
parser = super(ListKMSQuota, self).get_parser(prog_name)
return parser

def take_action(self, parsed_args):
client = self.app.client_manager.kms

data = client.quotas()

return (
self.columns,
(utils.get_item_properties(
s,
self.columns
) for s in data)
)
31 changes: 31 additions & 0 deletions otcextensions/sdk/kms/v1/quota.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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 openstack import resource
from otcextensions.sdk import quotamixin


class Quota(quotamixin.QuotaProxyMixin, resource.Resource):
"""Key Management Service Quota resource"""
resources_key = 'quotas.resources'
base_path = '/quota'

# capabilities
allow_list = True

#: Properties
#: Quota type, value is ``CMK`` or ``grant_per_CMK``
type = resource.Body('type')
#: Used quota
used = resource.Body('used', type=int)
#: Total quota
quota = resource.Body('quota', type=int)
33 changes: 33 additions & 0 deletions otcextensions/tests/functional/sdk/kms/v1/test_quota.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 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 openstack import _log

from otcextensions.tests.functional import base

_logger = _log.setup_logging('openstack')


class TestQuota(base.BaseFunctionalTest):

def setUp(self):
super(TestQuota, self).setUp()
self.kms = self.conn.kms

def test_list(self):
objects = list(self.kms.quotas())

self.assertEqual(len(objects), 2)

for obj in objects:
self.assertIsInstance(obj, dict)
self.assertIn("quota", obj.keys())
16 changes: 16 additions & 0 deletions otcextensions/tests/unit/osclient/kms/v1/fakes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from otcextensions.tests.unit.osclient import test_base

from otcextensions.sdk.kms.v1 import key
from otcextensions.sdk.kms.v1 import quota
# from otcextensions.sdk.kms.v1 import data_key


Expand Down Expand Up @@ -56,3 +57,18 @@ def generate(cls):
}
obj = key.Key.existing(**object_info)
return obj


class FakeQuota(test_base.Fake):
"""Fake one or more Quota"""

@classmethod
def generate(cls):

object_info = {
'quota': random.randint(1, 65535),
'used': random.randint(1, 65535),
'type': random.choice(['CMK', 'grant_per_CMK']),
}
obj = quota.Quota.existing(**object_info)
return obj
65 changes: 65 additions & 0 deletions otcextensions/tests/unit/osclient/kms/v1/test_quota.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 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 otcextensions.osclient.kms.v1 import quota
from otcextensions.tests.unit.osclient.kms.v1 import fakes


class TestKMSQuota(fakes.TestKMS):

def setUp(self):
super(TestKMSQuota, self).setUp()
self.client = self.app.client_manager.kms


class TestListKMSQuota(TestKMSQuota):
quotas = fakes.FakeQuota.create_multiple(2)
columns = ('quota', 'used', 'type')

data = []

for s in quotas:
data.append((
s.quota,
s.used,
s.type
))

def setUp(self):
super(TestListKMSQuota, self).setUp()
self.cmd = quota.ListKMSQuota(self.app, None)
self.client.quotas = mock.Mock()

def test_list_quota(self):
arglist = [
]

verifylist = [
]

# Verify cm is triggereg with default parameters
parsed_args = self.check_parser(self.cmd, arglist, verifylist)

# Set the response
self.client.quotas.side_effect = [
self.quotas
]

# Trigger the action
columns, data = self.cmd.take_action(parsed_args)

self.client.quotas.assert_called_once_with()

self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data))
28 changes: 28 additions & 0 deletions otcextensions/tests/unit/sdk/kms/v1/test_quota.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 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 openstack.tests.unit import base

from otcextensions.sdk.kms.v1 import quota


class TestQuota(base.TestCase):

def test_basic(self):
sot = quota.Quota()
self.assertEqual('quotas.resources', sot.resources_key)
self.assertEqual('/quota', sot.base_path)
self.assertTrue(sot.allow_list)
self.assertFalse(sot.allow_create)
self.assertFalse(sot.allow_fetch)
self.assertFalse(sot.allow_commit)
self.assertFalse(sot.allow_delete)
5 changes: 5 additions & 0 deletions releasenotes/notes/kms-quota-c71dbf42c03fa268.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
features:
- |
SDK KMS Quota list operation support.
CLI List KMS Quotas
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ openstack.kms.v1 =
kms_cmk_disable = otcextensions.osclient.kms.v1.cmk:DisableCMK
kms_cmk_delete = otcextensions.osclient.kms.v1.cmk:DeleteCMK
kms_cmk_cancel_delete = otcextensions.osclient.kms.v1.cmk:CancelDeleteCMK
kms_quota_list = otcextensions.osclient.kms.v1.quota:ListKMSQuota

openstack.cce.v1 =
cce_cluster_list = otcextensions.osclient.cce.v1.cluster:ListCCECluster
Expand Down

0 comments on commit 9dead1c

Please sign in to comment.