diff --git a/api-ref/source/v2/rating/modules.inc b/api-ref/source/v2/rating/modules.inc index 66d6ae11..3ea9f3bf 100644 --- a/api-ref/source/v2/rating/modules.inc +++ b/api-ref/source/v2/rating/modules.inc @@ -74,3 +74,38 @@ Response Example .. literalinclude:: ./api_samples/rating/module_get.json :language: javascript + +Update one module +================= + +.. rest_method:: PUT /v2/rating/modules/(module_id) + +.. rest_parameters:: rating/modules_parameters.yml + + - enabled: enabled_opt + - priority: priority_opt + +Status codes +------------ + +.. rest_status_code:: success http_status.yml + + - 204 + +.. rest_status_code:: error http_status.yml + + - 400 + - 401 + - 403 + - 404 + +Response +-------- + +.. rest_parameters:: rating/modules_parameters.yml + + - module_id: module_id + - description: description + - enabled: enabled + - hot_config: hot_config + - priority: priority diff --git a/cloudkitty/api/v2/rating/modules.py b/cloudkitty/api/v2/rating/modules.py index c74dfb6f..e9c4c2cd 100644 --- a/cloudkitty/api/v2/rating/modules.py +++ b/cloudkitty/api/v2/rating/modules.py @@ -82,6 +82,23 @@ class RatingModule(BaseRatingModule): 'priority': infos['priority'], } + @api_utils.add_input_schema('body', { + voluptuous.Optional('enabled'): voluptuous.Boolean(), + voluptuous.Optional('priority'): voluptuous.All(int, min=1), + }) + def put(self, module_id, enabled=None, priority=None): + policy.authorize(flask.request.context, 'v2_rating:update_module', {}) + try: + module = self.rating_modules[module_id].obj + except KeyError: + raise http_exceptions.NotFound( + "Module '{}' not found".format(module_id)) + if enabled is not None: + module.set_state(enabled) + if priority is not None: + module.set_priority(priority) + return "", 204 + class RatingModuleList(BaseRatingModule): diff --git a/cloudkitty/common/policies/v2/rating.py b/cloudkitty/common/policies/v2/rating.py index 253df07c..24ff5dd1 100644 --- a/cloudkitty/common/policies/v2/rating.py +++ b/cloudkitty/common/policies/v2/rating.py @@ -30,6 +30,12 @@ rating_policies = [ description='Get specified module.', operations=[{'path': '/v2/rating/modules/{module_id}', 'method': 'GET'}]), + policy.DocumentedRuleDefault( + name='v2_rating:update_module', + check_str=base.ROLE_ADMIN, + description='Change the state and priority of a module.', + operations=[{'path': '/v2/rating/modules/{module_id}', + 'method': 'PUT'}]) ] diff --git a/cloudkitty/tests/gabbi/gabbits/v2-rating-modules.yaml b/cloudkitty/tests/gabbi/gabbits/v2-rating-modules.yaml index fb800de7..3043953c 100644 --- a/cloudkitty/tests/gabbi/gabbits/v2-rating-modules.yaml +++ b/cloudkitty/tests/gabbi/gabbits/v2-rating-modules.yaml @@ -40,3 +40,64 @@ tests: status: 404 response_json_paths: $.message: "Module 'fakb' not found" + + - name: change priority of a module + url: /v2/rating/modules/fake3 + method: PUT + request_headers: + content-type: application/json + x-roles: admin + data: + priority: 5 + status: 204 + + - name: get information of the modified module (priority) + url: /v2/rating/modules/fake3 + status: 200 + response_json_paths: + $.priority: 5 + $.module_id: "fake3" + $.enabled: false + $.description: "fake rating module" + $.hot_config: false + + - name: change enabled status of a module + url: /v2/rating/modules/fake3 + method: PUT + request_headers: + content-type: application/json + x-roles: admin + data: + enabled: true + status: 204 + + - name: get information of the modified module (status) + url: /v2/rating/modules/fake3 + status: 200 + response_json_paths: + $.priority: 5 + $.module_id: "fake3" + $.enabled: true + $.description: "fake rating module" + $.hot_config: false + + - name: change status and priority of a module + url: /v2/rating/modules/fake3 + method: PUT + request_headers: + content-type: application/json + x-roles: admin + data: + priority: 3 + enabled: false + status: 204 + + - name: get information of the modified module (both) + url: /v2/rating/modules/fake3 + status: 200 + response_json_paths: + $.priority: 3 + $.module_id: "fake3" + $.enabled: false + $.description: "fake rating module" + $.hot_config: false diff --git a/doc/source/_static/cloudkitty.policy.yaml.sample b/doc/source/_static/cloudkitty.policy.yaml.sample index 793edb77..e07d10d9 100644 --- a/doc/source/_static/cloudkitty.policy.yaml.sample +++ b/doc/source/_static/cloudkitty.policy.yaml.sample @@ -125,3 +125,6 @@ # GET /v2/task/reprocesses #"schedule:get_task_reprocesses": "role:admin" +# Change the state and priority of a module. +# PUT /v2/rating/modules/{module_id} +#"v2_rating:update_module": "role:admin"