Exemple #1
0
def _port_for_request(user_id, network_dict):
    if not isinstance(network_dict, dict):
        raise faults.BadRequest("Malformed request. Invalid 'networks' field")
    port_id = network_dict.get("port")
    network_id = network_dict.get("uuid")
    if port_id is not None:
        return util.get_port(port_id, user_id, for_update=True)
    elif network_id is not None:
        address = network_dict.get("fixed_ip")
        network = util.get_network(network_id, user_id, non_deleted=True)
        if network.public:
            if network.subnet4 is not None:
                if not "fixed_ip" in network_dict:
                    return create_public_ipv4_port(user_id, network)
                elif address is None:
                    msg = "Cannot connect to public network"
                    raise faults.BadRequest(msg % network.id)
                else:
                    return create_public_ipv4_port(user_id, network, address)
            else:
                raise faults.Forbidden("Cannot connect to IPv6 only public"
                                       " network %" % network.id)
        else:
            return _create_port(user_id, network, address=address)
    else:
        raise faults.BadRequest("Network 'uuid' or 'port' attribute"
                                " is required.")
Exemple #2
0
def _port_for_request(user_id, network_dict):
    if not isinstance(network_dict, dict):
        raise faults.BadRequest("Malformed request. Invalid 'networks' field")
    port_id = network_dict.get("port")
    network_id = network_dict.get("uuid")
    if port_id is not None:
        return util.get_port(port_id, user_id, for_update=True)
    elif network_id is not None:
        address = network_dict.get("fixed_ip")
        network = util.get_network(network_id, user_id, non_deleted=True)
        if network.public:
            if network.subnet4 is not None:
                if not "fixed_ip" in network_dict:
                    return create_public_ipv4_port(user_id, network)
                elif address is None:
                    msg = "Cannot connect to public network"
                    raise faults.BadRequest(msg % network.id)
                else:
                    return create_public_ipv4_port(user_id, network, address)
            else:
                raise faults.Forbidden("Cannot connect to IPv6 only public"
                                       " network '%s'" % network.id)
        else:
            return _create_port(user_id, network, address=address)
    else:
        raise faults.BadRequest("Network 'uuid' or 'port' attribute"
                                " is required.")
Exemple #3
0
def delete_port(request, port_id):
    log.info('delete_port %s', port_id)
    user_id = request.user_uniq
    port = util.get_port(port_id, user_id, for_update=True)

    # Deleting port that is connected to a public network is allowed only if
    # the port has an associated floating IP address.
    if port.network.public and not port.ips.filter(floating_ip=True,
                                                   deleted=False).exists():
        raise faults.Forbidden("Cannot disconnect from public network.")

    servers.delete_port(port)
    return HttpResponse(status=204)
Exemple #4
0
def delete_port(request, port_id):
    log.info('delete_port %s', port_id)
    user_id = request.user_uniq
    port = util.get_port(port_id, user_id, for_update=True)

    # Deleting port that is connected to a public network is allowed only if
    # the port has an associated floating IP address.
    if port.network.public and not port.ips.filter(floating_ip=True,
                                                   deleted=False).exists():
        raise faults.Forbidden("Cannot disconnect from public network.")

    servers.delete_port(port)
    return HttpResponse(status=204)
Exemple #5
0
def update_port(port_id, credentials, name=None, security_groups=None):
    port = util.get_port(port_id, credentials, for_update=True)
    if name:
        port.name = name

    if security_groups:
        #clear the old security groups
        port.security_groups.clear()

        #add the new groups
        port.security_groups.add(*security_groups)
    port.save()
    log.info("User %s updated port %s", credentials.userid, port_id)
    return port
Exemple #6
0
def update_port(port_id, credentials, name=None, security_groups=None):
    port = util.get_port(port_id, credentials, for_update=True)
    if name:
        port.name = name

    if security_groups:
        #clear the old security groups
        port.security_groups.clear()

        #add the new groups
        port.security_groups.add(*security_groups)
    port.save()
    log.info("User %s updated port %s", credentials.userid, port_id)
    return port
Exemple #7
0
def update_port(request, port_id):
    '''
    You can update only name, security_groups
    '''
    port = util.get_port(port_id,
                         request.user_uniq,
                         request.user_projects,
                         for_update=True)
    req = api.utils.get_json_body(request)

    log.debug("User %s, Port %s, Action: update, Request: %s",
              request.user_uniq, port_id, req)

    port_info = api.utils.get_attribute(req,
                                        "port",
                                        required=True,
                                        attr_type=dict)
    name = api.utils.get_attribute(port_info,
                                   "name",
                                   required=False,
                                   attr_type=basestring)

    if name:
        port.name = name

    security_groups = api.utils.get_attribute(port_info,
                                              "security_groups",
                                              required=False,
                                              attr_type=list)

    if security_groups:
        sg_list = []
        #validate security groups
        for gid in security_groups:
            try:
                sg = util.get_security_group(int(gid))
            except (KeyError, ValueError):
                raise faults.BadRequest("Invalid 'security_groups' field.")
            sg_list.append(sg)

        #clear the old security groups
        port.security_groups.clear()

        #add the new groups
        port.security_groups.add(*sg_list)
    port.save()

    log.info("User %s updated port %s", request.user_uniq, port.id)

    return render_port(request, port_to_dict(port), 200)
Exemple #8
0
def delete_port(port_id, credentials):
    user_id = credentials.userid
    port = util.get_port(port_id, credentials, for_update=True)

    # Deleting port that is connected to a public network is allowed only if
    # the port has an associated floating IP address.
    if port.network.public and not port.ips.filter(floating_ip=True,
                                                   deleted=False).exists():
        raise faults.Forbidden("Cannot disconnect from public network.")

    vm = port.machine
    if vm is not None and vm.suspended:
        raise faults.Forbidden("Administratively Suspended VM.")

    _delete_port(port)

    log.info("User %s deleted port %s", user_id, port_id)
Exemple #9
0
def delete_port(port_id, credentials):
    user_id = credentials.userid
    port = util.get_port(port_id, credentials, for_update=True)

    # Deleting port that is connected to a public network is allowed only if
    # the port has an associated floating IP address.
    if port.network.public and not port.ips.filter(floating_ip=True,
                                                   deleted=False).exists():
        raise faults.Forbidden("Cannot disconnect from public network.")

    vm = port.machine
    if vm is not None and vm.suspended:
        raise faults.Forbidden("Administratively Suspended VM.")

    _delete_port(port)

    log.info("User %s deleted port %s", user_id, port_id)
Exemple #10
0
def update_port(request, port_id):
    '''
    You can update only name, security_groups
    '''
    port = util.get_port(port_id, request.user_uniq, request.user_projects,
                         for_update=True)
    req = api.utils.get_json_body(request)

    log.debug("User %s, Port %s, Action: update, Request: %s",
              request.user_uniq, port_id, req)

    port_info = api.utils.get_attribute(req, "port", required=True,
                                        attr_type=dict)
    name = api.utils.get_attribute(port_info, "name", required=False,
                                   attr_type=basestring)

    if name:
        port.name = name

    security_groups = api.utils.get_attribute(port_info, "security_groups",
                                              required=False, attr_type=list)

    if security_groups:
        sg_list = []
        #validate security groups
        for gid in security_groups:
            try:
                sg = util.get_security_group(int(gid))
            except (KeyError, ValueError):
                raise faults.BadRequest("Invalid 'security_groups' field.")
            sg_list.append(sg)

        #clear the old security groups
        port.security_groups.clear()

        #add the new groups
        port.security_groups.add(*sg_list)
    port.save()

    log.info("User %s updated port %s", request.user_uniq, port.id)

    return render_port(request, port_to_dict(port), 200)
Exemple #11
0
def delete_port(request, port_id):
    log.info('delete_port %s', port_id)
    user_id = request.user_uniq
    port = util.get_port(port_id, user_id, request.user_projects,
                         for_update=True)

    # Deleting port that is connected to a public network is allowed only if
    # the port has an associated floating IP address.
    if port.network.public and not port.ips.filter(floating_ip=True,
                                                   deleted=False).exists():
        raise faults.Forbidden("Cannot disconnect from public network.")

    vm = port.machine
    if vm is not None and vm.suspended:
        raise faults.Forbidden("Administratively Suspended VM.")

    servers.delete_port(port)

    log.info("User %s deleted port %s", user_id, port_id)

    return HttpResponse(status=204)
Exemple #12
0
def delete_port(request, port_id):
    log.info('delete_port %s', port_id)
    user_id = request.user_uniq
    port = util.get_port(port_id,
                         user_id,
                         request.user_projects,
                         for_update=True)

    # Deleting port that is connected to a public network is allowed only if
    # the port has an associated floating IP address.
    if port.network.public and not port.ips.filter(floating_ip=True,
                                                   deleted=False).exists():
        raise faults.Forbidden("Cannot disconnect from public network.")

    vm = port.machine
    if vm is not None and vm.suspended:
        raise faults.Forbidden("Administratively Suspended VM.")

    servers.delete_port(port)

    log.info("User %s deleted port %s", user_id, port_id)

    return HttpResponse(status=204)
Exemple #13
0
def get_port_details(request, port_id):
    port = util.get_port(port_id, request.user_uniq, request.user_projects)
    return render_port(request, port_to_dict(port))
Exemple #14
0
def get_port_details(request, port_id):
    log.debug('get_port_details %s', port_id)
    port = util.get_port(port_id, request.user_uniq)
    return render_port(request, port_to_dict(port))
Exemple #15
0
def get_port_details(request, port_id):
    log.debug('get_port_details %s', port_id)
    port = util.get_port(port_id, request.user_uniq)
    return render_port(request, port_to_dict(port))
Exemple #16
0
def get_port_details(request, port_id):
    port = util.get_port(port_id, request.credentials)
    return render_port(request, port_to_dict(port))
Exemple #17
0
def get_port_details(request, port_id):
    port = util.get_port(port_id, request.user_uniq, request.user_projects)
    return render_port(request, port_to_dict(port))