Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: replace hcapi.ImageClient usage with hcapi2 #445

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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