storagebox

package
v1.58.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ChangeProtectionCmds = base.ChangeProtectionCmds[*hcloud.StorageBox, hcloud.StorageBoxChangeProtectionOpts]{
	ResourceNameSingular: "Storage Box",

	NameSuggestions: func(client hcapi2.Client) func() []string {
		return client.StorageBox().Names
	},

	ProtectionLevels: map[string]func(opts *hcloud.StorageBoxChangeProtectionOpts, value bool){
		"delete": func(opts *hcloud.StorageBoxChangeProtectionOpts, value bool) {
			opts.Delete = &value
		},
	},

	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.StorageBox, *hcloud.Response, error) {
		return s.Client().StorageBox().Get(s, idOrName)
	},

	ChangeProtectionFunction: func(s state.State, storageBox *hcloud.StorageBox, opts hcloud.StorageBoxChangeProtectionOpts) (*hcloud.Action, *hcloud.Response, error) {
		return s.Client().StorageBox().ChangeProtection(s, storageBox, opts)
	},

	IDOrName: func(storageBox *hcloud.StorageBox) string {
		return fmt.Sprint(storageBox.ID)
	},

	Experimental: experimental.StorageBoxes,
}
View Source
var ChangeTypeCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:   "change-type <storage-box> <storage-box-type>",
			Short: "Change type of a Storage Box",
			Long: `Requests a Storage Box to be upgraded or downgraded to another Storage Box Type.
Please note that it is not possible to downgrade to a Storage Box Type that offers less disk space than you are currently using.`,
			ValidArgsFunction: cmpl.SuggestArgs(
				cmpl.SuggestCandidatesF(client.StorageBox().Names),
				cmpl.SuggestCandidatesF(client.StorageBoxType().Names),
			),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}
		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		idOrName := args[0]
		storageBox, _, err := s.Client().StorageBox().Get(s, idOrName)
		if err != nil {
			return err
		}
		if storageBox == nil {
			return fmt.Errorf("Storage Box not found: %s", idOrName)
		}

		storageBoxTypeIDOrName := args[1]
		storageBoxType, _, err := s.Client().StorageBoxType().Get(s, storageBoxTypeIDOrName)
		if err != nil {
			return err
		}
		if storageBoxType == nil {
			return fmt.Errorf("Storage Box Type not found: %s", storageBoxTypeIDOrName)
		}

		if storageBoxType.IsDeprecated() {
			cmd.Print(warningDeprecatedStorageBoxType(storageBoxType))
		}

		opts := hcloud.StorageBoxChangeTypeOpts{
			StorageBoxType: storageBoxType,
		}
		action, _, err := s.Client().StorageBox().ChangeType(s, storageBox, opts)
		if err != nil {
			return err
		}

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

		cmd.Printf("Storage Box %d upgraded to type %s\n", storageBox.ID, storageBoxType.Name)
		return nil
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var CreateCmd = base.CreateCmd[*hcloud.StorageBox]{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:   "create [options] --name <name> --type <type> --location <location> --password <password>",
			Short: `Create a new Storage Box`,
		}

		cmd.Flags().String("name", "", "Storage Box name (required)")
		_ = cmd.MarkFlagRequired("name")

		cmd.Flags().String("type", "", "Storage Box Type (ID or name) (required)")
		_ = cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidatesF(client.StorageBoxType().Names))
		_ = cmd.MarkFlagRequired("type")

		cmd.Flags().String("location", "", "Location (ID or name) (required)")
		_ = cmd.MarkFlagRequired("location")
		_ = cmd.RegisterFlagCompletionFunc("location", cmpl.SuggestCandidatesF(client.Location().Names))

		cmd.Flags().String("password", "", "The password that will be set for this Storage Box (required)")
		_ = cmd.MarkFlagRequired("password")

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

		cmd.Flags().StringArray("ssh-key", []string{}, "SSH public keys in OpenSSH format or as the ID or name of an existing SSH key")
		_ = cmd.RegisterFlagCompletionFunc("ssh-key", cmpl.SuggestCandidatesF(client.SSHKey().Names))

		cmd.Flags().Bool("enable-samba", false, "Whether the Samba subsystem should be enabled (true, false)")
		cmd.Flags().Bool("enable-ssh", false, "Whether the SSH subsystem should be enabled (true, false)")
		cmd.Flags().Bool("enable-webdav", false, "Whether the WebDAV subsystem should be enabled (true, false)")
		cmd.Flags().Bool("enable-zfs", false, "Whether the ZFS Snapshot folder should be visible (true, false)")
		cmd.Flags().Bool("reachable-externally", false, "Whether the Storage Box should be accessible from outside the Hetzner network (true, false)")

		cmd.Flags().StringSlice("enable-protection", []string{}, "Enable protection (delete) (default: none)")
		_ = cmd.RegisterFlagCompletionFunc("enable-protection", cmpl.SuggestCandidates("delete"))

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, _ []string) (*hcloud.StorageBox, any, error) {
		name, _ := cmd.Flags().GetString("name")
		sbType, _ := cmd.Flags().GetString("type")
		location, _ := cmd.Flags().GetString("location")
		password, _ := cmd.Flags().GetString("password")
		sshKeys, _ := cmd.Flags().GetStringArray("ssh-key")
		labels, _ := cmd.Flags().GetStringToString("label")
		protection, _ := cmd.Flags().GetStringSlice("enable-protection")

		protectionOpts, err := ChangeProtectionCmds.GetChangeProtectionOpts(true, protection)
		if err != nil {
			return nil, nil, err
		}

		enableSamba, _ := cmd.Flags().GetBool("enable-samba")
		enableSSH, _ := cmd.Flags().GetBool("enable-ssh")
		enableWebDAV, _ := cmd.Flags().GetBool("enable-webdav")
		enableZFS, _ := cmd.Flags().GetBool("enable-zfs")
		reachableExternally, _ := cmd.Flags().GetBool("reachable-externally")

		if !cmd.Flags().Changed("ssh-key") && config.OptionDefaultSSHKeys.Changed(s.Config()) {
			sshKeys, err = config.OptionDefaultSSHKeys.Get(s.Config())
			if err != nil {
				return nil, nil, err
			}
		}

		resolvedSSHKeys := make([]*hcloud.SSHKey, len(sshKeys))
		for i, sshKey := range sshKeys {
			resolvedSSHKeys[i], err = resolveSSHKey(s, sshKey)
			if err != nil {
				return nil, nil, err
			}
		}

		var accessSettings hcloud.StorageBoxCreateOptsAccessSettings
		if cmd.Flags().Changed("enable-samba") {
			accessSettings.SambaEnabled = &enableSamba
		}
		if cmd.Flags().Changed("enable-ssh") {
			accessSettings.SSHEnabled = &enableSSH
		}
		if cmd.Flags().Changed("enable-webdav") {
			accessSettings.WebDAVEnabled = &enableWebDAV
		}
		if cmd.Flags().Changed("enable-zfs") {
			accessSettings.ZFSEnabled = &enableZFS
		}
		if cmd.Flags().Changed("reachable-externally") {
			accessSettings.ReachableExternally = &reachableExternally
		}

		opts := hcloud.StorageBoxCreateOpts{
			Name:           name,
			StorageBoxType: &hcloud.StorageBoxType{Name: sbType},
			Location:       &hcloud.Location{Name: location},
			Labels:         labels,
			Password:       password,
			SSHKeys:        resolvedSSHKeys,
			AccessSettings: &accessSettings,
		}
		result, _, err := s.Client().StorageBox().Create(s, opts)
		if err != nil {
			return nil, nil, err
		}

		if err := s.WaitForActions(s, cmd, result.Action); err != nil {
			return nil, nil, err
		}
		cmd.Printf("Storage Box %d created\n", result.StorageBox.ID)

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

		if protectionOpts.Delete != nil {
			if err := ChangeProtectionCmds.ChangeProtection(s, cmd, storageBox, true, protectionOpts); err != nil {
				return nil, nil, err
			}
		}

		return storageBox, util.Wrap("storage_box", hcloud.SchemaFromStorageBox(result.StorageBox)), nil
	},
	PrintResource: func(_ state.State, cmd *cobra.Command, storageBox *hcloud.StorageBox) {
		cmd.Printf("Server: %s\n", storageBox.Server)
		cmd.Printf("Username: %s\n", storageBox.Username)
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var DeleteCmd = base.DeleteCmd[*hcloud.StorageBox]{
	ResourceNameSingular: "Storage Box",
	ResourceNamePlural:   "Storage Boxes",
	ShortDescription:     "Delete a Storage Box",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.StorageBox().Names },
	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.StorageBox, *hcloud.Response, error) {
		return s.Client().StorageBox().Get(s, idOrName)
	},
	Delete: func(s state.State, _ *cobra.Command, storageBox *hcloud.StorageBox) (*hcloud.Action, error) {
		result, _, err := s.Client().StorageBox().Delete(s, storageBox)
		return result.Action, err
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var DescribeCmd = base.DescribeCmd[*hcloud.StorageBox]{
	ResourceNameSingular: "Storage Box",
	ShortDescription:     "Describe a Storage Box",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.StorageBox().Names },
	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.StorageBox, any, error) {
		storageBox, _, err := s.Client().StorageBox().Get(s, idOrName)
		if err != nil {
			return nil, nil, err
		}
		return storageBox, hcloud.SchemaFromStorageBox(storageBox), nil
	},
	PrintText: func(s state.State, _ *cobra.Command, out io.Writer, storageBox *hcloud.StorageBox) error {

		fmt.Fprintf(out, "ID:\t%d\n", storageBox.ID)
		fmt.Fprintf(out, "Name:\t%s\n", storageBox.Name)
		fmt.Fprintf(out, "Created:\t%s (%s)\n", util.Datetime(storageBox.Created), humanize.Time(storageBox.Created))
		fmt.Fprintf(out, "Status:\t%s\n", storageBox.Status)
		fmt.Fprintf(out, "Username:\t%s\n", storageBox.Username)
		fmt.Fprintf(out, "Server:\t%s\n", storageBox.Server)
		fmt.Fprintf(out, "System:\t%s\n", storageBox.System)

		snapshotPlan := storageBox.SnapshotPlan
		fmt.Fprintln(out)
		fmt.Fprintf(out, "Snapshot Plan:\n")
		if snapshotPlan == nil {
			fmt.Fprintf(out, "  No snapshot plan active\n")
		} else {
			fmt.Fprintf(out, "  Max Snapshots:\t%d\n", snapshotPlan.MaxSnapshots)
			fmt.Fprintf(out, "  Minute:\t%d\n", snapshotPlan.Minute)
			fmt.Fprintf(out, "  Hour:\t%d\n", snapshotPlan.Hour)

			if snapshotPlan.DayOfWeek != nil {
				fmt.Fprintf(out, "  Day of Week:\t%s\n", *snapshotPlan.DayOfWeek)
			}
			if snapshotPlan.DayOfMonth != nil {
				fmt.Fprintf(out, "  Day of Month:\t%d\n", *snapshotPlan.DayOfMonth)
			}
		}

		protection := storageBox.Protection
		fmt.Fprintln(out)
		fmt.Fprintf(out, "Protection:\n")
		fmt.Fprintf(out, "  Delete:\t%t\n", protection.Delete)

		stats := storageBox.Stats
		fmt.Fprintln(out)
		fmt.Fprintf(out, "Stats:\n")
		fmt.Fprintf(out, "  Size:\t%s\n", humanize.IBytes(stats.Size))
		fmt.Fprintf(out, "  Size Data:\t%s\n", humanize.IBytes(stats.SizeData))
		fmt.Fprintf(out, "  Size Snapshots:\t%s\n", humanize.IBytes(stats.SizeSnapshots))

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

		accessSettings := storageBox.AccessSettings
		fmt.Fprintln(out)
		fmt.Fprintf(out, "Access Settings:\n")
		fmt.Fprintf(out, "  Reachable Externally:\t%t\n", accessSettings.ReachableExternally)
		fmt.Fprintf(out, "  Samba Enabled:\t%t\n", accessSettings.SambaEnabled)
		fmt.Fprintf(out, "  SSH Enabled:\t%t\n", accessSettings.SSHEnabled)
		fmt.Fprintf(out, "  WebDAV Enabled:\t%t\n", accessSettings.WebDAVEnabled)
		fmt.Fprintf(out, "  ZFS Enabled:\t%t\n", accessSettings.ZFSEnabled)

		typeDescription, _ := storageboxtype.DescribeStorageBoxType(s, storageBox.StorageBoxType, true)
		fmt.Fprintln(out)
		fmt.Fprintf(out, "Storage Box Type:\n")
		fmt.Fprintf(out, "%s", util.PrefixLines(typeDescription, "  "))

		fmt.Fprintln(out)
		fmt.Fprintf(out, "Location:\n")
		fmt.Fprintf(out, "%s", util.PrefixLines(location.DescribeLocation(storageBox.Location), "  "))

		return nil
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var DisableSnapshotPlanCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:   "disable-snapshot-plan <storage-box>",
			Short: "Disable automatic snapshots for a Storage Box",
			ValidArgsFunction: cmpl.SuggestArgs(
				cmpl.SuggestCandidatesF(client.StorageBox().Names),
			),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}
		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		idOrName := args[0]

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

		action, _, err := s.Client().StorageBox().DisableSnapshotPlan(s, storageBox)
		if err != nil {
			return err
		}

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

		cmd.Printf("Snapshot Plan disabled for Storage Box %d\n", storageBox.ID)
		return nil
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var EnableSnapshotPlanCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:   "enable-snapshot-plan [options] <storage-box>",
			Short: "Enable automatic snapshots for a Storage Box",
			Long: `Enable automatic snapshots for a Storage Box

Allowed values for --day-of-week are:
- Sunday, Sun, 0, 7
- Monday, Mon, 1
- Tuesday, Tue, 2
- Wednesday, Wed, 3
- Thursday, Thu, 4
- Friday, Fri, 5
- Saturday, Sat, 6`,
			ValidArgsFunction: cmpl.SuggestArgs(
				cmpl.SuggestCandidatesF(client.StorageBox().Names),
			),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}

		cmd.Flags().Int("max-snapshots", 0, "Maximum amount of Snapshots that should be created by this Snapshot Plan")
		_ = cmd.MarkFlagRequired("max-snapshots")

		cmd.Flags().Int("minute", 0, "Minute the Snapshot Plan should be executed on (UTC)")
		_ = cmd.MarkFlagRequired("minute")
		cmd.Flags().Int("hour", 0, "Hour the Snapshot Plan should be executed on (UTC)")
		_ = cmd.MarkFlagRequired("hour")

		cmd.Flags().String("day-of-week", "", "Day of the week the Snapshot Plan should be executed on. Not specified means every day")
		cmd.Flags().Int("day-of-month", 0, "Day of the month the Snapshot Plan should be executed on. Not specified means every day")

		_ = cmd.RegisterFlagCompletionFunc("day-of-week", cmpl.SuggestCandidates("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"))

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		idOrName := args[0]
		maxSnapshots, _ := cmd.Flags().GetInt("max-snapshots")
		minute, _ := cmd.Flags().GetInt("minute")
		hour, _ := cmd.Flags().GetInt("hour")
		dayOfWeek, _ := cmd.Flags().GetString("day-of-week")
		dayOfMonth, _ := cmd.Flags().GetInt("day-of-month")

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

		opts := hcloud.StorageBoxEnableSnapshotPlanOpts{
			MaxSnapshots: maxSnapshots,
			Minute:       minute,
			Hour:         hour,
		}

		if cmd.Flags().Changed("day-of-week") {
			weekday, err := util.WeekdayFromString(dayOfWeek)
			if err != nil {
				return err
			}
			opts.DayOfWeek = &weekday
		}
		if cmd.Flags().Changed("day-of-month") {
			opts.DayOfMonth = &dayOfMonth
		}

		action, _, err := s.Client().StorageBox().EnableSnapshotPlan(s, storageBox, opts)
		if err != nil {
			return err
		}

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

		cmd.Printf("Snapshot Plan enabled for Storage Box %d\n", storageBox.ID)
		return nil
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var FoldersCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:                   "folders <storage-box>",
			Short:                 "List folders of a Storage Box",
			ValidArgsFunction:     cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.StorageBox().Names)),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}
		cmd.Flags().String("path", "", "Relative path for which the listing is to be made")

		output.AddFlag(cmd, output.OptionJSON(), output.OptionYAML())
		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		idOrName := args[0]
		path, _ := cmd.Flags().GetString("path")
		outOpts := output.FlagsForCommand(cmd)

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

		var opts hcloud.StorageBoxFoldersOpts
		if cmd.Flags().Changed("path") {
			opts.Path = path
		}

		result, _, err := s.Client().StorageBox().Folders(s, storageBox, opts)
		if err != nil {
			return err
		}

		if outOpts.IsSet("json") || outOpts.IsSet("yaml") {
			schema := struct {
				Folders []string `json:"folders"`
			}{
				Folders: result.Folders,
			}

			if outOpts.IsSet("json") {
				return util.DescribeJSON(cmd.OutOrStdout(), schema)
			}
			return util.DescribeYAML(cmd.OutOrStdout(), schema)
		}

		if len(result.Folders) == 0 {
			cmd.Println("No folders found.")
		} else {
			cmd.Println("Folders:")
			for _, folder := range result.Folders {
				cmd.Printf("- %s\n", folder)
			}
		}
		return nil
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var LabelCmds = base.LabelCmds[*hcloud.StorageBox]{
	ResourceNameSingular:   "Storage Box",
	ShortDescriptionAdd:    "Add a label to a Storage Box",
	ShortDescriptionRemove: "Remove a label from a Storage Box",
	NameSuggestions:        func(c hcapi2.Client) func() []string { return c.StorageBox().Names },
	LabelKeySuggestions:    func(c hcapi2.Client) func(idOrName string) []string { return c.StorageBox().LabelKeys },
	Fetch: func(s state.State, idOrName string) (*hcloud.StorageBox, error) {
		storageBox, _, err := s.Client().StorageBox().Get(s, idOrName)
		if err != nil {
			return nil, err
		}
		if storageBox == nil {
			return nil, fmt.Errorf("Storage Box not found: %s", idOrName)
		}
		return storageBox, nil
	},
	SetLabels: func(s state.State, storageBox *hcloud.StorageBox, labels map[string]string) error {
		opts := hcloud.StorageBoxUpdateOpts{
			Labels: labels,
		}
		_, _, err := s.Client().StorageBox().Update(s, storageBox, opts)
		return err
	},
	GetLabels: func(storageBox *hcloud.StorageBox) map[string]string {
		return storageBox.Labels
	},
	GetIDOrName: func(storageBox *hcloud.StorageBox) string {
		return storageBox.Name
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var ListCmd = &base.ListCmd[*hcloud.StorageBox, schema.StorageBox]{
	ResourceNamePlural: "Storage Boxes",
	JSONKeyGetByName:   "storage_boxes",
	DefaultColumns:     []string{"id", "name", "username", "server", "type", "size", "location", "age"},
	SortOption:         config.OptionSortStorageBox,

	Fetch: func(s state.State, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]*hcloud.StorageBox, error) {
		opts := hcloud.StorageBoxListOpts{ListOpts: listOpts}
		if len(sorts) > 0 {
			opts.Sort = sorts
		}
		return s.Client().StorageBox().AllWithOpts(s, opts)
	},

	OutputTable: func(t *output.Table[*hcloud.StorageBox], _ hcapi2.Client) {
		t.
			AddAllowedFields(&hcloud.StorageBox{}).
			AddFieldFn("type", func(storageBox *hcloud.StorageBox) string {
				return storageBox.StorageBoxType.Name
			}).
			AddFieldFn("location", func(storageBox *hcloud.StorageBox) string {
				return storageBox.Location.Name
			}).
			AddFieldFn("size", func(storageBox *hcloud.StorageBox) string {
				return humanize.IBytes(storageBox.Stats.Size)
			}).
			AddFieldFn("labels", func(storageBox *hcloud.StorageBox) string {
				return util.LabelsToString(storageBox.Labels)
			}).
			AddFieldFn("created", func(storageBox *hcloud.StorageBox) string {
				return util.Datetime(storageBox.Created)
			}).
			AddFieldFn("age", func(storageBox *hcloud.StorageBox) string {
				return util.Age(storageBox.Created, time.Now())
			})
	},
	Schema:       hcloud.SchemaFromStorageBox,
	Experimental: experimental.StorageBoxes,
}
View Source
var ResetPasswordCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {
		cmd := &cobra.Command{
			Use:                   "reset-password --password <password> <storage-box>",
			Short:                 "Reset the password of a Storage Box",
			ValidArgsFunction:     cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.StorageBox().Names)),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}

		cmd.Flags().String("password", "", "New password for the Storage Box")
		_ = cmd.MarkFlagRequired("password")

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		idOrName := args[0]
		password, _ := cmd.Flags().GetString("password")

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

		opts := hcloud.StorageBoxResetPasswordOpts{
			Password: password,
		}

		action, _, err := s.Client().StorageBox().ResetPassword(s, storageBox, opts)
		if err != nil {
			return err
		}

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

		cmd.Printf("Password of Storage Box %d reset\n", storageBox.ID)
		return nil
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var RollbackSnapshotCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {

		cmd := &cobra.Command{
			Use:                   "rollback-snapshot --snapshot <snapshot> <storage-box>",
			Short:                 "Rolls back the Storage Box to the given Snapshot",
			ValidArgsFunction:     cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.StorageBox().Names)),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}

		cmd.Flags().String("snapshot", "", "The name or ID of the snapshot to roll back to")
		_ = cmd.MarkFlagRequired("snapshot")
		_ = cmd.RegisterFlagCompletionFunc("snapshot", snapshot.SuggestSnapshots(client))

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		idOrName := args[0]
		snapshotIDOrName, _ := cmd.Flags().GetString("snapshot")

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

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

		action, _, err := s.Client().StorageBox().RollbackSnapshot(s, storageBox, hcloud.StorageBoxRollbackSnapshotOpts{
			Snapshot: snapshot,
		})
		if err != nil {
			return err
		}

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

		cmd.Printf("Rolled back Storage Box %d to Snapshot %d\n", storageBox.ID, snapshot.ID)
		return nil
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var UpdateAccessSettingsCmd = base.Cmd{
	BaseCobraCommand: func(client hcapi2.Client) *cobra.Command {

		cmd := &cobra.Command{
			Use:                   "update-access-settings [options] <storage-box>",
			Short:                 "Update access settings of the primary Storage Box account",
			ValidArgsFunction:     cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.StorageBox().Names)),
			TraverseChildren:      true,
			DisableFlagsInUseLine: true,
		}

		cmd.Flags().Bool("enable-samba", false, "Whether the Samba subsystem should be enabled (true, false)")
		cmd.Flags().Bool("enable-ssh", false, "Whether the SSH subsystem should be enabled (true, false)")
		cmd.Flags().Bool("enable-webdav", false, "Whether the WebDAV subsystem should be enabled (true, false)")
		cmd.Flags().Bool("enable-zfs", false, "Whether the ZFS Snapshot folder should be visible (true, false)")
		cmd.Flags().Bool("reachable-externally", false, "Whether the Storage Box should be accessible from outside the Hetzner network (true, false)")

		cmd.MarkFlagsOneRequired("enable-samba", "enable-ssh", "enable-webdav", "enable-zfs", "reachable-externally")

		return cmd
	},
	Run: func(s state.State, cmd *cobra.Command, args []string) error {
		idOrName := args[0]
		enableSamba, _ := cmd.Flags().GetBool("enable-samba")
		enableSSH, _ := cmd.Flags().GetBool("enable-ssh")
		enableWebDAV, _ := cmd.Flags().GetBool("enable-webdav")
		enableZFS, _ := cmd.Flags().GetBool("enable-zfs")
		reachableExternally, _ := cmd.Flags().GetBool("reachable-externally")

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

		var opts hcloud.StorageBoxUpdateAccessSettingsOpts
		if cmd.Flags().Changed("enable-samba") {
			opts.SambaEnabled = &enableSamba
		}
		if cmd.Flags().Changed("enable-ssh") {
			opts.SSHEnabled = &enableSSH
		}
		if cmd.Flags().Changed("enable-webdav") {
			opts.WebDAVEnabled = &enableWebDAV
		}
		if cmd.Flags().Changed("enable-zfs") {
			opts.ZFSEnabled = &enableZFS
		}
		if cmd.Flags().Changed("reachable-externally") {
			opts.ReachableExternally = &reachableExternally
		}

		action, _, err := s.Client().StorageBox().UpdateAccessSettings(s, storageBox, opts)
		if err != nil {
			return err
		}

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

		cmd.Printf("Access settings updated for Storage Box %d\n", storageBox.ID)
		return nil
	},
	Experimental: experimental.StorageBoxes,
}
View Source
var UpdateCmd = base.UpdateCmd[*hcloud.StorageBox]{
	ResourceNameSingular: "Storage Box",
	ShortDescription:     "Update a Storage Box",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.StorageBox().Names },
	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.StorageBox, *hcloud.Response, error) {
		return s.Client().StorageBox().Get(s, idOrName)
	},
	DefineFlags: func(cmd *cobra.Command) {
		cmd.Flags().String("name", "", "Storage Box name")
	},
	Update: func(s state.State, _ *cobra.Command, storageBox *hcloud.StorageBox, flags map[string]pflag.Value) error {
		opts := hcloud.StorageBoxUpdateOpts{
			Name: flags["name"].String(),
		}
		_, _, err := s.Client().StorageBox().Update(s, storageBox, opts)
		if err != nil {
			return err
		}
		return nil
	},
	Experimental: experimental.StorageBoxes,
}

Functions

func NewCommand

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

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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