def _stubs(self, subnet=None, network=None, routes=None, dns=None): if network: net = models.Network() net.update(network) network = net subnet_mod = models.Subnet(network=models.Network()) dns_ips = subnet.pop("dns_nameservers", []) host_routes = subnet.pop("host_routes", []) subnet_mod.update(subnet) subnet["dns_nameservers"] = dns_ips subnet["host_routes"] = host_routes routes = routes or [] dns = dns or [] route_models = [models.Route(**r) for r in routes] dns_models = [models.DNSNameserver(**d) for d in dns] with contextlib.nested( mock.patch("quark.db.api.subnet_create"), mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.dns_create"), mock.patch("quark.db.api.route_create"), ) as (subnet_create, net_find, dns_create, route_create): subnet_create.return_value = subnet_mod net_find.return_value = network route_create.side_effect = route_models dns_create.side_effect = dns_models yield subnet_create, dns_create, route_create
def dns_create(context, **dns_dict): dns_nameserver = models.DNSNameserver() ip = dns_dict.pop("ip") dns_nameserver.update(dns_dict) dns_nameserver["ip"] = int(ip) dns_nameserver["tenant_id"] = context.tenant_id context.session.add(dns_nameserver) return dns_nameserver
def migrate_networks(self): """1. Migrate the m.ip_blocks -> q.quark_networks Migration of ip_blocks to networks requires one take into consideration that blocks can have 'children' blocks. A scan of the melange tables shows though that this feature hasn't been used. An ip_block has a cidr which maps to a corresponding subnet in quark. """ blocks = self.melange_session.query(melange.IpBlocks).all() networks = dict() """Create the networks using the network_id. It is assumed that a network can only belong to one tenant""" for block in blocks: init_id(self.json_data, 'networks', trim_br(block.network_id)) if trim_br(block.network_id) not in networks: networks[trim_br(block.network_id)] = { "tenant_id": block.tenant_id, "name": block.network_name, "max_allocation": block.max_allocation, "created_at": block.created_at } elif trim_br(block.network_id) in networks: if networks[trim_br( block.network_id )]["created_at"] > block.created_at: # noqa networks[trim_br( block.network_id )]["created_at"] = block.created_at # noqa elif networks[trim_br( block.network_id)]["tenant_id"] != block.tenant_id: r = "Found different tenant on network:{0} != {1}"\ .format(networks[trim_br( block.network_id)]["tenant_id"], block.tenant_id) self.log.critical(r) set_reason(self.json_data, 'networks', trim_br(block.network_id), r) raise Exception for net in networks: cache_net = networks[net] q_network = quarkmodels.Network( id=net, tenant_id=cache_net["tenant_id"], name=cache_net["name"], max_allocation=cache_net["max_allocation"]) self.add_to_session(q_network, 'networks', net) blocks_without_policy = 0 for block in blocks: init_id(self.json_data, 'subnets', block.id) q_subnet = quarkmodels.Subnet(id=block.id, network_id=trim_br(block.network_id), tenant_id=block.tenant_id, cidr=block.cidr, do_not_use=block.omg_do_not_use, created_at=block.created_at) self.add_to_session(q_subnet, 'subnets', q_subnet.id) q_dns1 = quarkmodels.DNSNameserver( tenant_id=block.tenant_id, created_at=block.created_at, ip=int(netaddr.IPAddress(block.dns1)), # noqa subnet_id=q_subnet.id) q_dns2 = quarkmodels.DNSNameserver( tenant_id=block.tenant_id, created_at=block.created_at, ip=int(netaddr.IPAddress(block.dns2)), # noqa subnet_id=q_subnet.id) self.new_to_session(q_dns1) self.new_to_session(q_dns2) self.migrate_ips(block=block) self.migrate_routes(block=block) # caching policy_ids for use in migrate_policies if block.policy_id: if block.policy_id not in self.policy_ids.keys(): self.policy_ids[block.policy_id] = {} self.policy_ids[block.policy_id][block.id] =\ trim_br(block.network_id) else: self.log.warning("Found block without a policy: {0}".format( block.id)) blocks_without_policy += 1 # have to add new routes as well: new_gates = 0 for block in blocks: if block.gateway: self.migrate_new_routes(block) new_gates += 1 self.log.info( "Cached {0} policy_ids. {1} blocks found without policy.".format( len(self.policy_ids), blocks_without_policy)) self.log.info("{0} brand new gateways created.".format(new_gates))
def _stubs(self, host_routes=None, new_routes=None, find_routes=True, new_dns_servers=None): if host_routes is None: host_routes = [] if new_routes: new_routes = [ models.Route(cidr=r["destination"], gateway=r["nexthop"], subnet_id=1) for r in new_routes ] if new_dns_servers: new_dns_servers = [ models.DNSNameserver(ip=ip, subnet_id=1) for ip in new_dns_servers ] subnet = dict(id=1, network_id=1, tenant_id=self.context.tenant_id, ip_version=4, cidr="172.16.0.0/24", host_routes=host_routes, dns_nameservers=["4.2.2.1", "4.2.2.2"], enable_dhcp=None) dns_ips = subnet.pop("dns_nameservers", []) host_routes = subnet.pop("host_routes", []) subnet_mod = models.Subnet() subnet_mod.update(subnet) subnet_mod["dns_nameservers"] = [ models.DNSNameserver(ip=ip) for ip in dns_ips ] subnet_mod["routes"] = [ models.Route(cidr=r["destination"], gateway=r["nexthop"], subnet_id=subnet_mod["id"]) for r in host_routes ] with contextlib.nested( mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_update"), mock.patch("quark.db.api.dns_create"), mock.patch("quark.db.api.route_find"), mock.patch("quark.db.api.route_update"), mock.patch("quark.db.api.route_create"), ) as (subnet_find, subnet_update, dns_create, route_find, route_update, route_create): subnet_find.return_value = subnet_mod route_find.return_value = subnet_mod["routes"][0] \ if subnet_mod["routes"] and find_routes else None new_subnet_mod = models.Subnet(network=models.Network()) new_subnet_mod.update(subnet_mod) if new_routes: new_subnet_mod["routes"] = new_routes if new_dns_servers: new_subnet_mod["dns_nameservers"] = new_dns_servers subnet_update.return_value = new_subnet_mod yield dns_create, route_update, route_create