Skip to content

Commit 8af2e88

Browse files
committed
Add Metadata API support
1 parent 3938028 commit 8af2e88

File tree

2 files changed

+190
-20
lines changed

2 files changed

+190
-20
lines changed

lib/pbench/server/api/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pbench.server import PbenchServerConfig
1515
from pbench.common.exceptions import BadConfig, ConfigFileNotSpecified
1616
from pbench.server.api.resources.upload_api import Upload, HostInfo
17-
from pbench.server.api.resources.graphql_api import GraphQL, UserMetadata
17+
from pbench.server.api.resources.graphql_api import GraphQL, UserMetadata, QueryMetadata
1818
from pbench.common.logger import get_pbench_logger
1919
from pbench.server.api.resources.query_apis.elasticsearch_api import Elasticsearch
2020
from pbench.server.api.resources.query_apis.query_controllers import QueryControllers
@@ -88,6 +88,9 @@ def register_endpoints(api, app, config):
8888
api.add_resource(
8989
UserMetadata, f"{base_uri}/user/metadata", resource_class_args=(config, logger),
9090
)
91+
api.add_resource(
92+
QueryMetadata, f"{base_uri}/user/metadata/<string:id>", resource_class_args=(config, logger),
93+
)
9194

9295

9396
def get_server_config():

lib/pbench/server/api/resources/graphql_api.py

Lines changed: 186 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import requests
2+
import datetime
3+
import json
24
from flask_restful import Resource, abort
35
from flask import request, make_response, jsonify
46
from pbench.server.api.resources.auth import auth
5-
from pbench.server.api.resources.graphql_schema import schema
7+
from pbench.server.api.resources.models import MetadataModel
8+
from pbench.server.api.resources.database import Database
69

710

811
class UserMetadata(Resource):
@@ -54,35 +57,199 @@ def post(self):
5457
self.logger.warning("Description not provided during metadata creation")
5558
abort(400, message="Please provide a description string")
5659
current_user_id = auth.current_user().id
60+
5761
try:
58-
# query GraphQL
59-
query = f"""
60-
mutation {{
61-
createMetadata (input: {{config:"{config}", description:"{description}", user_id:{current_user_id}}}) {{
62-
metadata {{
63-
id
64-
config
65-
description
66-
}}
67-
}}
68-
}}
69-
"""
70-
result = schema.execute(query)
71-
except Exception as e:
62+
# Create a new metadata session
63+
metadata_session = MetadataModel(
64+
created=str(datetime.datetime.now()),
65+
config=config,
66+
description=description,
67+
user_id=current_user_id
68+
)
69+
# insert the metadata session for a user
70+
Database.db_session.add(metadata_session)
71+
Database.db_session.commit()
72+
self.logger.info("New user metadata session created")
73+
except Exception:
7274
self.logger.exception("Exception occurred during Metadata creation")
7375
abort(500, message="INTERNAL ERROR")
7476
else:
75-
data = result.data["createMetadata"]["metadata"]
7677
response_object = {
7778
"status": "success",
7879
"data": {
79-
"id": data["id"],
80-
"config": data["config"],
81-
"description": data["description"],
80+
"id": metadata_session.id,
81+
"config": metadata_session.config,
82+
"description": metadata_session.description,
8283
},
8384
}
8485
return make_response(jsonify(response_object), 201)
8586

87+
@auth.login_required()
88+
def get(self):
89+
"""
90+
Get request for querying all the metadata sessions for a user.
91+
This requires a JWT auth token in the header field
92+
93+
Required headers include
94+
Authorization: JWT token (user received upon login)
95+
96+
:return: JSON Payload
97+
response_object = {
98+
"status": "success"
99+
"data": {
100+
"sessions": [
101+
{"id": "metadata_id",
102+
"config": "Config string"
103+
"description": "Description string"},
104+
{}]
105+
}
106+
}
107+
"""
108+
current_user_id = auth.current_user().id
109+
try:
110+
# Fetch the metadata session
111+
sessions = (
112+
Database.db_session.query(MetadataModel)
113+
.filter_by(user_id=current_user_id)
114+
.all()
115+
)
116+
117+
req_keys = ["id", "config", "description", "created"]
118+
data = json.dumps([{key: session.as_dict()[key] for key in req_keys} for session in sessions])
119+
except Exception:
120+
self.logger.exception("Exception occurred during querying Metadata model")
121+
abort(500, message="INTERNAL ERROR")
122+
123+
response_object = {
124+
"status": "success",
125+
"data": {
126+
"sessions": data
127+
},
128+
}
129+
return make_response(jsonify(response_object), 200)
130+
131+
132+
class QueryMetadata(Resource):
133+
"""
134+
Abstracted pbench API for querying a single user metadata session
135+
"""
136+
137+
def __init__(self, config, logger):
138+
self.server_config = config
139+
self.logger = logger
140+
141+
@auth.login_required()
142+
def get(self, id=None):
143+
"""
144+
Get request for querying a metadata session for a user given a metadata id.
145+
This requires a JWT auth token in the header field
146+
147+
This requires a JSON data with required user metadata fields to update
148+
{
149+
"description": "description",
150+
}
151+
152+
The url requires a metadata session id such as /user/metadata/<string:id>
153+
154+
Required headers include
155+
Authorization: JWT token (user received upon login)
156+
157+
:return: JSON Payload
158+
response_object = {
159+
"status": "success"
160+
"data" {
161+
"id": "metadata_id",
162+
"config": "Config string"
163+
"description": "Description string"
164+
}
165+
}
166+
"""
167+
if not id:
168+
self.logger.warning("Meatadata id not provided during metadata query")
169+
abort(400, message="Please provide a metadata id to query")
170+
171+
try:
172+
# Fetch the metadata session
173+
session = (
174+
Database.db_session.query(MetadataModel)
175+
.filter_by(id=id)
176+
.first()
177+
)
178+
except Exception:
179+
self.logger.exception("Exception occurred during querying Metadata model")
180+
abort(500, message="INTERNAL ERROR")
181+
else:
182+
response_object = {
183+
"status": "success",
184+
"data": {
185+
"id": session.id,
186+
"config": session.config,
187+
"description": session.description,
188+
},
189+
}
190+
return make_response(jsonify(response_object), 200)
191+
192+
@auth.login_required()
193+
def put(self, id=None):
194+
"""
195+
Put request for updating a metadata session for a user given a metadata id.
196+
This requires a JWT auth token in the header field
197+
198+
The url requires a metadata session id such as /user/metadata/<string:id>
199+
200+
Required headers include
201+
Authorization: JWT token (user received upon login)
202+
203+
:return: JSON Payload
204+
response_object = {
205+
"status": "success"
206+
"data" {
207+
"id": "metadata_id",
208+
"config": "Config string"
209+
"description": "Description string"
210+
}
211+
}
212+
"""
213+
if not id:
214+
self.logger.warning("Meatadata id not provided during metadata query")
215+
abort(400, message="Please provide a metadata id to query")
216+
217+
post_data = request.get_json()
218+
if not post_data:
219+
self.logger.warning("Invalid json object: %s", request.url)
220+
abort(400, message="Invalid json object in request")
221+
222+
description = post_data.get("description")
223+
if not description:
224+
self.logger.warning("Description not provided during metadata update")
225+
abort(400, message="Please provide a description string")
226+
227+
try:
228+
# Fetch the metadata session
229+
session = (
230+
Database.db_session.query(MetadataModel)
231+
.filter_by(id=id)
232+
.first()
233+
)
234+
session.description = description
235+
# Update the metadata session for a user
236+
Database.db_session.add(session)
237+
Database.db_session.commit()
238+
self.logger.info("User metadata session updated")
239+
except Exception:
240+
self.logger.exception("Exception occurred during querying Metadata model")
241+
abort(500, message="INTERNAL ERROR")
242+
else:
243+
response_object = {
244+
"status": "success",
245+
"data": {
246+
"id": session.id,
247+
"config": session.config,
248+
"description": session.description,
249+
},
250+
}
251+
return make_response(jsonify(response_object), 200)
252+
86253

87254
class GraphQL(Resource):
88255
"""GraphQL API for post request via server."""

0 commit comments

Comments
 (0)