Skip to content

Commit

Permalink
Add a URL field to region table
Browse files Browse the repository at this point in the history
Allow a URL to be associated with a certain region.

implements bp keystone-to-keystone-federation

Change-Id: Ib61f9c4c9727eb5e81b7c71102133ef000be395d
  • Loading branch information
Steve Martinelli committed Aug 13, 2014
1 parent 2ea3006 commit 327f733
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 3 deletions.
1 change: 1 addition & 0 deletions keystone/catalog/backends/kvs.py
Expand Up @@ -51,6 +51,7 @@ def _check_parent_region(self, region_ref):
def create_region(self, region):
region_id = region['id']
region.setdefault('parent_region_id')
region.setdefault('url')
self._check_parent_region(region)
self.db.set('region-%s' % region_id, region)
region_list = set(self.db.get('region_list', []))
Expand Down
3 changes: 2 additions & 1 deletion keystone/catalog/backends/sql.py
Expand Up @@ -28,9 +28,10 @@

class Region(sql.ModelBase, sql.DictBase):
__tablename__ = 'region'
attributes = ['id', 'description', 'parent_region_id']
attributes = ['id', 'description', 'parent_region_id', 'url']
id = sql.Column(sql.String(64), primary_key=True)
description = sql.Column(sql.String(255), nullable=False)
url = sql.Column(sql.String(255), nullable=True)
# NOTE(jaypipes): Right now, using an adjacency list model for
# storing the hierarchy of regions is fine, since
# the API does not support any kind of querying for
Expand Down
@@ -0,0 +1,34 @@
# Copyright 2014 IBM Corp.
#
# 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 sqlalchemy as sql

_REGION_TABLE_NAME = 'region'


def upgrade(migrate_engine):
meta = sql.MetaData()
meta.bind = migrate_engine

region_table = sql.Table(_REGION_TABLE_NAME, meta, autoload=True)
url_column = sql.Column('url', sql.String(255), nullable=True)
region_table.create_column(url_column)


def downgrade(migrate_engine):
meta = sql.MetaData()
meta.bind = migrate_engine

region_table = sql.Table(_REGION_TABLE_NAME, meta, autoload=True)
region_table.drop_column('url')
7 changes: 5 additions & 2 deletions keystone/tests/test_backend.py
Expand Up @@ -3568,6 +3568,7 @@ def test_region_crud(self):
# the endpoint, with None value.
expected_region = new_region.copy()
expected_region['parent_region_id'] = None
expected_region['url'] = None
self.assertDictEqual(res, expected_region)

# Test adding another region with the one above
Expand All @@ -3578,10 +3579,12 @@ def test_region_crud(self):
new_region = {
'id': region_id,
'description': uuid.uuid4().hex,
'parent_region_id': parent_region_id
'parent_region_id': parent_region_id,
'url': uuid.uuid4().hex
}
self.catalog_api.create_region(
res = self.catalog_api.create_region(
new_region.copy())
self.assertDictEqual(new_region, res)

# list
regions = self.catalog_api.list_regions()
Expand Down
40 changes: 40 additions & 0 deletions keystone/tests/test_sql_upgrade.py
Expand Up @@ -1228,6 +1228,46 @@ def test_id_mapping(self):
self.downgrade(50)
self.assertTableDoesNotExist('id_mapping')

def test_region_url_upgrade(self):
self.upgrade(52)
self.assertTableColumns('region',
['id', 'description', 'parent_region_id',
'extra', 'url'])

def test_region_url_downgrade(self):
self.upgrade(52)
self.downgrade(51)
self.assertTableColumns('region',
['id', 'description', 'parent_region_id',
'extra'])

def test_region_url_cleanup(self):
# make sure that the url field is dropped in the downgrade
self.upgrade(52)
session = self.Session()
beta = {
'id': uuid.uuid4().hex,
'description': uuid.uuid4().hex,
'parent_region_id': uuid.uuid4().hex,
'url': uuid.uuid4().hex
}
acme = {
'id': uuid.uuid4().hex,
'description': uuid.uuid4().hex,
'parent_region_id': uuid.uuid4().hex,
'url': None
}
self.insert_dict(session, 'region', beta)
self.insert_dict(session, 'region', acme)
region_table = sqlalchemy.Table('region', self.metadata, autoload=True)
self.assertEqual(2, session.query(region_table).count())
self.downgrade(51)
self.metadata.clear()
region_table = sqlalchemy.Table('region', self.metadata, autoload=True)
self.assertEqual(2, session.query(region_table).count())
region = session.query(region_table)[0]
self.assertRaises(AttributeError, getattr, region, 'url')

def populate_user_table(self, with_pass_enab=False,
with_pass_enab_domain=False):
# Populate the appropriate fields in the user
Expand Down
42 changes: 42 additions & 0 deletions keystone/tests/test_v3_catalog.py
Expand Up @@ -251,6 +251,48 @@ def test_delete_region(self):
self.delete('/regions/%(region_id)s' % {
'region_id': self.region_id})

def test_create_region_with_url(self):
"""Call ``POST /regions`` with a custom url field."""
ref = self.new_region_ref()
ref['url'] = 'http://beta.com:5000/v3'

r = self.post(
'/regions',
body={'region': ref},
expected_status=201)

self.assertEqual(ref['url'], r.json['region']['url'])
self.assertValidRegionResponse(r, ref)

r = self.get(
'/regions/%(region_id)s' % {
'region_id': ref['id']})
self.assertEqual(ref['url'], r.json['region']['url'])
self.assertValidRegionResponse(r, ref)

def test_update_region_with_url(self):
"""Call ``PUT /regions`` with a custom url field."""
ref = self.new_region_ref()

r = self.post(
'/regions',
body={'region': ref},
expected_status=201)

self.assertIsNone(r.json['region']['url'])
self.assertValidRegionResponse(r, ref)

region_id = r.json['region']['id']
ref = self.new_region_ref()
del ref['id']
ref['url'] = 'http://beta.com:5000/v3'

r = self.patch('/regions/%(region_id)s' % {
'region_id': region_id},
body={'region': ref})
self.assertEqual(ref['url'], r.json['region']['url'])
self.assertValidRegionResponse(r, ref)

# service crud tests

def test_create_service(self):
Expand Down

0 comments on commit 327f733

Please sign in to comment.