Skip to content

Commit 50959f0

Browse files
authored
fix(network): do not return error on valid dhcp-default-route values (#280)
1 parent 262d1c7 commit 50959f0

4 files changed

Lines changed: 52 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111
- 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.
1212

13+
### Fixed
14+
- Do not return error on valid `dhcp-default-route` values in `network create` and `network modify` commands.
15+
1316
## [3.2.1] - 2023-11-29
1417

1518
### Added

internal/commands/network/create_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ func TestCreateCommand(t *testing.T) {
6565
},
6666
},
6767
},
68+
{
69+
name: "with DHCP parameters",
70+
args: []string{
71+
"--name", n.Name,
72+
"--zone", n.Zone,
73+
"--ip-network", "address=127.0.0.1,dhcp=true,dhcp-default-route=true",
74+
},
75+
expected: request.CreateNetworkRequest{
76+
Name: n.Name,
77+
Zone: n.Zone,
78+
IPNetworks: []upcloud.IPNetwork{
79+
{
80+
Address: "127.0.0.1",
81+
Family: upcloud.IPAddressFamilyIPv4,
82+
DHCP: upcloud.FromBool(true),
83+
DHCPDefaultRoute: upcloud.FromBool(true),
84+
},
85+
},
86+
},
87+
},
6888
{
6989
name: "with multiple network",
7090
args: []string{

internal/commands/network/modify_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ func TestModifyCommand(t *testing.T) {
5555
},
5656
},
5757
},
58+
{
59+
name: "with DHCP parameters",
60+
flags: []string{
61+
"--name", n.Name,
62+
"--ip-network", "family=IPv4,dhcp=false,dhcp-default-route=false",
63+
},
64+
expected: request.ModifyNetworkRequest{
65+
Name: n.Name,
66+
UUID: n.UUID,
67+
IPNetworks: []upcloud.IPNetwork{
68+
{
69+
Family: upcloud.IPAddressFamilyIPv4,
70+
DHCP: upcloud.FromBool(false),
71+
DHCPDefaultRoute: upcloud.FromBool(false),
72+
},
73+
},
74+
},
75+
},
5876
{
5977
name: "with multiple network",
6078
flags: []string{

internal/commands/network/network.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (n *networkCommand) InitCommand() {
3333
func handleNetwork(in string) (*upcloud.IPNetwork, error) {
3434
result := &upcloud.IPNetwork{}
3535
var dhcp string
36-
var dhcpDefRout string
36+
var dhcpDefaultRoute string
3737
var dns string
3838

3939
args, err := commands.Parse(in)
@@ -47,32 +47,27 @@ func handleNetwork(in string) (*upcloud.IPNetwork, error) {
4747
fs.StringVar(&result.Family, "family", result.Address, "IP address family. Currently only IPv4 networks are supported.")
4848
fs.StringVar(&result.Gateway, "gateway", result.Gateway, "Gateway address given by the DHCP service. Defaults to first address of the network if not given.")
4949
fs.StringVar(&dhcp, "dhcp", dhcp, "Toggles DHCP service for the network.")
50-
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.")
50+
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.")
5151

5252
err = fs.Parse(args)
5353
if err != nil {
5454
return nil, err
5555
}
5656

5757
if dhcp != "" {
58-
switch dhcp {
59-
case "true":
60-
result.DHCP = upcloud.FromBool(true)
61-
case "false":
62-
result.DHCP = upcloud.FromBool(false)
63-
default:
64-
return nil, fmt.Errorf("%s is an invalid value for dhcp, it can be true of false", dhcp)
58+
val, err := commands.BoolFromString(dhcp)
59+
if err != nil {
60+
return nil, fmt.Errorf("could not parse dhcp value: %w", err)
6561
}
62+
result.DHCP = *val
6663
}
6764

68-
if dhcpDefRout != "" {
69-
if dhcpDefRout == "false" {
70-
result.DHCPDefaultRoute = upcloud.FromBool(false)
65+
if dhcpDefaultRoute != "" {
66+
val, err := commands.BoolFromString(dhcpDefaultRoute)
67+
if err != nil {
68+
return nil, fmt.Errorf("could not parse dhcp-default-route value: %w", err)
7169
}
72-
if dhcpDefRout == "true" {
73-
result.DHCPDefaultRoute = upcloud.FromBool(true)
74-
}
75-
return nil, fmt.Errorf("%s is an invalid value for dhcp default rout, it can be true of false", dhcp)
70+
result.DHCPDefaultRoute = *val
7671
}
7772

7873
if dns != "" {

0 commit comments

Comments
 (0)