Fixes the idf and pdf templates so that we can deploy opnfv
[pharos-tools.git] / laas-fog / pharoslaas / actions / add_booking.py
1 ##############################################################################
2 # Copyright 2017 Parker Berberian and Others                                 #
3 #                                                                            #
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                                    #
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,          #
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 ##############################################################################
16 import json
17 import requests
18 import time
19 import calendar
20 from st2actions.runners.pythonrunner import Action
21
22
23 class Add_Booking_Action(Action):
24
25     def run(self, booking_id):
26         dashboard = self.action_service.get_value(name="dashboard_url")
27         url = dashboard + "api/bookings/" + str(booking_id)
28         try:
29             booking = requests.get(url).json()
30             booking['start'] = self.convert_time(booking['start'])
31             booking['end'] = self.convert_time(booking['end'])
32             booking['status'] = 1
33
34             # add booking id to bookings list
35             bookings = json.loads(
36                     self.action_service.get_value(name="bookings", local=False)
37                     )
38             if booking['id'] in bookings:
39                 return
40             bookings.append(booking['id'])
41             self.action_service.set_value(
42                     name="bookings",
43                     value=json.dumps(bookings),
44                     local=False
45                     )
46
47             # add booking to datastore
48             name = "booking_" + str(booking['id'])
49             self.action_service.set_value(
50                     name=name,
51                     value=json.dumps(booking),
52                     local=False
53                     )
54
55         except Exception:
56             pass
57
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)
61         return epoch_time