"Fossies" - the Fresh Open Source Software Archive

Member "keystone-17.0.0/keystone/api/discovery.py" (13 May 2020, 4259 Bytes) of package /linux/misc/openstack/keystone-17.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "discovery.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 16.0.1_vs_17.0.0.

    1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 #    not use this file except in compliance with the License. You may obtain
    3 #    a copy of the License at
    4 #
    5 #         http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 #    Unless required by applicable law or agreed to in writing, software
    8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 #    License for the specific language governing permissions and limitations
   11 #    under the License.
   12 
   13 import flask
   14 from flask import request
   15 import http.client
   16 from oslo_serialization import jsonutils
   17 
   18 from keystone.common import json_home
   19 import keystone.conf
   20 from keystone.server import flask as ks_flask
   21 
   22 
   23 CONF = keystone.conf.CONF
   24 MEDIA_TYPE_JSON = 'application/vnd.openstack.identity-%s+json'
   25 _DISCOVERY_BLUEPRINT = flask.Blueprint('Discovery', __name__)
   26 
   27 
   28 def _get_versions_list(identity_url):
   29     versions = {}
   30     versions['v3'] = {
   31         'id': 'v3.14',
   32         'status': 'stable',
   33         'updated': '2020-04-07T00:00:00Z',
   34         'links': [{
   35             'rel': 'self',
   36             'href': identity_url,
   37         }],
   38         'media-types': [{
   39             'base': 'application/json',
   40             'type': MEDIA_TYPE_JSON % 'v3'
   41         }]
   42     }
   43     return versions
   44 
   45 
   46 class MimeTypes(object):
   47     JSON = 'application/json'
   48     JSON_HOME = 'application/json-home'
   49 
   50 
   51 def v3_mime_type_best_match():
   52     if not request.accept_mimetypes:
   53         return MimeTypes.JSON
   54 
   55     return request.accept_mimetypes.best_match(
   56         [MimeTypes.JSON, MimeTypes.JSON_HOME])
   57 
   58 
   59 @_DISCOVERY_BLUEPRINT.route('/')
   60 def get_versions():
   61     if v3_mime_type_best_match() == MimeTypes.JSON_HOME:
   62         # RENDER JSON-Home form, we have a clever client who will
   63         # understand the JSON-Home document.
   64         v3_json_home = json_home.JsonHomeResources.resources()
   65         json_home.translate_urls(v3_json_home, '/v3')
   66         return flask.Response(response=jsonutils.dumps(v3_json_home),
   67                               mimetype=MimeTypes.JSON_HOME)
   68     else:
   69         identity_url = '%s/' % ks_flask.base_url()
   70         versions = _get_versions_list(identity_url)
   71         # Set the preferred version to the latest "stable" version.
   72         # TODO(morgan): If we ever have more API versions find the latest
   73         # stable version instead of just using the "base_url", for now we
   74         # simply have a single version so use it as the preferred location.
   75         preferred_location = identity_url
   76 
   77         response = flask.Response(
   78             response=jsonutils.dumps(
   79                 {'versions': {
   80                     'values': list(versions.values())}}),
   81             mimetype=MimeTypes.JSON,
   82             status=http.client.MULTIPLE_CHOICES)
   83         response.headers['Location'] = preferred_location
   84         return response
   85 
   86 
   87 @_DISCOVERY_BLUEPRINT.route('/v3')
   88 def get_version_v3():
   89     if v3_mime_type_best_match() == MimeTypes.JSON_HOME:
   90         # RENDER JSON-Home form, we have a clever client who will
   91         # understand the JSON-Home document.
   92         content = json_home.JsonHomeResources.resources()
   93         return flask.Response(response=jsonutils.dumps(content),
   94                               mimetype=MimeTypes.JSON_HOME)
   95     else:
   96         identity_url = '%s/' % ks_flask.base_url()
   97         versions = _get_versions_list(identity_url)
   98         return flask.Response(
   99             response=jsonutils.dumps({'version': versions['v3']}),
  100             mimetype=MimeTypes.JSON)
  101 
  102 
  103 class DiscoveryAPI(object):
  104     # NOTE(morgan): The Discovery Bits are so special they cannot conform to
  105     # Flask-RESTful-isms. We are using straight flask Blueprint(s) here so that
  106     # we have a lot more control over what the heck is going on. This is just
  107     # a stub object to ensure we can load discovery in the same manner we
  108     # handle the rest of keystone.api
  109 
  110     @staticmethod
  111     def instantiate_and_register_to_app(flask_app):
  112         # This is a lot more magical than the normal setup as the discovery
  113         # API does not lean on flask-restful. We're statically building a
  114         # single blueprint here.
  115         flask_app.register_blueprint(_DISCOVERY_BLUEPRINT)
  116 
  117 
  118 APIs = (DiscoveryAPI,)