Skip to content

Commit

Permalink
chore: replace hcapi.ImageClient usage with hcapi2 (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
samcday authored Feb 17, 2023
1 parent 547bcdd commit 5c39ae0
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 195 deletions.
89 changes: 45 additions & 44 deletions internal/cmd/image/disable_protection.go
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
},
}
88 changes: 45 additions & 43 deletions internal/cmd/image/enable_protection.go
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
},
}
4 changes: 2 additions & 2 deletions internal/cmd/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
deleteCmd.CobraCommand(cli.Context, client, cli),
describeCmd.CobraCommand(cli.Context, client, cli),
updateCmd.CobraCommand(cli.Context, client, cli),
newEnableProtectionCommand(cli),
newDisableProtectionCommand(cli),
EnableProtectionCommand.CobraCommand(cli.Context, client, cli, cli),
DisableProtectionCommand.CobraCommand(cli.Context, client, cli, cli),
labelCmds.AddCobraCommand(cli.Context, client, cli),
labelCmds.RemoveCobraCommand(cli.Context, client, cli),
)
Expand Down
94 changes: 48 additions & 46 deletions internal/cmd/server/rebuild.go
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
},
}
2 changes: 1 addition & 1 deletion internal/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewCommand(cli *state.State, client hcapi2.Client) *cobra.Command {
newDetachISOCommand(cli),
updateCmd.CobraCommand(cli.Context, client, cli),
newChangeTypeCommand(cli),
newRebuildCommand(cli),
RebuildCommand.CobraCommand(cli.Context, client, cli, cli),
newEnableBackupCommand(cli),
newDisableBackupCommand(cli),
newEnableProtectionCommand(cli),
Expand Down
42 changes: 0 additions & 42 deletions internal/hcapi/image.go

This file was deleted.

Loading

0 comments on commit 5c39ae0

Please sign in to comment.