From 0b85e31489fb511b9cdd8e49e179ebdd3c6a6654 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Thu, 13 Jun 2024 11:05:57 +0800 Subject: [PATCH] feat(climc): update port_mappings of server network (#20518) --- cmd/climc/shell/compute/servernetworks.go | 24 ++++++++++++++++------- pkg/cloudcommon/cmdline/parser.go | 3 +++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/cmd/climc/shell/compute/servernetworks.go b/cmd/climc/shell/compute/servernetworks.go index ef40516195..633f0b028f 100644 --- a/cmd/climc/shell/compute/servernetworks.go +++ b/cmd/climc/shell/compute/servernetworks.go @@ -19,6 +19,7 @@ import ( "strconv" "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/printutils" "yunion.io/x/pkg/util/regutils" @@ -88,13 +89,14 @@ func init() { }) type ServerNetworkUpdateOptions struct { - SERVER string `help:"ID or Name of Server"` - NETWORK string `help:"ID or Name of Wire"` - Mac string `help:"Mac of NIC"` - Driver string `help:"Driver model of vNIC" choices:"virtio|e1000|vmxnet3|rtl8139"` - Index int64 `help:"Index of NIC" default:"-1"` - Ifname string `help:"Interface name of vNIC on host"` - Default bool `help:"is default nic?"` + SERVER string `help:"ID or Name of Server"` + NETWORK string `help:"ID or Name of Wire"` + Mac string `help:"Mac of NIC"` + Driver string `help:"Driver model of vNIC" choices:"virtio|e1000|vmxnet3|rtl8139"` + Index int64 `help:"Index of NIC" default:"-1"` + Ifname string `help:"Interface name of vNIC on host"` + Default bool `help:"is default nic?"` + PortMapping []string `help:"Network port mapping, e.g. 'port=80,host_port=8080,protocol=,host_port_range=-,remote_ips=x.x.x.x|y.y.y.y'" short-token:"p"` } R(&ServerNetworkUpdateOptions{}, "server-network-update", "Update server network settings", func(s *mcclient.ClientSession, args *ServerNetworkUpdateOptions) error { params := jsonutils.NewDict() @@ -110,6 +112,14 @@ func init() { if args.Default { params.Add(jsonutils.JSONTrue, "is_default") } + if len(args.PortMapping) > 0 { + psm, err := cmdline.ParseNetworkConfigPortMappings(args.PortMapping) + if err != nil { + return errors.Wrap(err, "parse port mapping") + } + ps := psm[0] + params.Add(jsonutils.Marshal(ps), "port_mappings") + } if params.Size() == 0 { return InvalidUpdateError() } diff --git a/pkg/cloudcommon/cmdline/parser.go b/pkg/cloudcommon/cmdline/parser.go index 947fb32a12..5f16aa6374 100644 --- a/pkg/cloudcommon/cmdline/parser.go +++ b/pkg/cloudcommon/cmdline/parser.go @@ -417,6 +417,9 @@ func parseNetworkConfigPortMapping(desc string) (int, *compute.GuestPortMapping, Start: start, End: end, } + case "remote_ips", "remote_ip": + ips := strings.Split(val, "|") + pm.RemoteIps = ips } } if pm.Port == 0 {