Skip to content

Commit

Permalink
Merge "Add support for Host header in REST queries" into stable/kilo
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenkins authored and openstack-gerrit committed Nov 30, 2015
2 parents 186a53e + bf47fe0 commit d2026cc
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 23 deletions.
2 changes: 2 additions & 0 deletions designate/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
help='Number of api worker processes to spawn'),
cfg.IntOpt('threads', default=1000,
help='Number of api greenthreads to spawn'),
cfg.BoolOpt('enable-host-header', default=False,
help='Enable host request headers'),
cfg.StrOpt('api-base-uri', default='http://127.0.0.1:9001/'),
cfg.StrOpt('api_host', default='0.0.0.0',
help='API Host'),
Expand Down
42 changes: 21 additions & 21 deletions designate/api/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import flask
from oslo.config import cfg

cfg.CONF.import_opt('enable_host_header', 'designate.api', group='service:api')


def factory(global_config, **local_conf):
app = flask.Flask('designate.api.versions')
Expand All @@ -24,34 +26,32 @@ def factory(global_config, **local_conf):

base = cfg.CONF['service:api'].api_base_uri.rstrip('/')

def _version(version, status):
if version.isdigit():
versions.append({
'id': 'v%s' % version,
'status': status,
'links': [{
'href': base + '/v' + version,
'rel': 'self'
}]
})
else:
versions.append({
'id': '%s' % version,
'status': status,
'links': [{
'href': base + '/' + version,
'rel': 'self'
}]
})
def _host_header_links():
del versions[:]
host_url = flask.request.host_url
_version('v1', 'DEPRECATED', host_url)
_version('v2', 'CURRENT', host_url)

def _version(version, status, base_uri):
versions.append({
'id': '%s' % version,
'status': status,
'links': [{
'href': base_uri + '/' + version,
'rel': 'self'
}]
})

if cfg.CONF['service:api'].enable_api_v1:
_version('1', 'DEPRECATED')
_version('v1', 'DEPRECATED', base)

if cfg.CONF['service:api'].enable_api_v2:
_version('2', 'CURRENT')
_version('v2', 'CURRENT', base)

@app.route('/', methods=['GET'])
def version_list():
if cfg.CONF['service:api'].enable_host_header:
_host_header_links()

return flask.jsonify({
"versions": {
Expand Down
21 changes: 19 additions & 2 deletions designate/objects/adapters/api_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
LOG = logging.getLogger(__name__)

cfg.CONF.import_opt('api_base_uri', 'designate.api', group='service:api')
cfg.CONF.import_opt('enable_host_header', 'designate.api', group='service:api')


class APIv2Adapter(base.DesignateAdapter):
Expand Down Expand Up @@ -78,8 +79,16 @@ def parse(cls, values, output_object, *args, **kwargs):

@classmethod
def _get_resource_links(cls, object, request):
if cfg.CONF['service:api'].enable_host_header:
try:
base_uri = request.host_url
except Exception:
base_uri = cls.BASE_URI
else:
base_uri = cls.BASE_URI

return {'self': '%s%s/%s' %
(cls.BASE_URI, cls._get_path(request), object.id)}
(base_uri, cls._get_path(request), object.id)}

@classmethod
def _get_path(cls, request):
Expand Down Expand Up @@ -128,8 +137,16 @@ def _get_collection_href(cls, request, extra_params=None):
if extra_params is not None:
params.update(extra_params)

if cfg.CONF['service:api'].enable_host_header:
try:
base_uri = request.host_url
except Exception:
base_uri = cls.BASE_URI
else:
base_uri = cls.BASE_URI

href = "%s%s?%s" % (
cls.BASE_URI,
base_uri,
cls._get_path(request),
urllib.urlencode(params))

Expand Down
52 changes: 52 additions & 0 deletions designate/tests/test_api/test_v2/test_hostheaders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (c) 2015 Rackspace Hosting
#
# Author: Mimi Lee <mimi.lee@rackspace.com>
#
# 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.

from designate.tests.test_api.test_v2 import ApiV2TestCase


class ApiV2HostHeadersTest(ApiV2TestCase):
def setUp(self):
super(ApiV2HostHeadersTest, self).setUp()

# Ensure v2 API and host headers are enabled
self.config(enable_api_v2=True, group='service:api')
self.config(enable_host_header=True, group='service:api')

def test_host_header(self):
# Create a zone with host header
fixture = self.get_domain_fixture(fixture=0)
response = self.client.post_json('/zones/',
fixture,
headers={'Host': 'testhost.com'})
# Check the headers are what we expect
self.assertEqual(202, response.status_int)
self.assertEqual('application/json', response.content_type)

# Check the host request header url
self.assertTrue('http://testhost.com/zones/' in
response.json_body['links']['self'])

# Get zone with host header
response = self.client.get('/zones/',
headers={'Host': 'testhost.com'})

# Check the headers are what we expect
self.assertEqual(200, response.status_int)
self.assertEqual('application/json', response.content_type)

# Check the host request header url
self.assertTrue('http://testhost.com/zones' in
response.json_body['links']['self'])
3 changes: 3 additions & 0 deletions etc/designate/designate.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ debug = False
# API Service
#-----------------------
[service:api]
# Enable host request headers
#enable_host_header = False

# Address to bind the API server
#api_host = 0.0.0.0

Expand Down

0 comments on commit d2026cc

Please sign in to comment.