From 0733dc0ad3aff7ce5a566080a586d9925642e762 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 29 Oct 2024 16:24:29 +0100 Subject: [PATCH] dhcp options as separate input fields. --- cmd/private_network.go | 50 --------------------------------- cmd/private_network_create.go | 47 ++++++++++++++++++++++++------- cmd/private_network_update.go | 53 +++++++++++++++++++++++++++-------- 3 files changed, 79 insertions(+), 71 deletions(-) diff --git a/cmd/private_network.go b/cmd/private_network.go index 931b2f2ae..4c0b33fa3 100644 --- a/cmd/private_network.go +++ b/cmd/private_network.go @@ -1,11 +1,6 @@ package cmd import ( - "fmt" - "net" - "strings" - - v3 "github.com/exoscale/egoscale/v3" "github.com/spf13/cobra" ) @@ -15,51 +10,6 @@ var privateNetworkCmd = &cobra.Command{ Aliases: []string{"privnet"}, } -func processPrivateNetworkOptions(options []string) (*v3.PrivateNetworkOptions, error) { - opts := &v3.PrivateNetworkOptions{} - optionsMap := make(map[string][]string) - - // Process each option flag - for _, opt := range options { - keyValue := strings.SplitN(opt, "=", 2) - if len(keyValue) != 2 { - return nil, fmt.Errorf("malformed option %q: must be in format key=\"value1 value2\"", opt) - } - key := keyValue[0] - values := strings.Split(keyValue[1], " ") - optionsMap[key] = append(optionsMap[key], values...) - } - - // Process collected values - for key, values := range optionsMap { - switch key { - case "dns-servers": - for _, v := range values { - if ip := net.ParseIP(v); ip != nil { - opts.DNSServers = append(opts.DNSServers, ip) - } - } - case "ntp-servers": - for _, v := range values { - if ip := net.ParseIP(v); ip != nil { - opts.NtpServers = append(opts.NtpServers, ip) - } - } - case "routers": - for _, v := range values { - if ip := net.ParseIP(v); ip != nil { - opts.Routers = append(opts.Routers, ip) - } - } - case "domain-search": - opts.DomainSearch = values - default: - return nil, fmt.Errorf("unrecognized option key %q: supported keys are: 'dns-servers', 'ntp-servers', 'routers', 'domain-search'", key) - } - } - return opts, nil -} - func init() { computeCmd.AddCommand(privateNetworkCmd) } diff --git a/cmd/private_network_create.go b/cmd/private_network_create.go index 37e35005a..8bfd0c325 100644 --- a/cmd/private_network_create.go +++ b/cmd/private_network_create.go @@ -20,12 +20,15 @@ type privateNetworkCreateCmd struct { Name string `cli-arg:"#"` - Description string `cli-usage:"Private Network description"` - EndIP string `cli-usage:"managed Private Network range end IP address"` - Netmask string `cli-usage:"managed Private Network netmask"` - StartIP string `cli-usage:"managed Private Network range start IP address"` - Zone v3.ZoneName `cli-short:"z" cli-usage:"Private Network zone"` - Option []string `cli-usage:"DHCP network option (format: option1=\"value1 value2\")" cli-flag-multi:"true"` + Description string `cli-usage:"Private Network description"` + EndIP string `cli-usage:"managed Private Network range end IP address"` + StartIP string `cli-usage:"managed Private Network range start IP address"` + Zone v3.ZoneName `cli-short:"z" cli-usage:"Private Network zone"` + Netmask string `cli-usage:"managed Private Network netmask"` + DNSServers []string `cli-usage:"DNS servers"` + NTPServers []string `cli-usage:"NTP servers"` + Routers []string `cli-usage:"Routers"` + DomainSearch []string `cli-usage:"Domain search list, limited to 255 octets"` } func (c *privateNetworkCreateCmd) cmdAliases() []string { return gCreateAlias } @@ -81,11 +84,35 @@ func (c *privateNetworkCreateCmd) cmdRun(_ *cobra.Command, _ []string) error { }(), } - if len(c.Option) > 0 { - opts, err := processPrivateNetworkOptions(c.Option) - if err != nil { - return err + // Process DHCP options if any are specified + if len(c.DNSServers) > 0 || len(c.NTPServers) > 0 || len(c.Routers) > 0 || len(c.DomainSearch) > 0 { + opts := &v3.PrivateNetworkOptions{} + + for _, server := range c.DNSServers { + if ip := net.ParseIP(server); ip != nil { + opts.DNSServers = append(opts.DNSServers, ip) + } else { + return fmt.Errorf("invalid DNS server IP address: %q", server) + } + } + + for _, server := range c.NTPServers { + if ip := net.ParseIP(server); ip != nil { + opts.NtpServers = append(opts.NtpServers, ip) + } else { + return fmt.Errorf("invalid NTP server IP address: %q", server) + } } + + for _, router := range c.Routers { + if ip := net.ParseIP(router); ip != nil { + opts.Routers = append(opts.Routers, ip) + } else { + return fmt.Errorf("invalid router IP address: %q", router) + } + } + + opts.DomainSearch = c.DomainSearch req.Options = opts } diff --git a/cmd/private_network_update.go b/cmd/private_network_update.go index adcd803d1..a5b707f32 100644 --- a/cmd/private_network_update.go +++ b/cmd/private_network_update.go @@ -19,13 +19,16 @@ type privateNetworkUpdateCmd struct { PrivateNetwork string `cli-arg:"#" cli-usage:"NAME|ID"` - Description string `cli-usage:"Private Network description"` - EndIP string `cli-usage:"managed Private Network range end IP address"` - Name string `cli-usage:"Private Network name"` - Netmask string `cli-usage:"managed Private Network netmask"` - StartIP string `cli-usage:"managed Private Network range start IP address"` - Zone v3.ZoneName `cli-short:"z" cli-usage:"Private Network zone"` - Option []string `cli-usage:"DHCP network option (format: option1=\"value1 value2\")" cli-flag-multi:"true"` + Description string `cli-usage:"Private Network description"` + EndIP string `cli-usage:"managed Private Network range end IP address"` + Name string `cli-usage:"Private Network name"` + StartIP string `cli-usage:"managed Private Network range start IP address"` + Zone v3.ZoneName `cli-short:"z" cli-usage:"Private Network zone"` + Netmask string `cli-usage:"managed Private Network netmask"` + DNSServers []string `cli-usage:"DNS servers"` + NTPServers []string `cli-usage:"NTP servers"` + Routers []string `cli-usage:"Routers"` + DomainSearch []string `cli-usage:"Domain search list, limited to 255 octets"` } func (c *privateNetworkUpdateCmd) cmdAliases() []string { return nil } @@ -93,11 +96,39 @@ func (c *privateNetworkUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error { updated = true } - if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Option)) { - opts, err := processPrivateNetworkOptions(c.Option) - if err != nil { - return err + // Process DHCP options if any are changed + if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.DNSServers)) || + cmd.Flags().Changed(mustCLICommandFlagName(c, &c.NTPServers)) || + cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Routers)) || + cmd.Flags().Changed(mustCLICommandFlagName(c, &c.DomainSearch)) { + + opts := &v3.PrivateNetworkOptions{} + + for _, server := range c.DNSServers { + if ip := net.ParseIP(server); ip != nil { + opts.DNSServers = append(opts.DNSServers, ip) + } else { + return fmt.Errorf("invalid DNS server IP address: %q", server) + } } + + for _, server := range c.NTPServers { + if ip := net.ParseIP(server); ip != nil { + opts.NtpServers = append(opts.NtpServers, ip) + } else { + return fmt.Errorf("invalid NTP server IP address: %q", server) + } + } + + for _, router := range c.Routers { + if ip := net.ParseIP(router); ip != nil { + opts.Routers = append(opts.Routers, ip) + } else { + return fmt.Errorf("invalid router IP address: %q", router) + } + } + + opts.DomainSearch = c.DomainSearch updateReq.Options = opts updated = true }