rrset

package
v1.56.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AddRecordsCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:   "add-records (--record <value>... | --records-file <file>) <zone> <name> <type>",
			Short: "Add records to a Zone RRSet",
			Long: `Add records to a Zone RRSet.

If the Zone RRSet doesn't exist, it will automatically be created.

` + recordsFileExample,
			ValidArgsFunction:     cmpl.SuggestArgs(rrsetArgumentsCompletionFuncs(client)...),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}

		addRecordsFlags(cmd)

		cmd.Flags().Int("ttl", 0, "Time To Live (TTL) of the Zone RRSet")

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		zoneIDOrName, rrsetName, rrsetType := args[0], args[1], args[2]

		zoneIDOrName, err := util.ParseZoneIDOrName(zoneIDOrName)
		if err != nil {
			return fmt.Errorf("failed to convert Zone name to ascii: %w", err)
		}

		zone, _, err := s.Client().Zone().Get(s, zoneIDOrName)
		if err != nil {
			return err
		}
		if zone == nil {
			return fmt.Errorf("Zone not found: %s", zoneIDOrName)
		}

		rrset := &hcloud.ZoneRRSet{
			Zone: zone,
			Name: rrsetName,
			Type: hcloud.ZoneRRSetType(rrsetType),
		}

		var opts hcloud.ZoneRRSetAddRecordsOpts

		opts.Records, err = recordsFromFlags(cmd)
		if err != nil {
			return err
		}

		if rrset.Type == hcloud.ZoneRRSetTypeTXT {
			FormatTXTRecords(cmd, opts.Records)
		}

		if ttl, _ := cmd.Flags().GetInt("ttl"); ttl != 0 {
			opts.TTL = &ttl
		}

		action, _, err := s.Client().Zone().AddRRSetRecords(s, rrset, opts)
		if err != nil {
			return err
		}

		if err := s.WaitForActions(s, cmd, action); err != nil {
			return err
		}

		cmd.Printf("Added records on Zone RRSet %s %s\n", rrset.Name, rrset.Type)
		return nil
	},
	Experimental: experimental.DNS,
}
View Source
var ChangeTTLCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:                   "change-ttl (--ttl <ttl> | --unset) <zone> <name> <type>",
			Short:                 "Changes the Time To Live (TTL) of a Zone RRSet",
			ValidArgsFunction:     cmpl.SuggestArgs(rrsetArgumentsCompletionFuncs(client)...),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}

		cmd.Flags().Int("ttl", 0, "Time To Live (TTL) of the Zone RRSet (required)")

		cmd.Flags().Bool("unset", false, "Unset the Time To Live of Zone RRSet (use the Zone default TTL instead)")

		cmd.MarkFlagsOneRequired("ttl", "unset")
		cmd.MarkFlagsMutuallyExclusive("ttl", "unset")

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		zoneIDOrName, rrsetName, rrsetType := args[0], args[1], args[2]

		ttl, _ := cmd.Flags().GetInt("ttl")
		unset, _ := cmd.Flags().GetBool("unset")

		zoneIDOrName, err := util.ParseZoneIDOrName(zoneIDOrName)
		if err != nil {
			return fmt.Errorf("failed to convert Zone name to ascii: %w", err)
		}

		zone, _, err := s.Client().Zone().Get(s, zoneIDOrName)
		if err != nil {
			return err
		}
		if zone == nil {
			return fmt.Errorf("Zone not found: %s", zoneIDOrName)
		}

		rrset, _, err := s.Client().Zone().GetRRSetByNameAndType(s, zone, rrsetName, hcloud.ZoneRRSetType(rrsetType))
		if err != nil {
			return err
		}
		if rrset == nil {
			return fmt.Errorf("Zone RRSet not found: %s %s", rrsetName, rrsetType)
		}

		var opts hcloud.ZoneRRSetChangeTTLOpts
		if !unset {
			opts.TTL = &ttl
		}

		action, _, err := s.Client().Zone().ChangeRRSetTTL(s, rrset, opts)
		if err != nil {
			return err
		}

		if err := s.WaitForActions(s, cmd, action); err != nil {
			return err
		}

		cmd.Printf("Changed TTL on Zone RRSet %s %s\n", rrset.Name, rrset.Type)
		return nil
	},
	Experimental: experimental.DNS,
}
View Source
var CreateCmd = base.CreateCmd[*hcloud.ZoneRRSet]{
	BaseCobraCommand: func(c hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:                   fmt.Sprintf("create [options] --name <name> --type <%s> (--record <record>... | --records-file <file>) <zone>", strings.Join(rrsetTypeStrings, "|")),
			ValidArgsFunction:     cmpl.SuggestArgs(cmpl.SuggestCandidatesF(c.Zone().Names)),
			Short:                 "Create a Zone RRSet",
			Long:                  "Create a Zone RRSet.\n\n" + recordsFileExample,
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}

		cmd.Flags().String("name", "", "Name of the Zone RRSet (required)")
		_ = cmd.MarkFlagRequired("name")

		cmd.Flags().String("type", "", "Type of the Zone RRSet (required)")
		_ = cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates(rrsetTypeStrings...))
		_ = cmd.MarkFlagRequired("type")

		cmd.Flags().Int("ttl", 0, "Time To Live (TTL) of the RRSet")

		cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)")

		addRecordsFlags(cmd)

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) (*hcloud.ZoneRRSet, any, error) {
		zoneIDOrName := args[0]
		zoneIDOrName, err := util.ParseZoneIDOrName(zoneIDOrName)
		if err != nil {
			return nil, nil, fmt.Errorf("failed to convert Zone name to ascii: %w", err)
		}

		name, _ := cmd.Flags().GetString("name")
		typ, _ := cmd.Flags().GetString("type")
		labels, _ := cmd.Flags().GetStringToString("label")

		if !slices.Contains(rrsetTypeStrings, typ) {
			return nil, nil, fmt.Errorf("unknown Zone RRSet type: %s", typ)
		}

		zone, _, err := s.Client().Zone().Get(s, zoneIDOrName)
		if err != nil {
			return nil, nil, err
		}
		if zone == nil {
			return nil, nil, fmt.Errorf("Zone not found: %s", zoneIDOrName)
		}

		createOpts := hcloud.ZoneRRSetCreateOpts{
			Name:   name,
			Type:   hcloud.ZoneRRSetType(typ),
			Labels: labels,
		}

		createOpts.Records, err = recordsFromFlags(cmd)
		if err != nil {
			return nil, nil, err
		}

		if createOpts.Type == hcloud.ZoneRRSetTypeTXT {
			FormatTXTRecords(cmd, createOpts.Records)
		}

		if cmd.Flags().Changed("ttl") {
			ttl, _ := cmd.Flags().GetInt("ttl")
			createOpts.TTL = &ttl
		}

		result, _, err := s.Client().Zone().CreateRRSet(s, zone, createOpts)
		if err != nil {
			return nil, nil, err
		}

		if err := s.WaitForActions(s, cmd, result.Action); err != nil {
			return nil, nil, err
		}
		cmd.Printf("Zone RRSet %s %s created\n", result.RRSet.Name, result.RRSet.Type)

		return result.RRSet, util.Wrap("rrset", hcloud.SchemaFromZoneRRSet(result.RRSet)), nil
	},

	Experimental: experimental.DNS,
}
View Source
var DeleteCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:                   "delete <zone> <name> <type>",
			Short:                 "Delete a Zone RRSet",
			Args:                  util.Validate,
			ValidArgsFunction:     cmpl.SuggestArgs(rrsetArgumentsCompletionFuncs(client)...),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}
		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		zoneIDOrName, rrsetName, rrsetType := args[0], args[1], args[2]

		zoneIDOrName, err := util.ParseZoneIDOrName(zoneIDOrName)
		if err != nil {
			return fmt.Errorf("failed to convert Zone name to ascii: %w", err)
		}

		zone, _, err := s.Client().Zone().Get(s, zoneIDOrName)
		if err != nil {
			return err
		}
		if zone == nil {
			return fmt.Errorf("Zone not found: %s", zoneIDOrName)
		}

		rrset, _, err := s.Client().Zone().GetRRSetByNameAndType(s, zone, rrsetName, hcloud.ZoneRRSetType(rrsetType))
		if err != nil {
			return err
		}
		if rrset == nil {
			return fmt.Errorf("Zone RRSet not found: %s %s %s", zoneIDOrName, rrsetName, rrsetType)
		}

		result, _, err := s.Client().Zone().DeleteRRSet(s, rrset)
		if err != nil {
			return err
		}

		err = s.WaitForActions(s, cmd, result.Action)
		if err != nil {
			return err
		}

		cmd.Printf("Zone RRSet %s %s deleted\n", rrsetName, rrsetType)

		return nil
	},
	Experimental: experimental.DNS,
}
View Source
var DescribeCmd = base.DescribeCmd[*hcloud.ZoneRRSet]{
	ResourceNameSingular:       "Zone RRSet",
	ShortDescription:           "Describe a Zone RRSet",
	PositionalArgumentOverride: []string{"zone", "name", "type"},
	ValidArgsFunction:          rrsetArgumentsCompletionFuncs,
	FetchWithArgs: func(s state.State, _ *cobra.Command, args []string) (*hcloud.ZoneRRSet, interface{}, error) {
		zoneIDOrName, rrsetName, rrsetType := args[0], args[1], args[2]

		zoneIDOrName, err := util.ParseZoneIDOrName(zoneIDOrName)
		if err != nil {
			return nil, nil, fmt.Errorf("failed to convert Zone name to ascii: %w", err)
		}

		zone, _, err := s.Client().Zone().Get(s, zoneIDOrName)
		if err != nil {
			return nil, nil, err
		}
		if zone == nil {
			return nil, nil, fmt.Errorf("Zone not found: %s", zoneIDOrName)
		}

		rrset, _, err := s.Client().Zone().GetRRSetByNameAndType(s, zone, rrsetName, hcloud.ZoneRRSetType(rrsetType))
		if err != nil {
			return nil, nil, err
		}
		return rrset, hcloud.SchemaFromZoneRRSet(rrset), nil
	},
	PrintText: func(_ state.State, _ *cobra.Command, out io.Writer, rrset *hcloud.ZoneRRSet) error {
		ttl := "-"
		if rrset.TTL != nil {
			ttl = strconv.Itoa(*rrset.TTL)
		}

		fmt.Fprintf(out, "ID:\t%s\n", rrset.ID)
		fmt.Fprintf(out, "Type:\t%s\n", rrset.Type)
		fmt.Fprintf(out, "Name:\t%s\n", rrset.Name)
		fmt.Fprintf(out, "TTL:\t%s\n", ttl)

		fmt.Fprintln(out)
		fmt.Fprintf(out, "Protection:\n")
		fmt.Fprintf(out, "  Change:\t%s\n", util.YesNo(rrset.Protection.Change))

		fmt.Fprintln(out)
		util.DescribeLabels(out, rrset.Labels, "")

		fmt.Fprintln(out)
		fmt.Fprintf(out, "Records:\n")
		if len(rrset.Records) == 0 {
			fmt.Fprintf(out, "  No Records\n")
		} else {
			for _, record := range rrset.Records {
				fmt.Fprintf(out, "  - Value:\t%s\n", record.Value)
				if record.Comment != "" {
					fmt.Fprintf(out, "    Comment:\t%s\n", record.Comment)
				}
			}
		}

		return nil
	},
	Experimental: experimental.DNS,
}
View Source
var DisableProtectionCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		return &cobra.Command{
			Use:                   "disable-protection <zone> <name> <type> change",
			Args:                  util.ValidateLenient,
			Short:                 "Disable resource protection for a Zone RRSet",
			ValidArgsFunction:     cmpl.SuggestArgs(rrsetArgumentsCompletionFuncs(client)...),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		zoneIDOrName, rrsetName, rrsetType := args[0], args[1], args[2]

		zoneIDOrName, err := util.ParseZoneIDOrName(zoneIDOrName)
		if err != nil {
			return fmt.Errorf("failed to convert Zone name to ascii: %w", err)
		}

		zone, _, err := s.Client().Zone().Get(s, zoneIDOrName)
		if err != nil {
			return err
		}
		if zone == nil {
			return fmt.Errorf("Zone not found: %s", zoneIDOrName)
		}

		rrset, _, err := s.Client().Zone().GetRRSetByNameAndType(s, zone, rrsetName, hcloud.ZoneRRSetType(rrsetType))
		if err != nil {
			return err
		}
		if rrset == nil {
			return fmt.Errorf("Zone RRSet not found: %s %s", rrsetName, rrsetType)
		}

		opts, err := getChangeProtectionOpts(false, args[3:])
		if err != nil {
			return err
		}

		return changeProtection(s, cmd, rrset, false, opts)
	},
	Experimental: experimental.DNS,
}
View Source
var EnableProtectionCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		return &cobra.Command{
			Use:                   "enable-protection <zone> <name> <type> change",
			Args:                  util.ValidateLenient,
			Short:                 "Enable resource protection for a Zone RRSet",
			ValidArgsFunction:     cmpl.SuggestArgs(rrsetArgumentsCompletionFuncs(client)...),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		zoneIDOrName, rrsetName, rrsetType := args[0], args[1], args[2]

		zoneIDOrName, err := util.ParseZoneIDOrName(zoneIDOrName)
		if err != nil {
			return fmt.Errorf("failed to convert Zone name to ascii: %w", err)
		}

		zone, _, err := s.Client().Zone().Get(s, zoneIDOrName)
		if err != nil {
			return err
		}
		if zone == nil {
			return fmt.Errorf("Zone not found: %s", zoneIDOrName)
		}

		rrset, _, err := s.Client().Zone().GetRRSetByNameAndType(s, zone, rrsetName, hcloud.ZoneRRSetType(rrsetType))
		if err != nil {
			return err
		}
		if rrset == nil {
			return fmt.Errorf("Zone RRSet not found: %s %s", rrsetName, rrsetType)
		}

		opts, err := getChangeProtectionOpts(true, args[3:])
		if err != nil {
			return err
		}

		return changeProtection(s, cmd, rrset, true, opts)
	},
	Experimental: experimental.DNS,
}
View Source
var LabelCmds = base.LabelCmds[*hcloud.ZoneRRSet]{
	ResourceNameSingular:   "Zone RRSet",
	ShortDescriptionAdd:    "Add a label to a Zone RRSet",
	ShortDescriptionRemove: "Remove a label from a Zone RRSet",

	PositionalArgumentOverride: []string{"zone", "name", "type"},
	ValidArgsFunction: func(client hcapi2.Client) []cobra.CompletionFunc {
		validArgsFunction := rrsetArgumentsCompletionFuncs(client)

		validArgsFunction = append(validArgsFunction, cmpl.SuggestCandidatesCtx(func(_ *cobra.Command, args []string) []string {
			if len(args) < 3 {
				return nil
			}
			zoneIDOrName, rrsetName, rrsetType := args[0], args[1], args[2]
			return client.Zone().RRSetLabelKeys(zoneIDOrName, rrsetName, hcloud.ZoneRRSetType(rrsetType))
		}))

		return validArgsFunction
	},
	FetchWithArgs: func(s state.State, args []string) (*hcloud.ZoneRRSet, error) {
		zoneIDOrName, rrsetName, rrsetType := args[0], args[1], args[2]

		zoneIDOrName, err := util.ParseZoneIDOrName(zoneIDOrName)
		if err != nil {
			return nil, fmt.Errorf("failed to convert Zone name to ascii: %w", err)
		}

		zone, _, err := s.Client().Zone().Get(s, zoneIDOrName)
		if err != nil {
			return nil, err
		}
		if zone == nil {
			return nil, fmt.Errorf("Zone not found: %s", zoneIDOrName)
		}

		rrset, _, err := s.Client().Zone().GetRRSetByNameAndType(s, zone, rrsetName, hcloud.ZoneRRSetType(rrsetType))
		if err != nil {
			return nil, err
		}
		if rrset == nil {
			return nil, fmt.Errorf("Zone RRSet not found: %s %s", rrsetName, rrsetType)
		}

		return rrset, nil
	},
	SetLabels: func(s state.State, rrset *hcloud.ZoneRRSet, labels map[string]string) error {
		opts := hcloud.ZoneRRSetUpdateOpts{
			Labels: labels,
		}
		_, _, err := s.Client().Zone().UpdateRRSet(s, rrset, opts)
		return err
	},
	GetLabels: func(rrset *hcloud.ZoneRRSet) map[string]string {
		return rrset.Labels
	},
	GetIDOrName: func(rrset *hcloud.ZoneRRSet) string {
		return fmt.Sprintf("%s %s", rrset.Name, rrset.Type)
	},
	Experimental: experimental.DNS,
}
View Source
var ListCmd = &base.ListCmd[*hcloud.ZoneRRSet, schema.ZoneRRSet]{
	ResourceNamePlural: "Zone RRSets",
	JSONKeyGetByName:   "rrsets",

	DefaultColumns: []string{"name", "type", "records"},
	SortOption:     config.OptionSortZoneRRSet,

	AdditionalFlags: func(cmd *cobra.Command) {
		cmd.Flags().StringSlice("type", nil, "Only Zone RRSets with one of these types are displayed")
		_ = cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates(rrsetTypeStrings...))
	},

	ValidArgsFunction: func(client hcapi2.Client) cobra.CompletionFunc {
		return cmpl.SuggestCandidatesF(client.Zone().Names)
	},

	PositionalArgumentOverride: []string{"zone"},

	FetchWithArgs: func(s state.State, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string, args []string) ([]*hcloud.ZoneRRSet, error) {
		zoneIDOrName := args[0]
		zoneIDOrName, err := util.ParseZoneIDOrName(zoneIDOrName)
		if err != nil {
			return nil, fmt.Errorf("failed to convert Zone name to ascii: %w", err)
		}

		types, _ := flags.GetStringSlice("type")

		zone, _, err := s.Client().Zone().Get(s, zoneIDOrName)
		if err != nil {
			return nil, err
		}
		if zone == nil {
			return nil, fmt.Errorf("Zone not found: %s", zoneIDOrName)
		}

		opts := hcloud.ZoneRRSetListOpts{ListOpts: listOpts}
		if len(sorts) > 0 {
			opts.Sort = sorts
		}

		if len(types) > 0 {
			for _, typ := range types {
				if !slices.Contains(rrsetTypeStrings, typ) {
					return nil, fmt.Errorf("unknown Zone RRSet type: %s", typ)
				}
				opts.Type = append(opts.Type, hcloud.ZoneRRSetType(typ))
			}
		}

		return s.Client().Zone().AllRRSetsWithOpts(s, zone, opts)
	},

	OutputTable: func(t *output.Table[*hcloud.ZoneRRSet], _ hcapi2.Client) {
		t.
			AddAllowedFields(&hcloud.ZoneRRSet{}).
			RemoveAllowedField("zone").
			AddFieldFn("protection", func(rrSet *hcloud.ZoneRRSet) string {
				var protection []string
				if rrSet.Protection.Change {
					protection = append(protection, "change")
				}
				return strings.Join(protection, ", ")
			}).
			AddFieldFn("labels", func(rrSet *hcloud.ZoneRRSet) string {
				return util.LabelsToString(rrSet.Labels)
			}).
			AddFieldFn("records", func(rrSet *hcloud.ZoneRRSet) string {
				var records []string
				for _, record := range rrSet.Records {
					records = append(records, record.Value)
				}
				return strings.Join(records, "\n")
			}).
			AddFieldFn("ttl", func(rrSet *hcloud.ZoneRRSet) string {
				if rrSet.TTL == nil {
					return "-"
				}
				return strconv.FormatInt(int64(*rrSet.TTL), 10)
			})
	},

	Schema: hcloud.SchemaFromZoneRRSet,

	Experimental: experimental.DNS,
}
View Source
var RemoveRecordsCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:   "remove-records (--record <value>... | --records-file <file>) <zone> <name> <type>",
			Short: "Remove records from a Zone RRSet.",
			Long: `Remove records from a Zone RRSet.

If the Zone RRSet doesn't contain any records, it will automatically be deleted.

` + recordsFileExample,
			ValidArgsFunction:     cmpl.SuggestArgs(rrsetArgumentsCompletionFuncs(client)...),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}

		addRecordsFlags(cmd)

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		zoneIDOrName, rrsetName, rrsetType := args[0], args[1], args[2]

		zoneIDOrName, err := util.ParseZoneIDOrName(zoneIDOrName)
		if err != nil {
			return fmt.Errorf("failed to convert Zone name to ascii: %w", err)
		}

		zone := &hcloud.Zone{Name: zoneIDOrName}

		rrset, _, err := s.Client().Zone().GetRRSetByNameAndType(s, zone, rrsetName, hcloud.ZoneRRSetType(rrsetType))
		if err != nil {
			return err
		}
		if rrset == nil {
			return fmt.Errorf("Zone RRSet not found: %s %s", rrsetName, rrsetType)
		}

		var opts hcloud.ZoneRRSetRemoveRecordsOpts

		opts.Records, err = recordsFromFlags(cmd)
		if err != nil {
			return err
		}

		if rrset.Type == hcloud.ZoneRRSetTypeTXT {
			FormatTXTRecords(cmd, opts.Records)
		}

		action, _, err := s.Client().Zone().RemoveRRSetRecords(s, rrset, opts)
		if err != nil {
			return err
		}

		if err := s.WaitForActions(s, cmd, action); err != nil {
			return err
		}

		cmd.Printf("Removed records from Zone RRSet %s %s\n", rrset.Name, rrset.Type)
		return nil
	},
	Experimental: experimental.DNS,
}
View Source
var SetRecordsCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:   "set-records (--record <value>... | --records-file <file>) <zone> <name> <type>",
			Short: "Set the records of a Zone RRSet",
			Long: `Set the records of a Zone RRSet.

- If the Zone RRSet doesn't exist, it will be created.
- If the Zone RRSet already exists, its records will be replaced.
- If the provided records are empty, the Zone RRSet will be deleted.

` + recordsFileExample,
			ValidArgsFunction:     cmpl.SuggestArgs(rrsetArgumentsCompletionFuncs(client)...),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}

		addRecordsFlags(cmd)

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		zoneIDOrName, rrsetName, rrsetType := args[0], args[1], args[2]

		records, err := recordsFromFlags(cmd)
		if err != nil {
			return err
		}

		if hcloud.ZoneRRSetType(rrsetType) == hcloud.ZoneRRSetTypeTXT {
			FormatTXTRecords(cmd, records)
		}

		zoneIDOrName, err = util.ParseZoneIDOrName(zoneIDOrName)
		if err != nil {
			return fmt.Errorf("failed to convert Zone name to ascii: %w", err)
		}

		zone, _, err := s.Client().Zone().Get(s, zoneIDOrName)
		if err != nil {
			return err
		}
		if zone == nil {
			return fmt.Errorf("Zone not found: %s", zoneIDOrName)
		}

		rrset, _, err := s.Client().Zone().GetRRSetByNameAndType(s, zone, rrsetName, hcloud.ZoneRRSetType(rrsetType))
		if err != nil {
			return err
		}

		if len(records) == 0 {
			if rrset == nil {
				cmd.Printf("Zone RRSet %s %s doesn't exist. No action necessary.\n", rrsetName, rrsetType)
			} else {
				result, _, err := s.Client().Zone().DeleteRRSet(s, rrset)
				if err != nil {
					return err
				}
				if err := s.WaitForActions(s, cmd, result.Action); err != nil {
					return err
				}
				cmd.Printf("Zone RRSet %s %s deleted\n", rrset.Name, rrset.Type)
			}
			return nil
		}

		if rrset == nil {
			result, _, err := s.Client().Zone().CreateRRSet(s, zone, hcloud.ZoneRRSetCreateOpts{
				Name:    rrsetName,
				Type:    hcloud.ZoneRRSetType(rrsetType),
				Records: records,
			})
			if err != nil {
				return err
			}
			if err := s.WaitForActions(s, cmd, result.Action); err != nil {
				return err
			}
			rrset := result.RRSet
			cmd.Printf("Created and set records on Zone RRSet %s %s\n", rrset.Name, rrset.Type)
			return nil
		}

		opts := hcloud.ZoneRRSetSetRecordsOpts{Records: records}

		action, _, err := s.Client().Zone().SetRRSetRecords(s, rrset, opts)
		if err != nil {
			return err
		}

		if err := s.WaitForActions(s, cmd, action); err != nil {
			return err
		}

		cmd.Printf("Set records on Zone RRSet %s %s\n", rrset.Name, rrset.Type)
		return nil
	},
	Experimental: experimental.DNS,
}

Functions

func FormatTXTRecords added in v1.56.0

func FormatTXTRecords(cmd *cobra.Command, records []hcloud.ZoneRRSetRecord)

func NewCommand

func NewCommand(s state.State) *cobra.Command

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL