snapshot

package
v1.57.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateCmd = base.CreateCmd[*hcloud.StorageBoxSnapshot]{
	BaseCobraCommand: func(c hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:                   "create [--description <description>] <storage-box>",
			ValidArgsFunction:     cmpl.SuggestArgs(cmpl.SuggestCandidatesF(c.StorageBox().Names)),
			Short:                 "Create a Storage Box Snapshot",
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}

		cmd.Flags().String("description", "", "Description of the Storage Box Snapshot")
		cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)")

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) (*hcloud.StorageBoxSnapshot, any, error) {
		storageBoxIDOrName := args[0]
		description, _ := cmd.Flags().GetString("description")
		labels, _ := cmd.Flags().GetStringToString("label")

		storageBox, _, err := s.Client().StorageBox().Get(s, storageBoxIDOrName)
		if err != nil {
			return nil, nil, err
		}
		if storageBox == nil {
			return nil, nil, fmt.Errorf("Storage Box not found: %s", storageBoxIDOrName)
		}

		opts := hcloud.StorageBoxSnapshotCreateOpts{
			Labels:      labels,
			Description: description,
		}

		result, _, err := s.Client().StorageBox().CreateSnapshot(s, storageBox, opts)
		if err != nil {
			return nil, nil, err
		}
		if err := s.WaitForActions(s, cmd, result.Action); err != nil {
			return nil, nil, err
		}

		snapshot, _, err := s.Client().StorageBox().GetSnapshotByID(s, storageBox, result.Snapshot.ID)
		if err != nil {
			return nil, nil, err
		}
		if snapshot == nil {
			return nil, nil, fmt.Errorf("Storage Box Snapshot not found: %d", result.Snapshot.ID)
		}

		cmd.Printf("Storage Box Snapshot %d created\n", snapshot.ID)

		return snapshot, util.Wrap("snapshot", hcloud.SchemaFromStorageBoxSnapshot(snapshot)), nil
	},
	PrintResource: func(_ state.State, cmd *cobra.Command, snapshot *hcloud.StorageBoxSnapshot) {
		cmd.Printf("Name: %s\n", snapshot.Name)
		cmd.Printf("Size: %s\n", humanize.IBytes(snapshot.Stats.Size))
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var DeleteCmd = base.DeleteCmd{
	ResourceNameSingular:       "Storage Box Snapshot",
	ResourceNamePlural:         "Storage Box Snapshots",
	ShortDescription:           "Delete a Storage Box Snapshot",
	PositionalArgumentOverride: []string{"storage-box", "snapshot"},
	ValidArgsFunction: func(client hcapi2.Client) []cobra.CompletionFunc {
		return []cobra.CompletionFunc{
			cmpl.SuggestCandidatesF(client.StorageBox().Names),
			SuggestSnapshots(client),
		}
	},

	FetchFunc: func(s state.State, _ *cobra.Command, args []string) (base.FetchFunc, error) {
		storageBox, _, err := s.Client().StorageBox().Get(s, args[0])
		if err != nil {
			return nil, err
		}
		if storageBox == nil {
			return nil, fmt.Errorf("Storage Box not found: %s", args[0])
		}
		return func(s state.State, _ *cobra.Command, idOrName string) (any, *hcloud.Response, error) {
			return s.Client().StorageBox().GetSnapshot(s, storageBox, idOrName)
		}, nil
	},

	Delete: func(s state.State, _ *cobra.Command, resource any) (*hcloud.Action, error) {
		snapshot := resource.(*hcloud.StorageBoxSnapshot)
		result, _, err := s.Client().StorageBox().DeleteSnapshot(s, snapshot)
		return result.Action, err
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var DescribeCmd = base.DescribeCmd[*hcloud.StorageBoxSnapshot]{
	ResourceNameSingular:       "Storage Box Snapshot",
	ShortDescription:           "Describe a Storage Box Snapshot",
	PositionalArgumentOverride: []string{"storage-box", "snapshot"},
	ValidArgsFunction: func(client hcapi2.Client) []cobra.CompletionFunc {
		return []cobra.CompletionFunc{
			cmpl.SuggestCandidatesF(client.StorageBox().Names),
			SuggestSnapshots(client),
		}
	},
	FetchWithArgs: func(s state.State, _ *cobra.Command, args []string) (*hcloud.StorageBoxSnapshot, any, error) {
		storageBoxIDOrName := args[0]

		storageBox, _, err := s.Client().StorageBox().Get(s, storageBoxIDOrName)
		if err != nil {
			return nil, nil, err
		}
		if storageBox == nil {
			return nil, nil, fmt.Errorf("Storage Box not found: %s", storageBoxIDOrName)
		}

		snapshot, _, err := s.Client().StorageBox().GetSnapshot(s, storageBox, args[1])
		if err != nil {
			return nil, nil, err
		}
		return snapshot, hcloud.SchemaFromStorageBoxSnapshot(snapshot), nil
	},
	PrintText: func(_ state.State, _ *cobra.Command, out io.Writer, snapshot *hcloud.StorageBoxSnapshot) error {
		fmt.Fprintf(out, "ID:\t%d\n", snapshot.ID)
		fmt.Fprintf(out, "Name:\t%s\n", snapshot.Name)
		fmt.Fprintf(out, "Description:\t%s\n", snapshot.Description)
		fmt.Fprintf(out, "Created:\t%s (%s)\n", util.Datetime(snapshot.Created), humanize.Time(snapshot.Created))
		fmt.Fprintf(out, "Is automatic:\t%s\n", util.YesNo(snapshot.IsAutomatic))

		fmt.Fprintln(out)
		fmt.Fprintf(out, "Stats:\n")
		fmt.Fprintf(out, "  Size:\t%s\n", humanize.IBytes(snapshot.Stats.Size))
		fmt.Fprintf(out, "  Filesystem Size:\t%s\n", humanize.IBytes(snapshot.Stats.SizeFilesystem))

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

		fmt.Fprintln(out)
		fmt.Fprintf(out, "Storage Box:\n")
		fmt.Fprintf(out, "  ID:\t%d\n", snapshot.StorageBox.ID)

		return nil
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var LabelCmds = base.LabelCmds[*hcloud.StorageBoxSnapshot]{
	ResourceNameSingular:   "Storage Box Snapshot",
	ShortDescriptionAdd:    "Add a label to a Storage Box Snapshot",
	ShortDescriptionRemove: "Remove a label from a Storage Box Snapshot",

	PositionalArgumentOverride: []string{"storage-box", "snapshot"},
	ValidArgsFunction: func(client hcapi2.Client) []cobra.CompletionFunc {
		return []cobra.CompletionFunc{
			cmpl.SuggestCandidatesF(client.StorageBox().Names),
			SuggestSnapshots(client),
			cmpl.SuggestCandidatesCtx(func(_ *cobra.Command, args []string) []string {
				if len(args) < 2 {
					return nil
				}
				return client.StorageBox().SnapshotLabelKeys(args[0], args[1])
			}),
		}
	},

	FetchWithArgs: func(s state.State, args []string) (*hcloud.StorageBoxSnapshot, error) {
		storageBoxIDOrName := args[0]

		storageBox, _, err := s.Client().StorageBox().Get(s, storageBoxIDOrName)
		if err != nil {
			return nil, err
		}
		if storageBox == nil {
			return nil, fmt.Errorf("Storage Box not found: %s", storageBoxIDOrName)
		}

		snapshot, _, err := s.Client().StorageBox().GetSnapshot(s, storageBox, args[1])
		if err != nil {
			return nil, err
		}
		return snapshot, nil
	},

	SetLabels: func(s state.State, snapshot *hcloud.StorageBoxSnapshot, labels map[string]string) error {
		opts := hcloud.StorageBoxSnapshotUpdateOpts{
			Labels: labels,
		}
		_, _, err := s.Client().StorageBox().UpdateSnapshot(s, snapshot, opts)
		return err
	},

	GetLabels: func(snapshot *hcloud.StorageBoxSnapshot) map[string]string {
		return snapshot.Labels
	},

	GetIDOrName: func(snapshot *hcloud.StorageBoxSnapshot) string {
		return snapshot.Name
	},

	Experimental: experimental.StorageBoxes,
}
View Source
var ListCmd = base.ListCmd[*hcloud.StorageBoxSnapshot, schema.StorageBoxSnapshot]{
	ResourceNamePlural: "Storage Box Snapshots",
	JSONKeyGetByName:   "snapshots",

	DefaultColumns: []string{"id", "name", "description", "size", "is_automatic", "age"},

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

	PositionalArgumentOverride: []string{"storage-box"},
	SortOption:                 config.OptionSortStorageBoxSnapshot,

	AdditionalFlags: func(cmd *cobra.Command) {
		cmd.Flags().Bool("automatic", false, "Only show automatic snapshots (true, false)")
	},

	FetchWithArgs: func(s state.State, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string, args []string) ([]*hcloud.StorageBoxSnapshot, error) {
		storageBoxIDOrName := args[0]
		isAutomatic, _ := flags.GetBool("automatic")

		storageBox, _, err := s.Client().StorageBox().Get(s, storageBoxIDOrName)
		if err != nil {
			return nil, err
		}
		if storageBox == nil {
			return nil, fmt.Errorf("Storage Box not found: %s", storageBoxIDOrName)
		}

		opts := hcloud.StorageBoxSnapshotListOpts{LabelSelector: listOpts.LabelSelector}
		if len(sorts) > 0 {
			opts.Sort = sorts
		}
		if flags.Changed("automatic") {
			opts.IsAutomatic = &isAutomatic
		}
		return s.Client().StorageBox().AllSnapshotsWithOpts(s, storageBox, opts)
	},

	OutputTable: func(t *output.Table[*hcloud.StorageBoxSnapshot], _ hcapi2.Client) {
		t.
			AddAllowedFields(&hcloud.StorageBoxSnapshot{}).
			AddFieldFn("size", func(snapshot *hcloud.StorageBoxSnapshot) string {
				return humanize.IBytes(snapshot.Stats.Size)
			}).
			AddFieldFn("size_filesystem", func(snapshot *hcloud.StorageBoxSnapshot) string {
				return humanize.IBytes(snapshot.Stats.SizeFilesystem)
			}).
			AddFieldFn("labels", func(snapshot *hcloud.StorageBoxSnapshot) string {
				return util.LabelsToString(snapshot.Labels)
			}).
			AddFieldFn("created", func(snapshot *hcloud.StorageBoxSnapshot) string {
				return util.Datetime(snapshot.Created)
			}).
			AddFieldFn("age", func(snapshot *hcloud.StorageBoxSnapshot) string {
				return util.Age(snapshot.Created, time.Now())
			})
	},

	Schema:       hcloud.SchemaFromStorageBoxSnapshot,
	Experimental: experimental.StorageBoxes,
}
View Source
var UpdateCmd = base.UpdateCmd{
	ResourceNameSingular:       "Storage Box Snapshot",
	ShortDescription:           "Update a Storage Box Snapshot",
	NameSuggestions:            func(c hcapi2.Client) func() []string { return c.StorageBox().Names },
	PositionalArgumentOverride: []string{"storage-box", "snapshot"},
	FetchWithArgs: func(s state.State, _ *cobra.Command, args []string) (any, *hcloud.Response, error) {
		storageBox, _, err := s.Client().StorageBox().Get(s, args[0])
		if err != nil {
			return nil, nil, err
		}
		if storageBox == nil {
			return nil, nil, fmt.Errorf("Storage Box not found: %s", args[0])
		}
		return s.Client().StorageBox().GetSnapshot(s, storageBox, args[1])
	},
	DefineFlags: func(cmd *cobra.Command) {
		cmd.Flags().String("description", "", "Description of the Storage Box Snapshot")
		cmd.MarkFlagsOneRequired("description")
	},
	Update: func(s state.State, cmd *cobra.Command, resource interface{}, _ map[string]pflag.Value) error {
		snapshot := resource.(*hcloud.StorageBoxSnapshot)
		var opts hcloud.StorageBoxSnapshotUpdateOpts
		if cmd.Flags().Changed("description") {
			description, _ := cmd.Flags().GetString("description")
			opts.Description = &description
		}
		_, _, err := s.Client().StorageBox().UpdateSnapshot(s, snapshot, opts)
		if err != nil {
			return err
		}
		return nil
	},
	Experimental: experimental.StorageBoxes,
}

Functions

func NewCommand

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

func SuggestSnapshots

func SuggestSnapshots(client hcapi2.Client) cobra.CompletionFunc

Types

This section is empty.

Jump to

Keyboard shortcuts

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