Skip to content

Commit

Permalink
refactor(primary-ip): make set-rdns consistent with other commands (#686
Browse files Browse the repository at this point in the history
)

Since
hetznercloud/hcloud-go@41a4c5a
Primary IPs support the RDNSSupporter interface, so the
`base.SetRdnsCmd` can be used for Primary IPs to make them consistent
with other subcommands.

Co-authored-by: pauhull <[email protected]>
  • Loading branch information
phm07 and phm07 authored Jan 31, 2024
1 parent f31bb6e commit e8f3620
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 73 deletions.
63 changes: 0 additions & 63 deletions internal/cmd/primaryip/changedns.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/cmd/primaryip/primaryip.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewCommand(s state.State) *cobra.Command {
DeleteCmd.CobraCommand(s),
AssignCmd.CobraCommand(s),
UnAssignCmd.CobraCommand(s),
ChangeDNSCmd.CobraCommand(s),
SetRDNSCmd.CobraCommand(s),
EnableProtectionCmd.CobraCommand(s),
DisableProtectionCmd.CobraCommand(s),
LabelCmds.AddCobraCommand(s),
Expand Down
25 changes: 25 additions & 0 deletions internal/cmd/primaryip/set_rdns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package primaryip

import (
"net"

"github.com/spf13/cobra"

"github.com/hetznercloud/cli/internal/cmd/base"
"github.com/hetznercloud/cli/internal/hcapi2"
"github.com/hetznercloud/cli/internal/state"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

var SetRDNSCmd = base.SetRdnsCmd{
ResourceNameSingular: "Primary IP",
ShortDescription: "Change reverse DNS of a Primary IP",
NameSuggestions: func(c hcapi2.Client) func() []string { return c.PrimaryIP().Names },
Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error) {
return s.Client().PrimaryIP().Get(s, idOrName)
},
GetDefaultIP: func(resource interface{}) net.IP {
primaryIP := resource.(*hcloud.PrimaryIP)
return primaryIP.IP
},
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package primaryip_test

import (
"net"
"testing"

"github.com/golang/mock/gomock"
Expand All @@ -11,11 +12,11 @@ import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

func TestChangeDNS(t *testing.T) {
func TestSetRDNS(t *testing.T) {
fx := testutil.NewFixture(t)
defer fx.Finish()

cmd := primaryip.ChangeDNSCmd.CobraCommand(fx.State())
cmd := primaryip.SetRDNSCmd.CobraCommand(fx.State())
action := &hcloud.Action{ID: 1}
fx.ExpectEnsureToken()
fx.Client.PrimaryIPClient.EXPECT().
Expand All @@ -28,14 +29,12 @@ func TestChangeDNS(t *testing.T) {
&hcloud.Response{},
nil,
)
fx.Client.PrimaryIPClient.EXPECT().
fx.Client.RDNSClient.EXPECT().
ChangeDNSPtr(
gomock.Any(),
hcloud.PrimaryIPChangeDNSPtrOpts{
ID: 13,
DNSPtr: "server.your-host.de",
IP: "192.168.0.1",
},
&hcloud.PrimaryIP{ID: 13},
net.ParseIP("192.168.0.1"),
hcloud.Ptr("server.your-host.de"),
).
Return(
action,
Expand All @@ -47,7 +46,7 @@ func TestChangeDNS(t *testing.T) {

out, _, err := fx.Run(cmd, []string{"--hostname=server.your-host.de", "--ip=192.168.0.1", "13"})

expOut := "Primary IP 13 DNS pointer: server.your-host.de associated to 192.168.0.1\n"
expOut := "Reverse DNS of Primary IP 13 changed\n"

assert.NoError(t, err)
assert.Equal(t, expOut, out)
Expand Down

0 comments on commit e8f3620

Please sign in to comment.