Skip to content

Commit

Permalink
process each flag seaparately.
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Oct 29, 2024
1 parent c9a4de7 commit c68e515
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
16 changes: 11 additions & 5 deletions cmd/private_network_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,38 +84,44 @@ func (c *privateNetworkCreateCmd) cmdRun(_ *cobra.Command, _ []string) error {
}(),
}

// 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{}
opts := &v3.PrivateNetworkOptions{}

if len(c.DNSServers) > 0 {
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)
}
}

}

if len(c.NTPServers) > 0 {
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)
}
}
}

if len(c.Routers) > 0 {
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)
}
}
}

if len(c.DomainSearch) > 0 {
opts.DomainSearch = c.DomainSearch
req.Options = opts
}

req.Options = opts

op, err := client.CreatePrivateNetwork(ctx, req)
if err != nil {
return err
Expand Down
20 changes: 11 additions & 9 deletions cmd/private_network_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,43 +96,45 @@ func (c *privateNetworkUpdateCmd) cmdRun(cmd *cobra.Command, _ []string) error {
updated = true
}

// 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{}
opts := &v3.PrivateNetworkOptions{}

if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.DNSServers)) {
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)
}
}
}

if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.NTPServers)) {
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)
}
}
}

if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.Routers)) {
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)
}
}
}

if cmd.Flags().Changed(mustCLICommandFlagName(c, &c.DomainSearch)) {
opts.DomainSearch = c.DomainSearch
updateReq.Options = opts
updated = true
}

updateReq.Options = opts
updated = true

if updated {
op, err := client.UpdatePrivateNetwork(ctx, pn.ID, updateReq)
if err != nil {
Expand Down

0 comments on commit c68e515

Please sign in to comment.