-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_storage.py
More file actions
353 lines (309 loc) · 12.8 KB
/
Copy pathmain_storage.py
File metadata and controls
353 lines (309 loc) · 12.8 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import logging
import os
import traceback
from typing import Any
from dotenv import load_dotenv
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse, PlainTextResponse
from __init__ import __version__
from commons.elemento_iam import ElementoIdentityAccessManagement
from elemento_billing_manager.billing_manager import BillingStatus, billing_manager
from errors.client_errors import ElementoBadRequest, ElementoNotFound
from errors.server_errors import ElementoCreationFailed, ElementoInternalServerError
from infrastructure.storage.storage_manager import (
create_storage,
delete_storage,
list_storage,
get_storage_by_uuid,
is_storage_config_available,
)
from models.StorageModel import Storage
from elogger.logger_manager_fastapi import setup_logging
from asgi_correlation_id import CorrelationIdMiddleware
load_dotenv()
elemento_iam = ElementoIdentityAccessManagement()
app = FastAPI(docs_url=None)
setup_logging()
app.add_middleware(
CorrelationIdMiddleware,
header_name="X-Request-ID",
update_request_header=True,
)
class HealthCheckFilter(logging.Filter):
def filter(self, record):
msg = record.getMessage()
return not any(path in msg for path in ["GET /api/v1.0/health", "GET / HTTP/1.1"])
uvicorn_logger = logging.getLogger("uvicorn.access")
uvicorn_logger.addFilter(HealthCheckFilter())
@app.get("/")
async def root(token: Any = None):
return PlainTextResponse(
status_code=200,
content=f"Hello, world! This is an Elemento Storage Meson for provider {os.getenv('PROVIDER', 'provider')}.",
)
@app.get("/api/v1.0/health")
def health(token: Any = None):
# TODO: perform a GET request and check the response to ensure the provider is healthy
return JSONResponse(
status_code=200,
content={"status": "UP", "version": __version__, "provider_status": "TODO"},
)
@app.post("/api/v1.0/info")
@elemento_iam.validate_request
async def route_get_storage_by_uuid(request: Request, token: Any = None):
try:
info = await request.json()
volume_uuid = info['volume_id']
response = get_storage_by_uuid(volume_uuid)
if response is None:
return ElementoNotFound(
origin="MESON",
error="Volume not found",
trace=traceback.format_exc(),
)
return JSONResponse(content=response.to_json_response(), status_code=200)
except Exception as ex:
logging.error(ex.__str__())
return ElementoInternalServerError(
origin="MESON",
error=f"Internal Server Error - {ex}",
trace=traceback.format_exc(),
)
@app.post("/api/v1.0/accessible")
@elemento_iam.validate_request
async def route_list_storage(request: Request, token: Any = None):
try:
accessible_json = await request.json()
client_uuid = accessible_json["client_uid"]
except Exception as ex:
logging.error(ex.__str__())
return ElementoBadRequest(
origin="MESON",
error=f"Bad Request - {ex}",
field_errors=[],
docs_url="",
trace=f"{traceback.format_exc()}",
meson_source="route_list_storage()",
)
try:
response = list_storage(client_uuid)
if len(response) == 0:
return ElementoNotFound(
origin="MESON",
error="Volume not found",
trace=traceback.format_exc(),
)
volumes_out = [volume.to_json_response() for volume in response]
return JSONResponse(content=volumes_out, status_code=200)
except Exception as ex:
logging.error(ex.__str__())
return ElementoInternalServerError(
origin="MESON",
error=f"Internal Server Error - {ex}",
trace=traceback.format_exc(),
)
@app.get("/api/v1.0/cancreate")
@elemento_iam.validate_request
async def route_cancreate_storage(request: Request, token: Any = None):
config = await request.json()
try:
size = config["size"]
except Exception as ex:
logging.error(ex.__str__())
return ElementoBadRequest(
origin="MESON",
error=f"Bad Request - {ex}",
field_errors=[],
docs_url="",
trace=f"{traceback.format_exc()}",
meson_source="route_cancreate_storage()",
)
is_available = is_storage_config_available(int(size))
if is_available is True:
return JSONResponse(content=config, status_code=200)
else:
return JSONResponse(content={'data': 'creation not available'}, status_code=400)
@app.post("/api/v1.0/create")
@elemento_iam.validate_request
async def route_create_storage(request: Request, token: Any = None):
create_json = await request.json()
interval = "month"
org = token.elemento_org
client_uuid = token.client_uuid
role = token.elemento_role.lower()
billing_uuid = create_json.get("billing_uuid", None)
parent_billing_uuid = create_json.get("parent_billing_uuid", None)
try:
if "creatorID" not in create_json and "creator_id" in create_json:
create_json["creatorID"] = create_json["creator_id"]
if "volumeID" not in create_json and "volume_uuid" in create_json:
create_json["volumeID"] = create_json["volume_uuid"]
storage_data = Storage.from_json(create_json)
storage_data.creator_id = create_json.get("creatorID", client_uuid)
resolved_zone = (
create_json.get("region") or
create_json.get("csp_region") or
create_json.get("zone") or
os.environ.get("PROVIDER_REGION")
)
storage_data.region = resolved_zone
storage_data.csp_region = resolved_zone
except Exception as ex:
logging.error(ex.__str__())
return ElementoBadRequest(
origin="MESON",
error=f"Bad Request - {ex}",
field_errors=[],
docs_url="",
trace=traceback.format_exc(),
meson_source="route_create_storage()",
)
try:
# -------- DEV BILLING BYPASS --------
# disk_name = create_storage(storage_data)
# storage_data.name = disk_name
# return JSONResponse(content=storage_data.to_json_response(), status_code=200)
# ------------------------------------
if not billing_uuid:
try:
if parent_billing_uuid:
billing_uuid = billing_manager.start_sub(
client_uuid=storage_data.creator_id,
parent_billing_uuid=parent_billing_uuid,
payload=create_json,
org=org,
service_type="storage",
service_sub_type="blockstorage",
region=storage_data.region,
provider=os.getenv("PROVIDER", "provider"),
)
else:
payment_link, billing_uuid = billing_manager.start(
client_uuid=storage_data.creator_id,
payload=create_json,
service_type="storage",
service_sub_type="blockstorage",
region=storage_data.region,
provider=os.getenv("PROVIDER", "provider"),
org=org,
interval=interval,
)
return JSONResponse(status_code=202, content={"payment_url": payment_link, "billing_uuid": str(billing_uuid)})
except Exception as e:
logging.error(f"Error: {str(e)}")
return JSONResponse(status_code=500, content={"error": "billing_failed", "details": str(e)})
if role == 'portal' or billing_uuid:
storage_data.billing_uuid = billing_uuid
if resolved_zone:
storage_data.region = resolved_zone
storage_data.csp_region = resolved_zone
billing_manager.update_status(billing_uuid, BillingStatus.PROVISIONING)
disk_name = create_storage(storage_data)
billing_manager.update_status(billing_uuid, BillingStatus.RUNNING)
storage_data.name = disk_name
return JSONResponse(content=storage_data.to_json_response(), status_code=200)
except Exception as ex:
if billing_uuid:
billing_manager.update_status(billing_uuid, BillingStatus.ERROR)
logging.error(f"Error during storage creation: {ex.__str__()}")
return ElementoCreationFailed(
origin="MESON",
error=f"Error during storage creation - {ex.__str__()}",
trace=traceback.format_exc(),
stopped_successfully=True,
billing_suspended=True,
meson_source="route_create_storage()",
)
@app.post("/api/v1.0/destroy")
@elemento_iam.validate_request
async def route_delete_storage(request: Request, token: Any = None):
try:
role = token.elemento_role.lower()
client_uuid = token.client_uuid
to_destroy = await request.json()
try:
volume_id = to_destroy["volume_id"]
except Exception as ex:
return ElementoBadRequest(
origin="MESON",
error=f"Bad Request - Missing volume_id: {ex}",
field_errors=[],
docs_url="",
trace=f"{traceback.format_exc()}",
meson_source="route_delete_storage()",
)
try:
volume = get_storage_by_uuid(volume_id)
except Exception as e:
volume = None
logging.debug(f"Volume lookup notice: {str(e)}")
if not volume:
return ElementoNotFound(
origin="MESON",
error="Volume not found on infrastructure. Cannot proceed."
)
# -------- DEV BILLING BYPASS --------
# isDeleted = delete_storage(volume_id)
# if isDeleted:
# return JSONResponse(content={"destroyed": True, "vid": volume_id}, status_code=200)
# else:
# return ElementoNotFound(
# origin="MESON",
# error="Volume not found or could not be deleted.",
# trace=traceback.format_exc(),
# )
# ------------------------------------
billing_uuid = volume.billing_uuid
if not billing_uuid:
return ElementoNotFound(
origin="MESON",
error="Billing UUID not found for this volume"
)
if role == 'portal' and to_destroy.get("client_uuid"):
try:
isDeleted = delete_storage(volume_id)
if isDeleted:
billing_manager.update_status(billing_uuid, BillingStatus.DELETED)
return JSONResponse(content={"destroyed": True, "vid": volume_id}, status_code=200)
else:
billing_manager.update_status(billing_uuid, BillingStatus.ERROR)
return ElementoNotFound(
origin="MESON",
error="Volume not found or could not be deleted synchronously",
trace=traceback.format_exc(),
)
except Exception as ex:
billing_manager.update_status(billing_uuid, BillingStatus.ERROR)
logging.error(str(ex))
return ElementoInternalServerError(
origin="MESON",
error=f"Internal Server Error during synchronous portal deletion - {ex}",
trace=f"{traceback.format_exc()}",
)
else:
try:
to_destroy['client_uuid'] = to_destroy.get('client_uid', client_uuid)
res = billing_manager.update_status(
billing_uuid,
BillingStatus.TO_DELETE,
service_type="storage",
service_sub_type="blockstorage",
payload=to_destroy
)
if res is not None:
return JSONResponse(status_code=202, content={"status": "to_delete", "billing_uuid": billing_uuid})
except Exception as ex:
return ElementoBadRequest(
origin="MESON",
error=f"Volume tracking error for standard user - {ex}",
field_errors=[],
docs_url="",
trace=f"{traceback.format_exc()}",
meson_source="route_delete_storage()",
)
except Exception as e:
return ElementoInternalServerError(
origin="MESON",
error=f"Fatal Error - {e}",
trace=f"{traceback.format_exc()}",
)