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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
##############################################################################
# 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
##############################################################################
# HTTP Requests from the user will need to be processed here first, before the appropriate liblaas endpoint is called
from account.models import UserProfile
from liblaas.views import *
from liblaas.utils import validate_collaborators
from django.http import JsonResponse, HttpResponse
from django.contrib.auth.models import User
from booking.models import Booking
from account.models import Lab
from django.utils import timezone
from datetime import timedelta
def request_list_flavors(request) -> HttpResponse:
data = json.loads(request.body.decode('utf-8'))
if not "project" in data:
return HttpResponse(status=422)
if not request.user.is_authenticated:
return HttpResponse(status=401)
response = flavor_list_flavors(data["project"])
return JsonResponse(status=200, data={"flavors_list": response})
def request_list_template(request) -> HttpResponse:
if not request.user.is_authenticated:
return HttpResponse(status=401)
uid = UserProfile.objects.get(user=request.user)
uid = uid.ipa_username
response = template_list_templates(uid)
return JsonResponse(status=200, data={"templates_list": response})
def request_create_template(request) -> HttpResponse:
data = json.loads(request.body.decode('utf-8'))
if not "template_blob" in data:
return HttpResponse(status=422)
if not request.user.is_authenticated:
return HttpResponse(status=401)
response = template_make_template(data["template_blob"])
return JsonResponse(status=200, data={"uuid": response})
def request_create_booking(request) -> HttpResponse:
# Validate request
data = json.loads(request.body.decode('utf-8'))
if not "booking_blob" in data:
return HttpResponse(status=422)
if not request.user.is_authenticated:
return HttpResponse(status=401)
data = data["booking_blob"]
# Validate and build collaborators list
valid = validate_collaborators(data["allowed_users"])
if (not valid["valid"]):
return JsonResponse(
data={"message": valid["message"], "error": True},
status=200,
safe=False
)
ipa_users = []
ipa_users.append(UserProfile.objects.get(user=request.user).ipa_username)
for user in data["allowed_users"]:
collab_profile = UserProfile.objects.get(user=User.objects.get(username=user))
ipa_users.append(collab_profile.ipa_username)
# Reformat post data
bookingBlob = {
"template_id": data["template_id"],
"allowed_users": ipa_users,
"global_cifile": data["global_cifile"],
"metadata": {
"booking_id": None, # fill in after creating django object
"owner": UserProfile.objects.get(user=request.user).ipa_username,
"lab": "UNH_IOL",
"purpose": data["metadata"]["purpose"],
"project": data["metadata"]["project"],
"length": int(data["metadata"]["length"])
},
"origin": "anuket" if os.environ.get("TEMPLATE_OVERRIDE_DIR") == 'laas' else "lfedge" # todo - refactor
}
# Create booking in dashboard
booking = Booking.objects.create(
purpose=bookingBlob["metadata"]["purpose"],
project=bookingBlob["metadata"]['project'],
lab=Lab.objects.get(name='UNH_IOL'),
owner=request.user,
start=timezone.now(),
end=timezone.now() + timedelta(days=int(bookingBlob["metadata"]['length'])),
)
for c in list(data["allowed_users"]):
booking.collaborators.add(User.objects.get(username=c))
# Create booking in liblaas
bookingBlob["metadata"]["booking_id"] = str(booking.id)
liblaas_response = booking_create_booking(bookingBlob)
if liblaas_response.status_code != 200:
print("received non success from liblaas, deleting booking from dashboard")
booking.delete()
return JsonResponse(
data={},
status=500,
safe=False
)
aggregateId = liblaas_response
booking.aggregateId = aggregateId
booking.save()
print(f"Created new booking with id {booking.id} and aggregate {aggregateId}")
return JsonResponse(
data = {"bookingId": booking.id},
status=200,
safe=False
)
def request_migrate_new(request) -> HttpResponse:
user = request.user
profile = UserProfile.objects.get(user=user)
data = json.loads(request.body.decode('utf-8'))
fn = data["firstname"].strip()
ln = data["lastname"].strip()
company = data["company"].strip()
if (fn == "" or ln == "" or company == ""):
return JsonResponse(
data = {"message": "Please fill out all fields."},
status=406
)
user_blob = {
"uid": str(user), # For this case, the username is not taken, so the dashboard username will be used
"givenname": str(fn),
"sn": str(ln),
"ou": str(company),
"mail": str(profile.email_addr),
'random': True
}
vpn_username = user_create_user(user_blob)
if (vpn_username):
profile.ipa_username = vpn_username
profile.save()
return HttpResponse(status=200)
return JsonResponse(
data = {"message": "Unable to create account. Please try again."},
status=406
)
def request_migrate_conflict(request) -> HttpResponse:
user = request.user
profile = UserProfile.objects.get(user=user)
data = json.loads(request.body.decode('utf-8'))
fn = data["firstname"].strip()
ln = data["lastname"].strip()
company = data["company"].strip()
username = data["username"].strip()
if (fn == "" or ln == "" or company == "" or username == ""):
return JsonResponse(
data = {"message": "Please fill out all fields."},
status=406
)
# Check if username is taken by querying for that user
existing_user = user_get_user(username)
if (existing_user):
return JsonResponse(
data = {"message": "Username is already taken. Please try again."},
status=406
)
# Create and link account
user_blob = {
"uid": str(username),
"givenname": str(fn),
"sn": str(ln),
"ou": str(company),
"mail": str(profile.email_addr),
'random': True
}
vpn_username = user_create_user(user_blob)
if (vpn_username):
profile.ipa_username = vpn_username
profile.save()
return HttpResponse(status=200)
return JsonResponse(
data = {"message": "Unable to create account. Please try again."},
status=406
)
def request_set_company(request):
data = json.loads(request.body.decode('utf-8'))
if not "data" in data:
return HttpResponse(status=422)
if not request.user.is_authenticated:
return HttpResponse(status=401)
up = UserProfile.objects.get(user=request.user)
success = user_set_company(up.ipa_username, data["data"].strip())
if (success):
return HttpResponse(status=200)
return JsonResponse(
data = {"message": "Unable to update details. Please check your input and try again."},
status=406
)
def request_set_ssh(request):
data = json.loads(request.body.decode('utf-8'))
if not "data" in data:
return HttpResponse(status=422)
if not request.user.is_authenticated:
return HttpResponse(status=401)
up = UserProfile.objects.get(user=request.user)
success = user_set_ssh(up.ipa_username, data["data"])
if (success):
return HttpResponse(status=200)
# API does not provide a way to verify keys were successfully added, so it is unlikely that this case will ever be hit
return JsonResponse(
data = {"message": "Unable to add SSH key. Please verify that the key is valid and try again."},
status=406
)
|