summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJiao Pengju <jiaopengju@cmss.chinamobile.com>2017-11-23 15:07:55 +0800
committerJiao Pengju <jiaopengju@cmss.chinamobile.com>2017-11-23 16:21:33 +0800
commitcce93819d1a718a73a75295e6170859b4cd85882 (patch)
tree4fd9413f5958c0f922e4d614d5f728076e5d030d
parent29740fce2109529a501e983bc64b69c7c4680a10 (diff)
Fix trigger creation failed
Currently, karbor use 'calendar' format as the default time format, but karbor-dashboard use 'crontab', this makes the trigger creation failed. This patch convert the time format of the dashboard to canlendar to make it work. Change-Id: I7a89365005af3256bdf02478dd60428a118a8bbe Closes-Bug: #1613587
Notes
Notes (review): Code-Review+2: chenying <ying.chen@huawei.com> Code-Review+2: Jiao Pengju <jiaopengju@cmss.chinamobile.com> Workflow+1: Jiao Pengju <jiaopengju@cmss.chinamobile.com> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Thu, 23 Nov 2017 14:05:09 +0000 Reviewed-on: https://review.openstack.org/522457 Project: openstack/karbor-dashboard Branch: refs/heads/master
-rw-r--r--karbor_dashboard/locale/en_GB/LC_MESSAGES/django.po6
-rw-r--r--karbor_dashboard/templates/triggers/_detail.html12
-rw-r--r--karbor_dashboard/triggers/forms.py6
-rw-r--r--karbor_dashboard/triggers/utils.py145
-rw-r--r--karbor_dashboard/triggers/views.py15
5 files changed, 159 insertions, 25 deletions
diff --git a/karbor_dashboard/locale/en_GB/LC_MESSAGES/django.po b/karbor_dashboard/locale/en_GB/LC_MESSAGES/django.po
index 71d047d..7f72fc2 100644
--- a/karbor_dashboard/locale/en_GB/LC_MESSAGES/django.po
+++ b/karbor_dashboard/locale/en_GB/LC_MESSAGES/django.po
@@ -112,6 +112,12 @@ msgstr "Error Deleting"
112msgid "Event Trigger" 112msgid "Event Trigger"
113msgstr "Event Trigger" 113msgstr "Event Trigger"
114 114
115msgid "Every Minute"
116msgstr "Every Minute"
117
118msgid "Every Hour"
119msgstr "Every Hour"
120
115msgid "Every Day" 121msgid "Every Day"
116msgstr "Every Day" 122msgstr "Every Day"
117 123
diff --git a/karbor_dashboard/templates/triggers/_detail.html b/karbor_dashboard/templates/triggers/_detail.html
index dd6998d..5654a4d 100644
--- a/karbor_dashboard/templates/triggers/_detail.html
+++ b/karbor_dashboard/templates/triggers/_detail.html
@@ -22,5 +22,17 @@
22 <dt>{% trans "Execution Time (HH:MM)" %}</dt> 22 <dt>{% trans "Execution Time (HH:MM)" %}</dt>
23 <dd>{{ trigger.time }}</dd> 23 <dd>{{ trigger.time }}</dd>
24 {% endif %} 24 {% endif %}
25 {% if trigger.hour %}
26 <dt>{% trans "Hour" %}</dt>
27 <dd>{{ trigger.hour }}</dd>
28 {% endif %}
29 {% if trigger.minute %}
30 <dt>{% trans "Minute" %}</dt>
31 <dd>{{ trigger.minute }}</dd>
32 {% endif %}
33 {% if trigger.interval %}
34 <dt>{% trans "Execution Interval" %}</dt>
35 <dd>{{ trigger.interval }}</dd>
36 {% endif %}
25 </dl> 37 </dl>
26</div> 38</div>
diff --git a/karbor_dashboard/triggers/forms.py b/karbor_dashboard/triggers/forms.py
index 2229b0d..7412175 100644
--- a/karbor_dashboard/triggers/forms.py
+++ b/karbor_dashboard/triggers/forms.py
@@ -34,7 +34,7 @@ class CreateTriggerForm(horizon_forms.SelfHandlingForm):
34 })) 34 }))
35 frequence = forms.ChoiceField( 35 frequence = forms.ChoiceField(
36 label=_('Frequence'), 36 label=_('Frequence'),
37 choices=utils.FREQUENCE_CHOICES, 37 choices=utils.CRONTAB_FREQUENCE_CHOICES,
38 widget=forms.Select(attrs={ 38 widget=forms.Select(attrs={
39 'class': 'switchable switched', 39 'class': 'switchable switched',
40 'data-slug': 'frequence'})) 40 'data-slug': 'frequence'}))
@@ -60,12 +60,12 @@ class CreateTriggerForm(horizon_forms.SelfHandlingForm):
60 def __init__(self, request, *args, **kwargs): 60 def __init__(self, request, *args, **kwargs):
61 super(CreateTriggerForm, self).__init__(request, *args, **kwargs) 61 super(CreateTriggerForm, self).__init__(request, *args, **kwargs)
62 62
63 self.fields['day'].choices = utils.DAY_CHOICES 63 self.fields['day'].choices = utils.CRONTAB_DAY_CHOICES
64 self.fields['date'].choices = [(e, e) for e in range(1, 31 + 1)] 64 self.fields['date'].choices = [(e, e) for e in range(1, 31 + 1)]
65 65
66 def handle(self, request, data): 66 def handle(self, request, data):
67 try: 67 try:
68 data_properties = utils.CrontabUtil.convert_to_crontab(data) 68 data_properties = utils.CalendarUtil.convert_to_calendar(data)
69 new_trigger = karborclient.trigger_create(request, 69 new_trigger = karborclient.trigger_create(request,
70 data["name"], 70 data["name"],
71 data["type"], 71 data["type"],
diff --git a/karbor_dashboard/triggers/utils.py b/karbor_dashboard/triggers/utils.py
index 9fcfcf5..955a7ad 100644
--- a/karbor_dashboard/triggers/utils.py
+++ b/karbor_dashboard/triggers/utils.py
@@ -14,29 +14,32 @@
14 14
15import collections 15import collections
16from django.utils.translation import ugettext_lazy as _ 16from django.utils.translation import ugettext_lazy as _
17from icalendar.cal import Component
18from icalendar import Event
19from oslo_serialization import jsonutils
17 20
18TRIGGERTYPE_CHOICES = [('time', _('Time Trigger')), 21TRIGGERTYPE_CHOICES = [('time', _('Time Trigger')),
19 ('event', _('Event Trigger'))] 22 ('event', _('Event Trigger'))]
20 23
21CRONTAB = 'crontab' 24CRONTAB = 'crontab'
22 25
23DAY_CHOICES = [('1', _('Monday')), 26CRONTAB_DAY_CHOICES = [('1', _('Monday')),
24 ('2', _('Tuesday')), 27 ('2', _('Tuesday')),
25 ('3', _('Wednesday')), 28 ('3', _('Wednesday')),
26 ('4', _('Thursday')), 29 ('4', _('Thursday')),
27 ('5', _('Friday')), 30 ('5', _('Friday')),
28 ('6', _('Saturday')), 31 ('6', _('Saturday')),
29 ('0', _('Sunday'))] 32 ('0', _('Sunday'))]
30DAY_DICT = collections.OrderedDict(DAY_CHOICES) 33CRONTAB_DAY_DICT = collections.OrderedDict(CRONTAB_DAY_CHOICES)
31 34
32EVERYDAY = 'everyday' 35EVERYDAY = 'everyday'
33EVERYWEEK = 'everyweek' 36EVERYWEEK = 'everyweek'
34EVERYMONTH = 'everymonth' 37EVERYMONTH = 'everymonth'
35 38
36FREQUENCE_CHOICES = [(EVERYDAY, _('Every Day')), 39CRONTAB_FREQUENCE_CHOICES = [(EVERYDAY, _('Every Day')),
37 (EVERYWEEK, _('Every Week')), 40 (EVERYWEEK, _('Every Week')),
38 (EVERYMONTH, _('Every Month'))] 41 (EVERYMONTH, _('Every Month'))]
39FREQUENCE_DICT = collections.OrderedDict(FREQUENCE_CHOICES) 42CRONTAB_FREQUENCE_DICT = collections.OrderedDict(CRONTAB_FREQUENCE_CHOICES)
40 43
41 44
42class CrontabUtil(object): 45class CrontabUtil(object):
@@ -78,7 +81,9 @@ class CrontabUtil(object):
78 81
79 @staticmethod 82 @staticmethod
80 def convert_from_crontab(dict_crontab): 83 def convert_from_crontab(dict_crontab):
81 data = {} 84 data = {
85 'format': dict_crontab['format']
86 }
82 if dict_crontab["format"] == CRONTAB: 87 if dict_crontab["format"] == CRONTAB:
83 pattern = dict_crontab["pattern"] 88 pattern = dict_crontab["pattern"]
84 patterns = pattern.split(" ") 89 patterns = pattern.split(" ")
@@ -86,18 +91,124 @@ class CrontabUtil(object):
86 if patterns[2] == "*" \ 91 if patterns[2] == "*" \
87 and patterns[3] == "*" \ 92 and patterns[3] == "*" \
88 and patterns[4] == "*": 93 and patterns[4] == "*":
89 data["frequence"] = FREQUENCE_DICT[EVERYDAY] 94 data["frequence"] = CRONTAB_FREQUENCE_DICT[EVERYDAY]
90 elif patterns[2] == "*" \ 95 elif patterns[2] == "*" \
91 and patterns[3] == "*" \ 96 and patterns[3] == "*" \
92 and patterns[4] != "*": 97 and patterns[4] != "*":
93 data["frequence"] = FREQUENCE_DICT[EVERYWEEK] 98 data["frequence"] = CRONTAB_FREQUENCE_DICT[EVERYWEEK]
94 data["day"] = DAY_DICT[patterns[4]] 99 data["day"] = CRONTAB_DAY_DICT[patterns[4]]
95 elif patterns[2] != "*" \ 100 elif patterns[2] != "*" \
96 and patterns[3] == "*" \ 101 and patterns[3] == "*" \
97 and patterns[4] == "*": 102 and patterns[4] == "*":
98 data["frequence"] = FREQUENCE_DICT[EVERYMONTH] 103 data["frequence"] = CRONTAB_FREQUENCE_DICT[EVERYMONTH]
99 data["date"] = patterns[2] 104 data["date"] = patterns[2]
100 105
101 data["time"] = '%s:%s' % (patterns[1].zfill(2), 106 data["time"] = '%s:%s' % (patterns[1].zfill(2),
102 patterns[0].zfill(2)) 107 patterns[0].zfill(2))
103 return data 108 return data
109
110CALENDAR = 'calendar'
111CALENDAR_DAY_CHOICES = [('1', _('MO')),
112 ('2', _('TU')),
113 ('3', _('WE')),
114 ('4', _('TH')),
115 ('5', _('FR')),
116 ('6', _('SA')),
117 ('0', _('SU'))]
118CALENDAR_DAY_DICT = collections.OrderedDict(CALENDAR_DAY_CHOICES)
119CALENDAR_DAY_MAPPING = [('MO', _('Monday')),
120 ('TU', _('Tuesday')),
121 ('WE', _('Wednesday')),
122 ('TH', _('Thursday')),
123 ('FR', _('Friday')),
124 ('SA', _('Saturday')),
125 ('SU', _('Sunday'))]
126CALENDAR_DAY_MAPPING_DICT = collections.OrderedDict(CALENDAR_DAY_MAPPING)
127MINUTELY = 'MINUTELY'
128HOURLY = 'HOURLY'
129DAILY = 'DAILY'
130WEEKLY = 'WEEKLY'
131MONTHLY = 'MONTHLY'
132
133
134class CalendarUtil(object):
135 """Convert to or from calendar format."""
136 @staticmethod
137 def convert_to_calendar(data):
138 dict_calendar = {
139 "format": CALENDAR,
140 }
141
142 pattern_frequence = ''
143 if data['frequence'] == EVERYMONTH:
144 pattern_frequence = MONTHLY
145 elif data['frequence'] == EVERYWEEK:
146 pattern_frequence = WEEKLY
147 elif data['frequence'] == EVERYDAY:
148 pattern_frequence = DAILY
149 calendar_event = Event()
150 rule_pattern = {
151 'freq': pattern_frequence,
152 'byhour': data["time"].hour,
153 'byminute': data["time"].minute,
154 }
155
156 if pattern_frequence == MONTHLY:
157 rule_pattern['bymonthday'] = data["date"]
158 elif pattern_frequence == WEEKLY:
159 rule_pattern['byday'] = CALENDAR_DAY_DICT[data["day"]]
160
161 calendar_event.add('rrule', rule_pattern)
162 dict_calendar['pattern'] = calendar_event.to_ical()
163 return dict_calendar
164
165 @staticmethod
166 def decode_calendar_pattern(pattern):
167 try:
168 pattern.index('\\')
169 pattern_dict = jsonutils.loads('{"pattern": "%s"}' % pattern)
170 return pattern_dict["pattern"]
171 except Exception:
172 return pattern
173
174 @staticmethod
175 def convert_from_calendar(dict_calendar):
176 data = {
177 'format': dict_calendar['format']
178 }
179 if dict_calendar["format"] == CALENDAR:
180 pattern = dict_calendar["pattern"]
181 calendar_event = Component.from_ical(
182 CalendarUtil.decode_calendar_pattern(pattern)
183 )
184 if isinstance(calendar_event, Event):
185 calendar_event_rule = calendar_event['RRULE']
186 data['frequence'] = calendar_event_rule['FREQ'][0]
187
188 if data['frequence'] == MONTHLY and not (
189 'INTERVAL' in calendar_event['RRULE']):
190 data['date'] = ' '.join(
191 str(date)
192 for date in calendar_event_rule['BYMONTHDAY'])
193
194 if data['frequence'] == WEEKLY and not (
195 'INTERVAL' in calendar_event['RRULE']):
196 data['day'] = ' '.join(
197 str(CALENDAR_DAY_MAPPING_DICT[day])
198 for day in calendar_event_rule['BYDAY'])
199
200 if 'BYHOUR' in calendar_event['RRULE']:
201 data['hour'] = ' '.join(
202 str(hour) for hour in calendar_event_rule['BYHOUR'])
203
204 if 'BYMINUTE' in calendar_event['RRULE']:
205 data['minute'] = ' '.join(
206 str(minute)
207 for minute in calendar_event_rule['BYMINUTE'])
208
209 if 'INTERVAL' in calendar_event['RRULE']:
210 data['interval'] = ' '.join(
211 str(interval)
212 for interval in calendar_event_rule['INTERVAL'])
213
214 return data
diff --git a/karbor_dashboard/triggers/views.py b/karbor_dashboard/triggers/views.py
index fefdc92..4d53319 100644
--- a/karbor_dashboard/triggers/views.py
+++ b/karbor_dashboard/triggers/views.py
@@ -89,11 +89,16 @@ class DetailView(horizon_views.HorizonTemplateView):
89 89
90 if trigger is not None and trigger.properties is not None: 90 if trigger is not None and trigger.properties is not None:
91 if trigger.properties["format"] == utils.CRONTAB: 91 if trigger.properties["format"] == utils.CRONTAB:
92 data = utils.CrontabUtil\ 92 data = utils.CrontabUtil.convert_from_crontab(
93 .convert_from_crontab(trigger.properties) 93 trigger.properties
94 if data: 94 )
95 for key, value in data.items(): 95 else:
96 setattr(trigger, key, value) 96 data = utils.CalendarUtil.convert_from_calendar(
97 trigger.properties
98 )
99 if data:
100 for key, value in data.items():
101 setattr(trigger, key, value)
97 102
98 context["trigger"] = trigger 103 context["trigger"] = trigger
99 context["url"] = reverse("horizon:karbor:triggers:index") 104 context["url"] = reverse("horizon:karbor:triggers:index")