diff --git a/cmd/instance_reset_password.go b/cmd/instance_reset_password.go index 3ef9bb99..f1b941b0 100644 --- a/cmd/instance_reset_password.go +++ b/cmd/instance_reset_password.go @@ -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" ) @@ -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 } @@ -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 } diff --git a/go.mod b/go.mod index 8dab5582..05ae06e3 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 5db47038..198a0379 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/client/client.go b/pkg/client/client.go new file mode 100644 index 00000000..34162a12 --- /dev/null +++ b/pkg/client/client.go @@ -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(), + )) +} diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go new file mode 100644 index 00000000..b1e886d8 --- /dev/null +++ b/pkg/instance/instance.go @@ -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 +} diff --git a/vendor/github.com/exoscale/egoscale/v3/api/iam/org_policy.gen.go b/vendor/github.com/exoscale/egoscale/v3/api/iam/org_policy.gen.go index 8200d3d7..d4e54286 100644 --- a/vendor/github.com/exoscale/egoscale/v3/api/iam/org_policy.gen.go +++ b/vendor/github.com/exoscale/egoscale/v3/api/iam/org_policy.gen.go @@ -16,7 +16,7 @@ func NewOrgPolicy(c *oapi.ClientWithResponses) *OrgPolicy { return &OrgPolicy{c} } -func (a *OrgPolicy) Get(ctx context.Context) ([]oapi.IamPolicy, error) { +func (a *OrgPolicy) Get(ctx context.Context) (*oapi.IamPolicy, error) { resp, err := a.oapiClient.GetIamOrganizationPolicyWithResponse(ctx) if err != nil { return nil, err @@ -27,7 +27,7 @@ func (a *OrgPolicy) Get(ctx context.Context) ([]oapi.IamPolicy, error) { return nil, err } - return *resp.JSON200, nil + return resp.JSON200, nil } func (a *OrgPolicy) Update(ctx context.Context, body oapi.UpdateIamOrganizationPolicyJSONRequestBody) (*oapi.Operation, error) { diff --git a/vendor/github.com/exoscale/egoscale/v3/oapi/oapi.gen.go b/vendor/github.com/exoscale/egoscale/v3/oapi/oapi.gen.go index c3adb430..c7cb32d2 100644 --- a/vendor/github.com/exoscale/egoscale/v3/oapi/oapi.gen.go +++ b/vendor/github.com/exoscale/egoscale/v3/oapi/oapi.gen.go @@ -1,6 +1,6 @@ // Package oapi provides primitives to interact with the openapi HTTP API. // -// Code generated by github.com/deepmap/oapi-codegen version v1.13.2 DO NOT EDIT. +// Code generated by github.com/deepmap/oapi-codegen version v1.13.4 DO NOT EDIT. package oapi import ( @@ -268,6 +268,17 @@ const ( Deny IamServicePolicyRuleAction = "deny" ) +// Defines values for InstancePoolState. +const ( + InstancePoolStateCreating InstancePoolState = "creating" + InstancePoolStateDestroying InstancePoolState = "destroying" + InstancePoolStateRunning InstancePoolState = "running" + InstancePoolStateScalingDown InstancePoolState = "scaling-down" + InstancePoolStateScalingUp InstancePoolState = "scaling-up" + InstancePoolStateSuspended InstancePoolState = "suspended" + InstancePoolStateUpdating InstancePoolState = "updating" +) + // Defines values for InstanceState. const ( InstanceStateDestroyed InstanceState = "destroyed" @@ -281,17 +292,6 @@ const ( InstanceStateStopping InstanceState = "stopping" ) -// Defines values for InstancePoolState. -const ( - InstancePoolStateCreating InstancePoolState = "creating" - InstancePoolStateDestroying InstancePoolState = "destroying" - InstancePoolStateRunning InstancePoolState = "running" - InstancePoolStateScalingDown InstancePoolState = "scaling-down" - InstancePoolStateScalingUp InstancePoolState = "scaling-up" - InstancePoolStateSuspended InstancePoolState = "suspended" - InstancePoolStateUpdating InstancePoolState = "updating" -) - // Defines values for InstanceTypeFamily. const ( InstanceTypeFamilyColossus InstanceTypeFamily = "colossus" @@ -2440,10 +2440,8 @@ type Instance struct { SshKey *SshKey `json:"ssh-key,omitempty"` // SshKeys Instance SSH Keys - SshKeys *[]SshKey `json:"ssh-keys,omitempty"` - - // State Instance state - State *InstanceState `json:"state,omitempty"` + SshKeys *[]SshKey `json:"ssh-keys,omitempty"` + State *InstanceState `json:"state,omitempty"` // Template Instance template Template *Template `json:"template,omitempty"` @@ -2452,9 +2450,6 @@ type Instance struct { UserData *string `json:"user-data,omitempty"` } -// InstanceState Instance state -type InstanceState string - // InstancePassword Instance password type InstancePassword struct { // Password Password @@ -2532,6 +2527,9 @@ type InstancePool struct { // InstancePoolState Instance Pool state type InstancePoolState string +// InstanceState defines model for instance-state. +type InstanceState string + // InstanceTarget Target Instance type InstanceTarget struct { // Id Instance ID @@ -22855,7 +22853,7 @@ func (r ListEventsResponse) StatusCode() int { type GetIamOrganizationPolicyResponse struct { Body []byte HTTPResponse *http.Response - JSON200 *[]IamPolicy + JSON200 *IamPolicy } // Status returns HTTPResponse.Status @@ -23068,10 +23066,8 @@ type ListInstancesResponse struct { SshKey *SshKey `json:"ssh-key,omitempty"` // SshKeys Instance SSH Keys - SshKeys *[]SshKey `json:"ssh-keys,omitempty"` - - // State Instance state - State *[]InstanceState `json:"state,omitempty"` + SshKeys *[]SshKey `json:"ssh-keys,omitempty"` + State *InstanceState `json:"state,omitempty"` // Template Instance template Template *Template `json:"template,omitempty"` @@ -31466,7 +31462,7 @@ func ParseGetIamOrganizationPolicyResponse(rsp *http.Response) (*GetIamOrganizat switch { case strings.Contains(rsp.Header.Get("Content-Type"), "json") && rsp.StatusCode == 200: - var dest []IamPolicy + var dest IamPolicy if err := json.Unmarshal(bodyBytes, &dest); err != nil { return nil, err } @@ -31711,10 +31707,8 @@ func ParseListInstancesResponse(rsp *http.Response) (*ListInstancesResponse, err SshKey *SshKey `json:"ssh-key,omitempty"` // SshKeys Instance SSH Keys - SshKeys *[]SshKey `json:"ssh-keys,omitempty"` - - // State Instance state - State *[]InstanceState `json:"state,omitempty"` + SshKeys *[]SshKey `json:"ssh-keys,omitempty"` + State *InstanceState `json:"state,omitempty"` // Template Instance template Template *Template `json:"template,omitempty"` diff --git a/vendor/github.com/exoscale/egoscale/v3/oapi/oapi_nested_structs.go b/vendor/github.com/exoscale/egoscale/v3/oapi/oapi_nested_structs.go index 5f1b1188..71c35b4d 100644 --- a/vendor/github.com/exoscale/egoscale/v3/oapi/oapi_nested_structs.go +++ b/vendor/github.com/exoscale/egoscale/v3/oapi/oapi_nested_structs.go @@ -20,7 +20,7 @@ type InstancesListElement struct { SecurityGroups *[]SecurityGroup SshKey *SshKey SshKeys *[]SshKey - State *[]InstanceState + State *InstanceState Template *Template } diff --git a/vendor/modules.txt b/vendor/modules.txt index e6f54338..d874ab2e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -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 @@ -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