Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var AttachCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "attach [FLAGS] VOLUME", Short: "Attach a volume to a server", Args: cobra.ExactArgs(1), ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Volume().Names)), TraverseChildren: true, DisableFlagsInUseLine: true, } cmd.Flags().String("server", "", "Server (ID or name) (required)") cmd.RegisterFlagCompletionFunc("server", cmpl.SuggestCandidatesF(client.Server().Names)) cmd.MarkFlagRequired("server") cmd.Flags().Bool("automount", false, "Automount volume after attach") return cmd }, Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { volume, _, err := client.Volume().Get(ctx, args[0]) if err != nil { return err } if volume == nil { return fmt.Errorf("volume not found: %s", args[0]) } serverIDOrName, _ := cmd.Flags().GetString("server") server, _, err := client.Server().Get(ctx, serverIDOrName) if err != nil { return err } if server == nil { return fmt.Errorf("server not found: %s", serverIDOrName) } automount, _ := cmd.Flags().GetBool("automount") action, _, err := client.Volume().AttachWithOpts(ctx, volume, hcloud.VolumeAttachOpts{ Server: server, Automount: &automount, }) if err != nil { return err } if err := waiter.ActionProgress(ctx, action); err != nil { return err } fmt.Printf("Volume %d attached to server %s\n", volume.ID, server.Name) return nil }, }
View Source
var CreateCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "create FLAGS", Short: "Create a volume", Args: cobra.NoArgs, TraverseChildren: true, DisableFlagsInUseLine: true, } cmd.Flags().String("name", "", "Volume name (required)") cmd.MarkFlagRequired("name") cmd.Flags().String("server", "", "Server (ID or name)") cmd.RegisterFlagCompletionFunc("server", cmpl.SuggestCandidatesF(client.Server().Names)) cmd.Flags().String("location", "", "Location (ID or name)") cmd.RegisterFlagCompletionFunc("location", cmpl.SuggestCandidatesF(client.Location().Names)) cmd.Flags().Int("size", 0, "Size (GB) (required)") cmd.MarkFlagRequired("size") cmd.Flags().Bool("automount", false, "Automount volume after attach (server must be provided)") cmd.Flags().String("format", "", "Format volume after creation (ext4 or xfs)") cmd.RegisterFlagCompletionFunc("format", cmpl.SuggestCandidates("ext4", "xfs")) cmd.Flags().StringToString("label", nil, "User-defined labels ('key=value') (can be specified multiple times)") cmd.Flags().StringSlice("enable-protection", []string{}, "Enable protection (delete) (default: none)") cmd.RegisterFlagCompletionFunc("enable-protection", cmpl.SuggestCandidates("delete")) return cmd }, Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { name, _ := cmd.Flags().GetString("name") serverIDOrName, _ := cmd.Flags().GetString("server") size, _ := cmd.Flags().GetInt("size") location, _ := cmd.Flags().GetString("location") automount, _ := cmd.Flags().GetBool("automount") format, _ := cmd.Flags().GetString("format") labels, _ := cmd.Flags().GetStringToString("label") protection, _ := cmd.Flags().GetStringSlice("enable-protection") protectionOpts, err := getChangeProtectionOpts(true, protection) if err != nil { return err } createOpts := hcloud.VolumeCreateOpts{ Name: name, Size: size, Labels: labels, } if location != "" { id, err := strconv.ParseInt(location, 10, 64) if err == nil { createOpts.Location = &hcloud.Location{ID: id} } else { createOpts.Location = &hcloud.Location{Name: location} } } if serverIDOrName != "" { server, _, err := client.Server().Get(ctx, serverIDOrName) if err != nil { return err } if server == nil { return fmt.Errorf("server not found: %s", serverIDOrName) } createOpts.Server = server } if automount { createOpts.Automount = &automount } if format != "" { createOpts.Format = &format } result, _, err := client.Volume().Create(ctx, createOpts) if err != nil { return err } if err := waiter.ActionProgress(ctx, result.Action); err != nil { return err } if err := waiter.WaitForActions(ctx, result.NextActions); err != nil { return err } fmt.Printf("Volume %d created\n", result.Volume.ID) return changeProtection(ctx, client, waiter, result.Volume, true, protectionOpts) }, }
View Source
var DetachCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "detach [FLAGS] VOLUME", Short: "Detach a volume", Args: cobra.ExactArgs(1), ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Volume().Names)), TraverseChildren: true, DisableFlagsInUseLine: true, } }, Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { volume, _, err := client.Volume().Get(ctx, args[0]) if err != nil { return err } if volume == nil { return fmt.Errorf("volume not found: %s", args[0]) } action, _, err := client.Volume().Detach(ctx, volume) if err != nil { return err } if err := waiter.ActionProgress(ctx, action); err != nil { return err } fmt.Printf("Volume %d detached\n", volume.ID) return nil }, }
View Source
var DisableProtectionCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "disable-protection [FLAGS] VOLUME PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Disable resource protection for a volume", Args: cobra.MinimumNArgs(2), ValidArgsFunction: cmpl.SuggestArgs( cmpl.SuggestCandidatesF(client.Volume().Names), cmpl.SuggestCandidates("delete"), ), TraverseChildren: true, DisableFlagsInUseLine: true, } }, Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { volume, _, err := client.Volume().Get(ctx, args[0]) if err != nil { return err } if volume == nil { return fmt.Errorf("volume not found: %s", args[0]) } opts, err := getChangeProtectionOpts(false, args[1:]) if err != nil { return err } return changeProtection(ctx, client, waiter, volume, false, opts) }, }
View Source
var EnableProtectionCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { return &cobra.Command{ Use: "enable-protection [FLAGS] VOLUME PROTECTIONLEVEL [PROTECTIONLEVEL...]", Short: "Enable resource protection for a volume", Args: cobra.MinimumNArgs(2), ValidArgsFunction: cmpl.SuggestArgs( cmpl.SuggestCandidatesF(client.Volume().Names), cmpl.SuggestCandidates("delete"), ), TraverseChildren: true, DisableFlagsInUseLine: true, } }, Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { volume, _, err := client.Volume().Get(ctx, args[0]) if err != nil { return err } if volume == nil { return fmt.Errorf("volume not found: %s", args[0]) } opts, err := getChangeProtectionOpts(true, args[1:]) if err != nil { return err } return changeProtection(ctx, client, waiter, volume, true, opts) }, }
View Source
var ListCmd = base.ListCmd{ ResourceNamePlural: "Volumes", JSONKeyGetByName: "volumes", DefaultColumns: []string{"id", "name", "size", "server", "location", "age"}, Fetch: func(ctx context.Context, client hcapi2.Client, _ *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]interface{}, error) { opts := hcloud.VolumeListOpts{ListOpts: listOpts} if len(sorts) > 0 { opts.Sort = sorts } volumes, err := client.Volume().AllWithOpts(ctx, opts) var resources []interface{} for _, n := range volumes { resources = append(resources, n) } return resources, err }, OutputTable: func(client hcapi2.Client) *output.Table { return output.NewTable(). AddAllowedFields(hcloud.Volume{}). AddFieldFn("server", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) var server string if volume.Server != nil { return client.Server().ServerName(volume.Server.ID) } return util.NA(server) })). AddFieldFn("size", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) return humanize.Bytes(uint64(volume.Size * humanize.GByte)) })). AddFieldFn("location", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) return volume.Location.Name })). AddFieldFn("protection", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) var protection []string if volume.Protection.Delete { protection = append(protection, "delete") } return strings.Join(protection, ", ") })). AddFieldFn("labels", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) return util.LabelsToString(volume.Labels) })). AddFieldFn("created", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) return util.Datetime(volume.Created) })). AddFieldFn("age", output.FieldFn(func(obj interface{}) string { volume := obj.(*hcloud.Volume) return util.Age(volume.Created, time.Now()) })) }, JSONSchema: func(resources []interface{}) interface{} { volumesSchema := make([]schema.Volume, 0, len(resources)) for _, resource := range resources { volume := resource.(*hcloud.Volume) volumeSchema := schema.Volume{ ID: volume.ID, Name: volume.Name, Location: util.LocationToSchema(*volume.Location), Size: volume.Size, LinuxDevice: volume.LinuxDevice, Labels: volume.Labels, Created: volume.Created, Protection: schema.VolumeProtection{Delete: volume.Protection.Delete}, } if volume.Server != nil { volumeSchema.Server = hcloud.Ptr(volume.Server.ID) } volumesSchema = append(volumesSchema, volumeSchema) } return volumesSchema }, }
View Source
var ResizeCommand = base.Cmd{ BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { cmd := &cobra.Command{ Use: "resize [FLAGS] VOLUME", Short: "Resize a volume", Args: cobra.ExactArgs(1), ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Volume().Names)), TraverseChildren: true, DisableFlagsInUseLine: true, } cmd.Flags().Int("size", 0, "New size (GB) of the volume (required)") cmd.MarkFlagRequired("size") return cmd }, Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { volume, _, err := client.Volume().Get(ctx, args[0]) if err != nil { return err } if volume == nil { return fmt.Errorf("volume not found: %s", args[0]) } size, _ := cmd.Flags().GetInt("size") action, _, err := client.Volume().Resize(ctx, volume, size) if err != nil { return err } if err := waiter.ActionProgress(ctx, action); err != nil { return err } fmt.Printf("Volume %d resized\n", volume.ID) fmt.Printf("You might need to adjust the filesystem size on the server too\n") return nil }, }
Functions ¶
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.