Skip to content

Commit

Permalink
Add set-rdns commands (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
LKaemmerling authored and thcyron committed Sep 18, 2018
1 parent 83aa3ad commit c5ebff7
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

* Add `hcloud ssh-key update` command
* Add `-u/--user` and `-p/--port` flags to `hcloud server ssh` command
* Add `hcloud server set-rdns` command
* Add `hcloud floating-ip set-rdns` command

## v1.7.0

Expand Down
1 change: 1 addition & 0 deletions cli/floatingip.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func newFloatingIPCommand(cli *CLI) *cobra.Command {
newFloatingIPDisableProtectionCommand(cli),
newFloatingIPAddLabelCommand(cli),
newFloatingIPRemoveLabelCommand(cli),
newFloatingIPSetRDNSCommand(cli),
)
return cmd
}
Expand Down
61 changes: 61 additions & 0 deletions cli/floatingip_set_rdns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cli

import (
"fmt"
"strconv"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)

func newFloatingIPSetRDNSCommand(cli *CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "set-rdns [FLAGS] FLOATINGIP",
Short: "Change reverse DNS of a Floating IP",
Args: cobra.ExactArgs(1),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: cli.ensureToken,
RunE: cli.wrap(runFloatingIPSetRDNS),
}

cmd.Flags().StringP("hostname", "r", "", "Hostname to set as a reverse DNS PTR entry")
cmd.MarkFlagRequired("hostname")

cmd.Flags().StringP("ip", "i", "", "IP address for which the reverse DNS entry should be set")
return cmd
}

func runFloatingIPSetRDNS(cli *CLI, cmd *cobra.Command, args []string) error {
id, err := strconv.Atoi(args[0])
if err != nil {
return err
}

floatingIP, _, err := cli.Client().FloatingIP.GetByID(cli.Context, id)
if err != nil {
return err
}
if floatingIP == nil {
return fmt.Errorf("Floating IP not found: %d", id)
}

ip, _ := cmd.Flags().GetString("ip")
if ip == "" {
ip = floatingIP.IP.String()
}

hostname, _ := cmd.Flags().GetString("hostname")
action, _, err := cli.Client().FloatingIP.ChangeDNSPtr(cli.Context, floatingIP, ip, hcloud.String(hostname))
if err != nil {
return err
}

if err := cli.ActionProgress(cli.Context, action); err != nil {
return err
}

fmt.Printf("Reverse DNS of Floating IP %d changed\n", floatingIP.ID)

return nil
}
1 change: 1 addition & 0 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func newServerCommand(cli *CLI) *cobra.Command {
newServerSSHCommand(cli),
newServerAddLabelCommand(cli),
newServerRemoveLabelCommand(cli),
newServerSetRDNSCommand(cli),
)
return cmd
}
Expand Down
56 changes: 56 additions & 0 deletions cli/server_set_rdns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package cli

import (
"fmt"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/spf13/cobra"
)

func newServerSetRDNSCommand(cli *CLI) *cobra.Command {
cmd := &cobra.Command{
Use: "set-rdns [FLAGS] SERVER",
Short: "Change reverse DNS of a server",
Args: cobra.ExactArgs(1),
TraverseChildren: true,
DisableFlagsInUseLine: true,
PreRunE: cli.ensureToken,
RunE: cli.wrap(runServerSetRDNS),
}

cmd.Flags().StringP("hostname", "r", "", "Hostname to set as a reverse DNS PTR entry")
cmd.MarkFlagRequired("hostname")

cmd.Flags().StringP("ip", "i", "", "IP address for which the reverse DNS entry should be set")

return cmd
}

func runServerSetRDNS(cli *CLI, cmd *cobra.Command, args []string) error {
idOrName := args[0]
server, _, err := cli.Client().Server.Get(cli.Context, idOrName)
if err != nil {
return err
}
if server == nil {
return fmt.Errorf("server not found: %s", idOrName)
}
ip, _ := cmd.Flags().GetString("ip")
if ip == "" {
ip = server.PublicNet.IPv4.IP.String()
}

hostname, _ := cmd.Flags().GetString("hostname")
action, _, err := cli.Client().Server.ChangeDNSPtr(cli.Context, server, ip, hcloud.String(hostname))
if err != nil {
return err
}

if err := cli.ActionProgress(cli.Context, action); err != nil {
return err
}

fmt.Printf("Reverse DNS of server %d changed\n", server.ID)

return nil
}

0 comments on commit c5ebff7

Please sign in to comment.