Skip to content

Commit

Permalink
Refactor add_address_alias into dbwrappers/dns.py
Browse files Browse the repository at this point in the history
Lifting add_address_alias into dbwrappers/dns.py for reuse.

Change-Id: Ie61a8674d9def56843c8ef2d585f54230cb9e8e7
Addresses-Issue: Jira/AQUILON-6374
Reviewed-by: Tomasz Kotarba <Tomasz.Kotarba@morganstanley.com>
Tested-by: Aquilon Template Build testing and verification
  • Loading branch information
Fred Barnes authored and Fred Barnes committed Nov 20, 2019
1 parent afe2b8c commit 8def9a5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 52 deletions.
58 changes: 11 additions & 47 deletions lib/aquilon/worker/commands/add_address_alias.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- cpy-indent-level: 4; indent-tabs-mode: nil -*-
# ex: set expandtab softtabstop=4 shiftwidth=4:
#
# Copyright (C) 2008,2009,2010,2011,2012,2013,2014,2015,2016,2017,2018 Contributor
# Copyright (C) 2008-2019 Contributor
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -16,11 +16,12 @@
# limitations under the License.
"""Contains the logic for `aq add address alias`."""

from aquilon.exceptions_ import ArgumentError
from aquilon.aqdb.model import AddressAlias, Fqdn, ReservedName
from aquilon.aqdb.model import (
Fqdn,
)
from aquilon.worker.broker import BrokerCommand
from aquilon.worker.dbwrappers.grn import lookup_grn
from aquilon.worker.dbwrappers.change_management import ChangeManagement
from aquilon.worker.dbwrappers.dns import add_address_alias


class CommandAddAddressAlias(BrokerCommand):
Expand All @@ -37,17 +38,6 @@ def render(self, session, logger, fqdn, dns_environment, target,
dbfqdn = Fqdn.get_or_create(session, dns_environment=dns_environment,
fqdn=fqdn)

if dbfqdn.dns_environment.is_default and \
dbfqdn.dns_domain.name == "ms.com":
raise ArgumentError("%s record in DNS domain ms.com, DNS "
"environment %s is not allowed." %
(AddressAlias._get_class_label(),
dbfqdn.dns_environment.name))

if dbfqdn.dns_domain.restricted:
raise ArgumentError("{0} is restricted, aliases are not allowed."
.format(dbfqdn.dns_domain))

dbtarget = Fqdn.get_unique(session, fqdn=target,
dns_environment=target_environment,
compel=True)
Expand All @@ -57,37 +47,11 @@ def render(self, session, logger, fqdn, dns_environment, target,
cm.consider(dbtarget)
cm.validate()

dbgrn = None
if grn or eon_id:
dbgrn = lookup_grn(session, grn, eon_id, logger=logger,
config=self.config)

# Make sure all AddressAlias records under the same Fqdn has
# the same GRN.
for rec in dbfqdn.dns_records:
if isinstance(rec, AddressAlias):
if not dbgrn:
dbgrn = rec.owner_grn
elif dbgrn != rec.owner_grn:
raise ArgumentError("{0} with target {1} is set to a "
"different GRN."
.format(dbfqdn, rec.target.fqdn))
break

db_record = AddressAlias(fqdn=dbfqdn, target=dbtarget, ttl=ttl,
owner_grn=dbgrn, comments=comments,
require_grn=False)
session.add(db_record)

if exporter:
if any(
dr != db_record and
not isinstance(dr, ReservedName)
for dr in dbfqdn.dns_records):
exporter.update(dbfqdn)
else:
exporter.create(dbfqdn)

session.flush()
add_address_alias(session, logger, config=self.config,
dbsrcfqdn=dbfqdn,
dbtargetfqdn=dbtarget,
ttl=ttl, grn=grn, eon_id=eon_id,
comments=comments, exporter=exporter,
flush_session=True)

return
81 changes: 76 additions & 5 deletions lib/aquilon/worker/dbwrappers/dns.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@

import socket

from sqlalchemy.orm import object_session, joinedload, lazyload
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.sql import or_

from aquilon.exceptions_ import ArgumentError, AquilonError, NotFoundException
from aquilon.exceptions_ import (
AquilonError,
ArgumentError,
NotFoundException,
)
from aquilon.aqdb.model import (
AddressAlias,
AddressAssignment,
Expand All @@ -41,8 +41,17 @@
)
from aquilon.aqdb.model.dns_domain import parse_fqdn
from aquilon.aqdb.model.network import get_net_id_from_ip
from aquilon.worker.dbwrappers.grn import lookup_grn
from aquilon.worker.dbwrappers.interface import check_ip_restrictions

from sqlalchemy.orm import (
joinedload,
lazyload,
object_session,
)
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.sql import or_


def delete_dns_record(dbdns_rec, locked=False, verify_assignments=False, exporter=None):
"""
Expand Down Expand Up @@ -516,3 +525,65 @@ def set_reverse_ptr(session, logger, dbdns_rec, reverse_ptr):
raise ArgumentError(err)
else:
dbdns_rec.reverse_ptr = None


def add_address_alias(session, logger, config, dbsrcfqdn, dbtargetfqdn,
ttl, grn, eon_id, comments, exporter=None,
flush_session=False):
"""Add an address-alias record (from source and target FQDN DB objects).
Does not allow the addition of an address-alias into 'ms.com' in the
'internal' environment, nor the addition of an address-alias in restricted
DNS domains.
Ensures that all address-aliases have the same GRN where set.
Returns the newly created AddressAlias object on success, raises an
exception on error.
"""

if (dbsrcfqdn.dns_environment.is_default and
dbsrcfqdn.dns_domain.name == 'ms.com'):
raise ArgumentError("{0:s} record in DNS domain ms.com, DNS "
"environment {1:s} is not allowed."
.format(AddressAlias._get_class_label(),
dbsrcfqdn.dns_environment.name))

if dbsrcfqdn.dns_domain.restricted:
raise ArgumentError("{0} is restricted, aliases are not allowed."
.format(dbsrcfqdn.dns_domain))

# Make sure all AddressAlias records under the same FQDN have the
# same GRN (if set).
dbgrn = None
if grn or eon_id:
dbgrn = lookup_grn(session, grn, eon_id, logger=logger,
config=config)

for rec in dbsrcfqdn.dns_records:
if isinstance(rec, AddressAlias):
if not dbgrn:
dbgrn = rec.owner_grn
elif dbgrn != rec.owner_grn:
raise ArgumentError("{0} with target {1} is set to a "
"different GRN."
.format(dbsrcfqdn, rec.target.fqdn))
break # assume the rest are checked

dbaa = AddressAlias(fqdn=dbsrcfqdn, target=dbtargetfqdn, ttl=ttl,
owner_grn=dbgrn, comments=comments,
require_grn=False)
session.add(dbaa)

if exporter:
other_recs = [dr for dr in dbsrcfqdn.dns_records
if dr != dbaa and not isinstance(dr, ReservedName)]
if other_recs:
exporter.update(dbsrcfqdn)
else:
exporter.create(dbsrcfqdn)

if flush_session:
session.flush()

return dbaa

0 comments on commit 8def9a5

Please sign in to comment.