Example #1
0
    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
Example #2
0
    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
Example #3
0
 def migrate_new_routes(self, block=None):
     gateway = netaddr.IPAddress(block.gateway)
     destination = None
     if gateway.version == 4:
         destination = '0.0.0.0/0'  # 3
     else:
         destination = '0:0:0:0:0:0:0:0/0'  # 4
     q_route = quarkmodels.Route(cidr=destination,
                                 tenant_id=block.tenant_id,
                                 gateway=block.gateway,
                                 subnet_id=block.id,
                                 created_at=dt.utcnow())
     self.new_to_session(q_route, 'routes')
Example #4
0
 def migrate_routes(self, block=None):
     routes = self.melange_session.query(melange.IpRoutes)\
         .filter_by(source_block_id=block.id).all()
     for route in routes:
         init_id(self.json_data, 'routes', route.id)
         q_route = quarkmodels.Route(id=route.id,
                                     cidr=translate_netmask(
                                         route.netmask, route.destination),
                                     tenant_id=block.tenant_id,
                                     gateway=route.gateway,
                                     created_at=block.created_at,
                                     subnet_id=block.id)
         self.add_to_session(q_route, 'routes', q_route.id)
Example #5
0
def route_create(context, **route_dict):
    new_route = models.Route()
    new_route.update(route_dict)
    new_route["tenant_id"] = context.tenant_id
    context.session.add(new_route)
    return new_route
Example #6
0
    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