Skip to content

Commit

Permalink
Add endpoint configuration query
Browse files Browse the repository at this point in the history
  • Loading branch information
dbutenhof committed Jan 8, 2021
1 parent 16f5522 commit 447c6fc
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/pbench/server/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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
from pbench.server.api.resources.endpoint_configure import EndpointConfig
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 All @@ -35,6 +36,11 @@ def register_endpoints(api, app, config):
api.add_resource(
HostInfo, f"{base_uri}/host_info", resource_class_args=(config, app.logger),
)
api.add_resource(
EndpointConfig,
f"{base_uri}/endpoints",
resource_class_args=(config, app.logger),
)
api.add_resource(
Elasticsearch,
f"{base_uri}/elasticsearch",
Expand Down
99 changes: 99 additions & 0 deletions lib/pbench/server/api/resources/endpoint_configure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from flask_restful import Resource, abort
from flask import jsonify

from pbench.server.api.resources.query_apis import get_index_prefix


class EndpointConfig(Resource):
"""
EndpointConfig API resource: this supports dynamic dashboard configuration
from the Pbench server rather than constructing a disconnected dashboard
config file.
"""

def __init__(self, config, logger):
"""
__init__ Construct the API resource
Args:
config (PbenchServerConfig): server config values
logger (Logger): message logging
"""
self.logger = logger
port = config.get("pbench-server", "rest_port")
host = config.get("pbench-server", "host")
self.host = f"{str(host)}"
self.port = port
uri_prefix = config.rest_uri
self.uri = f"{host}:{port}{uri_prefix}"
self.prefix = get_index_prefix(config)
self.commit_id = config.COMMIT_ID

def get(self):
"""
Return server configuration information required by the UI
dashboard. This includes
metadata: Information about the server configuration
prefix: The indexing prefix applied by the server to partition a
shared Elasticsearch cluster index namespace.
result_index: The "root" index name for Pbench result data,
qualified by the current index version and prefix.
result_sample_index: The "result-data" index has been broken into
"result-data" and "result-data-sample" indices for the
Elasticsearch V7 transition. TODO: If we update the dashboard
queries without full migration to native Pbench APIs, it'll
need this.
run_index: The "master" run-data index root.
run_toc_index: The Elasticsearch V7 index for run TOC data; again, the
dashboard will need this if we don't move directly to native
Pbench APIs.
api: A list of the server APIs supported; we give a name, which
corresponds to either the current config.json property name
(e.g., "elasticsearch") or the current src/service method name
being replaced (e.g., "queryControllers").
results: Direct access to Pbench data sets for the dashboard; this
is likely also something that should be superceded, as Pbench
will need to be a front-end for future S3 storage schemas.
elasticsearch: The Pbench pass-through URI for the Elasticsearch
cluster. This will eventually be superceded by the native
Pbench APIs, though might remain accessible with special
user/group privileges to support special cases?
graphql: The GraphQL frontend on postgreSQL currently used by the
dashboard user mocks. This will be superceded and decprecated
by native Pbench user management APIs.
queryControllers: Return information about the run documents that
were created within a specified range of months.
Meta TODO: We're giving the pass-through APIs for Elasticsearch and
GraphQL here, which implies adoption of Nikhil's and Fuqing's work to
move the dashboard away from direct access to the backend DB servers.
The alternative would be to expose the direct Elasticsearch and GraphQL
URIs here.
"""
try:
endpoints = {
"metadata": {
"identification": f"Pbench server {self.commit_id}",
"prefix": self.prefix,
"run_index": f"{self.prefix}.v6.run-data.",
"run_toc_index": f"{self.prefix}.v6.run-toc.",
"result_index": f"{self.prefix}.v4.result-data.",
"result_sample_index": f"{self.prefix}.v4.result-data-sample.",
},
"api": {
"results": f"{self.host}:8901",
"elasticsearch": f"{self.uri}/elasticsearch",
"endpoints": f"{self.uri}/endpoints",
"graphql": f"{self.uri}/graphql",
"queryControllers": f"{self.uri}/queryControllers",
},
}
response = jsonify(endpoints)
except Exception:
self.logger.exception("Something went wrong constructing the endpoint info")
abort(500, message="INTERNAL ERROR")
else:
response.status_code = 200
return response
42 changes: 42 additions & 0 deletions lib/pbench/test/unit/server/test_endpoint_configure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from pbench.server.api.resources.query_apis import get_index_prefix


class TestEnpointConfig:
"""
Unit testing for EndpointConfig class.
In a web service context, we access class functions mostly via the
Flask test client rather than trying to directly invoke the class
constructor and `post` service.
"""

def test_query(self, client, server_config):
"""
test_query Check that endpoint data matches the config file.
"""
port = server_config.get("pbench-server", "rest_port")
host = server_config.get("pbench-server", "host")
uri_prefix = server_config.rest_uri
uri = f"{host}:{port}{uri_prefix}"
prefix = get_index_prefix(server_config)
expected_results = {
"metadata": {
"identification": f"Pbench server {server_config.COMMIT_ID}",
"prefix": prefix,
"run_index": f"{prefix}.v6.run-data.",
"run_toc_index": f"{prefix}.v6.run-toc.",
"result_index": f"{prefix}.v4.result-data.",
"result_sample_index": f"{prefix}.v4.result-data-sample.",
},
"api": {
"results": f"{host}:8901",
"elasticsearch": f"{uri}/elasticsearch",
"endpoints": f"{uri}/endpoints",
"graphql": f"{uri}/graphql",
"queryControllers": f"{uri}/queryControllers",
},
}

response = client.get(f"{server_config.rest_uri}/endpoints")
res_json = response.json
assert res_json == expected_results
1 change: 1 addition & 0 deletions server/rpm/pbench-server.spec.j2
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ fi
/%{installdir}/lib/pbench/server/api/__init__.py
/%{installdir}/lib/pbench/server/api/resources/graphql_api.py
/%{installdir}/lib/pbench/server/api/resources/upload_api.py
/%{installdir}/lib/pbench/server/api/resources/endpoint_configure.py
/%{installdir}/lib/pbench/server/api/resources/query_apis/__init__.py
/%{installdir}/lib/pbench/server/api/resources/query_apis/elasticsearch_api.py
/%{installdir}/lib/pbench/server/api/resources/query_apis/query_controllers.py
Expand Down

0 comments on commit 447c6fc

Please sign in to comment.