|
1 | 1 | import requests |
| 2 | +import datetime |
| 3 | +import json |
2 | 4 | from flask_restful import Resource, abort |
3 | 5 | from flask import request, make_response, jsonify |
4 | 6 | 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 |
6 | 9 |
|
7 | 10 |
|
8 | 11 | class UserMetadata(Resource): |
@@ -54,35 +57,199 @@ def post(self): |
54 | 57 | self.logger.warning("Description not provided during metadata creation") |
55 | 58 | abort(400, message="Please provide a description string") |
56 | 59 | current_user_id = auth.current_user().id |
| 60 | + |
57 | 61 | 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: |
72 | 74 | self.logger.exception("Exception occurred during Metadata creation") |
73 | 75 | abort(500, message="INTERNAL ERROR") |
74 | 76 | else: |
75 | | - data = result.data["createMetadata"]["metadata"] |
76 | 77 | response_object = { |
77 | 78 | "status": "success", |
78 | 79 | "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, |
82 | 83 | }, |
83 | 84 | } |
84 | 85 | return make_response(jsonify(response_object), 201) |
85 | 86 |
|
| 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 | + |
86 | 253 |
|
87 | 254 | class GraphQL(Resource): |
88 | 255 | """GraphQL API for post request via server.""" |
|
0 commit comments