-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: replace hcapi.ImageClient usage with hcapi2 (#445)
- Loading branch information
Showing
8 changed files
with
141 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,65 @@ | ||
package image | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/base" | ||
"github.com/hetznercloud/cli/internal/cmd/cmpl" | ||
"github.com/hetznercloud/cli/internal/hcapi2" | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/hetznercloud/hcloud-go/hcloud" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newDisableProtectionCommand(cli *state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "disable-protection [FLAGS] IMAGE PROTECTIONLEVEL [PROTECTIONLEVEL...]", | ||
Short: "Disable resource protection for an image", | ||
Args: cobra.MinimumNArgs(2), | ||
ValidArgsFunction: cmpl.SuggestArgs( | ||
cmpl.SuggestCandidatesF(cli.ImageNames), | ||
cmpl.SuggestCandidates("delete"), | ||
), | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
PreRunE: cli.EnsureToken, | ||
RunE: cli.Wrap(runDisableProtection), | ||
} | ||
return cmd | ||
} | ||
|
||
func runDisableProtection(cli *state.State, cmd *cobra.Command, args []string) error { | ||
imageID, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
return errors.New("invalid image ID") | ||
} | ||
image := &hcloud.Image{ID: imageID} | ||
var DisableProtectionCommand = base.Cmd{ | ||
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "disable-protection [FLAGS] IMAGE PROTECTIONLEVEL [PROTECTIONLEVEL...]", | ||
Short: "Disable resource protection for an image", | ||
Args: cobra.MinimumNArgs(2), | ||
ValidArgsFunction: cmpl.SuggestArgs( | ||
cmpl.SuggestCandidatesF(client.Image().Names), | ||
cmpl.SuggestCandidates("delete"), | ||
), | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
} | ||
}, | ||
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, command *cobra.Command, args []string) error { | ||
imageID, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
return errors.New("invalid image ID") | ||
} | ||
image := &hcloud.Image{ID: imageID} | ||
|
||
var unknown []string | ||
opts := hcloud.ImageChangeProtectionOpts{} | ||
for _, arg := range args[1:] { | ||
switch strings.ToLower(arg) { | ||
case "delete": | ||
opts.Delete = hcloud.Bool(false) | ||
default: | ||
unknown = append(unknown, arg) | ||
var unknown []string | ||
opts := hcloud.ImageChangeProtectionOpts{} | ||
for _, arg := range args[1:] { | ||
switch strings.ToLower(arg) { | ||
case "delete": | ||
opts.Delete = hcloud.Bool(false) | ||
default: | ||
unknown = append(unknown, arg) | ||
} | ||
} | ||
if len(unknown) > 0 { | ||
return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) | ||
} | ||
} | ||
if len(unknown) > 0 { | ||
return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) | ||
} | ||
|
||
action, _, err := cli.Client().Image.ChangeProtection(cli.Context, image, opts) | ||
if err != nil { | ||
return err | ||
} | ||
action, _, err := client.Image().ChangeProtection(ctx, image, opts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := cli.ActionProgress(cli.Context, action); err != nil { | ||
return err | ||
} | ||
if err := waiter.ActionProgress(ctx, action); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Resource protection disabled for image %d\n", image.ID) | ||
return nil | ||
fmt.Printf("Resource protection disabled for image %d\n", image.ID) | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,66 @@ | ||
package image | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/base" | ||
"github.com/hetznercloud/cli/internal/cmd/cmpl" | ||
"github.com/hetznercloud/cli/internal/hcapi2" | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/hetznercloud/hcloud-go/hcloud" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newEnableProtectionCommand(cli *state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "enable-protection [FLAGS] IMAGE PROTECTIONLEVEL [PROTECTIONLEVEL...]", | ||
Short: "Enable resource protection for an image", | ||
Args: cobra.MinimumNArgs(2), | ||
ValidArgsFunction: cmpl.SuggestArgs( | ||
cmpl.SuggestCandidatesF(cli.ImageNames), | ||
cmpl.SuggestCandidates("delete"), | ||
), | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
PreRunE: cli.EnsureToken, | ||
RunE: cli.Wrap(runEnableProtection), | ||
} | ||
return cmd | ||
} | ||
var EnableProtectionCommand = base.Cmd{ | ||
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { | ||
|
||
func runEnableProtection(cli *state.State, cmd *cobra.Command, args []string) error { | ||
imageID, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
return errors.New("invalid image ID") | ||
} | ||
image := &hcloud.Image{ID: imageID} | ||
return &cobra.Command{ | ||
Use: "enable-protection [FLAGS] IMAGE PROTECTIONLEVEL [PROTECTIONLEVEL...]", | ||
Short: "Enable resource protection for an image", | ||
Args: cobra.MinimumNArgs(2), | ||
ValidArgsFunction: cmpl.SuggestArgs( | ||
cmpl.SuggestCandidatesF(client.Image().Names), | ||
cmpl.SuggestCandidates("delete"), | ||
), | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
} | ||
}, | ||
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, command *cobra.Command, args []string) error { | ||
imageID, err := strconv.Atoi(args[0]) | ||
if err != nil { | ||
return errors.New("invalid image ID") | ||
} | ||
image := &hcloud.Image{ID: imageID} | ||
|
||
var unknown []string | ||
opts := hcloud.ImageChangeProtectionOpts{} | ||
for _, arg := range args[1:] { | ||
switch strings.ToLower(arg) { | ||
case "delete": | ||
opts.Delete = hcloud.Bool(true) | ||
default: | ||
unknown = append(unknown, arg) | ||
var unknown []string | ||
opts := hcloud.ImageChangeProtectionOpts{} | ||
for _, arg := range args[1:] { | ||
switch strings.ToLower(arg) { | ||
case "delete": | ||
opts.Delete = hcloud.Bool(true) | ||
default: | ||
unknown = append(unknown, arg) | ||
} | ||
} | ||
if len(unknown) > 0 { | ||
return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) | ||
} | ||
} | ||
if len(unknown) > 0 { | ||
return fmt.Errorf("unknown protection level: %s", strings.Join(unknown, ", ")) | ||
} | ||
|
||
action, _, err := cli.Client().Image.ChangeProtection(cli.Context, image, opts) | ||
if err != nil { | ||
return err | ||
} | ||
action, _, err := client.Image().ChangeProtection(ctx, image, opts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := cli.ActionProgress(cli.Context, action); err != nil { | ||
return err | ||
} | ||
if err := waiter.ActionProgress(ctx, action); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Resource protection enabled for image %d\n", image.ID) | ||
return nil | ||
fmt.Printf("Resource protection enabled for image %d\n", image.ID) | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,66 @@ | ||
package server | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hetznercloud/cli/internal/cmd/base" | ||
"github.com/hetznercloud/cli/internal/cmd/cmpl" | ||
"github.com/hetznercloud/cli/internal/hcapi2" | ||
"github.com/hetznercloud/cli/internal/state" | ||
"github.com/hetznercloud/hcloud-go/hcloud" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newRebuildCommand(cli *state.State) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "rebuild [FLAGS] SERVER", | ||
Short: "Rebuild a server", | ||
Args: cobra.ExactArgs(1), | ||
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(cli.ServerNames)), | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
PreRunE: cli.EnsureToken, | ||
RunE: cli.Wrap(runRebuild), | ||
} | ||
var RebuildCommand = base.Cmd{ | ||
BaseCobraCommand: func(client hcapi2.Client) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "rebuild [FLAGS] SERVER", | ||
Short: "Rebuild a server", | ||
Args: cobra.ExactArgs(1), | ||
ValidArgsFunction: cmpl.SuggestArgs(cmpl.SuggestCandidatesF(client.Server().Names)), | ||
TraverseChildren: true, | ||
DisableFlagsInUseLine: true, | ||
} | ||
|
||
cmd.Flags().String("image", "", "ID or name of image to rebuild from (required)") | ||
cmd.RegisterFlagCompletionFunc("image", cmpl.SuggestCandidatesF(cli.ImageNames)) | ||
cmd.MarkFlagRequired("image") | ||
cmd.Flags().String("image", "", "ID or name of image to rebuild from (required)") | ||
cmd.RegisterFlagCompletionFunc("image", cmpl.SuggestCandidatesF(client.Image().Names)) | ||
cmd.MarkFlagRequired("image") | ||
|
||
return cmd | ||
} | ||
|
||
func runRebuild(cli *state.State, cmd *cobra.Command, args []string) error { | ||
serverIDOrName := args[0] | ||
server, _, err := cli.Client().Server.Get(cli.Context, serverIDOrName) | ||
if err != nil { | ||
return err | ||
} | ||
if server == nil { | ||
return fmt.Errorf("server not found: %s", serverIDOrName) | ||
} | ||
return cmd | ||
}, | ||
Run: func(ctx context.Context, client hcapi2.Client, waiter state.ActionWaiter, cmd *cobra.Command, args []string) error { | ||
serverIDOrName := args[0] | ||
server, _, err := client.Server().Get(ctx, serverIDOrName) | ||
if err != nil { | ||
return err | ||
} | ||
if server == nil { | ||
return fmt.Errorf("server not found: %s", serverIDOrName) | ||
} | ||
|
||
imageIDOrName, _ := cmd.Flags().GetString("image") | ||
image, _, err := cli.Client().Image.Get(cli.Context, imageIDOrName) | ||
if err != nil { | ||
return err | ||
} | ||
if image == nil { | ||
return fmt.Errorf("image not found: %s", imageIDOrName) | ||
} | ||
imageIDOrName, _ := cmd.Flags().GetString("image") | ||
image, _, err := client.Image().Get(ctx, imageIDOrName) | ||
if err != nil { | ||
return err | ||
} | ||
if image == nil { | ||
return fmt.Errorf("image not found: %s", imageIDOrName) | ||
} | ||
|
||
opts := hcloud.ServerRebuildOpts{ | ||
Image: image, | ||
} | ||
action, _, err := cli.Client().Server.Rebuild(cli.Context, server, opts) | ||
if err != nil { | ||
return err | ||
} | ||
opts := hcloud.ServerRebuildOpts{ | ||
Image: image, | ||
} | ||
action, _, err := client.Server().Rebuild(ctx, server, opts) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := cli.ActionProgress(cli.Context, action); err != nil { | ||
return err | ||
} | ||
if err := waiter.ActionProgress(ctx, action); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Printf("Server %d rebuilt with image %s\n", server.ID, image.Name) | ||
return nil | ||
fmt.Printf("Server %d rebuilt with image %s\n", server.ID, image.Name) | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.