-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83aa3ad
commit c5ebff7
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |