Skip to content

Commit

Permalink
dhcp options as separate input fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 29, 2024
1 parent 5e52c90 commit 0733dc0
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 71 deletions.
50 changes: 0 additions & 50 deletions cmd/private_network.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package cmd

import (
"fmt"
"net"
"strings"

v3 "github.com/exoscale/egoscale/v3"
"github.com/spf13/cobra"
)

Expand All @@ -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)
}
47 changes: 37 additions & 10 deletions cmd/private_network_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
}

Expand Down
53 changes: 42 additions & 11 deletions cmd/private_network_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 0733dc0

Please sign in to comment.