From e599ac9f84b043eb9882e02c110ac9a31709aff3 Mon Sep 17 00:00:00 2001 From: Quentin Anglade Date: Mon, 30 Sep 2019 15:40:00 +0200 Subject: [PATCH] Add support for v2/rating/modules endpoints This adds support for the new v2 API rating modules endpoints. Unit tests included. Change-Id: Ie116c518f30128b49c3991f36101db7535af7db1 Story: 2006572 Task: 36680 --- cloudkittyclient/tests/unit/v2/base.py | 3 + cloudkittyclient/tests/unit/v2/test_rating.py | 38 ++++++++++++ cloudkittyclient/v2/client.py | 2 + cloudkittyclient/v2/rating/__init__.py | 0 cloudkittyclient/v2/rating/modules.py | 59 +++++++++++++++++++ cloudkittyclient/v2/rating/modules_cli.py | 29 +++++++++ 6 files changed, 131 insertions(+) create mode 100644 cloudkittyclient/tests/unit/v2/test_rating.py create mode 100644 cloudkittyclient/v2/rating/__init__.py create mode 100644 cloudkittyclient/v2/rating/modules.py create mode 100644 cloudkittyclient/v2/rating/modules_cli.py diff --git a/cloudkittyclient/tests/unit/v2/base.py b/cloudkittyclient/tests/unit/v2/base.py index 70b9af1..681b839 100644 --- a/cloudkittyclient/tests/unit/v2/base.py +++ b/cloudkittyclient/tests/unit/v2/base.py @@ -12,8 +12,10 @@ # License for the specific language governing permissions and limitations # under the License. + from cloudkittyclient.tests import utils from cloudkittyclient.v2 import dataframes +from cloudkittyclient.v2.rating import modules from cloudkittyclient.v2 import scope from cloudkittyclient.v2 import summary @@ -26,3 +28,4 @@ class BaseAPIEndpointTestCase(utils.BaseTestCase): self.dataframes = dataframes.DataframesManager(self.api_client) self.scope = scope.ScopeManager(self.api_client) self.summary = summary.SummaryManager(self.api_client) + self.rating = modules.RatingManager(self.api_client) diff --git a/cloudkittyclient/tests/unit/v2/test_rating.py b/cloudkittyclient/tests/unit/v2/test_rating.py new file mode 100644 index 0000000..fbe2491 --- /dev/null +++ b/cloudkittyclient/tests/unit/v2/test_rating.py @@ -0,0 +1,38 @@ +# Copyright 2019 Objectif Libre +# +# 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 cloudkittyclient.tests.unit.v2 import base + + +class TestRating(base.BaseAPIEndpointTestCase): + + def test_get_modules(self): + self.rating.get_module() + self.api_client.get.assert_called_once_with('/v2/rating/modules') + + def test_get_one_module(self): + self.rating.get_module(module_id="moduleidtest") + self.api_client.get.assert_called_once_with( + '/v2/rating/modules/moduleidtest') + + def test_update_one_module(self): + self.rating.update_module(module_id="moduleidtest", + enabled=False, priority=42) + self.api_client.put.assert_called_once_with( + '/v2/rating/modules/moduleidtest', + json={ + 'enabled': False, + 'priority': 42, + }) diff --git a/cloudkittyclient/v2/client.py b/cloudkittyclient/v2/client.py index 0a61184..2b443cc 100644 --- a/cloudkittyclient/v2/client.py +++ b/cloudkittyclient/v2/client.py @@ -15,6 +15,7 @@ # from cloudkittyclient.v1 import client from cloudkittyclient.v2 import dataframes +from cloudkittyclient.v2.rating import modules from cloudkittyclient.v2 import scope from cloudkittyclient.v2 import summary @@ -40,3 +41,4 @@ class Client(client.Client): self.dataframes = dataframes.DataframesManager(self.api_client) self.scope = scope.ScopeManager(self.api_client) self.summary = summary.SummaryManager(self.api_client) + self.rating = modules.RatingManager(self.api_client) diff --git a/cloudkittyclient/v2/rating/__init__.py b/cloudkittyclient/v2/rating/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/cloudkittyclient/v2/rating/modules.py b/cloudkittyclient/v2/rating/modules.py new file mode 100644 index 0000000..375855e --- /dev/null +++ b/cloudkittyclient/v2/rating/modules.py @@ -0,0 +1,59 @@ + +# Copyright 2019 Objectif Libre +# +# 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 cloudkittyclient import exc +from cloudkittyclient.v1.client import rating + + +class RatingManager(rating.RatingManager): + """Class used to handle /v2/rating/modules endpoint""" + + url = '/v2/rating/modules' + + def get_module(self, **kwargs): + """Returns the given module. + + If module_id is not specified, returns the list of loaded modules. + + :param module_id: ID of the module on which you want information. + :type module_id: str + """ + module_id = kwargs.get('module_id', None) + if module_id is not None: + url = "{}/{}".format(self.url, module_id) + else: + url = self.url + return self.api_client.get(url).json() + + def update_module(self, **kwargs): + """Update the given module. + + :param module_id: Id of the module to update. + :type module_id: str + :param enabled: Set to True to enable the module, False to disable it. + :type enabled: bool + :param priority: New priority of the module. + :type priority: int + """ + if not kwargs.get('module_id', None): + raise exc.ArgumentRequired("'module_id' argument is required.") + mutable_fields = ['enabled', 'priority'] + changes = {} + for key, value in kwargs.items(): + if value is not None and key in mutable_fields: + changes[key] = value + self.api_client.put("{}/{}".format(self.url, kwargs['module_id']), + json=changes) + return self.get_module(**kwargs) diff --git a/cloudkittyclient/v2/rating/modules_cli.py b/cloudkittyclient/v2/rating/modules_cli.py new file mode 100644 index 0000000..4ff7156 --- /dev/null +++ b/cloudkittyclient/v2/rating/modules_cli.py @@ -0,0 +1,29 @@ +# Copyright 2019 Objectif Libre +# +# 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 cliff import lister + +from cloudkittyclient import utils + + +class CliModuleList(lister.Lister): + """Get loaded rating modules list""" + + def get_parser(self, prog_name): + parser = super(CliModuleList, self).get_parser(prog_name) + return parser + + def take_action(self, parsed_args): + resp = utils.get_client_from_osc(self).ratingmodules.get_modules_list() + return resp['modules']