From 286af7c3f089bca679eaa18ea74c407dc37a0816 Mon Sep 17 00:00:00 2001 From: Victor Morales Date: Wed, 28 Sep 2016 10:16:53 -0500 Subject: [PATCH] Relocate ProviderResourceAssociation DB models This patch will separate ProviderResourceAssociation db models from mixins for Oslo-Versioned Objects implementation work. Change-Id: If650e0f6bdc41d175ba8be646cd043c875894ed7 Partial-Bug: #1597913 --- neutron/db/migration/models/head.py | 1 - neutron/db/models/servicetype.py | 27 +++++++++++++++++ neutron/db/servicetype_db.py | 30 +++++++++---------- .../tests/unit/extensions/test_servicetype.py | 5 ++-- 4 files changed, 44 insertions(+), 19 deletions(-) create mode 100644 neutron/db/models/servicetype.py diff --git a/neutron/db/migration/models/head.py b/neutron/db/migration/models/head.py index ecca9a5540d..a63fbbc12d7 100644 --- a/neutron/db/migration/models/head.py +++ b/neutron/db/migration/models/head.py @@ -48,7 +48,6 @@ from neutron.db.qos import models as qos_models # noqa from neutron.db.quota import models as quota_models # noqa from neutron.db import rbac_db_models # noqa from neutron.db import segments_db # noqa -from neutron.db import servicetype_db # noqa from neutron.db import tag_db # noqa from neutron.ipam.drivers.neutrondb_ipam import db_models # noqa from neutron.plugins.ml2.drivers import type_geneve # noqa diff --git a/neutron/db/models/servicetype.py b/neutron/db/models/servicetype.py new file mode 100644 index 00000000000..b5fe044d486 --- /dev/null +++ b/neutron/db/models/servicetype.py @@ -0,0 +1,27 @@ +# Copyright 2013 OpenStack Foundation. +# All 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. + +from neutron_lib.db import model_base +import sqlalchemy as sa + +from neutron.api.v2 import attributes as attr + + +class ProviderResourceAssociation(model_base.BASEV2): + provider_name = sa.Column(sa.String(attr.NAME_MAX_LEN), + nullable=False, primary_key=True) + # should be manually deleted on resource deletion + resource_id = sa.Column(sa.String(36), nullable=False, primary_key=True, + unique=True) diff --git a/neutron/db/servicetype_db.py b/neutron/db/servicetype_db.py index 3d9a0bc2582..d873d49f102 100644 --- a/neutron/db/servicetype_db.py +++ b/neutron/db/servicetype_db.py @@ -15,22 +15,15 @@ from itertools import chain -from neutron_lib.db import model_base from oslo_log import log as logging -import sqlalchemy as sa -from neutron.api.v2 import attributes as attr +from neutron.common import _deprecate +from neutron.db.models import servicetype as st_model from neutron.services import provider_configuration as pconf LOG = logging.getLogger(__name__) - -class ProviderResourceAssociation(model_base.BASEV2): - provider_name = sa.Column(sa.String(attr.NAME_MAX_LEN), - nullable=False, primary_key=True) - # should be manually deleted on resource deletion - resource_id = sa.Column(sa.String(36), nullable=False, primary_key=True, - unique=True) +_deprecate._moved_global('ProviderResourceAssociation', new_module=st_model) class ServiceTypeManager(object): @@ -80,8 +73,9 @@ class ServiceTypeManager(object): def get_provider_names_by_resource_ids(self, context, resource_ids): query = ( - context.session.query(ProviderResourceAssociation). - filter(ProviderResourceAssociation.resource_id.in_(resource_ids))) + context.session.query(st_model.ProviderResourceAssociation). + filter(st_model.ProviderResourceAssociation.resource_id.in_( + resource_ids))) return {rec.resource_id: rec.provider_name for rec in query} def add_resource_association(self, context, service_type, provider_name, @@ -96,8 +90,8 @@ class ServiceTypeManager(object): # we don't actually need service type for association. # resource_id is unique and belongs to specific service # which knows its type - assoc = ProviderResourceAssociation(provider_name=provider_name, - resource_id=resource_id) + assoc = st_model.ProviderResourceAssociation( + provider_name=provider_name, resource_id=resource_id) context.session.add(assoc) # NOTE(blogan): the ProviderResourceAssociation relationship will not # be populated if a resource was created before this. The expire_all @@ -112,7 +106,11 @@ class ServiceTypeManager(object): if not resource_ids: return with context.session.begin(subtransactions=True): - (context.session.query(ProviderResourceAssociation). + (context.session.query(st_model.ProviderResourceAssociation). filter( - ProviderResourceAssociation.resource_id.in_(resource_ids)). + st_model.ProviderResourceAssociation.resource_id.in_( + resource_ids)). delete(synchronize_session='fetch')) + + +_deprecate._MovedGlobals() diff --git a/neutron/tests/unit/extensions/test_servicetype.py b/neutron/tests/unit/extensions/test_servicetype.py index 05c173e4252..9afe3c6732a 100644 --- a/neutron/tests/unit/extensions/test_servicetype.py +++ b/neutron/tests/unit/extensions/test_servicetype.py @@ -21,6 +21,7 @@ import webtest from neutron.api import extensions from neutron import context +from neutron.db.models import servicetype as st_model from neutron.db import servicetype_db as st_db from neutron.extensions import servicetype from neutron.plugins.common import constants @@ -135,9 +136,9 @@ class ServiceTypeManagerTestCase(testlib_api.SqlTestCase): constants.LOADBALANCER, 'lbaas1', '123-123') self.assertEqual(ctx.session. - query(st_db.ProviderResourceAssociation).count(), + query(st_model.ProviderResourceAssociation).count(), 1) - assoc = ctx.session.query(st_db.ProviderResourceAssociation).one() + assoc = ctx.session.query(st_model.ProviderResourceAssociation).one() ctx.session.delete(assoc) def test_invalid_resource_association(self):