Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ hcloud load-balancer attach-to-network [--ip <ip>] --network <network> <load-bal
```
-h, --help help for attach-to-network
--ip ip IP address to assign to the Load Balancer (auto-assigned if omitted)
--ip-range ipNet IP range in CIDR block notation of the subnet to attach to
-n, --network string Network (ID or name) (required)
```

Expand Down
1 change: 1 addition & 0 deletions docs/reference/manual/hcloud_server_attach-to-network.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ hcloud server attach-to-network [options] --network <network> <server>
--alias-ips ipSlice Additional IP addresses to be assigned to the Server (default [])
-h, --help help for attach-to-network
--ip ip IP address to assign to the Server (auto-assigned if omitted)
--ip-range ipNet IP range in CIDR block notation of the subnet to attach to
-n, --network string Network (ID or name) (required)
```

Expand Down
6 changes: 6 additions & 0 deletions internal/cmd/loadbalancer/attach_to_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package loadbalancer

import (
"fmt"
"net"

"github.com/spf13/cobra"

Expand All @@ -27,6 +28,7 @@ var AttachToNetworkCmd = base.Cmd{
_ = cmd.MarkFlagRequired("network")

cmd.Flags().IP("ip", nil, "IP address to assign to the Load Balancer (auto-assigned if omitted)")
cmd.Flags().IPNet("ip-range", net.IPNet{}, "IP range in CIDR block notation of the subnet to attach to (auto-assigned if omitted)")

return cmd
},
Expand All @@ -50,11 +52,15 @@ var AttachToNetworkCmd = base.Cmd{
}

ip, _ := cmd.Flags().GetIP("ip")
ipRange, _ := cmd.Flags().GetIPNet("ip-range")

opts := hcloud.LoadBalancerAttachToNetworkOpts{
Network: network,
IP: ip,
}
if cmd.Flags().Changed("ip-range") {
opts.IPRange = &ipRange
}
action, _, err := s.Client().LoadBalancer().AttachToNetwork(s, loadBalancer, opts)

if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion internal/cmd/loadbalancer/attach_to_network_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package loadbalancer_test

import (
"net"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -28,13 +29,18 @@ func TestAttachToNetwork(t *testing.T) {
fx.Client.LoadBalancerClient.EXPECT().
AttachToNetwork(gomock.Any(), &hcloud.LoadBalancer{ID: 123}, hcloud.LoadBalancerAttachToNetworkOpts{
Network: &hcloud.Network{ID: 321},
IP: net.ParseIP("10.0.1.1"),
IPRange: &net.IPNet{
IP: net.IP{10, 0, 0, 0},
Mask: net.IPMask{255, 255, 0, 0},
},
}).
Return(&hcloud.Action{ID: 123}, nil, nil)
fx.ActionWaiter.EXPECT().
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 123}).
Return(nil)

out, errOut, err := fx.Run(cmd, []string{"123", "--network", "my-network"})
out, errOut, err := fx.Run(cmd, []string{"123", "--network", "my-network", "--ip", "10.0.1.1", "--ip-range", "10.0.0.0/16"})

expOut := "Load Balancer 123 attached to Network 321\n"

Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/server/attach_to_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var AttachToNetworkCmd = base.Cmd{
_ = cmd.MarkFlagRequired("network")

cmd.Flags().IP("ip", nil, "IP address to assign to the Server (auto-assigned if omitted)")
cmd.Flags().IPNet("ip-range", net.IPNet{}, "IP range in CIDR block notation of the subnet to attach to (auto-assigned if omitted)")
cmd.Flags().IPSlice("alias-ips", []net.IP{}, "Additional IP addresses to be assigned to the Server")

return cmd
Expand All @@ -52,13 +53,17 @@ var AttachToNetworkCmd = base.Cmd{
}

ip, _ := cmd.Flags().GetIP("ip")
ipRange, _ := cmd.Flags().GetIPNet("ip-range")
aliasIPs, _ := cmd.Flags().GetIPSlice("alias-ips")

opts := hcloud.ServerAttachToNetworkOpts{
Network: network,
IP: ip,
}
opts.AliasIPs = append(opts.AliasIPs, aliasIPs...)
if cmd.Flags().Changed("ip-range") {
opts.IPRange = &ipRange
}
action, _, err := s.Client().Server().AttachToNetwork(s, server, opts)

if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion internal/cmd/server/attach_to_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func TestAttachToNetwork(t *testing.T) {
AttachToNetwork(gomock.Any(), srv, hcloud.ServerAttachToNetworkOpts{
Network: n,
IP: net.ParseIP("192.168.0.1"),
IPRange: &net.IPNet{
IP: net.IP{192, 168, 0, 0},
Mask: net.IPMask{255, 255, 255, 0},
},
AliasIPs: []net.IP{
net.ParseIP("10.0.1.2"),
net.ParseIP("10.0.1.3"),
Expand All @@ -43,7 +47,7 @@ func TestAttachToNetwork(t *testing.T) {
WaitForActions(gomock.Any(), gomock.Any(), &hcloud.Action{ID: 789}).
Return(nil)

args := []string{"my-server", "--network", "my-network", "--ip", "192.168.0.1", "--alias-ips", "10.0.1.2,10.0.1.3"}
args := []string{"my-server", "--network", "my-network", "--ip", "192.168.0.1", "--ip-range", "192.168.0.0/24", "--alias-ips", "10.0.1.2,10.0.1.3"}
out, errOut, err := fx.Run(cmd, args)

require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/combined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestCombined(t *testing.T) {
})

t.Run("attach-to-network", func(t *testing.T) {
out, err := runCommand(t, "load-balancer", "attach-to-network", strconv.FormatInt(loadBalancerID, 10), "--network", strconv.FormatInt(networkID, 10))
out, err := runCommand(t, "load-balancer", "attach-to-network", strconv.FormatInt(loadBalancerID, 10), "--network", strconv.FormatInt(networkID, 10), "--ip-range", "10.0.1.0/24")
require.NoError(t, err)
assert.Equal(t, fmt.Sprintf("Load Balancer %d attached to Network %d\n", loadBalancerID, networkID), out)
})
Expand Down
Loading