1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
##############################################################################
# Copyright (c) 2018 Sawyer Bergeron and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
# Unauthenticated requests to liblaas. If a call makes it to here, it is assumed to be authenticated
import os
import requests
import json
base = os.environ.get("LIBLAAS_BASE_URL")
post_headers = {'Content-Type': 'application/json'}
def liblaas_docs():
endpoint = f'docs'
url = f'{base}{endpoint}'
try:
response = requests.get(url)
return response.json()
except:
print(f"Error at {url}")
return None
### BOOKING
# DELETE
def booking_end_booking(agg_id: str) -> requests.Response:
endpoint = f'booking/{agg_id}/end'
url = f'{base}{endpoint}'
try:
response = requests.delete(url)
return response.json()
except:
print(f"Error at {url}")
return None
# GET
def booking_booking_status(agg_id: str) -> requests.Response:
endpoint = f'booking/{agg_id}/status'
url = f'{base}{endpoint}'
try:
response = requests.get(url)
return response.json()
except:
print(f"Error at {url}")
return None
# POST
def booking_create_booking(booking_blob: dict) -> requests.Response:
endpoint = f'booking/create'
url = f'{base}{endpoint}'
try:
response = requests.post(url, data=json.dumps(booking_blob), headers=post_headers)
return response.json()
except:
print(f"Error at {url}")
return None
### FLAVOR
# GET
def flavor_list_flavors(project: str) -> requests.Response:
endpoint = f'flavor' #todo - add project to url once liblaas supports it
url = f'{base}{endpoint}'
try:
response = requests.get(url)
return response.json()
except:
print(f"Error at {url}")
return None
# GET
def flavor_list_hosts(project: str) -> requests.Response:
endpoint = f'flavor/hosts/{project}' #todo - support project in liblaas
url = f'{base}{endpoint}'
try:
response = requests.get(url)
return response.json()
except:
print(f"Error at {url}")
return None
### TEMPLATE
# GET
def template_list_templates(uid: str) -> requests.Response:
endpoint = f'template/list/{uid}' # todo - templates need to be restricted by project
url = f'{base}{endpoint}'
try:
response = requests.get(url)
return response.json()
except:
print(f"Error at {url}")
return None
# DELETE
def template_delete_template(template_id: str) -> requests.Response:
endpoint = f'template/{template_id}'
url = f'{base}{endpoint}'
try:
response = requests.delete(url)
return response.json()
except:
print(f"Error at {url}")
return None
#POST
def template_make_template(template_blob: dict) -> requests.Response:
endpoint = f'template/create' # todo - needs to be restricted by project
url = f'{base}{endpoint}'
try:
response = requests.post(url, data=json.dumps(template_blob), headers=post_headers)
return response.json()
except:
print(f"Error at {url}")
return None
### USER
# GET
def user_get_user(uid: str) -> requests.Response:
endpoint = f'user/{uid}'
url = f'{base}{endpoint}'
try:
response = requests.get(url)
return response.json()
except:
print(f"Error at {url}")
return None
# POST
def user_create_user(user_blob: dict) -> requests.Response:
endpoint = f'user/create'
url = f'{base}{endpoint}'
try:
response = requests.post(url, data=json.dumps(user_blob), headers=post_headers)
# LibLaaS is not returning anything here regardless, so we need some way of determining if it was successful
# return response.json()
if response.status_code == 200:
return user_blob["uid"]
return response.json()
except:
print(f"Error at {url}")
return None
# POST
def user_set_ssh(uid: str, keys: list) -> requests.Response:
endpoint = f'user/{uid}/ssh'
url = f'{base}{endpoint}'
try:
response = requests.post(url, data=json.dumps(clean_ssh_keys(keys)), headers=post_headers)
# return response.json()
return response.status_code == 200
except:
print(f"Error at {url}")
return None
# POST
def user_set_company(uid: str, company: str):
endpoint = f'user/{uid}/company'
url = f'{base}{endpoint}'
try:
response = requests.post(url, data=json.dumps(company), headers=post_headers)
# return response.json()
return response.status_code == 200
except:
print(f"Error at {url}")
return None
# POST
def user_set_email(uid: str, email: str):
endpoint = f'user/{uid}/email'
url = f'{base}{endpoint}'
try:
response = requests.post(url, data=json.dumps(email), headers=post_headers)
# return response.json()
return response.status_code == 200
except:
print(f"Error at {url}")
return None
# utils
def clean_ssh_keys(ssh_key_list):
cleaned = []
for key in ssh_key_list:
cleaned.append(key.strip())
return cleaned
|