279418673a
If a method is not allowed, Deckhand currently uses convoluted logic
to throw a 405. Instead, Deckhand should rely on falcon's native
logic which already does that.
This PS relies on falcon logic to throw a 404/405 when an endpoint
is invalid or if a HTTP verb is not support for an endpoint
(respectively).
This PS also adds a VersionsResource to Deckhand to allow information
about the API versions to be returned. This implementation is modeled
after Shipyard [0].
[0] c84e91bad1/shipyard_airflow/control/api.py (L32)
Change-Id: I805801892dbe1a6bca4d0ba5d445fee554c256f8
71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
# Copyright 2017 AT&T Intellectual Property. All other rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import yaml
|
|
|
|
import falcon
|
|
from oslo_context import context
|
|
|
|
|
|
class BaseResource(object):
|
|
"""Base resource class for implementing API resources."""
|
|
|
|
def on_options(self, req, resp):
|
|
self_attrs = dir(self)
|
|
|
|
methods = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'PATCH']
|
|
allowed_methods = []
|
|
|
|
for m in methods:
|
|
if 'on_' + m.lower() in self_attrs:
|
|
allowed_methods.append(m)
|
|
|
|
resp.headers['Allow'] = ','.join(allowed_methods)
|
|
resp.status = falcon.HTTP_200
|
|
|
|
def to_yaml_body(self, dict_body):
|
|
"""Converts JSON body into YAML response body.
|
|
|
|
:param dict_body: response body to be converted to YAML.
|
|
:returns: YAML encoding of `dict_body`.
|
|
"""
|
|
if isinstance(dict_body, dict):
|
|
return yaml.safe_dump(dict_body)
|
|
elif isinstance(dict_body, list):
|
|
return yaml.safe_dump_all(dict_body)
|
|
raise TypeError('Unrecognized dict_body type when converting response '
|
|
'body to YAML format.')
|
|
|
|
|
|
class DeckhandRequest(falcon.Request):
|
|
|
|
def __init__(self, env, options=None):
|
|
super(DeckhandRequest, self).__init__(env, options)
|
|
self.context = context.RequestContext.from_environ(self.env)
|
|
|
|
@property
|
|
def project_id(self):
|
|
return self.context.tenant
|
|
|
|
@property
|
|
def user_id(self):
|
|
return self.context.user
|
|
|
|
@property
|
|
def roles(self):
|
|
return self.context.roles
|
|
|
|
def __repr__(self):
|
|
return '%s, context=%s' % (self.path, self.context)
|