Skip to content

Commit

Permalink
Refactoring, address comments, more unit tests, metadata_api updates
Browse files Browse the repository at this point in the history
  • Loading branch information
npalaska committed Mar 9, 2021
1 parent 0d12b0c commit ba71d77
Show file tree
Hide file tree
Showing 6 changed files with 634 additions and 317 deletions.
13 changes: 9 additions & 4 deletions lib/pbench/server/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
import os
import sys

from flask import Flask, request
from flask import Flask
from flask_restful import Api
from flask_cors import CORS

from pbench.server import PbenchServerConfig
from pbench.common.exceptions import BadConfig, ConfigFileNotSpecified
from pbench.server.api.resources.upload_api import Upload, HostInfo
from pbench.server.api.resources.graphql_api import GraphQL, UserMetadata, QueryMetadata
from pbench.server.api.resources.graphql_api import GraphQL
from pbench.server.api.resources.metadata_api import UserMetadata, QueryMetadata
from pbench.common.logger import get_pbench_logger
from pbench.server.api.resources.query_apis.elasticsearch_api import Elasticsearch
from pbench.server.api.resources.query_apis.query_controllers import QueryControllers
Expand Down Expand Up @@ -86,10 +87,14 @@ def register_endpoints(api, app, config):
)

api.add_resource(
UserMetadata, f"{base_uri}/metadata", resource_class_args=(config, logger, token_auth),
UserMetadata,
f"{base_uri}/metadata",
resource_class_args=(config, logger, token_auth),
)
api.add_resource(
QueryMetadata, f"{base_uri}/metadata/<string:id>", resource_class_args=(config, logger),
QueryMetadata,
f"{base_uri}/metadata/<string:id>",
resource_class_args=(config, logger),
)


Expand Down
251 changes: 1 addition & 250 deletions lib/pbench/server/api/resources/graphql_api.py
Original file line number Diff line number Diff line change
@@ -1,255 +1,6 @@
import requests
import datetime
import json
from flask_restful import Resource, abort
from flask import request, make_response, jsonify
from pbench.server.database.models.metadata import MetadataModel
from pbench.server.database.database import Database
from pbench.server.api.auth import Auth


class UserMetadata(Resource):
"""
Abstracted pbench API for handling user metadata by using graphql schema
"""

def __init__(self, config, logger, auth):
self.server_config = config
self.logger = logger
self.auth = auth

@Auth.token_auth.login_required(f=Auth().verify_auth())
def post(self):
"""
Post request for creating metadata instance for a user.
This requires a JWT auth token in the header field
This requires a JSON data with required user metadata fields
{
"config": "config",
"description": "description",
}
Required headers include
Authorization: JWT token (user received upon login)
:return: JSON Payload
response_object = {
"status": "success"
"data" {
"id": "metadata_id",
"config": "Config string"
"description": "Description string"
}
}
"""
post_data = request.get_json()
if not post_data:
self.logger.warning("Invalid json object: %s", request.url)
abort(400, message="Invalid json object in request")

config = post_data.get("config")
if not config:
self.logger.warning("Config not provided during metadata creation")
abort(400, message="Please provide a config string")

description = post_data.get("description")
if not description:
self.logger.warning("Description not provided during metadata creation")
abort(400, message="Please provide a description string")
current_user_id = self.auth.token_auth.current_user().id

try:
# Create a new metadata session
metadata_session = MetadataModel(
created=str(datetime.datetime.now()),
config=config,
description=description,
user_id=current_user_id
)
# insert the metadata session for a user
Database.db_session.add(metadata_session)
Database.db_session.commit()
self.logger.info("New user metadata session created")
except Exception:
self.logger.exception("Exception occurred during Metadata creation")
abort(500, message="INTERNAL ERROR")
else:
response_object = {
"status": "success",
"data": {
"id": metadata_session.id,
"config": metadata_session.config,
"description": metadata_session.description,
},
}
return make_response(jsonify(response_object), 201)

@Auth.token_auth.login_required(f=Auth().verify_auth())
def get(self):
"""
Get request for querying all the metadata sessions for a user.
This requires a JWT auth token in the header field
Required headers include
Authorization: JWT token (user received upon login)
:return: JSON Payload
response_object = {
"status": "success"
"data": {
"sessions": [
{"id": "metadata_id",
"config": "Config string"
"description": "Description string"},
{}]
}
}
"""
current_user_id = self.auth.token_auth.current_user().id
try:
# Fetch the metadata session
sessions = (
Database.db_session.query(MetadataModel)
.filter_by(user_id=current_user_id)
.all()
)

req_keys = ["id", "config", "description", "created"]
data = json.dumps([{key: session.as_dict()[key] for key in req_keys} for session in sessions])
except Exception:
self.logger.exception("Exception occurred during querying Metadata model")
abort(500, message="INTERNAL ERROR")

response_object = {
"status": "success",
"data": {
"sessions": data
},
}
return make_response(jsonify(response_object), 200)


class QueryMetadata(Resource):
"""
Abstracted pbench API for querying a single user metadata session
"""

def __init__(self, config, logger):
self.server_config = config
self.logger = logger

@Auth.token_auth.login_required(f=Auth().verify_auth())
def get(self, id=None):
"""
Get request for querying a metadata session for a user given a metadata id.
This requires a JWT auth token in the header field
This requires a JSON data with required user metadata fields to update
{
"description": "description",
}
The url requires a metadata session id such as /user/metadata/<string:id>
Required headers include
Authorization: JWT token (user received upon login)
:return: JSON Payload
response_object = {
"status": "success"
"data" {
"id": "metadata_id",
"config": "Config string"
"description": "Description string"
}
}
"""
if not id:
self.logger.warning("Meatadata id not provided during metadata query")
abort(400, message="Please provide a metadata id to query")

try:
# Fetch the metadata session
session = (
Database.db_session.query(MetadataModel)
.filter_by(id=id)
.first()
)
except Exception:
self.logger.exception("Exception occurred during querying Metadata model")
abort(500, message="INTERNAL ERROR")
else:
response_object = {
"status": "success",
"data": {
"id": session.id,
"config": session.config,
"description": session.description,
},
}
return make_response(jsonify(response_object), 200)

@Auth.token_auth.login_required(f=Auth().verify_auth())
def put(self, id=None):
"""
Put request for updating a metadata session for a user given a metadata id.
This requires a JWT auth token in the header field
The url requires a metadata session id such as /user/metadata/<string:id>
Required headers include
Authorization: JWT token (user received upon login)
:return: JSON Payload
response_object = {
"status": "success"
"data" {
"id": "metadata_id",
"config": "Config string"
"description": "Description string"
}
}
"""
if not id:
self.logger.warning("Meatadata id not provided during metadata query")
abort(400, message="Please provide a metadata id to query")

post_data = request.get_json()
if not post_data:
self.logger.warning("Invalid json object: %s", request.url)
abort(400, message="Invalid json object in request")

description = post_data.get("description")
if not description:
self.logger.warning("Description not provided during metadata update")
abort(400, message="Please provide a description string")

try:
# Fetch the metadata session
session = (
Database.db_session.query(MetadataModel)
.filter_by(id=id)
.first()
)
session.description = description
# Update the metadata session for a user
Database.db_session.add(session)
Database.db_session.commit()
self.logger.info("User metadata session updated")
except Exception:
self.logger.exception("Exception occurred during querying Metadata model")
abort(500, message="INTERNAL ERROR")
else:
response_object = {
"status": "success",
"data": {
"id": session.id,
"config": session.config,
"description": session.description,
},
}
return make_response(jsonify(response_object), 200)
from flask import request, make_response


class GraphQL(Resource):
Expand Down
Loading

0 comments on commit ba71d77

Please sign in to comment.