示例#1
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)
示例#2
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)
示例#3
0
文件: ports.py 项目: vgerak/synnefo
def update_port(request, port_id):
    '''
    You can update only name, security_groups
    '''
    credentials = request.credentials
    req = api.utils.get_json_body(request)

    log.debug("User %s, Port %s, Action: update, Request: %s",
              credentials.userid, 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)

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

    sg_list = []
    if security_groups:
        #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)

    port = servers.update_port(port_id,
                               credentials,
                               name=name,
                               security_groups=sg_list)
    return render_port(request, port_to_dict(port), 200)
示例#4
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        name = options["name"]
        user_id = options["user_id"]
        network_id = options["network_id"]
        server_id = options["server_id"]
        #router_id = options["router_id"]
        router_id = None
        # assume giving security groups comma separated
        security_group_ids = options["security-groups"]
        wait = parse_bool(options["wait"])

        if not name:
            name = ""

        if not network_id:
            raise CommandError("Please specify a 'network'")

        vm = None
        owner = None
        if server_id:
            owner = "vm"
            vm = common.get_resource("server", server_id)
            #if vm.router:
            #    raise CommandError("Server '%s' does not exist." % server_id)
        elif router_id:
            owner = "router"
            vm = common.get_resource("server", router_id)
            if not vm.router:
                raise CommandError("Router '%s' does not exist." % router_id)

        if user_id is None:
            if vm is not None:
                user_id = vm.userid
            else:
                raise CommandError("Please specify the owner of the port.")

        # Get either floating IP or fixed ip address
        ipaddress = None
        floating_ip_id = options["floating_ip_id"]
        ipv4_address = options["ipv4_address"]
        if floating_ip_id:
            ipaddress = common.get_resource("floating-ip", floating_ip_id)
            address = ipaddress.address
            if ipv4_address is not None and address != ipv4_address:
                raise CommandError("Floating IP address '%s' is different from"
                                   " specified address '%s'" %
                                   (address, ipv4_address))
        else:
            address = ipv4_address

        # validate security groups
        sg_list = []
        if security_group_ids:
            security_group_ids = security_group_ids.split(",")
            for gid in security_group_ids:
                sg = util.get_security_group(int(gid))
                sg_list.append(sg)

        credentials = Credentials(user_id)
        new_port = servers.create_port(credentials,
                                       network_id,
                                       machine=vm.id,
                                       name=name,
                                       address=address,
                                       security_groups=sg_list,
                                       device_owner=owner)
        self.stdout.write("Created port '%s' in DB:\n" % new_port)
        pprint.pprint_port(new_port, stdout=self.stdout)
        pprint.pprint_port_ips(new_port, stdout=self.stdout)
        self.stdout.write("\n")
        if vm is not None:
            common.wait_server_task(new_port.machine, wait, stdout=self.stdout)
示例#5
0
def create_port(request):
    user_id = request.user_uniq
    req = api.utils.get_json_body(request)

    log.debug("User: %s, Action: create_port, Request: %s", user_id, req)

    port_dict = api.utils.get_attribute(req, "port", attr_type=dict)
    net_id = api.utils.get_attribute(port_dict,
                                     "network_id",
                                     attr_type=(basestring, int))

    device_id = api.utils.get_attribute(port_dict,
                                        "device_id",
                                        required=False,
                                        attr_type=(basestring, int))
    vm = None
    if device_id is not None:
        vm = util.get_vm(device_id,
                         user_id,
                         request.user_projects,
                         for_update=True,
                         non_deleted=True,
                         non_suspended=True)

    # Check if the request contains a valid IPv4 address
    fixed_ips = api.utils.get_attribute(port_dict,
                                        "fixed_ips",
                                        required=False,
                                        attr_type=list)
    if fixed_ips is not None and len(fixed_ips) > 0:
        if len(fixed_ips) > 1:
            msg = "'fixed_ips' attribute must contain only one fixed IP."
            raise faults.BadRequest(msg)
        fixed_ip = fixed_ips[0]
        if not isinstance(fixed_ip, dict):
            raise faults.BadRequest("Invalid 'fixed_ips' field.")
        fixed_ip_address = fixed_ip.get("ip_address")
        if fixed_ip_address is not None:
            try:
                ip = ipaddr.IPAddress(fixed_ip_address)
                if ip.version == 6:
                    msg = "'ip_address' can be only an IPv4 address'"
                    raise faults.BadRequest(msg)
            except ValueError:
                msg = "%s is not a valid IPv4 Address" % fixed_ip_address
                raise faults.BadRequest(msg)
    else:
        fixed_ip_address = None

    network = util.get_network(net_id,
                               user_id,
                               request.user_projects,
                               non_deleted=True,
                               for_update=True)

    ipaddress = None
    if network.public:
        # Creating a port to a public network is only allowed if the user has
        # already a floating IP address in this network which is specified
        # as the fixed IP address of the port
        if fixed_ip_address is None:
            msg = ("'fixed_ips' attribute must contain a floating IP address"
                   " in order to connect to a public network.")
            raise faults.BadRequest(msg)
        ipaddress = util.get_floating_ip_by_address(user_id,
                                                    request.user_projects,
                                                    fixed_ip_address,
                                                    for_update=True)
    elif fixed_ip_address:
        ipaddress = ips.allocate_ip(network, user_id, address=fixed_ip_address)

    name = api.utils.get_attribute(port_dict,
                                   "name",
                                   required=False,
                                   attr_type=basestring)
    if name is None:
        name = ""

    security_groups = api.utils.get_attribute(port_dict,
                                              "security_groups",
                                              required=False,
                                              attr_type=list)
    #validate security groups
    # like get security group from db
    sg_list = []
    if 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)

    new_port = servers.create_port(user_id,
                                   network,
                                   use_ipaddress=ipaddress,
                                   machine=vm,
                                   name=name)

    log.info("User %s created port %s, network: %s, machine: %s, ip: %s",
             user_id, new_port.id, network, vm, ipaddress)

    response = render_port(request, port_to_dict(new_port), status=201)

    return response
示例#6
0
文件: ports.py 项目: AthinaB/synnefo
def create_port(request):
    user_id = request.user_uniq
    req = api.utils.get_json_body(request)
    log.info('create_port user: %s request: %s', user_id, req)

    port_dict = api.utils.get_attribute(req, "port", attr_type=dict)
    net_id = api.utils.get_attribute(port_dict, "network_id",
                                     attr_type=(basestring, int))

    device_id = api.utils.get_attribute(port_dict, "device_id", required=False,
                                        attr_type=(basestring, int))
    vm = None
    if device_id is not None:
        vm = util.get_vm(device_id, user_id, for_update=True, non_deleted=True,
                         non_suspended=True)

    # Check if the request contains a valid IPv4 address
    fixed_ips = api.utils.get_attribute(port_dict, "fixed_ips", required=False,
                                        attr_type=list)
    if fixed_ips is not None and len(fixed_ips) > 0:
        if len(fixed_ips) > 1:
            msg = "'fixed_ips' attribute must contain only one fixed IP."
            raise faults.BadRequest(msg)
        fixed_ip = fixed_ips[0]
        if not isinstance(fixed_ip, dict):
            raise faults.BadRequest("Invalid 'fixed_ips' field.")
        fixed_ip_address = fixed_ip.get("ip_address")
        if fixed_ip_address is not None:
            try:
                ip = ipaddr.IPAddress(fixed_ip_address)
                if ip.version == 6:
                    msg = "'ip_address' can be only an IPv4 address'"
                    raise faults.BadRequest(msg)
            except ValueError:
                msg = "%s is not a valid IPv4 Address" % fixed_ip_address
                raise faults.BadRequest(msg)
    else:
        fixed_ip_address = None

    network = util.get_network(net_id, user_id, non_deleted=True,
                               for_update=True)

    ipaddress = None
    if network.public:
        # Creating a port to a public network is only allowed if the user has
        # already a floating IP address in this network which is specified
        # as the fixed IP address of the port
        if fixed_ip_address is None:
            msg = ("'fixed_ips' attribute must contain a floating IP address"
                   " in order to connect to a public network.")
            raise faults.BadRequest(msg)
        ipaddress = util.get_floating_ip_by_address(user_id, fixed_ip_address,
                                                    for_update=True)
    elif fixed_ip_address:
        ipaddress = ips.allocate_ip(network, user_id,
                                    address=fixed_ip_address)

    name = api.utils.get_attribute(port_dict, "name", required=False,
                                   attr_type=basestring)
    if name is None:
        name = ""

    security_groups = api.utils.get_attribute(port_dict,
                                              "security_groups",
                                              required=False,
                                              attr_type=list)
    #validate security groups
    # like get security group from db
    sg_list = []
    if 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)

    new_port = servers.create_port(user_id, network, use_ipaddress=ipaddress,
                                   machine=vm, name=name)

    response = render_port(request, port_to_dict(new_port), status=201)

    return response
示例#7
0
文件: ports.py 项目: vgerak/synnefo
def create_port(request):
    credentials = request.credentials
    user_id = credentials.userid
    req = api.utils.get_json_body(request)

    log.debug("User: %s, Action: create_port, Request: %s", user_id, req)

    port_dict = api.utils.get_attribute(req, "port", attr_type=dict)
    net_id = api.utils.get_attribute(port_dict,
                                     "network_id",
                                     attr_type=(basestring, int))

    device_id = api.utils.get_attribute(port_dict,
                                        "device_id",
                                        required=False,
                                        attr_type=(basestring, int))

    # Check if the request contains a valid IPv4 address
    fixed_ips = api.utils.get_attribute(port_dict,
                                        "fixed_ips",
                                        required=False,
                                        attr_type=list)
    if fixed_ips is not None and len(fixed_ips) > 0:
        if len(fixed_ips) > 1:
            msg = "'fixed_ips' attribute must contain only one fixed IP."
            raise faults.BadRequest(msg)
        fixed_ip = fixed_ips[0]
        if not isinstance(fixed_ip, dict):
            raise faults.BadRequest("Invalid 'fixed_ips' field.")
        fixed_ip_address = fixed_ip.get("ip_address")
        if fixed_ip_address is not None:
            try:
                ip = ipaddr.IPAddress(fixed_ip_address)
                if ip.version == 6:
                    msg = "'ip_address' can be only an IPv4 address'"
                    raise faults.BadRequest(msg)
            except ValueError:
                msg = "%s is not a valid IPv4 Address" % fixed_ip_address
                raise faults.BadRequest(msg)
    else:
        fixed_ip_address = None

    name = api.utils.get_attribute(port_dict,
                                   "name",
                                   required=False,
                                   attr_type=basestring)
    if name is None:
        name = ""

    security_groups = api.utils.get_attribute(port_dict,
                                              "security_groups",
                                              required=False,
                                              attr_type=list)
    #validate security groups
    # like get security group from db
    sg_list = []
    if 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)

    new_port = servers.create_port(credentials,
                                   net_id,
                                   address=fixed_ip_address,
                                   machine_id=device_id,
                                   name=name,
                                   security_groups=sg_list)

    response = render_port(request, port_to_dict(new_port), status=201)

    return response
示例#8
0
    def handle(self, *args, **options):
        if args:
            raise CommandError("Command doesn't accept any arguments")

        name = options["name"]
        user_id = options["user_id"]
        network_id = options["network_id"]
        server_id = options["server_id"]
        #router_id = options["router_id"]
        router_id = None
        # assume giving security groups comma separated
        security_group_ids = options["security-groups"]
        wait = parse_bool(options["wait"])

        if not name:
            name = ""

        if not network_id:
            raise CommandError("Please specify a 'network'")

        vm = None
        owner = None
        if server_id:
            owner = "vm"
            vm = common.get_resource("server", server_id)
            #if vm.router:
            #    raise CommandError("Server '%s' does not exist." % server_id)
        elif router_id:
            owner = "router"
            vm = common.get_resource("server", router_id)
            if not vm.router:
                raise CommandError("Router '%s' does not exist." % router_id)

        if user_id is None:
            if vm is not None:
                user_id = vm.userid
            else:
                raise CommandError("Please specify the owner of the port.")

        # Get either floating IP or fixed ip address
        ipaddress = None
        floating_ip_id = options["floating_ip_id"]
        ipv4_address = options["ipv4_address"]
        if floating_ip_id:
            ipaddress = common.get_resource("floating-ip", floating_ip_id)
            address = ipaddress.address
            if ipv4_address is not None and address != ipv4_address:
                raise CommandError("Floating IP address '%s' is different from"
                                   " specified address '%s'" %
                                   (address, ipv4_address))
        else:
            address = ipv4_address

        # validate security groups
        sg_list = []
        if security_group_ids:
            security_group_ids = security_group_ids.split(",")
            for gid in security_group_ids:
                sg = util.get_security_group(int(gid))
                sg_list.append(sg)

        credentials = Credentials(user_id)
        new_port = servers.create_port(credentials, network_id, machine=vm.id,
                                       name=name,
                                       address=address,
                                       security_groups=sg_list,
                                       device_owner=owner)
        self.stdout.write("Created port '%s' in DB:\n" % new_port)
        pprint.pprint_port(new_port, stdout=self.stdout)
        pprint.pprint_port_ips(new_port, stdout=self.stdout)
        self.stdout.write("\n")
        if vm is not None:
            common.wait_server_task(new_port.machine, wait, stdout=self.stdout)