def _save_subnet(self, context, network, subnet_args, dns_nameservers, host_routes, allocation_pools): allocation_pools = self._prepare_allocation_pools( context, allocation_pools, subnet_args) self._validate_subnet_cidr(context, network, subnet_args['cidr']) self._validate_network_subnetpools(network, subnet_args['subnetpool_id'], subnet_args['ip_version']) subnet = models_v2.Subnet(**subnet_args) context.session.add(subnet) if attributes.is_attr_set(dns_nameservers): for addr in dns_nameservers: ns = models_v2.DNSNameServer(address=addr, subnet_id=subnet.id) context.session.add(ns) if attributes.is_attr_set(host_routes): for rt in host_routes: route = models_v2.SubnetRoute(subnet_id=subnet.id, destination=rt['destination'], nexthop=rt['nexthop']) context.session.add(route) self._save_allocation_pools(context, subnet, allocation_pools) return subnet
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']) subnet = models_v2.Subnet(**subnet_args) context.session.add(subnet) # NOTE(changzhi) Store DNS nameservers with order into DB one # by one when create subnet with DNS nameservers if attributes.is_attr_set(dns_nameservers): for order, server in enumerate(dns_nameservers): dns = models_v2.DNSNameServer( address=server, order=order, subnet_id=subnet.id) context.session.add(dns) if attributes.is_attr_set(host_routes): for rt in host_routes: route = models_v2.SubnetRoute( subnet_id=subnet.id, destination=rt['destination'], nexthop=rt['nexthop']) context.session.add(route) self.save_allocation_pools(context, subnet, subnet_request.allocation_pools) return subnet
def _update_subnet_dns_nameservers(self, context, id, s): old_dns_list = self._get_dns_by_subnet(context, id) new_dns_addr_set = set(s["dns_nameservers"]) old_dns_addr_set = set([dns['address'] for dns in old_dns_list]) new_dns = list(new_dns_addr_set) for dns_addr in old_dns_addr_set - new_dns_addr_set: for dns in old_dns_list: if dns['address'] == dns_addr: context.session.delete(dns) for dns_addr in new_dns_addr_set - old_dns_addr_set: dns = models_v2.DNSNameServer(address=dns_addr, subnet_id=id) context.session.add(dns) del s["dns_nameservers"] return new_dns
def _update_subnet_dns_nameservers(self, context, id, s): old_dns_list = self._get_dns_by_subnet(context, id) new_dns_addr_list = s["dns_nameservers"] # NOTE(changzhi) delete all dns nameservers from db # when update subnet's DNS nameservers. And store new # nameservers with order one by one. for dns in old_dns_list: context.session.delete(dns) for order, server in enumerate(new_dns_addr_list): dns = models_v2.DNSNameServer(address=server, order=order, subnet_id=id) context.session.add(dns) del s["dns_nameservers"] return new_dns_addr_list