示例#1
0
def add_floating_ip(request, vm, args):
    address = args.get("address")
    if address is None:
        raise faults.BadRequest("Missing 'address' attribute")

    userid = vm.userid
    floating_ip = util.get_floating_ip_by_address(userid, address,
                                                  for_update=True)
    servers.create_port(userid, floating_ip.network, machine=vm,
                        user_ipaddress=floating_ip)
    return HttpResponse(status=202)
示例#2
0
def add_floating_ip(request, vm, args):
    address = args.get("address")
    if address is None:
        raise faults.BadRequest("Missing 'address' attribute")

    userid = vm.userid
    floating_ip = util.get_floating_ip_by_address(userid,
            request.user_projects, address, for_update=True)

    servers.create_port(userid, floating_ip.network, machine=vm,
                        user_ipaddress=floating_ip)

    log.info("User %s attached floating IP %s to VM %s, address: %s,"
             " network %s", request.user_uniq, floating_ip.id, vm.id,
             floating_ip.address, floating_ip.network_id)

    return HttpResponse(status=202)
示例#3
0
    def handle(self, *args, **options):
        if not args or len(args) > 1:
            raise CommandError("Command accepts exactly one argument")

        floating_ip_id = args[0]  # this is the floating-ip address
        device = options['machine']

        if not device:
            raise CommandError('Please give either a server or a router id')

        #get the vm
        vm = common.get_resource("server", device, for_update=True)
        floating_ip = common.get_resource("floating-ip", floating_ip_id,
                                          for_update=True)
        servers.create_port(vm.userid, floating_ip.network,
                            use_ipaddress=floating_ip, machine=vm)

        self.stdout.write("Attached %s to %s.\n" % (floating_ip, vm))
示例#4
0
    def handle(self, *args, **options):
        if not args or len(args) > 1:
            raise CommandError("Command accepts exactly one argument")

        floating_ip_id = args[0]  # this is the floating-ip address
        device = options['machine']

        if not device:
            raise CommandError('Please give either a server or a router id')

        #get the vm
        vm = common.get_resource("server", device)
        floating_ip = common.get_resource("floating-ip", floating_ip_id)
        credentials = Credentials(vm.userid)
        servers.create_port(credentials, floating_ip.network_id,
                            address=floating_ip.address, machine_id=device)

        self.stdout.write("Attached %s to %s.\n" % (floating_ip, vm))
示例#5
0
    def handle(self, *args, **options):
        if not args or len(args) > 1:
            raise CommandError("Command accepts exactly one argument")

        floating_ip_id = args[0]  # this is the floating-ip address
        device = options['machine']

        if not device:
            raise CommandError('Please give either a server or a router id')

        #get the vm
        vm = common.get_vm(device, for_update=True)
        floating_ip = common.get_floating_ip_by_id(floating_ip_id,
                                                   for_update=True)
        servers.create_port(vm.userid,
                            floating_ip.network,
                            use_ipaddress=floating_ip,
                            machine=vm)

        self.stdout.write("Attached %s to %s.\n" % (floating_ip, vm))
示例#6
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)
示例#7
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
示例#8
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
示例#9
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
示例#10
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)