Skip to content

Commit

Permalink
fix(network): do not return error on valid dhcp-default-route values (
Browse files Browse the repository at this point in the history
  • Loading branch information
kangasta authored Jan 2, 2024
1 parent 262d1c7 commit 50959f0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Support nested properties in `database properties *` and `database properties * show *` outputs. For example upctl `max_background_workers` sub-property of `timescaledb` PostgreSQL property is listed as `timescaledb.max_background_workers` in human output of `database properties pg` and its details can printed with `upctl database properties pg show timescaledb.max_background_workers` command.

### Fixed
- Do not return error on valid `dhcp-default-route` values in `network create` and `network modify` commands.

## [3.2.1] - 2023-11-29

### Added
Expand Down
20 changes: 20 additions & 0 deletions internal/commands/network/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ func TestCreateCommand(t *testing.T) {
},
},
},
{
name: "with DHCP parameters",
args: []string{
"--name", n.Name,
"--zone", n.Zone,
"--ip-network", "address=127.0.0.1,dhcp=true,dhcp-default-route=true",
},
expected: request.CreateNetworkRequest{
Name: n.Name,
Zone: n.Zone,
IPNetworks: []upcloud.IPNetwork{
{
Address: "127.0.0.1",
Family: upcloud.IPAddressFamilyIPv4,
DHCP: upcloud.FromBool(true),
DHCPDefaultRoute: upcloud.FromBool(true),
},
},
},
},
{
name: "with multiple network",
args: []string{
Expand Down
18 changes: 18 additions & 0 deletions internal/commands/network/modify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,24 @@ func TestModifyCommand(t *testing.T) {
},
},
},
{
name: "with DHCP parameters",
flags: []string{
"--name", n.Name,
"--ip-network", "family=IPv4,dhcp=false,dhcp-default-route=false",
},
expected: request.ModifyNetworkRequest{
Name: n.Name,
UUID: n.UUID,
IPNetworks: []upcloud.IPNetwork{
{
Family: upcloud.IPAddressFamilyIPv4,
DHCP: upcloud.FromBool(false),
DHCPDefaultRoute: upcloud.FromBool(false),
},
},
},
},
{
name: "with multiple network",
flags: []string{
Expand Down
27 changes: 11 additions & 16 deletions internal/commands/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (n *networkCommand) InitCommand() {
func handleNetwork(in string) (*upcloud.IPNetwork, error) {
result := &upcloud.IPNetwork{}
var dhcp string
var dhcpDefRout string
var dhcpDefaultRoute string
var dns string

args, err := commands.Parse(in)
Expand All @@ -47,32 +47,27 @@ func handleNetwork(in string) (*upcloud.IPNetwork, error) {
fs.StringVar(&result.Family, "family", result.Address, "IP address family. Currently only IPv4 networks are supported.")
fs.StringVar(&result.Gateway, "gateway", result.Gateway, "Gateway address given by the DHCP service. Defaults to first address of the network if not given.")
fs.StringVar(&dhcp, "dhcp", dhcp, "Toggles DHCP service for the network.")
fs.StringVar(&dhcpDefRout, "dhcp-default-route", dhcpDefRout, "Defines if the gateway should be given as default route by DHCP. Defaults to yes on public networks, and no on other ones.")
fs.StringVar(&dhcpDefaultRoute, "dhcp-default-route", dhcpDefaultRoute, "Defines if the gateway should be given as default route by DHCP. Defaults to yes on public networks, and no on other ones.")

err = fs.Parse(args)
if err != nil {
return nil, err
}

if dhcp != "" {
switch dhcp {
case "true":
result.DHCP = upcloud.FromBool(true)
case "false":
result.DHCP = upcloud.FromBool(false)
default:
return nil, fmt.Errorf("%s is an invalid value for dhcp, it can be true of false", dhcp)
val, err := commands.BoolFromString(dhcp)
if err != nil {
return nil, fmt.Errorf("could not parse dhcp value: %w", err)
}
result.DHCP = *val
}

if dhcpDefRout != "" {
if dhcpDefRout == "false" {
result.DHCPDefaultRoute = upcloud.FromBool(false)
if dhcpDefaultRoute != "" {
val, err := commands.BoolFromString(dhcpDefaultRoute)
if err != nil {
return nil, fmt.Errorf("could not parse dhcp-default-route value: %w", err)
}
if dhcpDefRout == "true" {
result.DHCPDefaultRoute = upcloud.FromBool(true)
}
return nil, fmt.Errorf("%s is an invalid value for dhcp default rout, it can be true of false", dhcp)
result.DHCPDefaultRoute = *val
}

if dns != "" {
Expand Down

0 comments on commit 50959f0

Please sign in to comment.