Skip to content

Commit

Permalink
fix & refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterp committed Aug 30, 2023
1 parent 25c74dd commit 3d9fc8d
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 63 deletions.
36 changes: 10 additions & 26 deletions cmd/instance_reset_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"github.com/google/uuid"
"github.com/spf13/cobra"

"github.com/exoscale/cli/pkg/account"
ego3 "github.com/exoscale/egoscale/v3"
"github.com/exoscale/cli/pkg/client"
"github.com/exoscale/cli/pkg/instance"
"github.com/exoscale/egoscale/v3/oapi"
)

Expand All @@ -19,9 +19,7 @@ type instanceResetPasswordCmd struct {

Instance string `cli-arg:"#" cli-usage:"NAME|ID"`

// TODO
Force bool `cli-short:"f" cli-usage:"don't prompt for confirmation"`
Zone string `cli-short:"z" cli-usage:"instance zone"`
Zone string `cli-short:"z" cli-usage:"instance zone"`
}

func (c *instanceResetPasswordCmd) cmdAliases() []string { return gRemoveAlias }
Expand All @@ -43,42 +41,28 @@ func (c *instanceResetPasswordCmd) cmdRun(_ *cobra.Command, _ []string) error {
ctx = context.Background()
)

client, err := ego3.DefaultClient(ego3.ClientOptWithCredentials(
account.CurrentAccount.Key,
account.CurrentAccount.APISecret(),
))
v3Client, err := client.Get()
if err != nil {
return err
}

client.SetZone(oapi.ZoneName(c.Zone))
v3Client.SetZone(oapi.ZoneName(c.Zone))

instanceUUID, err := uuid.Parse(c.Instance)
if err != nil {
// TODO move this to a function named FindInstanceByName
instanceList, err := client.Compute().Instance().List(ctx, nil)
instance, err := instance.FindInstanceByName(ctx, &v3Client.Client, c.Instance)
if err != nil {
return fmt.Errorf("failed to list instances: %w", err)
return err
}

var instanceID uuid.NullUUID
for _, instance := range instanceList {
if *instance.Name == c.Instance {
instanceID = uuid.NullUUID{
UUID: *instance.Id,
Valid: true,
}
}
}

if !instanceID.Valid {
if instance == nil {
return fmt.Errorf("unable to find instance by name %q", c.Instance)
}

instanceUUID = instanceID.UUID
instanceUUID = *instance.Id
}

_, err = client.Compute().Instance().ResetPassword(ctx, instanceUUID)
_, err = v3Client.Compute().Instance().ResetPassword(ctx, instanceUUID)
return err
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/exoscale/cli

replace (
github.com/exoscale/egoscale/v3 => github.com/exoscale/egoscale/v3 v3.0.0-20230830145018-2a52db7bef92
github.com/exoscale/egoscale/v3 => github.com/exoscale/egoscale/v3 v3.0.0-20230830163612-a8c92b4e9a83
gopkg.ini/ini.v1 => github.com/go-ini/ini v1.42.0
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/envoyproxy/protoc-gen-validate v0.6.2/go.mod h1:2t7qjJNvHPx8IjnBOzl9E9/baC+qXE/TeeyBRzgJDws=
github.com/exoscale/egoscale v0.100.3 h1:tXALrvTcwhnS0PG28a6PBWEUwuAwrNwoMjpuPrNIgeA=
github.com/exoscale/egoscale v0.100.3/go.mod h1:szh4hWSVh+ylgfti4AFR4mkRaCfUyUXSKS3PihlcOco=
github.com/exoscale/egoscale/v3 v3.0.0-20230830145018-2a52db7bef92 h1:4D1a9tFXBHtj7in2KS6m09St6AwlM9cl3mZvjDt0N3k=
github.com/exoscale/egoscale/v3 v3.0.0-20230830145018-2a52db7bef92/go.mod h1:bndUz/cWlSOuQwTUcbROmnY/Q0SxvAJ3iDtZAJxFmW4=
github.com/exoscale/egoscale/v3 v3.0.0-20230830163612-a8c92b4e9a83 h1:pm14BWcj0Lrq2QhByCA9OdEBswnNX30uyztNi0hiITM=
github.com/exoscale/egoscale/v3 v3.0.0-20230830163612-a8c92b4e9a83/go.mod h1:bndUz/cWlSOuQwTUcbROmnY/Q0SxvAJ3iDtZAJxFmW4=
github.com/exoscale/openapi-cli-generator v1.1.0 h1:fYjmPqHR5vxlOBrbvde7eo7bISNQIFxsGn4A5/acwKA=
github.com/exoscale/openapi-cli-generator v1.1.0/go.mod h1:TZBnbT7f3hJ5ImyUphJwRM+X5xF/zCQZ6o8a42gQeTs=
github.com/fatih/camelcase v1.0.0 h1:hxNvNX/xYBp0ovncs8WyWZrOrpBNub/JfaMvbURyft8=
Expand Down
13 changes: 13 additions & 0 deletions pkg/client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package client

import (
"github.com/exoscale/cli/pkg/account"
egov3 "github.com/exoscale/egoscale/v3"
)

func Get() (*egov3.ZonedClient, error) {
return egov3.DefaultClient(egov3.ClientOptWithCredentials(
account.CurrentAccount.Key,
account.CurrentAccount.APISecret(),
))
}
24 changes: 24 additions & 0 deletions pkg/instance/instance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package instance

import (
"context"
"fmt"

egov3 "github.com/exoscale/egoscale/v3"
"github.com/exoscale/egoscale/v3/oapi"
)

func FindInstanceByName(ctx context.Context, client *egov3.Client, name string) (*oapi.InstancesListElement, error) {
instanceList, err := client.Compute().Instance().List(ctx, nil)
if err != nil {
return nil, fmt.Errorf("failed to list instances: %w", err)
}

for _, instance := range instanceList {
if *instance.Name == name {
return &instance, nil
}
}

return nil, nil
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 23 additions & 29 deletions vendor/github.com/exoscale/egoscale/v3/oapi/oapi.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ github.com/exoscale/egoscale/v2
github.com/exoscale/egoscale/v2/api
github.com/exoscale/egoscale/v2/oapi
github.com/exoscale/egoscale/version
# github.com/exoscale/egoscale/v3 v3.0.0-00010101000000-000000000000 => github.com/exoscale/egoscale/v3 v3.0.0-20230830145018-2a52db7bef92
# github.com/exoscale/egoscale/v3 v3.0.0-00010101000000-000000000000 => github.com/exoscale/egoscale/v3 v3.0.0-20230830163612-a8c92b4e9a83
## explicit; go 1.20
github.com/exoscale/egoscale/v3
github.com/exoscale/egoscale/v3/api/compute
Expand Down Expand Up @@ -584,5 +584,5 @@ gopkg.in/yaml.v2
# gopkg.in/yaml.v3 v3.0.1
## explicit
gopkg.in/yaml.v3
# github.com/exoscale/egoscale/v3 => github.com/exoscale/egoscale/v3 v3.0.0-20230830145018-2a52db7bef92
# github.com/exoscale/egoscale/v3 => github.com/exoscale/egoscale/v3 v3.0.0-20230830163612-a8c92b4e9a83
# gopkg.ini/ini.v1 => github.com/go-ini/ini v1.42.0

0 comments on commit 3d9fc8d

Please sign in to comment.