Skip to content

Commit 277b477

Browse files
Introduce reprocessing task API in the CLI
Change-Id: Ieab5df4deb9cbf5eddfc8eca3b028942f6303abd
1 parent de6c8b2 commit 277b477

5 files changed

Lines changed: 191 additions & 0 deletions

File tree

cloudkittyclient/v2/client.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from cloudkittyclient.v1 import client
1717
from cloudkittyclient.v2 import dataframes
1818
from cloudkittyclient.v2.rating import modules
19+
from cloudkittyclient.v2 import reprocessing
1920
from cloudkittyclient.v2 import scope
2021
from cloudkittyclient.v2 import summary
2122

@@ -42,3 +43,4 @@ def __init__(self,
4243
self.scope = scope.ScopeManager(self.api_client)
4344
self.summary = summary.SummaryManager(self.api_client)
4445
self.rating = modules.RatingManager(self.api_client)
46+
self.reprocessing = reprocessing.ReprocessingManager(self.api_client)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
from cloudkittyclient.common import base
14+
from cloudkittyclient import exc
15+
16+
17+
class ReprocessingManager(base.BaseManager):
18+
"""Class used to handle /v2/task/reprocesses endpoint"""
19+
20+
url = '/v2/task/reprocesses'
21+
22+
url_to_post = '/v2/task/reprocess'
23+
24+
def get_reprocessing_tasks(self, offset=0, limit=100, scope_ids=[],
25+
order="DESC", **kwargs):
26+
"""Returns a paginated list of reprocessing tasks.
27+
28+
Some optional filters can be provided.
29+
30+
:param offset: Index of the first reprocessing task
31+
that should be returned.
32+
:type offset: int
33+
:param limit: Maximal number of reprocessing task to return.
34+
:type limit: int
35+
:param scope_ids: Optional scope_ids to filter on.
36+
:type scope_ids: list of str
37+
:param order: Optional order (asc/desc) to sort tasks.
38+
:type order: str
39+
"""
40+
kwargs = kwargs or {}
41+
kwargs['order'] = order
42+
kwargs['offset'] = offset
43+
kwargs['limit'] = limit
44+
45+
authorized_args = ['offset', 'limit', 'order']
46+
url = self.get_url(None, kwargs, authorized_args=authorized_args)
47+
48+
if scope_ids:
49+
url += "&scope_ids=%s" % (",".join(scope_ids))
50+
return self.api_client.get(url).json()
51+
52+
def post_reprocessing_task(self, scope_ids=[], start=None, end=None,
53+
reason=None, **kwargs):
54+
"""Creates a reprocessing task
55+
56+
:param start: The start date of the reprocessing task
57+
:type start: timeutils.parse_isotime
58+
:param end: The end date of the reprocessing task
59+
:type end: timeutils.parse_isotime
60+
:param scope_ids: The scope IDs to create the reprocessing task to
61+
:type scope_ids: list of str
62+
:param reason: The reason for the reprocessing task
63+
:type reason: str
64+
"""
65+
66+
if not scope_ids:
67+
raise exc.ArgumentRequired("'scope-id' argument is required")
68+
69+
body = dict(
70+
scope_ids=scope_ids,
71+
start_reprocess_time=start,
72+
end_reprocess_time=end,
73+
reason=reason
74+
)
75+
76+
body = dict(filter(lambda elem: bool(elem[1]), body.items()))
77+
78+
return self.api_client.post(self.url_to_post, json=body).json()
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
from cliff import lister
14+
from oslo_utils import timeutils
15+
16+
from cloudkittyclient import utils
17+
18+
19+
class CliReprocessingTasksGet(lister.Lister):
20+
"""Get reprocessing tasks."""
21+
22+
result_columns = [
23+
('scope_id', 'Scope ID'),
24+
('reason', 'Reason'),
25+
('start_reprocess_time', 'Start reprocessing time'),
26+
('end_reprocess_time', 'End reprocessing time'),
27+
('current_reprocess_time', 'Current reprocessing time'),
28+
]
29+
30+
def get_parser(self, prog_name):
31+
parser = super(CliReprocessingTasksGet, self).get_parser(prog_name)
32+
33+
parser.add_argument('--scope-id', type=str, default=[],
34+
action='append', help='Optional filter on scope '
35+
'IDs. This filter can be '
36+
'used multiple times.')
37+
38+
parser.add_argument('--offset', type=int, default=0,
39+
help='Index of the first scope. '
40+
'The default value is 0.')
41+
parser.add_argument('--limit', type=int, default=100,
42+
help='Maximal number of scopes. '
43+
'The default value is 100.')
44+
parser.add_argument('--order', type=str, default="DESC",
45+
help='The order to sort the reprocessing tasks '
46+
'(ASC or DESC).')
47+
48+
return parser
49+
50+
def take_action(self, parsed_args):
51+
resp = utils.get_client_from_osc(
52+
self).reprocessing.get_reprocessing_tasks(
53+
scope_ids=parsed_args.scope_id, offset=parsed_args.offset,
54+
limit=parsed_args.limit, order=parsed_args.order
55+
)
56+
57+
values = utils.list_to_cols(resp['results'], self.result_columns)
58+
return [col[1] for col in self.result_columns], values
59+
60+
61+
class CliReprocessingTasksPost(lister.Lister):
62+
"""Create a reprocessing task."""
63+
64+
def get_parser(self, prog_name):
65+
parser = super(CliReprocessingTasksPost, self).get_parser(prog_name)
66+
67+
parser.add_argument('--scope-id', type=str, default=[],
68+
action='append',
69+
help='The scope IDs to reprocess. This option can '
70+
'be used multiple times to execute the same '
71+
'reprocessing task for different scope IDs.')
72+
73+
parser.add_argument('--start-reprocess-time',
74+
type=timeutils.parse_isotime,
75+
help="Start of the period to reprocess in ISO8601 "
76+
"format. Example: '2022-04-22T00:00:00Z.'")
77+
78+
parser.add_argument('--end-reprocess-time',
79+
type=timeutils.parse_isotime,
80+
help="End of the period to reprocess in ISO8601 "
81+
"format. Example: '2022-04-22T00:00:00Z.'")
82+
83+
parser.add_argument('--reason', type=str,
84+
help="The reason to create the reprocessing task.")
85+
86+
return parser
87+
88+
def take_action(self, parsed_args):
89+
return ["Result"], utils.get_client_from_osc(
90+
self).reprocessing.post_reprocessing_task(
91+
scope_ids=parsed_args.scope_id,
92+
start=parsed_args.start_reprocess_time,
93+
end=parsed_args.end_reprocess_time,
94+
reason=parsed_args.reason
95+
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
features:
3+
- |
4+
Introduce reprocessing task API in the CLI. The following new commands
5+
are added to the OpenStack CLI "rating tasks reprocessing get" and
6+
"rating tasks reprocessing create". For CloudKitty CLI, we added the
7+
following new commands "tasks reprocessing get" and "tasks reprocessing
8+
create". Both command sets work in a similar fashion, but one is
9+
targetting the OpenStack CLI integration, whereas the other is
10+
targetting CloudKitty client only.

setup.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ openstack.rating.v1 =
8989
rating_pyscript_delete = cloudkittyclient.v1.rating.pyscripts_cli:CliDeleteScript
9090

9191
openstack.rating.v2 =
92+
rating_tasks_reprocessing_get = cloudkittyclient.v2.reprocessing_cli:CliReprocessingTasksGet
93+
rating_tasks_reprocessing_create = cloudkittyclient.v2.reprocessing_cli:CliReprocessingTasksPost
94+
9295
rating_dataframes_get = cloudkittyclient.v2.dataframes_cli:CliDataframesGet
9396
rating_dataframes_add = cloudkittyclient.v2.dataframes_cli:CliDataframesAdd
9497

@@ -205,6 +208,9 @@ cloudkittyclient_v1 =
205208
pyscript_delete = cloudkittyclient.v1.rating.pyscripts_cli:CliDeleteScript
206209

207210
cloudkittyclient_v2 =
211+
tasks_reprocessing_get = cloudkittyclient.v2.reprocessing_cli:CliReprocessingTasksGet
212+
tasks_reprocessing_create = cloudkittyclient.v2.reprocessing_cli:CliReprocessingTasksPost
213+
208214
dataframes_add = cloudkittyclient.v2.dataframes_cli:CliDataframesAdd
209215
dataframes_get = cloudkittyclient.v2.dataframes_cli:CliDataframesGet
210216

0 commit comments

Comments
 (0)