Skip to content
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
4 changes: 2 additions & 2 deletions docs/stackit_opensearch_instance_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ stackit opensearch instance create [flags]
--enable-monitoring Enable monitoring
--graphite string Graphite host
-h, --help Help for "stackit opensearch instance create"
--metrics-frequency int Metrics frequency
--metrics-frequency int32 Metrics frequency
--metrics-prefix string Metrics prefix
--monitoring-instance-id string Monitoring instance ID
-n, --name string Instance name
--plan-id string Plan ID
--plan-name string Plan name
--plugin strings Plugin
--plugin strings Plugins (multiple of: [repository-s3, repository-azure, analysis-phonetic]) (default [])
--syslog strings Syslog
--version string Instance OpenSearch version
```
Expand Down
4 changes: 2 additions & 2 deletions docs/stackit_opensearch_instance_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ stackit opensearch instance update INSTANCE_ID [flags]
--enable-monitoring Enable monitoring
--graphite string Graphite host
-h, --help Help for "stackit opensearch instance update"
--metrics-frequency int Metrics frequency
--metrics-frequency int32 Metrics frequency
--metrics-prefix string Metrics prefix
--monitoring-instance-id string Monitoring instance ID
--plan-id string Plan ID
--plan-name string Plan name
--plugin strings Plugin
--plugin strings Plugins (multiple of: [repository-s3, repository-azure, analysis-phonetic]) (default [])
--syslog strings Syslog
--version string Instance OpenSearch version
```
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ require (
github.com/stackitcloud/stackit-sdk-go/services/intake v0.7.1
github.com/stackitcloud/stackit-sdk-go/services/logs v0.5.2
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v1.12.0
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.24.6
github.com/stackitcloud/stackit-sdk-go/services/opensearch v1.1.0
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.3.5
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.24.0
github.com/stackitcloud/stackit-sdk-go/services/runcommand v1.4.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -624,8 +624,8 @@ github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.9.0 h1:T+ll3lS0
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v1.9.0/go.mod h1:QsPtoqAYvumyPU6ToX/5j1PbudN+VSTuvh6mp154ecM=
github.com/stackitcloud/stackit-sdk-go/services/observability v0.24.0 h1:KhuWPXr1hl8BFLGLSi6wjxh5o6OQXH9amfquMhYQROU=
github.com/stackitcloud/stackit-sdk-go/services/observability v0.24.0/go.mod h1:0fEZQHm729mBdvg4sNrAhM6KmHROHJSeS2FwCMRk46k=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.24.6 h1:oTVx1+O177Ojn8OvXIOUbRSwtx7L59jhxDPrZEQFOfQ=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.24.6/go.mod h1:6ZBeCCY6qG8w1oK7osf61Egyv3mp7Ahv6GDGxiarDGo=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v1.1.0 h1:hooc/E9Qabn8xno1NUd3uJQfUbW5KoY6mgURlnd776c=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v1.1.0/go.mod h1:L+NlfC1hilLOqlLLukCj/UDnxlnNrc/oMikcw3Ansyw=
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.3.5 h1:H67e3KnHQx954yI8fuQmxXwRf/myqAdLg2KvxImp00g=
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v1.3.5/go.mod h1:xmAWk9eom8wznvLuLfm0F4xyeiBX8LaggXsKFmos+dw=
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v1.1.0 h1:HRJwodJX4aOn/487zaqJIKw13yIj4T6dn7/kEHLxeTg=
Expand Down
40 changes: 21 additions & 19 deletions internal/cmd/opensearch/credentials/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/types"

"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/opensearch"
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v2api"

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
Expand All @@ -17,7 +17,6 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/client"
opensearchUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/opensearch/utils"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
)

const (
Expand Down Expand Up @@ -58,7 +57,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
return err
}

instanceLabel, err := opensearchUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId)
instanceLabel, err := opensearchUtils.GetInstanceName(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, model.InstanceId)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
instanceLabel = model.InstanceId
Expand All @@ -77,7 +76,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
return fmt.Errorf("create OpenSearch credentials: %w", err)
}

return outputResult(params.Printer, model.OutputFormat, model.ShowPassword, instanceLabel, resp)
return outputResult(params.Printer, *model, instanceLabel, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -109,35 +108,38 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
}

func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch.APIClient) opensearch.ApiCreateCredentialsRequest {
req := apiClient.CreateCredentials(ctx, model.ProjectId, model.InstanceId)
req := apiClient.DefaultAPI.CreateCredentials(ctx, model.ProjectId, model.Region, model.InstanceId)
return req
}

func outputResult(p *print.Printer, outputFormat string, showPassword bool, instanceLabel string, resp *opensearch.CredentialsResponse) error {
if resp == nil || resp.Raw == nil || resp.Raw.Credentials == nil || resp.Uri == nil {
func outputResult(p *print.Printer, model inputModel, instanceLabel string, resp *opensearch.CredentialsResponse) error {
if model.GlobalFlagModel == nil {
return fmt.Errorf("no global flags defined")
}
if resp == nil || resp.Raw == nil {
return fmt.Errorf("response or response content is nil")
}

if !showPassword {
resp.Raw.Credentials.Password = utils.Ptr("hidden")
if !model.ShowPassword {
resp.Raw.Credentials.Password = "hidden"
}

return p.OutputResult(outputFormat, resp, func() error {
p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, utils.PtrString(resp.Id))
return p.OutputResult(model.OutputFormat, resp, func() error {
p.Outputf("Created credentials for instance %q. Credentials ID: %s\n\n", instanceLabel, resp.Id)
// The username field cannot be set by the user so we only display it if it's not returned empty
if resp.HasRaw() && resp.Raw.Credentials != nil {
if username := resp.Raw.Credentials.Username; username != nil && *username != "" {
p.Outputf("Username: %s\n", *username)
if resp.HasRaw() {
if username := resp.Raw.Credentials.Username; username != "" {
p.Outputf("Username: %s\n", username)
}
if !showPassword {
if !model.ShowPassword {
p.Outputf("Password: <hidden>\n")
} else {
p.Outputf("Password: %s\n", utils.PtrString(resp.Raw.Credentials.Password))
p.Outputf("Password: %s\n", resp.Raw.Credentials.Password)
}
p.Outputf("Host: %s\n", utils.PtrString(resp.Raw.Credentials.Host))
p.Outputf("Port: %s\n", utils.PtrString(resp.Raw.Credentials.Port))
p.Outputf("Host: %s\n", resp.Raw.Credentials.Host)
p.Outputf("Port: %d\n", resp.Raw.Credentials.Port)
}
p.Outputf("URI: %s\n", *resp.Uri)
p.Outputf("URI: %s\n", resp.Uri)
return nil
})
}
64 changes: 25 additions & 39 deletions internal/cmd/opensearch/credentials/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-sdk-go/services/opensearch"
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v2api"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
"github.com/stackitcloud/stackit-cli/internal/pkg/testutils"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
)

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
var testClient = &opensearch.APIClient{}
var testProjectId = uuid.NewString()
var testInstanceId = uuid.NewString()
var (
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
testClient = &opensearch.APIClient{DefaultAPI: &opensearch.DefaultAPIService{}}
testProjectId = uuid.NewString()
testRegion = "eu01"
testInstanceId = uuid.NewString()
)

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
globalflags.ProjectIdFlag: testProjectId,
globalflags.RegionFlag: testRegion,
instanceIdFlag: testInstanceId,
}
for _, mod := range mods {
Expand All @@ -37,6 +40,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
model := &inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{
ProjectId: testProjectId,
Region: testRegion,
Verbosity: globalflags.VerbosityDefault,
},
InstanceId: testInstanceId,
Expand All @@ -48,7 +52,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
}

func fixtureRequest(mods ...func(request *opensearch.ApiCreateCredentialsRequest)) opensearch.ApiCreateCredentialsRequest {
request := testClient.CreateCredentials(testCtx, testProjectId, testInstanceId)
request := testClient.DefaultAPI.CreateCredentials(testCtx, testProjectId, testRegion, testInstanceId)
for _, mod := range mods {
mod(&request)
}
Expand Down Expand Up @@ -154,7 +158,7 @@ func TestBuildRequest(t *testing.T) {

diff := cmp.Diff(request, tt.expectedRequest,
cmp.AllowUnexported(tt.expectedRequest),
cmpopts.EquateComparable(testCtx),
cmpopts.EquateComparable(testCtx, opensearch.DefaultAPIService{}),
)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
Expand All @@ -165,8 +169,7 @@ func TestBuildRequest(t *testing.T) {

func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
showPassword bool
model inputModel
instanceLabel string
resp *opensearch.CredentialsResponse
}
Expand All @@ -181,40 +184,20 @@ func TestOutputResult(t *testing.T) {
wantErr: true,
},
{
name: "set no raw in response",
args: args{
resp: &opensearch.CredentialsResponse{
Uri: utils.Ptr("https://opensearch.example.com"),
},
},
wantErr: true,
},
{
name: "set empty raw in response",
args: args{
resp: &opensearch.CredentialsResponse{
Raw: &opensearch.RawCredentials{},
Uri: utils.Ptr("https://opensearch.example.com"),
},
},
wantErr: true,
},
{
name: "set raw but no uri in response",
name: "set uri but no raw in response",
args: args{
resp: &opensearch.CredentialsResponse{
Raw: &opensearch.RawCredentials{
Credentials: &opensearch.Credentials{},
},
Uri: "https://opensearch.example.com",
},
},
wantErr: true,
},
{
name: "set uri but no raw in response",
name: "empty response should not cause panic when password should be hidden",
args: args{
resp: &opensearch.CredentialsResponse{
Uri: utils.Ptr("https://opensearch.example.com"),
model: inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{},
ShowPassword: false,
},
},
wantErr: true,
Expand All @@ -224,9 +207,12 @@ func TestOutputResult(t *testing.T) {
args: args{
resp: &opensearch.CredentialsResponse{
Raw: &opensearch.RawCredentials{
Credentials: &opensearch.Credentials{},
Credentials: opensearch.Credentials{},
},
Uri: utils.Ptr("https://opensearch.example.com"),
Uri: "https://opensearch.example.com",
},
model: inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{},
},
},
wantErr: false,
Expand All @@ -235,7 +221,7 @@ func TestOutputResult(t *testing.T) {
params := testparams.NewTestParams()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(params.Printer, tt.args.outputFormat, tt.args.showPassword, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr {
if err := outputResult(params.Printer, tt.args.model, tt.args.instanceLabel, tt.args.resp); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/opensearch/credentials/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/opensearch"
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v2api"
)

const (
Expand Down Expand Up @@ -56,13 +56,13 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
return err
}

instanceLabel, err := opensearchUtils.GetInstanceName(ctx, apiClient, model.ProjectId, model.InstanceId)
instanceLabel, err := opensearchUtils.GetInstanceName(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, model.InstanceId)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get instance name: %v", err)
instanceLabel = model.InstanceId
}

credentialsLabel, err := opensearchUtils.GetCredentialsUsername(ctx, apiClient, model.ProjectId, model.InstanceId, model.CredentialsId)
credentialsLabel, err := opensearchUtils.GetCredentialsUsername(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, model.InstanceId, model.CredentialsId)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get credentials user name: %v", err)
credentialsLabel = model.CredentialsId
Expand Down Expand Up @@ -115,6 +115,6 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
}

func buildRequest(ctx context.Context, model *inputModel, apiClient *opensearch.APIClient) opensearch.ApiDeleteCredentialsRequest {
req := apiClient.DeleteCredentials(ctx, model.ProjectId, model.InstanceId, model.CredentialsId)
req := apiClient.DefaultAPI.DeleteCredentials(ctx, model.ProjectId, model.Region, model.InstanceId, model.CredentialsId)
return req
}
21 changes: 13 additions & 8 deletions internal/cmd/opensearch/credentials/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-sdk-go/services/opensearch"
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v2api"
)

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
var testClient = &opensearch.APIClient{}
var testProjectId = uuid.NewString()
var testInstanceId = uuid.NewString()
var testCredentialsId = uuid.NewString()
var (
testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
testClient = &opensearch.APIClient{DefaultAPI: &opensearch.DefaultAPIService{}}
testProjectId = uuid.NewString()
testRegion = "eu01"
testInstanceId = uuid.NewString()
testCredentialsId = uuid.NewString()
)

func fixtureArgValues(mods ...func(argValues []string)) []string {
argValues := []string{
Expand All @@ -34,6 +37,7 @@ func fixtureArgValues(mods ...func(argValues []string)) []string {
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
globalflags.ProjectIdFlag: testProjectId,
globalflags.RegionFlag: testRegion,
instanceIdFlag: testInstanceId,
}
for _, mod := range mods {
Expand All @@ -46,6 +50,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
model := &inputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{
ProjectId: testProjectId,
Region: testRegion,
Verbosity: globalflags.VerbosityDefault,
},
InstanceId: testInstanceId,
Expand All @@ -58,7 +63,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
}

func fixtureRequest(mods ...func(request *opensearch.ApiDeleteCredentialsRequest)) opensearch.ApiDeleteCredentialsRequest {
request := testClient.DeleteCredentials(testCtx, testProjectId, testInstanceId, testCredentialsId)
request := testClient.DefaultAPI.DeleteCredentials(testCtx, testProjectId, testRegion, testInstanceId, testCredentialsId)
for _, mod := range mods {
mod(&request)
}
Expand Down Expand Up @@ -186,7 +191,7 @@ func TestBuildRequest(t *testing.T) {

diff := cmp.Diff(request, tt.expectedRequest,
cmp.AllowUnexported(tt.expectedRequest),
cmpopts.EquateComparable(testCtx),
cmpopts.EquateComparable(testCtx, opensearch.DefaultAPIService{}),
)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
Expand Down
Loading
Loading