diff --git a/internal/cmd/primaryip/changedns.go b/internal/cmd/primaryip/changedns.go deleted file mode 100644 index c7ab9af1..00000000 --- a/internal/cmd/primaryip/changedns.go +++ /dev/null @@ -1,63 +0,0 @@ -package primaryip - -import ( - "fmt" - - "github.com/spf13/cobra" - - "github.com/hetznercloud/cli/internal/cmd/base" - "github.com/hetznercloud/cli/internal/cmd/cmpl" - "github.com/hetznercloud/cli/internal/hcapi2" - "github.com/hetznercloud/cli/internal/state" - "github.com/hetznercloud/hcloud-go/v2/hcloud" -) - -var ChangeDNSCmd = base.Cmd{ - BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { - cmd := &cobra.Command{ - Use: "set-rdns [FLAGS] PRIMARYIP", - Short: "Change the reverse DNS from a Primary IP", - Args: cobra.ExactArgs(1), - TraverseChildren: true, - ValidArgsFunction: cmpl.SuggestArgs( - cmpl.SuggestCandidatesF(client.PrimaryIP().Names), - ), - DisableFlagsInUseLine: true, - } - cmd.Flags().String("hostname", "", "Hostname to set as a reverse DNS PTR entry (required)") - cmd.MarkFlagRequired("hostname") - cmd.Flags().String("ip", "", "IP address for which the reverse DNS entry should be set (required)") - cmd.MarkFlagRequired("ip") - return cmd - }, - Run: func(s state.State, cmd *cobra.Command, args []string) error { - idOrName := args[0] - primaryIP, _, err := s.Client().PrimaryIP().Get(s, idOrName) - if err != nil { - return err - } - if primaryIP == nil { - return fmt.Errorf("Primary IP not found: %v", idOrName) - } - - DNSPtr, _ := cmd.Flags().GetString("hostname") - ip, _ := cmd.Flags().GetString("ip") - opts := hcloud.PrimaryIPChangeDNSPtrOpts{ - ID: primaryIP.ID, - DNSPtr: DNSPtr, - IP: ip, - } - - action, _, err := s.Client().PrimaryIP().ChangeDNSPtr(s, opts) - if err != nil { - return err - } - - if err := s.ActionProgress(cmd, s, action); err != nil { - return err - } - - cmd.Printf("Primary IP %d DNS pointer: %s associated to %s\n", opts.ID, opts.DNSPtr, opts.IP) - return nil - }, -} diff --git a/internal/cmd/primaryip/primaryip.go b/internal/cmd/primaryip/primaryip.go index ac2a29cf..601b9372 100644 --- a/internal/cmd/primaryip/primaryip.go +++ b/internal/cmd/primaryip/primaryip.go @@ -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), diff --git a/internal/cmd/primaryip/set_rdns.go b/internal/cmd/primaryip/set_rdns.go new file mode 100644 index 00000000..47c4292d --- /dev/null +++ b/internal/cmd/primaryip/set_rdns.go @@ -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 + }, +} diff --git a/internal/cmd/primaryip/changedns_test.go b/internal/cmd/primaryip/set_rdns_test.go similarity index 72% rename from internal/cmd/primaryip/changedns_test.go rename to internal/cmd/primaryip/set_rdns_test.go index 0efdf422..24cea00f 100644 --- a/internal/cmd/primaryip/changedns_test.go +++ b/internal/cmd/primaryip/set_rdns_test.go @@ -1,6 +1,7 @@ package primaryip_test import ( + "net" "testing" "github.com/golang/mock/gomock" @@ -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(). @@ -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, @@ -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)