def create_network(request): userid = request.user_uniq req = api.utils.get_json_body(request) log.info('create_network user: %s request: %s', userid, req) network_dict = api.utils.get_attribute(req, "network", attr_type=dict) flavor = api.utils.get_attribute(network_dict, "type", attr_type=basestring) if flavor not in Network.FLAVORS.keys(): raise api.faults.BadRequest("Invalid network type '%s'" % flavor) if flavor not in settings.API_ENABLED_NETWORK_FLAVORS: raise api.faults.Forbidden("Cannot create network of type '%s'." % flavor) name = api.utils.get_attribute(network_dict, "name", attr_type=basestring, required=False) if name is None: name = "" project = network_dict.get('project', None) network = networks.create(userid=userid, name=name, flavor=flavor, public=False, project=project) networkdict = network_to_dict(network, detail=True) response = render_network(request, networkdict, status=201) return response
def create_network(request): userid = request.user_uniq req = api.utils.get_request_dict(request) log.info('create_network user: %s request: %s', userid, req) network_dict = api.utils.get_attribute(req, "network", attr_type=dict) flavor = api.utils.get_attribute(network_dict, "type", attr_type=basestring) if flavor not in Network.FLAVORS.keys(): raise api.faults.BadRequest("Invalid network type '%s'" % flavor) if flavor not in settings.API_ENABLED_NETWORK_FLAVORS: raise api.faults.Forbidden("Cannot create network of type '%s'." % flavor) name = api.utils.get_attribute(network_dict, "name", attr_type=basestring, required=False) if name is None: name = "" network = networks.create(userid=userid, name=name, flavor=flavor, public=False) networkdict = network_to_dict(network, detail=True) response = render_network(request, networkdict, status=201) return response
def create_network(request): userid = request.credentials.userid req = api.utils.get_json_body(request) log.debug("User: %s, Action: create_network, Request: %s", userid, req) network_dict = api.utils.get_attribute(req, "network", attr_type=dict) flavor = api.utils.get_attribute(network_dict, "type", attr_type=basestring) if flavor not in Network.FLAVORS.keys(): raise api.faults.BadRequest("Invalid network type '%s'" % flavor) if flavor not in settings.API_ENABLED_NETWORK_FLAVORS: raise api.faults.Forbidden("Cannot create network of type '%s'." % flavor) name = api.utils.get_attribute(network_dict, "name", attr_type=basestring, required=False) if name is None: name = "" project = network_dict.get('project', None) shared_to_project = network_dict.get("shared_to_project", False) network = networks.create(userid=userid, name=name, flavor=flavor, public=False, project=project, shared_to_project=shared_to_project) log.info("User %s created network %s, shared: %s", userid, network.id, shared_to_project) networkdict = network_to_dict(network, detail=True) response = render_network(request, networkdict, status=201) return response
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 test_create(self): kwargs = { "name": "test", "userid": "user", "flavor": "CUSTOM", } # wrong flavor kw = copy(kwargs) kw["flavor"] = "UNKNOWN" self.assertRaises(faults.BadRequest, networks.create, **kw) # Test create objet kwargs["public"] = True with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.public, True) self.assertEqual(net.flavor, "CUSTOM") self.assertEqual(net.action, "CREATE") self.assertEqual(net.state, "ACTIVE") self.assertEqual(net.name, "test") self.assertEqual(net.userid, "user") # Test for each for flavor type # MAC_FILTERED kwargs["flavor"] = "MAC_FILTERED" # Test exception if no rules exists self.assertRaises(faults.ServiceUnavailable, networks.create, **kwargs) mfactory.MacPrefixPoolTableFactory(base="aa:bb:0") with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "bridged") self.assertEqual(net.mac_prefix, "aa:bb:1") self.assertEqual(net.link, settings.DEFAULT_MAC_FILTERED_BRIDGE) self.assertEqual(net.backend_tag, ["private-filtered"]) with transaction.atomic(): pool = MacPrefixPoolTable.get_pool() self.assertFalse(pool.is_available("aa:bb:1")) # PHYSICAL_VLAN kwargs["flavor"] = "PHYSICAL_VLAN" # Test exception if no rules exists self.assertRaises(faults.ServiceUnavailable, networks.create, **kwargs) mfactory.BridgePoolTableFactory(base="prv") with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "bridged") self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX) self.assertEqual(net.link, "prv1") self.assertEqual(net.backend_tag, ["physical-vlan"]) with transaction.atomic(): pool = BridgePoolTable.get_pool() self.assertFalse(pool.is_available(net.link)) # IP_LESS_ROUTED kwargs["flavor"] = "IP_LESS_ROUTED" with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "routed") self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX) self.assertEqual(net.link, "%slink-%d" % (settings.BACKEND_PREFIX_ID, net.id)) self.assertEqual(net.backend_tag, ["ip-less-routed"]) # CUSTOM kwargs["flavor"] = "CUSTOM" with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "bridged") self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX) self.assertEqual(net.link, settings.DEFAULT_BRIDGE) self.assertEqual(net.backend_tag, [])
def test_create(self): kwargs = { "name": "test", "userid": "user", "flavor": "CUSTOM", } # wrong flavor kw = copy(kwargs) kw["flavor"] = "UNKNOWN" self.assertRaises(faults.BadRequest, networks.create, **kw) # Test create objet kwargs["public"] = True with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.public, True) self.assertEqual(net.flavor, "CUSTOM") self.assertEqual(net.action, "CREATE") self.assertEqual(net.state, "ACTIVE") self.assertEqual(net.name, "test") self.assertEqual(net.userid, "user") # Test for each for flavor type # MAC_FILTERED kwargs["flavor"] = "MAC_FILTERED" # Test exception if no rules exists self.assertRaises(faults.ServiceUnavailable, networks.create, **kwargs) mfactory.MacPrefixPoolTableFactory(base="aa:bb:0") with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "bridged") self.assertEqual(net.mac_prefix, "aa:bb:1") self.assertEqual(net.link, settings.DEFAULT_MAC_FILTERED_BRIDGE) self.assertEqual(net.backend_tag, ["private-filtered"]) pool = MacPrefixPoolTable.get_pool() self.assertFalse(pool.is_available("aa:bb:1")) # PHYSICAL_VLAN kwargs["flavor"] = "PHYSICAL_VLAN" # Test exception if no rules exists self.assertRaises(faults.ServiceUnavailable, networks.create, **kwargs) mfactory.BridgePoolTableFactory(base="prv") with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "bridged") self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX) self.assertEqual(net.link, "prv1") self.assertEqual(net.backend_tag, ["physical-vlan"]) pool = BridgePoolTable.get_pool() self.assertFalse(pool.is_available(net.link)) # IP_LESS_ROUTED kwargs["flavor"] = "IP_LESS_ROUTED" with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "routed") self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX) self.assertEqual(net.link, "%slink-%d" % (settings.BACKEND_PREFIX_ID, net.id)) self.assertEqual(net.backend_tag, ["ip-less-routed"]) # CUSTOM kwargs["flavor"] = "CUSTOM" with mocked_quotaholder(): net = networks.create(**kwargs) self.assertEqual(net.mode, "bridged") self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX) self.assertEqual(net.link, settings.DEFAULT_BRIDGE) self.assertEqual(net.backend_tag, [])