Relocate DNS db models

This patch relocate db models for dns.

Partial-Bug: #1597913

Change-Id: I6d1b43d8c5c8d6f8b80730a16eedac5e59cbed44
This commit is contained in:
Manjeet Singh Bhatia 2016-06-27 22:33:31 +00:00
parent 0f13da179c
commit ead39faa37
6 changed files with 146 additions and 116 deletions

View File

@ -14,17 +14,15 @@
# under the License.
from neutron_lib.api import validators
from neutron_lib.db import model_base
from neutron_lib import exceptions as n_exc
from oslo_config import cfg
from oslo_log import log as logging
import sqlalchemy as sa
from sqlalchemy import orm
from neutron._i18n import _, _LE
from neutron.common import _deprecate
from neutron.common import utils
from neutron.db import db_base_plugin_v2
from neutron.db.models import l3 as l3_models
from neutron.db.models import dns as dns_models
from neutron.db import models_v2
from neutron.extensions import dns
from neutron.extensions import l3
@ -33,78 +31,9 @@ from neutron.services.externaldns import driver
LOG = logging.getLogger(__name__)
class NetworkDNSDomain(model_base.BASEV2):
network_id = sa.Column(sa.String(36),
sa.ForeignKey('networks.id', ondelete="CASCADE"),
primary_key=True,
index=True)
dns_domain = sa.Column(sa.String(255),
nullable=False)
# Add a relationship to the Network model in order to instruct
# SQLAlchemy to eagerly load this association
network = orm.relationship(models_v2.Network,
backref=orm.backref("dns_domain",
lazy='joined',
uselist=False,
cascade='delete'))
revises_on_change = ('network', )
class FloatingIPDNS(model_base.BASEV2):
__tablename__ = 'floatingipdnses'
floatingip_id = sa.Column(sa.String(36),
sa.ForeignKey('floatingips.id',
ondelete="CASCADE"),
primary_key=True,
index=True)
dns_name = sa.Column(sa.String(255),
nullable=False)
dns_domain = sa.Column(sa.String(255),
nullable=False)
published_dns_name = sa.Column(sa.String(255),
nullable=False)
published_dns_domain = sa.Column(sa.String(255),
nullable=False)
# Add a relationship to the FloatingIP model in order to instruct
# SQLAlchemy to eagerly load this association
floatingip = orm.relationship(l3_models.FloatingIP,
backref=orm.backref("dns",
lazy='joined',
uselist=False,
cascade='delete'))
revises_on_change = ('floatingip', )
class PortDNS(model_base.BASEV2):
__tablename__ = 'portdnses'
port_id = sa.Column(sa.String(36),
sa.ForeignKey('ports.id',
ondelete="CASCADE"),
primary_key=True,
index=True)
current_dns_name = sa.Column(sa.String(255),
nullable=False)
current_dns_domain = sa.Column(sa.String(255),
nullable=False)
previous_dns_name = sa.Column(sa.String(255),
nullable=False)
previous_dns_domain = sa.Column(sa.String(255),
nullable=False)
dns_name = sa.Column(sa.String(255), nullable=False)
# Add a relationship to the Port model in order to instruct
# SQLAlchemy to eagerly load this association
port = orm.relationship(models_v2.Port,
backref=orm.backref("dns",
lazy='joined',
uselist=False,
cascade='delete'))
revises_on_change = ('port', )
_deprecate._moved_global('PortDNS', new_module=dns_models)
_deprecate._moved_global('NetworkDNSDomain', new_module=dns_models)
_deprecate._moved_global('FloatingIPDNS', new_module=dns_models)
class DNSActionsData(object):
@ -168,7 +97,7 @@ class DNSDbMixin(object):
context, floatingip_data, req_data))
dns_actions_data = None
if current_dns_name and current_dns_domain:
context.session.add(FloatingIPDNS(
context.session.add(dns_models.FloatingIPDNS(
floatingip_id=floatingip_data['id'],
dns_name=req_data[dns.DNSNAME],
dns_domain=req_data[dns.DNSDOMAIN],
@ -199,8 +128,9 @@ class DNSDbMixin(object):
return
if not self.dns_driver:
return
dns_data_db = context.session.query(FloatingIPDNS).filter_by(
floatingip_id=floatingip_data['id']).one_or_none()
dns_data_db = context.session.query(
dns_models.FloatingIPDNS).filter_by(
floatingip_id=floatingip_data['id']).one_or_none()
if dns_data_db and dns_data_db['dns_name']:
# dns_name and dns_domain assigned for floating ip. It doesn't
# matter whether they are defined for internal port
@ -225,7 +155,7 @@ class DNSDbMixin(object):
else:
return
if current_dns_name and current_dns_domain:
context.session.add(FloatingIPDNS(
context.session.add(dns_models.FloatingIPDNS(
floatingip_id=floatingip_data['id'],
dns_name='',
dns_domain='',
@ -254,8 +184,9 @@ class DNSDbMixin(object):
if not utils.is_extension_supported(self._core_plugin,
dns.Dns.get_alias()):
return
dns_data_db = context.session.query(FloatingIPDNS).filter_by(
floatingip_id=floatingip_data['id']).one_or_none()
dns_data_db = context.session.query(
dns_models.FloatingIPDNS).filter_by(
floatingip_id=floatingip_data['id']).one_or_none()
if dns_data_db:
self._delete_floatingip_from_external_dns_service(
context, dns_data_db['published_dns_domain'],
@ -271,14 +202,15 @@ class DNSDbMixin(object):
raise n_exc.BadRequest(resource='floatingip', msg=msg)
def _get_internal_port_dns_data(self, context, floatingip_data):
port_dns = context.session.query(PortDNS).filter_by(
port_dns = context.session.query(dns_models.PortDNS).filter_by(
port_id=floatingip_data['port_id']).one_or_none()
if not (port_dns and port_dns['dns_name']):
return None, None
net_dns = context.session.query(NetworkDNSDomain).join(
models_v2.Port, NetworkDNSDomain.network_id ==
models_v2.Port.network_id).filter_by(
id=floatingip_data['port_id']).one_or_none()
net_dns = context.session.query(
dns_models.NetworkDNSDomain).join(
models_v2.Port, dns_models.NetworkDNSDomain.network_id ==
models_v2.Port.network_id).filter_by(
id=floatingip_data['port_id']).one_or_none()
if not net_dns:
return port_dns['dns_name'], None
return port_dns['dns_name'], net_dns['dns_domain']
@ -327,3 +259,6 @@ class DNSDbMixin(object):
{"name": dns_name,
"domain": dns_domain,
"message": e.msg})
_deprecate._MovedGlobals()

View File

@ -28,7 +28,6 @@ from neutron_lib.db import model_base
from neutron.common import utils
from neutron.db import agents_db # noqa
from neutron.db import agentschedulers_db # noqa
from neutron.db import dns_db # noqa
from neutron.db.extra_dhcp_opt import models as edo_models # noqa
from neutron.db import l3_dvrscheduler_db # noqa
from neutron.db import l3_gwmode_db # noqa

92
neutron/db/models/dns.py Normal file
View File

@ -0,0 +1,92 @@
# 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 sqlalchemy import orm
from neutron.db.models import l3 as l3_models
from neutron.db import models_v2
class NetworkDNSDomain(model_base.BASEV2):
network_id = sa.Column(sa.String(36),
sa.ForeignKey('networks.id', ondelete="CASCADE"),
primary_key=True,
index=True)
dns_domain = sa.Column(sa.String(255),
nullable=False)
# Add a relationship to the Network model in order to instruct
# SQLAlchemy to eagerly load this association
network = orm.relationship(models_v2.Network,
backref=orm.backref("dns_domain",
lazy='joined',
uselist=False,
cascade='delete'))
revises_on_change = ('network', )
class FloatingIPDNS(model_base.BASEV2):
__tablename__ = 'floatingipdnses'
floatingip_id = sa.Column(sa.String(36),
sa.ForeignKey('floatingips.id',
ondelete="CASCADE"),
primary_key=True,
index=True)
dns_name = sa.Column(sa.String(255),
nullable=False)
dns_domain = sa.Column(sa.String(255),
nullable=False)
published_dns_name = sa.Column(sa.String(255),
nullable=False)
published_dns_domain = sa.Column(sa.String(255),
nullable=False)
# Add a relationship to the FloatingIP model in order to instruct
# SQLAlchemy to eagerly load this association
floatingip = orm.relationship(l3_models.FloatingIP,
backref=orm.backref("dns",
lazy='joined',
uselist=False,
cascade='delete'))
revises_on_change = ('floatingip', )
class PortDNS(model_base.BASEV2):
__tablename__ = 'portdnses'
port_id = sa.Column(sa.String(36),
sa.ForeignKey('ports.id',
ondelete="CASCADE"),
primary_key=True,
index=True)
current_dns_name = sa.Column(sa.String(255),
nullable=False)
current_dns_domain = sa.Column(sa.String(255),
nullable=False)
previous_dns_name = sa.Column(sa.String(255),
nullable=False)
previous_dns_domain = sa.Column(sa.String(255),
nullable=False)
dns_name = sa.Column(sa.String(255), nullable=False)
# Add a relationship to the Port model in order to instruct
# SQLAlchemy to eagerly load this association
port = orm.relationship(models_v2.Port,
backref=orm.backref("dns",
lazy='joined',
uselist=False,
cascade='delete'))
revises_on_change = ('port', )

View File

@ -21,7 +21,7 @@ from oslo_versionedobjects import fields as obj_fields
from neutron.common import utils
from neutron.db import api as db_api
from neutron.db import dns_db as dns_models
from neutron.db.models import dns as dns_models
from neutron.db.models import securitygroup as sg_models
from neutron.db import models_v2
from neutron.db.qos import models as qos_models

View File

@ -21,7 +21,7 @@ from neutron._i18n import _LE, _LI
from neutron.callbacks import events
from neutron.callbacks import registry
from neutron.callbacks import resources
from neutron.db import dns_db
from neutron.db.models import dns as dns_models
from neutron.db import models_v2
from neutron.db import segments_db
from neutron.extensions import dns
@ -47,7 +47,7 @@ class DNSExtensionDriver(api.ExtensionDriver):
return
if dns_domain:
plugin_context.session.add(dns_db.NetworkDNSDomain(
plugin_context.session.add(dns_models.NetworkDNSDomain(
network_id=db_data['id'], dns_domain=dns_domain))
db_data[dns.DNSDOMAIN] = dns_domain
@ -63,7 +63,7 @@ class DNSExtensionDriver(api.ExtensionDriver):
net_id = db_data['id']
if current_dns_domain:
net_dns_domain = plugin_context.session.query(
dns_db.NetworkDNSDomain).filter_by(network_id=net_id).one()
dns_models.NetworkDNSDomain).filter_by(network_id=net_id).one()
if new_value:
net_dns_domain['dns_domain'] = new_value
db_data[dns.DNSDOMAIN] = new_value
@ -71,7 +71,7 @@ class DNSExtensionDriver(api.ExtensionDriver):
plugin_context.session.delete(net_dns_domain)
db_data[dns.DNSDOMAIN] = ''
elif new_value:
plugin_context.session.add(dns_db.NetworkDNSDomain(
plugin_context.session.add(dns_models.NetworkDNSDomain(
network_id=net_id, dns_domain=new_value))
db_data[dns.DNSDOMAIN] = new_value
@ -91,7 +91,7 @@ class DNSExtensionDriver(api.ExtensionDriver):
current_dns_name = dns_name
current_dns_domain = network[dns.DNSDOMAIN]
plugin_context.session.add(dns_db.PortDNS(
plugin_context.session.add(dns_models.PortDNS(
port_id=db_data['id'],
current_dns_name=current_dns_name,
current_dns_domain=current_dns_domain,
@ -101,8 +101,9 @@ class DNSExtensionDriver(api.ExtensionDriver):
def _update_dns_db(self, dns_name, dns_domain, db_data,
plugin_context, has_fixed_ips):
dns_data_db = plugin_context.session.query(dns_db.PortDNS).filter_by(
port_id=db_data['id']).one_or_none()
dns_data_db = plugin_context.session.query(
dns_models.PortDNS).filter_by(
port_id=db_data['id']).one_or_none()
if dns_data_db:
is_dns_name_changed = (dns_name is not None and
dns_data_db['current_dns_name'] != dns_name)
@ -123,12 +124,13 @@ class DNSExtensionDriver(api.ExtensionDriver):
return dns_data_db
if dns_name:
dns_data_db = dns_db.PortDNS(port_id=db_data['id'],
current_dns_name=dns_name,
current_dns_domain=dns_domain,
previous_dns_name='',
previous_dns_domain='',
dns_name=dns_name)
dns_data_db = dns_models.PortDNS(
port_id=db_data['id'],
current_dns_name=dns_name,
current_dns_domain=dns_domain,
previous_dns_name='',
previous_dns_domain='',
dns_name=dns_name)
plugin_context.session.add(dns_data_db)
return dns_data_db
@ -161,18 +163,20 @@ class DNSExtensionDriver(api.ExtensionDriver):
dns_data_db)
def _process_only_dns_name_update(self, plugin_context, db_data, dns_name):
dns_data_db = plugin_context.session.query(dns_db.PortDNS).filter_by(
port_id=db_data['id']).one_or_none()
dns_data_db = plugin_context.session.query(
dns_models.PortDNS).filter_by(
port_id=db_data['id']).one_or_none()
if dns_data_db:
dns_data_db['dns_name'] = dns_name
return dns_data_db
if dns_name:
dns_data_db = dns_db.PortDNS(port_id=db_data['id'],
current_dns_name='',
current_dns_domain='',
previous_dns_name='',
previous_dns_domain='',
dns_name=dns_name)
dns_data_db = dns_models.PortDNS(
port_id=db_data['id'],
current_dns_name='',
current_dns_domain='',
previous_dns_name='',
previous_dns_domain='',
dns_name=dns_name)
plugin_context.session.add(dns_data_db)
return dns_data_db
@ -338,7 +342,7 @@ def _create_port_in_external_dns_service(resource, event, trigger, **kwargs):
return
context = kwargs['context']
port = kwargs['port']
dns_data_db = context.session.query(dns_db.PortDNS).filter_by(
dns_data_db = context.session.query(dns_models.PortDNS).filter_by(
port_id=port['id']).one_or_none()
if not (dns_data_db and dns_data_db['current_dns_name']):
return
@ -394,7 +398,7 @@ def _update_port_in_external_dns_service(resource, event, trigger, **kwargs):
if (updated_port[dns.DNSNAME] == original_port[dns.DNSNAME] and
not original_port[dns.DNSNAME]):
return
dns_data_db = context.session.query(dns_db.PortDNS).filter_by(
dns_data_db = context.session.query(dns_models.PortDNS).filter_by(
port_id=updated_port['id']).one_or_none()
if not (dns_data_db and (dns_data_db['previous_dns_name'] or dns_data_db[
'current_dns_name'])):
@ -416,7 +420,7 @@ def _delete_port_in_external_dns_service(resource, event, trigger, **kwargs):
return
context = kwargs['context']
port_id = kwargs['port_id']
dns_data_db = context.session.query(dns_db.PortDNS).filter_by(
dns_data_db = context.session.query(dns_models.PortDNS).filter_by(
port_id=port_id).one_or_none()
if not dns_data_db:
return

View File

@ -21,7 +21,7 @@ from neutron_lib import constants
import testtools
from neutron import context
from neutron.db import dns_db
from neutron.db.models import dns as dns_models
from neutron.extensions import dns
from neutron.extensions import providernet as pnet
from neutron import manager
@ -96,7 +96,7 @@ class DNSIntegrationTestCase(test_plugin.Ml2PluginV2TestCase):
port = self.deserialize(self.fmt, res)['port']
ctx = context.get_admin_context()
dns_data_db = ctx.session.query(
dns_db.PortDNS).filter_by(
dns_models.PortDNS).filter_by(
port_id=port['id']).one_or_none()
return network['network'], port, dns_data_db
@ -138,7 +138,7 @@ class DNSIntegrationTestCase(test_plugin.Ml2PluginV2TestCase):
port = self.deserialize(self.fmt, res)['port']
ctx = context.get_admin_context()
dns_data_db = ctx.session.query(
dns_db.PortDNS).filter_by(
dns_models.PortDNS).filter_by(
port_id=port['id']).one_or_none()
return port, dns_data_db