Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow deleting rDNS entries #839

Merged
merged 4 commits into from
Aug 14, 2024
Merged
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
25 changes: 16 additions & 9 deletions internal/cmd/base/set_rdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ type SetRdnsCmd struct {
ResourceNameSingular string // e.g. "server"
ShortDescription string
NameSuggestions func(client hcapi2.Client) func() []string
AdditionalFlags func(*cobra.Command)
Fetch func(s state.State, cmd *cobra.Command, idOrName string) (interface{}, *hcloud.Response, error)
GetDefaultIP func(resource interface{}) net.IP
}

// CobraCommand creates a command that can be registered with cobra.
func (rc *SetRdnsCmd) CobraCommand(s state.State) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("set-rdns [options] --hostname <hostname> <%s>", util.ToKebabCase(rc.ResourceNameSingular)),
Use: fmt.Sprintf("set-rdns [--ip <ip>] (--hostname <hostname> | --reset) <%s>", util.ToKebabCase(rc.ResourceNameSingular)),
Short: rc.ShortDescription,
Args: util.Validate,
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(rc.NameSuggestions(s.Client()))),
Expand All @@ -38,19 +37,27 @@ func (rc *SetRdnsCmd) CobraCommand(s state.State) *cobra.Command {
return rc.Run(s, cmd, args)
},
}
cmd.Flags().StringP("hostname", "r", "", "Hostname to set as a reverse DNS PTR entry (required)")
cmd.MarkFlagRequired("hostname")
cmd.Flags().StringP("hostname", "r", "", "Hostname to set as a reverse DNS PTR entry")
cmd.Flags().Bool("reset", false, "Reset the reverse DNS entry to the default value")

cmd.Flags().IPP("ip", "i", net.IP{}, "IP address for which the reverse DNS entry should be set")
if rc.AdditionalFlags != nil {
rc.AdditionalFlags(cmd)
}
return cmd
}

// Run executes a setRDNS command.
func (rc *SetRdnsCmd) Run(s state.State, cmd *cobra.Command, args []string) error {

var hostnamePtr *string
if reset, _ := cmd.Flags().GetBool("reset"); reset {
hostnamePtr = nil
} else {
hostname, _ := cmd.Flags().GetString("hostname")
if hostname == "" {
return fmt.Errorf("either --hostname or --reset must be specified")
}
hostnamePtr = &hostname
}

idOrName := args[0]
resource, _, err := rc.Fetch(s, cmd, idOrName)
if err != nil {
Expand All @@ -67,8 +74,8 @@ func (rc *SetRdnsCmd) Run(s state.State, cmd *cobra.Command, args []string) erro
if ip.IsUnspecified() || ip == nil {
ip = rc.GetDefaultIP(resource)
}
hostname, _ := cmd.Flags().GetString("hostname")
action, _, err := s.Client().RDNS().ChangeDNSPtr(s, resource.(hcloud.RDNSSupporter), ip, hcloud.String(hostname))

action, _, err := s.Client().RDNS().ChangeDNSPtr(s, resource.(hcloud.RDNSSupporter), ip, hostnamePtr)
if err != nil {
return err
}
Expand Down