Ejemplo n.º 1
0
def create_network(request):
    # Normal Response Code: 202
    # Error Response Codes: computeFault (400, 500),
    #                       serviceUnavailable (503),
    #                       unauthorized (401),
    #                       badMediaType(415),
    #                       badRequest (400),
    #                       forbidden (403)
    #                       overLimit (413)

    req = utils.get_request_dict(request)
    log.info('create_network %s', req)

    user_id = request.user_uniq
    try:
        d = req['network']
        name = d['name']
    except KeyError:
        raise faults.BadRequest("Malformed request")

    # Get and validate flavor. Flavors are still exposed as 'type' in the
    # API.
    flavor = d.get("type", None)
    if flavor is None:
        raise faults.BadRequest("Missing request parameter 'type'")
    elif flavor not in Network.FLAVORS.keys():
        raise faults.BadRequest("Invalid network type '%s'" % flavor)
    elif flavor not in settings.API_ENABLED_NETWORK_FLAVORS:
        raise faults.Forbidden("Can not create network of type '%s'" %
                               flavor)

    public = d.get("public", False)
    if public:
        raise faults.Forbidden("Can not create a public network.")

    dhcp = d.get('dhcp', True)

    # Get and validate network parameters
    subnet = d.get('cidr', '192.168.1.0/24')
    subnet6 = d.get('cidr6', None)
    gateway = d.get('gateway', None)
    gateway6 = d.get('gateway6', None)
    # Check that user provided a valid subnet
    util.validate_network_params(subnet, gateway, subnet6, gateway6)

    try:
        mode, link, mac_prefix, tags = util.values_from_flavor(flavor)
        validate_mac(mac_prefix + "0:00:00:00")
        network = Network.objects.create(
            name=name,
            userid=user_id,
            subnet=subnet,
            subnet6=subnet6,
            gateway=gateway,
            gateway6=gateway6,
            dhcp=dhcp,
            flavor=flavor,
            mode=mode,
            link=link,
            mac_prefix=mac_prefix,
            tags=tags,
            action='CREATE',
            state='ACTIVE')
    except EmptyPool:
        log.error("Failed to allocate resources for network of type: %s",
                  flavor)
        raise faults.ServiceUnavailable("Failed to allocate network resources")

    # Issue commission to Quotaholder and accept it since at the end of
    # this transaction the Network object will be created in the DB.
    # Note: the following call does a commit!
    quotas.issue_and_accept_commission(network)

    networkdict = network_to_dict(network, request.user_uniq)
    response = render_network(request, networkdict, status=202)

    return response
Ejemplo n.º 2
0
def create(userid,
           name,
           flavor,
           link=None,
           mac_prefix=None,
           mode=None,
           floating_ip_pool=False,
           tags=None,
           public=False,
           drained=False,
           project=None,
           shared_to_project=False):
    if flavor is None:
        raise faults.BadRequest("Missing request parameter 'type'")
    elif flavor not in Network.FLAVORS.keys():
        raise faults.BadRequest("Invalid network type '%s'" % flavor)

    if mac_prefix is not None and flavor == "MAC_FILTERED":
        raise faults.BadRequest("Cannot override MAC_FILTERED mac-prefix")
    if link is not None and flavor == "PHYSICAL_VLAN":
        raise faults.BadRequest("Cannot override PHYSICAL_VLAN link")

    utils.check_name_length(name, Network.NETWORK_NAME_LENGTH, "Network name "
                            "is too long")

    try:
        fmode, flink, fmac_prefix, ftags = util.values_from_flavor(flavor)
    except EmptyPool:
        log.error("Failed to allocate resources for network of type: %s",
                  flavor)
        msg = "Failed to allocate resources for network."
        raise faults.ServiceUnavailable(msg)

    mode = mode or fmode
    link = link or flink
    mac_prefix = mac_prefix or fmac_prefix
    tags = tags or ftags

    validate_mac(mac_prefix + "0:00:00:00")

    # Check that given link is unique!
    if (link is not None and flavor == "IP_LESS_ROUTED"
            and Network.objects.filter(deleted=False, mode=mode,
                                       link=link).exists()):
        msg = "Link '%s' is already used." % link
        raise faults.BadRequest(msg)

    if project is None:
        project = userid

    network = Network.objects.create(name=name,
                                     userid=userid,
                                     project=project,
                                     shared_to_project=shared_to_project,
                                     flavor=flavor,
                                     mode=mode,
                                     link=link,
                                     mac_prefix=mac_prefix,
                                     tags=tags,
                                     public=public,
                                     external_router=public,
                                     floating_ip_pool=floating_ip_pool,
                                     action='CREATE',
                                     state='ACTIVE',
                                     drained=drained)

    if link is None:
        network.link = "%slink-%d" % (settings.BACKEND_PREFIX_ID, network.id)
        network.save()

    # Issue commission to Quotaholder and accept it since at the end of
    # this transaction the Network object will be created in the DB.
    # Note: the following call does a commit!
    if not public:
        quotas.issue_and_accept_commission(network)

    return network
Ejemplo n.º 3
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        dry_run = options["dry_run"]
        name = options['name']
        subnet = options['subnet']
        backend_id = options['backend_id']
        public = options['public']
        flavor = options['flavor']
        mode = options['mode']
        link = options['link']
        mac_prefix = options['mac_prefix']
        tags = options['tags']
        userid = options["owner"]

        if not name:
            raise CommandError("Name is required")
        if not subnet:
            raise CommandError("Subnet is required")
        if not flavor:
            raise CommandError("Flavor is required")
        if public and not backend_id:
            raise CommandError("backend-id is required")
        if not userid and not public:
            raise CommandError("'owner' is required for private networks")

        if mac_prefix and flavor == "MAC_FILTERED":
            raise CommandError("Can not override MAC_FILTERED mac-prefix")
        if link and flavor == "PHYSICAL_VLAN":
            raise CommandError("Can not override PHYSICAL_VLAN link")

        if backend_id:
            backend = get_backend(backend_id)

        fmode, flink, fmac_prefix, ftags = values_from_flavor(flavor)
        mode = mode or fmode
        link = link or flink
        mac_prefix = mac_prefix or fmac_prefix
        tags = tags or ftags

        try:
            validate_mac(mac_prefix + "0:00:00:00")
        except InvalidMacAddress:
            raise CommandError("Invalid MAC prefix '%s'" % mac_prefix)
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)

        if not link or not mode:
            raise CommandError("Can not create network."
                               " No connectivity link or mode")
        netinfo = {
           "name": name,
           "userid": options["owner"],
           "subnet": subnet,
           "gateway": gateway,
           "gateway6": gateway6,
           "subnet6": subnet6,
           "dhcp": options["dhcp"],
           "flavor": flavor,
           "public": public,
           "mode": mode,
           "link": link,
           "mac_prefix": mac_prefix,
           "tags": tags,
           "state": "ACTIVE"}

        if dry_run:
            self.stdout.write("Creating network:\n")
            pprint_table(self.stdout, tuple(netinfo.items()))
            return

        network = Network.objects.create(**netinfo)
        if userid:
            quotas.issue_and_accept_commission(network)

        if backend_id:
            # Create BackendNetwork only to the specified Backend
            network.create_backend_network(backend)
            create_network(network=network, backend=backend, connect=True)
Ejemplo n.º 4
0
def create_network(request):
    # Normal Response Code: 202
    # Error Response Codes: computeFault (400, 500),
    #                       serviceUnavailable (503),
    #                       unauthorized (401),
    #                       badMediaType(415),
    #                       badRequest (400),
    #                       forbidden (403)
    #                       overLimit (413)

    try:
        req = utils.get_request_dict(request)
        log.info('create_network %s', req)

        user_id = request.user_uniq
        try:
            d = req['network']
            name = d['name']
        except KeyError:
            raise faults.BadRequest("Malformed request")

        # Get and validate flavor. Flavors are still exposed as 'type' in the
        # API.
        flavor = d.get("type", None)
        if flavor is None:
            raise faults.BadRequest("Missing request parameter 'type'")
        elif flavor not in Network.FLAVORS.keys():
            raise faults.BadRequest("Invalid network type '%s'" % flavor)
        elif flavor not in settings.API_ENABLED_NETWORK_FLAVORS:
            raise faults.Forbidden("Can not create network of type '%s'" %
                                   flavor)

        public = d.get("public", False)
        if public:
            raise faults.Forbidden("Can not create a public network.")

        dhcp = d.get('dhcp', True)

        # Get and validate network parameters
        subnet = d.get('cidr', '192.168.1.0/24')
        subnet6 = d.get('cidr6', None)
        gateway = d.get('gateway', None)
        gateway6 = d.get('gateway6', None)
        # Check that user provided a valid subnet
        util.validate_network_params(subnet, gateway, subnet6, gateway6)

        try:
            mode, link, mac_prefix, tags = util.values_from_flavor(flavor)
            validate_mac(mac_prefix + "0:00:00:00")
            network = Network.objects.create(
                name=name,
                userid=user_id,
                subnet=subnet,
                subnet6=subnet6,
                gateway=gateway,
                gateway6=gateway6,
                dhcp=dhcp,
                flavor=flavor,
                mode=mode,
                link=link,
                mac_prefix=mac_prefix,
                tags=tags,
                action='CREATE',
                state='ACTIVE')
        except EmptyPool:
            log.error("Failed to allocate resources for network of type: %s",
                      flavor)
            raise faults.ServiceUnavailable("Failed to allocate network"
                                            " resources")

        # Issue commission to Quotaholder and accept it since at the end of
        # this transaction the Network object will be created in the DB.
        # Note: the following call does a commit!
        quotas.issue_and_accept_commission(network)
    except:
        transaction.rollback()
        raise
    else:
        transaction.commit()

    networkdict = network_to_dict(network, request.user_uniq)
    response = render_network(request, networkdict, status=202)

    return response
Ejemplo n.º 5
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        dry_run = options["dry_run"]
        name = options['name']
        subnet = options['subnet']
        backend_id = options['backend_id']
        public = options['public']
        flavor = options['flavor']
        mode = options['mode']
        link = options['link']
        mac_prefix = options['mac_prefix']
        tags = options['tags']
        userid = options["owner"]

        if not name:
            raise CommandError("Name is required")
        if not subnet:
            raise CommandError("Subnet is required")
        if not flavor:
            raise CommandError("Flavor is required")
        if public and not backend_id:
            raise CommandError("backend-id is required")
        if not userid and not public:
            raise CommandError("'owner' is required for private networks")

        if mac_prefix and flavor == "MAC_FILTERED":
            raise CommandError("Can not override MAC_FILTERED mac-prefix")
        if link and flavor == "PHYSICAL_VLAN":
            raise CommandError("Can not override PHYSICAL_VLAN link")

        if backend_id:
            backend = get_backend(backend_id)

        fmode, flink, fmac_prefix, ftags = values_from_flavor(flavor)
        mode = mode or fmode
        link = link or flink
        mac_prefix = mac_prefix or fmac_prefix
        tags = tags or ftags

        try:
            validate_mac(mac_prefix + "0:00:00:00")
        except InvalidMacAddress:
            raise CommandError("Invalid MAC prefix '%s'" % mac_prefix)
        subnet, gateway, subnet6, gateway6 = validate_network_info(options)

        if not link or not mode:
            raise CommandError("Can not create network."
                               " No connectivity link or mode")
        netinfo = {
            "name": name,
            "userid": options["owner"],
            "subnet": subnet,
            "gateway": gateway,
            "gateway6": gateway6,
            "subnet6": subnet6,
            "dhcp": options["dhcp"],
            "flavor": flavor,
            "public": public,
            "mode": mode,
            "link": link,
            "mac_prefix": mac_prefix,
            "tags": tags,
            "state": "ACTIVE"
        }

        if dry_run:
            self.stdout.write("Creating network:\n")
            pprint_table(self.stdout, tuple(netinfo.items()))
            return

        network = Network.objects.create(**netinfo)
        if userid:
            quotas.issue_and_accept_commission(network)

        if backend_id:
            # Create BackendNetwork only to the specified Backend
            network.create_backend_network(backend)
            create_network(network=network, backend=backend, connect=True)
Ejemplo n.º 6
0
def create(userid, name, flavor, link=None, mac_prefix=None, mode=None,
           floating_ip_pool=False, tags=None, public=False, drained=False):
    if flavor is None:
        raise faults.BadRequest("Missing request parameter 'type'")
    elif flavor not in Network.FLAVORS.keys():
        raise faults.BadRequest("Invalid network type '%s'" % flavor)

    if mac_prefix is not None and flavor == "MAC_FILTERED":
        raise faults.BadRequest("Cannot override MAC_FILTERED mac-prefix")
    if link is not None and flavor == "PHYSICAL_VLAN":
        raise faults.BadRequest("Cannot override PHYSICAL_VLAN link")

    utils.check_name_length(name, Network.NETWORK_NAME_LENGTH, "Network name "
                            "is too long")

    try:
        fmode, flink, fmac_prefix, ftags = util.values_from_flavor(flavor)
    except EmptyPool:
        log.error("Failed to allocate resources for network of type: %s",
                  flavor)
        msg = "Failed to allocate resources for network."
        raise faults.ServiceUnavailable(msg)

    mode = mode or fmode
    link = link or flink
    mac_prefix = mac_prefix or fmac_prefix
    tags = tags or ftags

    validate_mac(mac_prefix + "0:00:00:00")

    # Check that given link is unique!
    if (link is not None and flavor == "IP_LESS_ROUTED" and
       Network.objects.filter(deleted=False, mode=mode, link=link).exists()):
        msg = "Link '%s' is already used." % link
        raise faults.BadRequest(msg)

    network = Network.objects.create(
        name=name,
        userid=userid,
        flavor=flavor,
        mode=mode,
        link=link,
        mac_prefix=mac_prefix,
        tags=tags,
        public=public,
        external_router=public,
        floating_ip_pool=floating_ip_pool,
        action='CREATE',
        state='ACTIVE',
        drained=drained)

    if link is None:
        network.link = "%slink-%d" % (settings.BACKEND_PREFIX_ID, network.id)
        network.save()

    # Issue commission to Quotaholder and accept it since at the end of
    # this transaction the Network object will be created in the DB.
    # Note: the following call does a commit!
    if not public:
        quotas.issue_and_accept_commission(network)

    return network