-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
96 lines (78 loc) · 3.02 KB
/
main.py
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
from typing import Any, Optional
from fastapi import FastAPI
from pydantic import BaseModel, Json, Field
from consul_client import consul_client
from oslo_config import cfg
import os, re
from oslo_serialization import jsonutils
import uvicorn
import keystonemiddleware.auth_token
from fastapi import FastAPI
from fastapi.middleware.wsgi import WSGIMiddleware
from flask import Flask, request, Response as FResponse
flask_app = Flask(__name__)
flask_app.wsgi_app = keystonemiddleware.auth_token.AuthProtocol(flask_app.wsgi_app, {})
@flask_app.post("/")
def flask_main():
data = request.get_json()
instance_id = data.get("instance-id")
print(f"instance_id={instance_id}. whole json: {data}")
index, data = consul_client.kv.get(f"cloud/instances/{instance_id}/vendor-data")
if data is None:
return FResponse({}, status=200, mimetype="application/json")
else:
res = data["Value"].decode("utf-8")
return jsonutils.dumps(res)
@flask_app.post("/cloud-init")
def fetch_cloud_init():
data = request.get_json()
instance_id = data.get("instance-id")
print(f"instance_id={instance_id}. whole json: {data}")
index, data = consul_client.kv.get(f"cloud/instances/{instance_id}/cloud-config")
if data is None:
return FResponse({}, status=200, mimetype="application/json")
else:
# We decode bytes to string
res = data["Value"].decode("utf-8")
# HACK: Then we encode it JSON string using oslo.serialization
# DynamicJSON able to correctly decode this string with no exceptions
return jsonutils.dumps(res)
app = FastAPI()
app.mount("/ocdv", WSGIMiddleware(flask_app))
class VendordataPayload(BaseModel):
# Get the data nova handed us for this request
#
# An example of this data:
# {
# "hostname": "foo",
# "image-id": "75a74383-f276-4774-8074-8c4e3ff2ca64",
# "instance-id": "2ae914e9-f5ab-44ce-b2a2-dcf8373d899d",
# "metadata": {},
# "project-id": "039d104b7a5c4631b4ba6524d0b9e981",
# "user-data": null
# }
project_id: str = Field(alias="project-id", default=None)
instance_id: Optional[str] = Field(alias="instance-id", default=None)
image_id: Optional[str] = Field(alias="image-id", default=None)
user_data: Optional[str] = Field(alias="user-data", default=None)
hostname: Optional[str] = Field(alias="hostname", default=None)
metadata: Optional[Json[Any]] = Field(alias="metadata", default=None)
@app.post("/")
async def instance_vendordata(payload: VendordataPayload):
# TODO: Implement using only fastapi
return "Not Implemented"
# print(f"Data: {json.dumps(payload.__dict__)}")
# index, data = consul_client.kv.get(
# f"cloud/instances/{payload.instance_id}/vendor-data"
# )
# if data is None:
# return {}
# else:
# return Response(content=data["Value"], media_type="application/json")
if __name__ == "__main__":
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
reload=True,
)