def handle(self, *args, **options): if args: raise CommandError("Command doesn't accept any arguments") network_id = options["network_id"] cidr = options["cidr"] if not network_id: raise CommandError("network-id is mandatory") if not cidr: raise CommandError("cidr is mandatory") user_id = common.get_network(network_id).userid name = options["name"] or "" allocation_pools = options["allocation_pools"] ipversion = options["ipversion"] or 4 ipversion = int(ipversion) gateway = options["gateway"] dhcp = parse_bool(options["dhcp"]) dns = options["dns"] host_routes = options["host_routes"] alloc = None if allocation_pools is not None: alloc = subnets.parse_allocation_pools(allocation_pools) alloc.sort() sub = subnets.create_subnet(name=name, network_id=network_id, cidr=cidr, allocation_pools=alloc, gateway=gateway, ipversion=ipversion, dhcp=dhcp, slaac=dhcp, dns_nameservers=dns, host_routes=host_routes, user_id=user_id) pprint.pprint_subnet_in_db(sub, stdout=self.stdout) self.stdout.write("\n\n") pprint.pprint_ippool(sub, stdout=self.stdout)
def handle(self, *args, **options): if args: raise CommandError("Command doesn't accept any arguments") network_id = options["network_id"] cidr = options["cidr"] if not network_id: raise CommandError("network is mandatory") if not cidr: raise CommandError("cidr is mandatory") user_id = common.get_resource("network", network_id).userid name = options["name"] or "" allocation_pools = options["allocation_pools"] ipversion = options["ipversion"] or 4 ipversion = int(ipversion) gateway = options["gateway"] dhcp = parse_bool(options["dhcp"]) dns = options["dns"] host_routes = options["host_routes"] alloc = None if allocation_pools is not None: alloc = subnets.parse_allocation_pools(allocation_pools) alloc.sort() sub = subnets.create_subnet(name=name, network_id=network_id, cidr=cidr, allocation_pools=alloc, gateway=gateway, ipversion=ipversion, dhcp=dhcp, slaac=dhcp, dns_nameservers=dns, host_routes=host_routes, user_id=user_id) pprint.pprint_subnet_in_db(sub, stdout=self.stdout) self.stdout.write("\n\n") pprint.pprint_ippool(sub, stdout=self.stdout)
def handle(self, *args, **options): if args: raise CommandError("Command doesn't accept any arguments") name = options['name'] subnet = options['subnet'] gateway = options['gateway'] subnet6 = options['subnet6'] gateway6 = options['gateway6'] public = options['public'] flavor = options['flavor'] mode = options['mode'] link = options['link'] mac_prefix = options['mac_prefix'] tags = options['tags'] userid = options["user"] project = options['project'] allocation_pools = options["allocation_pools"] floating_ip_pool = parse_bool(options["floating_ip_pool"]) dhcp = parse_bool(options["dhcp"]) drained = parse_bool(options["drained"]) if name is None: name = "" if flavor is None: raise CommandError("flavor is required") if ((subnet is None) and (subnet6 is None)) and dhcp is not False: raise CommandError("Cannot set DHCP without subnet or subnet6") if subnet is None and gateway is not None: raise CommandError("Cannot use gateway without subnet") if subnet is None and allocation_pools is not None: raise CommandError("Cannot use allocation-pools without subnet") if subnet6 is None and gateway6 is not None: raise CommandError("Cannot use gateway6 without subnet6") if flavor == "IP_LESS_ROUTED" and not (subnet or subnet6): raise CommandError("Cannot create 'IP_LESS_ROUTED' network without" " subnet") if not (userid or public): raise CommandError("'user' is required for private networks") if not project: project = userid network = networks.create(userid=userid, name=name, flavor=flavor, public=public, mode=mode, project=project, link=link, mac_prefix=mac_prefix, tags=tags, floating_ip_pool=floating_ip_pool, drained=drained) if subnet is not None: alloc = None if allocation_pools is not None: alloc = subnets.parse_allocation_pools(allocation_pools) alloc.sort() name = "IPv4 Subnet of Network %s" % network.id subnets.create_subnet(network.id, cidr=subnet, name=name, ipversion=4, gateway=gateway, dhcp=dhcp, user_id=userid, allocation_pools=alloc) if subnet6 is not None: name = "IPv6 Subnet of Network %s" % network.id subnets.create_subnet(network.id, cidr=subnet6, name=name, ipversion=6, gateway=gateway6, dhcp=dhcp, user_id=userid) self.stdout.write("Created network '%s' in DB:\n" % network) pprint.pprint_network(network, stdout=self.stdout) pprint.pprint_network_subnets(network, stdout=self.stdout) networks.create_network_in_backends(network) # TODO: Add --wait option to track job progress and report successful # creation in each backend. self.stdout.write("\nSuccessfully issued job to create network in" " backends\n")
def create_subnet(request): """Create a subnet network_id and the desired cidr are mandatory, everything else is optional """ dictionary = utils.get_json_body(request) user_id = request.credentials.userid log.info('create subnet user: %s request: %s', user_id, dictionary) try: subnet = dictionary['subnet'] network_id = subnet['network_id'] cidr = subnet['cidr'] except KeyError: raise api.faults.BadRequest("Malformed request") name = subnet.get('name', None) ipversion = subnet.get('ip_version', 4) allocation_pools = subnet.get('allocation_pools', None) if allocation_pools is not None: allocation_pools = parse_ip_pools(allocation_pools) try: cidr_ip = ipaddr.IPNetwork(cidr) except ValueError: raise api.faults.BadRequest("Malformed CIDR '%s'" % cidr) # If no gateway is specified, send an empty string, because None is used # if the user wants no gateway at all gateway = subnet.get('gateway_ip', "") if gateway is "": gateway = str(cidr_ip.network + 1) dhcp = subnet.get('enable_dhcp', True) slaac = subnet.get('enable_slaac', None) if ipversion == 6: if slaac is not None: dhcp = check_boolean_value(slaac, "enable_slaac") else: dhcp = check_boolean_value(dhcp, "dhcp") else: dhcp = check_boolean_value(dhcp, "dhcp") dns = subnet.get('dns_nameservers', None) hosts = subnet.get('host_routes', None) sub = subnets.create_subnet(network_id=network_id, cidr=cidr, name=name, ipversion=ipversion, gateway=gateway, dhcp=dhcp, slaac=slaac, dns_nameservers=dns, allocation_pools=allocation_pools, host_routes=hosts, user_id=user_id) subnet_dict = subnet_to_dict(sub) data = json.dumps({'subnet': subnet_dict}) return HttpResponse(data, status=201)