Esempio n. 1
0
    def _save_subnet(self, context, network, subnet_args, dns_nameservers,
                     host_routes, subnet_request):
        network_scope = addr_scope_obj.AddressScope.get_network_address_scope(
            context, network.id, subnet_args['ip_version'])
        # 'subnetpool' is not necessarily an object
        subnetpool = subnet_args.get('subnetpool_id')
        if subnetpool and subnetpool != const.IPV6_PD_POOL_ID:
            subnetpool = self._get_subnetpool(context, subnetpool)

        self._validate_subnet_cidr(context, network, subnet_args['cidr'])
        self._validate_network_subnetpools(network, subnet_args['ip_version'],
                                           subnetpool, network_scope)

        service_types = subnet_args.pop('service_types', [])

        segment_id = subnet_args.get('segment_id')
        if segment_id:
            # TODO(slaweq): integrate check if segment exists in
            # self._validate_segment() method
            if not network_obj.NetworkSegment.get_object(context,
                                                         id=segment_id):
                raise segment_exc.SegmentNotFound(segment_id=segment_id)

        subnet = subnet_obj.Subnet(context, **subnet_args)
        subnet.create()
        # TODO(slaweq): when check is segment exists will be integrated in
        # self._validate_segment() method, it should be moved to be done before
        # subnet object is created
        self._validate_segment(context, network['id'], segment_id)

        # NOTE(changzhi) Store DNS nameservers with order into DB one
        # by one when create subnet with DNS nameservers
        if validators.is_attr_set(dns_nameservers):
            for order, server in enumerate(dns_nameservers):
                dns = subnet_obj.DNSNameServer(context,
                                               address=server,
                                               order=order,
                                               subnet_id=subnet.id)
                dns.create()

        if validators.is_attr_set(host_routes):
            for rt in host_routes:
                route = subnet_obj.Route(
                    context,
                    subnet_id=subnet.id,
                    destination=net_utils.AuthenticIPNetwork(
                        rt['destination']),
                    nexthop=netaddr.IPAddress(rt['nexthop']))
                route.create()

        if validators.is_attr_set(service_types):
            for service_type in service_types:
                service_type_obj = subnet_obj.SubnetServiceType(
                    context, subnet_id=subnet.id, service_type=service_type)
                service_type_obj.create()

        self.save_allocation_pools(context, subnet,
                                   subnet_request.allocation_pools)

        return subnet_obj.Subnet.get_object(context, id=subnet.id)
 def _update_subnet_service_types(self, context, subnet_id, s):
     subnet_obj.SubnetServiceType.delete_objects(context,
                                                 subnet_id=subnet_id)
     updated_types = s.pop('service_types')
     for service_type in updated_types:
         new_type = subnet_obj.SubnetServiceType(context,
                                                 subnet_id=subnet_id,
                                                 service_type=service_type)
         new_type.create()
     return updated_types
Esempio n. 3
0
    def _save_subnet(self, context,
                     network,
                     subnet_args,
                     dns_nameservers,
                     host_routes,
                     subnet_request):
        self._validate_subnet_cidr(context, network, subnet_args['cidr'])
        self._validate_network_subnetpools(network,
                                           subnet_args['subnetpool_id'],
                                           subnet_args['ip_version'])

        service_types = subnet_args.pop('service_types', [])

        subnet = models_v2.Subnet(**subnet_args)
        segment_id = subnet_args.get('segment_id')
        try:
            context.session.add(subnet)
            context.session.flush()
        except db_exc.DBReferenceError:
            raise segment_exc.SegmentNotFound(segment_id=segment_id)
        self._validate_segment(context, network['id'], segment_id)

        # NOTE(changzhi) Store DNS nameservers with order into DB one
        # by one when create subnet with DNS nameservers
        if validators.is_attr_set(dns_nameservers):
            for order, server in enumerate(dns_nameservers):
                dns = subnet_obj.DNSNameServer(context,
                                               address=server,
                                               order=order,
                                               subnet_id=subnet.id)
                dns.create()

        if validators.is_attr_set(host_routes):
            for rt in host_routes:
                route = subnet_obj.Route(
                    context,
                    subnet_id=subnet.id,
                    destination=common_utils.AuthenticIPNetwork(
                        rt['destination']),
                    nexthop=netaddr.IPAddress(rt['nexthop']))
                route.create()

        if validators.is_attr_set(service_types):
            for service_type in service_types:
                service_type_obj = subnet_obj.SubnetServiceType(
                    context, subnet_id=subnet.id, service_type=service_type)
                service_type_obj.create()

        self.save_allocation_pools(context, subnet,
                                   subnet_request.allocation_pools)

        return subnet
Esempio n. 4
0
    def test_get_service_types(self):
        obj = self._make_object(self.obj_fields[0])
        obj.create()

        service_type_obj = subnet.SubnetServiceType(self.context,
                                                    subnet_id=obj.id,
                                                    service_type='dhcp-agent')
        service_type_obj.create()

        listed_obj = subnet.Subnet.get_object(self.context, id=obj.id)

        self.assertEqual([service_type_obj.service_type],
                         listed_obj.service_types)

        # Try to load the service_types by obj_load_attr
        obj1 = self._make_object(self.obj_fields[0])
        self.assertEqual([service_type_obj.service_type], obj1.service_types)