1 ##############################################################################
2 # Copyright 2017 Parker Berberian and Others #
4 # Licensed under the Apache License, Version 2.0 (the "License"); #
5 # you may not use this file except in compliance with the License. #
6 # You may obtain a copy of the License at #
8 # http://www.apache.org/licenses/LICENSE-2.0 #
10 # Unless required by applicable law or agreed to in writing, software #
11 # distributed under the License is distributed on an "AS IS" BASIS, #
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13 # See the License for the specific language governing permissions and #
14 # limitations under the License. #
15 ##############################################################################
20 from st2actions.runners.pythonrunner import Action
23 class Add_Booking_Action(Action):
25 def run(self, booking_id):
26 dashboard = self.action_service.get_value(name="dashboard_url")
27 url = dashboard + "api/bookings/" + str(booking_id)
29 booking = requests.get(url).json()
30 booking['start'] = self.convert_time(booking['start'])
31 booking['end'] = self.convert_time(booking['end'])
34 # add booking id to bookings list
35 bookings = json.loads(
36 self.action_service.get_value(name="bookings", local=False)
38 if booking['id'] in bookings:
40 bookings.append(booking['id'])
41 self.action_service.set_value(
43 value=json.dumps(bookings),
47 # add booking to datastore
48 name = "booking_" + str(booking['id'])
49 self.action_service.set_value(
51 value=json.dumps(booking),
58 def convert_time(self, timestr):
59 time_struct = time.strptime(timestr, '%Y-%m-%dT%H:%M:%SZ')
60 epoch_time = calendar.timegm(time_struct)