本文整理汇总了Python中aquilon.aqdb.model.NetworkEnvironment.get_unique_or_default方法的典型用法代码示例。如果您正苦于以下问题:Python NetworkEnvironment.get_unique_or_default方法的具体用法?Python NetworkEnvironment.get_unique_or_default怎么用?Python NetworkEnvironment.get_unique_or_default使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aquilon.aqdb.model.NetworkEnvironment
的用法示例。
在下文中一共展示了NetworkEnvironment.get_unique_or_default方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_net_id_from_ip
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def get_net_id_from_ip(session, ip, network_environment=None):
"""Requires a session, and will return the Network for a given ip."""
if ip is None:
return None
if isinstance(network_environment, NetworkEnvironment):
dbnet_env = network_environment
else:
dbnet_env = NetworkEnvironment.get_unique_or_default(session,
network_environment)
# Query the last network having an address smaller than the given ip. There
# is no guarantee that the returned network does in fact contain the given
# ip, so this must be checked separately.
subq = session.query(Network.ip)
subq = subq.filter_by(network_environment=dbnet_env)
subq = subq.filter(Network.ip <= ip)
subq = subq.order_by(desc(Network.ip)).limit(1)
q = session.query(Network)
q = q.filter_by(network_environment=dbnet_env)
q = q.filter(Network.ip == subq.as_scalar())
net = q.first()
if not net or not ip in net.network:
raise NotFoundException("Could not determine network containing IP "
"address %s." % ip)
return net
示例2: del_dynamic_range
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def del_dynamic_range(self, session, logger, startip, endip):
dbnet_env = NetworkEnvironment.get_unique_or_default(session)
startnet = get_net_id_from_ip(session, startip, dbnet_env)
endnet = get_net_id_from_ip(session, endip, dbnet_env)
if startnet != endnet:
raise ArgumentError("IP addresses %s (%s) and %s (%s) must be "
"on the same subnet." %
(startip, startnet.ip, endip, endnet.ip))
q = session.query(ARecord)
q = q.filter_by(network=startnet)
q = q.filter(ARecord.ip >= startip)
q = q.filter(ARecord.ip <= endip)
q = q.order_by(ARecord.ip)
q = q.options(joinedload('fqdn'),
joinedload('fqdn.aliases'),
joinedload('fqdn.srv_records'),
joinedload('reverse_ptr'))
existing = q.all()
if not existing:
raise ArgumentError("Nothing found in range.")
if existing[0].ip != startip:
raise ArgumentError("No system found with IP address %s." % startip)
if existing[-1].ip != endip:
raise ArgumentError("No system found with IP address %s." % endip)
invalid = [s for s in existing if s.dns_record_type != 'dynamic_stub']
if invalid:
raise ArgumentError("The range contains non-dynamic systems:\n" +
"\n".join([format(i, "a") for i in invalid]))
self.del_dynamic_stubs(session, logger, existing)
示例3: render
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def render(self, session, gateway, ip, netmask, prefixlen, network_environment, comments, **arguments):
dbnet_env = NetworkEnvironment.get_unique_or_default(session, network_environment)
dbnetwork = get_net_id_from_ip(session, gateway, dbnet_env)
if netmask:
dest = IPv4Network("%s/%s" % (ip, netmask))
else:
dest = IPv4Network("%s/%s" % (ip, prefixlen))
# TODO: this will have to be changed if we want equal cost multipath
# etc.
for route in dbnetwork.static_routes:
if dest.overlaps(route.destination):
raise ArgumentError(
"{0} already has an overlapping route to "
"{1} using gateway {2}.".format(dbnetwork, route.destination, route.gateway_ip)
)
route = StaticRoute(
network=dbnetwork, dest_ip=dest.ip, dest_cidr=dest.prefixlen, gateway_ip=gateway, comments=comments
)
session.add(route)
session.flush()
# TODO: refresh affected host templates
return
示例4: render
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def render(self, session, dbuser,
ip, fqdn, network_environment, **arguments):
dbnet_env = NetworkEnvironment.get_unique_or_default(session,
network_environment)
self.az.check_network_environment(dbuser, dbnet_env)
if fqdn:
dbdns_rec = ARecord.get_unique(session, fqdn=fqdn,
dns_environment=dbnet_env.dns_environment,
compel=True)
ip = dbdns_rec.ip
elif not ip:
raise ArgumentError("Please specify either --ip or --fqdn.")
dbnetwork = get_net_id_from_ip(session, ip, dbnet_env)
dbrouter = None
for rtaddr in dbnetwork.routers:
if rtaddr.ip == ip:
dbrouter = rtaddr
break
if not dbrouter:
raise NotFoundException("IP address {0} is not a router on "
"{1:l}.".format(ip, dbnetwork))
map(delete_dns_record, dbrouter.dns_records)
dbnetwork.routers.remove(dbrouter)
session.flush()
# TODO: update the templates of Zebra hosts on the network
return
示例5: render
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def render(self, session, ip, fqdn, all, network_environment, **arguments):
dbnet_env = NetworkEnvironment.get_unique_or_default(session,
network_environment)
q = session.query(RouterAddress)
q = q.join(Network)
q = q.filter_by(network_environment=dbnet_env)
q = q.options(contains_eager('network'))
q = q.reset_joinpoint()
q = q.options(undefer(RouterAddress.comments))
q = q.options(joinedload('location'))
q = q.options(joinedload('dns_records'))
if all:
return q.all()
if fqdn:
dbdns_rec = ARecord.get_unique(session, fqdn=fqdn, compel=True)
ip = dbdns_rec.ip
errmsg = "named %s" % fqdn
elif ip:
errmsg = "with IP address %s" % ip
else:
raise ArgumentError("Please specify either --ip or --fqdn.")
q = q.filter(RouterAddress.ip == ip)
try:
return q.one()
except NoResultFound:
raise NotFoundException("Router %s not found." % errmsg)
示例6: render
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def render(self, session, gateway, ip, netmask, prefixlen,
network_environment, **arguments):
dbnet_env = NetworkEnvironment.get_unique_or_default(session,
network_environment)
dbnetwork = get_net_id_from_ip(session, gateway, dbnet_env)
if netmask:
dest = IPv4Network("%s/%s" % (ip, netmask))
else:
dest = IPv4Network("%s/%s" % (ip, prefixlen))
q = session.query(StaticRoute)
q = q.filter_by(network=dbnetwork)
q = q.filter_by(gateway_ip=gateway)
q = q.filter_by(dest_ip=dest.ip)
q = q.filter_by(dest_cidr=dest.prefixlen)
try:
dbroute = q.one()
except NoResultFound:
raise NotFoundException("Static Route to {0} using gateway {1} "
"not found.".format(dest, gateway))
session.delete(dbroute)
session.flush()
# TODO: refresh affected host templates
return
示例7: render
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def render(self, session, network, ip, network_environment, all, style,
type=False, hosts=False, **arguments):
options = [undefer('comments'), joinedload('location')]
if hosts or style == "proto":
options.extend([subqueryload("assignments"),
joinedload("assignments.interface"),
joinedload("assignments.dns_records"),
subqueryload("dynamic_stubs")])
dbnet_env = NetworkEnvironment.get_unique_or_default(session,
network_environment)
dbnetwork = network and get_network_byname(session, network, dbnet_env,
query_options=options) or None
dbnetwork = ip and get_network_byip(session, ip, dbnet_env,
query_options=options) or dbnetwork
q = session.query(Network)
q = q.filter_by(network_environment=dbnet_env)
q = q.options(*options)
if dbnetwork:
if hosts:
return NetworkHostList([dbnetwork])
else:
return dbnetwork
if type:
q = q.filter_by(network_type=type)
dblocation = get_location(session, **arguments)
if dblocation:
childids = dblocation.offspring_ids()
q = q.filter(Network.location_id.in_(childids))
q = q.order_by(Network.ip)
q = q.options(*options)
if hosts:
return NetworkHostList(q.all())
else:
return SimpleNetworkList(q.all())
示例8: render
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def render(self, session, dbuser,
ip, netmask, prefixlen, network_environment, **arguments):
if netmask:
# There must me a faster way, but this is the easy one
net = IPv4Network("127.0.0.0/%s" % netmask)
prefixlen = net.prefixlen
if prefixlen is None or prefixlen < 8 or prefixlen > 31:
raise ArgumentError("The prefix length must be between 8 and 31.")
dbnet_env = NetworkEnvironment.get_unique_or_default(session,
network_environment)
self.az.check_network_environment(dbuser, dbnet_env)
dbnetwork = get_net_id_from_ip(session, ip,
network_environment=dbnet_env)
if prefixlen >= dbnetwork.cidr:
raise ArgumentError("The specified --prefixlen must be smaller "
"than the current value.")
# IPv4Network has a supernet() object, but that does not normalize the
# IP address, i.e. IPv4Network('1.2.3.0/24').supernet() will return
# IPv4Network('1.2.3.0/23'). Do the normalization manually.
supernet = dbnetwork.network.supernet(new_prefix=prefixlen)
supernet = IPv4Network("%s/%d" % (supernet.network, supernet.prefixlen))
q = session.query(Network)
q = q.filter_by(network_environment=dbnet_env)
q = q.filter(and_(Network.ip >= supernet.ip,
Network.ip < supernet.broadcast))
q = q.order_by(Network.ip)
dbnets = q.all()
if dbnets[0].ip == supernet.ip:
dbsuper = dbnets.pop(0)
dbsuper.cidr = prefixlen
else:
# Create a new network, copying the parameters from the one
# specified on the command line
dbsuper = Network(name=dbnetwork.name, network=supernet,
network_environment=dbnet_env,
location=dbnetwork.location,
side=dbnetwork.side,
comments=dbnetwork.comments)
session.add(dbsuper)
for oldnet in dbnets:
# Delete routers of the old subnets
for dbrouter in oldnet.routers:
map(delete_dns_record, dbrouter.dns_records)
oldnet.routers = []
fix_foreign_links(session, oldnet, dbsuper)
session.delete(oldnet)
session.flush()
示例9: del_dynamic_network
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def del_dynamic_network(self, session, logger, network):
dbnet_env = NetworkEnvironment.get_unique_or_default(session)
dbnetwork = Network.get_unique(session, network,
network_environment=dbnet_env,
compel=True)
q = session.query(DynamicStub)
q = q.filter_by(network=dbnetwork)
q = q.order_by(asc(DynamicStub.ip))
existing = q.all()
if not existing:
raise ArgumentError("No dynamic stubs found on network.")
self.del_dynamic_stubs(session, logger, existing)
示例10: poll_vlan
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def poll_vlan(self, session, logger, switch, now, ssh_args):
if not switch.primary_ip:
raise ArgumentError("Cannot poll VLAN info for {0:l} without "
"a registered IP address.".format(switch))
session.query(ObservedVlan).filter_by(switch=switch).delete()
session.flush()
# Restrict operations to the internal network
dbnet_env = NetworkEnvironment.get_unique_or_default(session)
args = []
if ssh_args:
args.extend(ssh_args)
args.append(self.config.get("broker", "vlan2net"))
args.append("-ip")
args.append(switch.primary_ip)
out = run_command(args)
try:
reader = DictReader(StringIO(out))
for row in reader:
vlan = row.get("vlan", None)
network = row.get("network", None)
bitmask = row.get("bitmask", None)
if vlan is None or network is None or bitmask is None or \
len(vlan) == 0 or len(network) == 0 or len(bitmask) == 0:
logger.info("Missing value for vlan, network or bitmask in "
"output line #%d: %s" % (reader.line_num, row))
continue
try:
vlan_int = int(vlan)
except ValueError, e:
logger.info("Error parsing vlan number in output "
"line #%d: %s error: %s" %
(reader.line_num, row, e))
continue
try:
network = force_ipv4("network", network)
except ArgumentError, e:
raise InternalError(e)
try:
bitmask_int = int(bitmask)
except ValueError, e:
logger.info("Error parsing bitmask in output "
"line #%d: %s error: %s" %
(reader.line_num, row, e))
continue
示例11: render
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def render(self, session, dbuser,
fqdn, building, ip, network_environment, comments, **arguments):
dbnet_env = NetworkEnvironment.get_unique_or_default(session,
network_environment)
self.az.check_network_environment(dbuser, dbnet_env)
if building:
dbbuilding = Building.get_unique(session, building, compel=True)
else:
dbbuilding = None
(short, dbdns_domain) = parse_fqdn(session, fqdn)
dbfqdn = Fqdn.get_or_create(session, name=short,
dns_domain=dbdns_domain,
dns_environment=dbnet_env.dns_environment)
if ip:
dbnetwork = get_net_id_from_ip(session, ip, dbnet_env)
dbdns_rec = ARecord.get_or_create(session, fqdn=dbfqdn, ip=ip,
network=dbnetwork)
else:
dbdns_rec = ARecord.get_unique(session, dbfqdn, compel=True)
ip = dbdns_rec.ip
dbnetwork = dbdns_rec.network
assert ip in dbnetwork.network, "IP %s is outside network %s" % (ip,
dbnetwork.ip)
if ip in dbnetwork.router_ips:
raise ArgumentError("IP address {0} is already present as a router "
"for {1:l}.".format(ip, dbnetwork))
# Policy checks are valid only for internal networks
if dbnetwork.is_internal:
if ip >= dbnetwork.first_usable_host or \
int(ip) - int(dbnetwork.ip) in dbnetwork.reserved_offsets:
raise ArgumentError("IP address {0} is not a valid router address "
"on {1:l}.".format(ip, dbnetwork))
dbnetwork.routers.append(RouterAddress(ip=ip, location=dbbuilding,
dns_environment=dbdns_rec.fqdn.dns_environment,
comments=comments))
session.flush()
# TODO: update the templates of Zebra hosts on the network
return
示例12: render
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def render(self, session, service, instance, archetype, personality,
networkip, **kwargs):
dbservice = Service.get_unique(session, service, compel=True)
dblocation = get_location(session, **kwargs)
dbinstance = get_service_instance(session, dbservice, instance)
if networkip:
dbnet_env = NetworkEnvironment.get_unique_or_default(session)
dbnetwork = get_network_byip(session, networkip, dbnet_env)
else:
dbnetwork = None
if archetype is None and personality:
# Can't get here with the standard aq client.
raise ArgumentError("Specifying --personality requires you to "
"also specify --archetype.")
kwargs = {}
if archetype and personality:
dbpersona = Personality.get_unique(session, name=personality,
archetype=archetype, compel=True)
map_class = PersonalityServiceMap
query = session.query(map_class).filter_by(personality=dbpersona)
kwargs["personality"] = dbpersona
else:
map_class = ServiceMap
query = session.query(map_class)
dbmap = query.filter_by(location=dblocation,
service_instance=dbinstance,
network=dbnetwork).first()
if not dbmap:
dbmap = map_class(service_instance=dbinstance,
location=dblocation,
network=dbnetwork, **kwargs)
session.add(dbmap)
session.flush()
return
示例13: search_system_query
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def search_system_query(session, dns_record_type=DnsRecord, **kwargs):
q = session.query(dns_record_type)
# Outer-join in all the subclasses so that each access of
# system doesn't (necessarily) issue another query.
if dns_record_type is DnsRecord:
q = q.with_polymorphic('*')
dbdns_env = DnsEnvironment.get_unique_or_default(session,
kwargs.get("dns_environment",
None))
q = q.join((Fqdn, DnsRecord.fqdn_id == Fqdn.id))
q = q.filter_by(dns_environment=dbdns_env)
q = q.options(contains_eager('fqdn'))
if kwargs.get('fqdn', None):
(short, dbdns_domain) = parse_fqdn(session, kwargs['fqdn'])
q = q.filter_by(name=short, dns_domain=dbdns_domain)
if kwargs.get('dns_domain', None):
dbdns_domain = DnsDomain.get_unique(session, kwargs['dns_domain'],
compel=True)
q = q.filter_by(dns_domain=dbdns_domain)
if kwargs.get('shortname', None):
q = q.filter_by(name=kwargs['shortname'])
q = q.reset_joinpoint()
if kwargs.get('ip', None):
q = q.filter(ARecord.ip == kwargs['ip'])
if kwargs.get('networkip', None):
net_env = kwargs.get('network_environment', None)
dbnet_env = NetworkEnvironment.get_unique_or_default(session, net_env)
dbnetwork = get_network_byip(session, kwargs['networkip'], dbnet_env)
q = q.filter(ARecord.network == dbnetwork)
if kwargs.get('mac', None):
raise UnimplementedError("search_system --mac is no longer supported, "
"try search_hardware.")
if kwargs.get('type', None):
# Deprecated... remove if it becomes a problem.
type_arg = kwargs['type'].strip().lower()
q = q.filter_by(dns_record_type=type_arg)
return q
示例14: render
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def render(self, session, dbuser, network, ip, network_environment, type,
side, comments, **arguments):
dbnet_env = NetworkEnvironment.get_unique_or_default(session,
network_environment)
self.az.check_network_environment(dbuser, dbnet_env)
if not network and not ip:
raise ArgumentError("Please specify either --network or --ip.")
q = session.query(Network)
q = q.filter_by(network_environment=dbnet_env)
if network:
q = q.filter_by(name=network)
if ip:
q = q.filter_by(ip=ip)
networks = q.all()
if not networks:
raise NotFoundException("No matching network was found.")
dblocation = get_location(session, **arguments)
for dbnetwork in q.all():
if type:
dbnetwork.network_type = type
if side:
dbnetwork.side = side
if dblocation:
dbnetwork.location = dblocation
if comments is not None:
if comments.strip() == "":
dbnetwork.comments = None
else:
dbnetwork.comments = comments
session.flush()
return
示例15: render
# 需要导入模块: from aquilon.aqdb.model import NetworkEnvironment [as 别名]
# 或者: from aquilon.aqdb.model.NetworkEnvironment import get_unique_or_default [as 别名]
def render(self, session, fqdn, record_type, dns_environment,
network_environment=None, **arguments):
if network_environment:
if not isinstance(network_environment, NetworkEnvironment):
network_environment = NetworkEnvironment.get_unique_or_default(session,
network_environment)
if not dns_environment:
dns_environment = network_environment.dns_environment
dbdns_env = DnsEnvironment.get_unique_or_default(session,
dns_environment)
# No compel here. query(DnsRecord).filter_by(fqdn=None) will fail if the
# FQDN is invalid, and that will give a better error message.
dbfqdn = Fqdn.get_unique(session, fqdn=fqdn,
dns_environment=dbdns_env)
if record_type:
if record_type in DNS_RRTYPE_MAP:
cls = DNS_RRTYPE_MAP[record_type]
else:
cls = DnsRecord.polymorphic_subclass(record_type,
"Unknown DNS record type")
else:
cls = DnsRecord
# We want to query(ARecord) instead of
# query(DnsRecord).filter_by(record_type='a_record'), because the former
# works for DynamicStub as well
q = session.query(cls)
if cls == DnsRecord:
q = q.with_polymorphic('*')
q = q.filter_by(fqdn=dbfqdn)
result = q.all()
if not result:
raise NotFoundException("%s %s not found." %
(cls._get_class_label(), fqdn))
return result