Debian Codegraph @

mistralclient/commands/v2/cron_triggers.py

raw symbols SCIP
1 # Copyright 2014 - Mirantis, Inc.
2 # All Rights Reserved
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15 #
16 import datetime
17 import time
18
19 from osc_lib.command import command
20
21 from mistralclient.commands.v2 import base
22 from mistralclient import utils
23
24
25 class CronTriggerFormatter(base.MistralFormatter):
26 COLUMNS = [
27 ('name', 'Name'),
28 ('workflow_name', 'Workflow'),
29 ('workflow_params', 'Params'),
30 ('pattern', 'Pattern'),
31 # TODO(rakhmerov): Uncomment when passwords are handled properly.
32 # TODO(rakhmerov): Add 'Workflow input' column.
33 ('next_execution_time', 'Next execution time'),
34 ('remaining_executions', 'Remaining executions'),
35 ('created_at', 'Created at'),
36 ('updated_at', 'Updated at')
37 ]
38
39 @staticmethod
40 def format(trigger=None, lister=False):
41 if trigger:
42 # TODO(rakhmerov): Add following here:
43 # TODO(rakhmerov): wf_input = trigger.workflow_input if not lister
44 # TODO(rakhmerov:): else base.cut(trigger.workflow_input)
45
46 data = (
47 trigger.name,
48 trigger.workflow_name,
49 trigger.workflow_params,
50 trigger.pattern,
51 # TODO(rakhmerov): Uncomment when passwords are handled
52 # properly.
53 # TODo(rakhmerov): Add 'wf_input' here.
54 trigger.next_execution_time,
55 trigger.remaining_executions,
56 trigger.created_at,
57 )
58
59 if hasattr(trigger, 'updated_at'):
60 data += (trigger.updated_at,)
61 else:
62 data += (None,)
63 else:
64 data = (tuple('' for _ in
65 range(len(CronTriggerFormatter.COLUMNS))),)
66
67 return CronTriggerFormatter.headings(), data
68
69
70 class List(base.MistralLister):
71 """List all cron triggers."""
72
73 def _get_format_function(self):
74 return CronTriggerFormatter.format_list
75
76 def _get_resources(self, parsed_args):
77 mistral_client = self.app.client_manager.workflow_engine
78 return mistral_client.cron_triggers.list(
79 marker=parsed_args.marker,
80 limit=parsed_args.limit,
81 sort_keys=parsed_args.sort_keys,
82 sort_dirs=parsed_args.sort_dirs,
83 fields=CronTriggerFormatter.fields(),
84 **base.get_filters(parsed_args)
85 )
86
87
88 class Get(command.ShowOne):
89 """Show specific cron trigger."""
90
91 def get_parser(self, prog_name):
92 parser = super(Get, self).get_parser(prog_name)
93
94 parser.add_argument('cron_trigger', help='Cron trigger name')
95
96 return parser
97
98 def take_action(self, parsed_args):
99 mistral_client = self.app.client_manager.workflow_engine
100
101 return CronTriggerFormatter.format(mistral_client.cron_triggers.get(
102 parsed_args.cron_trigger
103 ))
104
105
106 class Create(command.ShowOne):
107 """Create new trigger."""
108
109 def get_parser(self, prog_name):
110 parser = super(Create, self).get_parser(prog_name)
111
112 parser.add_argument('name', help='Cron trigger name')
113 parser.add_argument('workflow_identifier', help='Workflow name or ID')
114
115 parser.add_argument(
116 'workflow_input',
117 nargs='?',
118 help='Workflow input'
119 )
120
121 parser.add_argument(
122 '--params',
123 help='Workflow params',
124 )
125
126 parser.add_argument(
127 '--pattern',
128 type=str,
129 help='Cron trigger pattern',
130 metavar='<* * * * *>'
131 )
132 parser.add_argument(
133 '--first-time',
134 type=str,
135 default=None,
136 help=("Date and time of the first execution. Time is treated as "
137 "local time unless --utc is also specified"),
138 metavar='<YYYY-MM-DD HH:MM>'
139 )
140 parser.add_argument(
141 '--count',
142 type=int,
143 help="Number of wanted executions",
144 metavar='<integer>'
145 )
146 parser.add_argument(
147 '--utc',
148 action='store_true',
149 help="All times specified should be treated as UTC"
150 )
151
152 return parser
153
154 @staticmethod
155 def _get_file_content_or_dict(string):
156 if string:
157 return utils.load_json(string)
158 else:
159 return {}
160
161 @staticmethod
162 def _convert_time_string_to_utc(time_string):
163 datetime_format = '%Y-%m-%d %H:%M'
164
165 the_time = time_string
166 if the_time:
167 the_time = datetime.datetime.strptime(
168 the_time, datetime_format)
169
170 is_dst = time.daylight and time.localtime().tm_isdst > 0
171 utc_offset = - (time.altzone if is_dst else time.timezone)
172
173 the_time = (the_time - datetime.timedelta(
174 0, utc_offset)).strftime(datetime_format)
175
176 return the_time
177
178 def take_action(self, parsed_args):
179 mistral_client = self.app.client_manager.workflow_engine
180
181 wf_input = self._get_file_content_or_dict(parsed_args.workflow_input)
182 wf_params = self._get_file_content_or_dict(parsed_args.params)
183
184 first_time = parsed_args.first_time
185 if not parsed_args.utc:
186 first_time = self._convert_time_string_to_utc(
187 parsed_args.first_time)
188
189 trigger = mistral_client.cron_triggers.create(
190 parsed_args.name,
191 parsed_args.workflow_identifier,
192 wf_input,
193 wf_params,
194 parsed_args.pattern,
195 first_time,
196 parsed_args.count
197 )
198
199 return CronTriggerFormatter.format(trigger)
200
201
202 class Delete(command.Command):
203 """Delete trigger."""
204
205 def get_parser(self, prog_name):
206 parser = super(Delete, self).get_parser(prog_name)
207
208 parser.add_argument(
209 'cron_trigger',
210 nargs='+', help='Name of cron trigger(s).'
211 )
212
213 return parser
214
215 def take_action(self, parsed_args):
216 mistral_client = self.app.client_manager.workflow_engine
217
218 utils.do_action_on_many(
219 lambda s: mistral_client.cron_triggers.delete(s),
220 parsed_args.cron_trigger,
221 "Request to delete cron trigger %s has been accepted.",
222 "Unable to delete the specified cron trigger(s)."
223 )