Add uuid to services entries

Our services ID column is still just an integer and that's *ok* for the
most part.  There are things that it would be nice to advertise a
more meaningful and unique identifier to the users of services though.

This change adds an indexable UUID column to the services, this will
be useful for things like identifying backends without leaking
details to end users.

Change-Id: I67e52c6a8634b74bf5975290298d6fbcadc7dd50
This commit is contained in:
j-griffith 2017-09-28 15:57:22 +00:00 committed by John Griffith
parent 68c668cfc8
commit 2becd847fb
4 changed files with 50 additions and 0 deletions

View File

@ -557,6 +557,9 @@ def service_get_all(context, backend_match_level=None, **filters):
@require_admin_context
def service_create(context, values):
if not values.get('uuid'):
values['uuid'] = str(uuid.uuid4())
service_ref = models.Service()
service_ref.update(values)
if not CONF.enable_new_services:

View File

@ -0,0 +1,41 @@
# 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 six
import uuid
from sqlalchemy import Column
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy import Index
from sqlalchemy import MetaData
from sqlalchemy import String
from sqlalchemy import Table
def upgrade(migrate_engine):
"""Add uuid column to services."""
meta = MetaData(bind=migrate_engine)
services = Table('services', meta, autoload=True)
if not hasattr(services.c, 'uuid'):
services.create_column(Column('uuid', String(36), nullable=True))
uuid_index_name = 'services_uuid_idx'
indexes = Inspector(migrate_engine).get_indexes('services')
if uuid_index_name not in (i['name'] for i in indexes):
services = Table('services', meta, autoload=True)
Index(uuid_index_name, services.c.uuid, unique=True).create()
service_list = list(services.select().execute())
for s in service_list:
if not s.uuid:
services.update().where(services.c.id == s.id).\
values(uuid=six.text_type(uuid.uuid4())).execute()

View File

@ -65,6 +65,7 @@ class Service(BASE, CinderBase):
__tablename__ = 'services'
id = Column(Integer, primary_key=True)
uuid = Column(String(36), nullable=True, index=True)
cluster_name = Column(String(255), nullable=True)
host = Column(String(255)) # , ForeignKey('hosts.id'))
binary = Column(String(255))

View File

@ -1289,6 +1289,11 @@ class MigrationsMixin(test_migrations.WalkVersionsMixin):
self.assertTrue(db_utils.index_exists_on_columns(
engine, 'quota_usages', ['project_id', 'resource']))
def _check_112(self, engine, data):
services = db_utils.get_table(engine, 'services')
self.assertIsInstance(services.c.uuid.type,
self.VARCHAR_TYPE)
def test_walk_versions(self):
self.walk_versions(False, False)
self.assert_each_foreign_key_is_part_of_an_index()