def _stubs(self, net=None, subnet=None, ports=None, find_net=False): net_mod = net subnet_mod = None if net: net_mod = models.Network() net_mod.update(net) if subnet: subnet_mod = models.Subnet() subnet_mod.update(subnet) found_net = None if find_net: found_net = models.Network() db_mod = "quark.db.api" with contextlib.nested( mock.patch("%s.network_create" % db_mod), mock.patch("%s.subnet_create" % db_mod), mock.patch("quark.drivers.base.BaseDriver.create_network"), mock.patch("%s.network_find" % db_mod)) as (net_create, sub_create, driver_net_create, net_find): net_create.return_value = net_mod sub_create.return_value = subnet_mod net_find.return_value = found_net yield net_create
def _stubs(self, subnets=None, routes=None): if routes is None: routes = [] route_models = [] for route in routes: r = models.Route() r.update(route) route_models.append(r) if isinstance(subnets, list): subnet_models = [] for subnet in subnets: s_dict = subnet.copy() s_dict["routes"] = route_models s = models.Subnet(network=models.Network()) s.update(s_dict) subnet_models.append(s) elif subnets: mod = models.Subnet(network=models.Network()) mod.update(subnets) mod["routes"] = route_models subnet_models = mod else: subnet_models = None with mock.patch("quark.db.api.subnet_find") as subnet_find: subnet_find.return_value = subnet_models yield
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 _stubs(self, nets=None, subnets=None): net_mods = [] subnet_mods = [] if subnets: for subnet in subnets: subnet_mod = models.Subnet() subnet_mod.update(subnet) subnet_mods.append(subnet_mod) if nets: for net in nets: net_mod = models.Network() net_mod["network_plugin"] = "BASE" net_mod.update(net) net_mod["subnets"] = subnet_mods net_mods.append(net_mod) else: if nets: net_mods = nets.copy() net_mods["subnets"] = subnet_mods else: net_mods = nets db_mod = "quark.db.api" with mock.patch("%s.network_find" % db_mod) as net_find: net_find.return_value = net_mods yield
def _stubs(self, net=None, ports=None, subnets=None): subnets = subnets or [] net_mod = net port_mods = [] subnet_mods = [] for port in ports: port_model = models.Port() port_model.update(port) port_mods.append(port_model) for subnet in subnets: subnet_mod = models.Subnet() subnet_mod.update(subnet) subnet_mods.append(subnet_mod) if net: net_mod = models.Network() net_mod.update(net) net_mod.ports = port_mods net_mod["subnets"] = subnet_mods net_mod["network_plugin"] = "BASE" db_mod = "quark.db.api" with contextlib.nested( mock.patch("%s.network_find" % db_mod), mock.patch("%s.network_delete" % db_mod), mock.patch("quark.drivers.base.BaseDriver.delete_network"), mock.patch("%s.subnet_delete" % db_mod)) as (net_find, net_delete, driver_net_delete, subnet_del): net_find.return_value = net_mod yield net_delete
def _stubs(self, nets=None, subnets=None): net_mods = [] if isinstance(nets, list): for net in nets: subnet_mods = [] subnets = net.pop('subnets', []) for subnet in subnets: subnet_mod = models.Subnet() subnet_mod.update(subnet) subnet_mods.append(subnet_mod) net_mod = models.Network() net_mod.update(net) net_mod["subnets"] = subnet_mods net_mods.append(net_mod) else: if nets: net_mods = nets.copy() else: net_mods = nets db_mod = "quark.db.api" db_api.STRATEGY = network_strategy.JSONStrategy(self.strategy_json) network_strategy.STRATEGY = network_strategy.JSONStrategy( self.strategy_json) with mock.patch("%s._network_find" % db_mod) as net_find: net_find.return_value = net_mods yield net_find
def _stubs(self, net=None, ports=None, subnets=None): subnets = subnets or [] net_mod = net port_mods = [] subnet_mods = [] for port in ports: port_model = models.Port() port_model.update(port) port_mods.append(port_model) for subnet in subnets: subnet_mod = models.Subnet() subnet_mod.update(subnet) subnet_mods.append(subnet_mod) if net: net_mod = models.Network() net_mod.update(net) net_mod.ports = port_mods net_mod["subnets"] = subnet_mods net_mod["network_plugin"] = "BASE" db_mod = "quark.db.api" strategy_prefix = "quark.network_strategy.JSONStrategy" with mock.patch("%s.network_find" % db_mod) as net_find, \ mock.patch("%s.network_delete" % db_mod) as net_delete, \ mock.patch("quark.drivers.base.BaseDriver.delete_network"), \ mock.patch("%s.subnet_delete" % db_mod), \ mock.patch("%s.is_provider_network" % strategy_prefix) as \ is_provider_network: net_find.return_value = net_mod is_provider_network.return_value = True yield net_delete
def _stubs(self, flip=None, port=None, ips=None, network=None): port_model = None if port: port_model = models.Port() port_model.update(dict(port=port)) if ips: for ip in ips: ip_model = models.IPAddress() ip_model.update(ip) addr_type = ip.get("address_type") if addr_type == "floating" and "fixed_ip_addr" in ip: fixed_ip = models.IPAddress() fixed_ip.update( next(ip_addr for ip_addr in ips if (ip_addr["address_readable"] == ip["fixed_ip_addr"]))) ip_model.fixed_ip = fixed_ip port_model.ip_addresses.append(ip_model) flip_model = None if flip: flip_model = models.IPAddress() flip_model.update(flip) net_model = None if network: net_model = models.Network() net_model.update(network) def _alloc_ip(context, new_addr, net_id, port_m, *args, **kwargs): new_addr.append(flip_model) def _port_assoc(context, ports, addr, enable_port=None): addr.ports = ports return addr def _flip_fixed_ip_assoc(context, addr, fixed_ip): addr.fixed_ip = fixed_ip return addr with contextlib.nested( mock.patch("quark.db.api.floating_ip_find"), mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.port_find"), mock.patch("quark.ipam.QuarkIpam.allocate_ip_address"), mock.patch("quark.drivers.unicorn_driver.UnicornDriver" ".register_floating_ip"), mock.patch("quark.db.api.port_associate_ip"), mock.patch("quark.db.api.floating_ip_associate_fixed_ip")) as ( flip_find, net_find, port_find, alloc_ip, mock_reg_flip, port_assoc, fixed_ip_assoc): flip_find.return_value = flip_model net_find.return_value = net_model port_find.return_value = port_model alloc_ip.side_effect = _alloc_ip port_assoc.side_effect = _port_assoc fixed_ip_assoc.side_effect = _flip_fixed_ip_assoc yield
def _stubs(self, subnets=None): if subnets is None: subnets = [] subnet_models = [] for subnet in subnets: s = models.Subnet() s.update(subnet) subnet_models.append(s) network = models.Network() network.update(dict(id=1, subnets=subnet_models)) with contextlib.nested( mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_create")) as (net_find, subnet_find, subnet_create): net_find.return_value = network subnet_find.return_value = subnet_models subnet_create.return_value = models.Subnet( network=models.Network(), cidr="192.168.1.1/24") yield subnet_create
def _stubs(self, subnet): s = models.Subnet(network=models.Network(id=1, subnets=[])) s.update(subnet) with contextlib.nested( mock.patch("quark.db.api.network_find"), mock.patch("quark.db.api.subnet_find"), mock.patch("quark.db.api.subnet_create")) as (net_find, subnet_find, subnet_create): net_find.return_value = s["network"] subnet_find.return_value = [] subnet_create.return_value = s yield subnet_create
def network_create(context, **network): new_net = models.Network() new_net.update(network) context.session.add(new_net) return new_net
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