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
20 changes: 10 additions & 10 deletions internal/cmd/object-storage/bucket/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)

var projectIdFlag = globalflags.ProjectIdFlag
var regionFlag = globalflags.RegionFlag

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
var testClient = &objectstorage.APIClient{}
var testProjectId = uuid.NewString()
var testRegion = "eu01"
var testBucketName = "my-bucket"

const (
testRegion = "eu01"
testBucketName = "my-bucket"
)

func fixtureArgValues(mods ...func(argValues []string)) []string {
argValues := []string{
Expand All @@ -38,8 +38,8 @@ func fixtureArgValues(mods ...func(argValues []string)) []string {

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
projectIdFlag: testProjectId,
regionFlag: testRegion,
globalflags.ProjectIdFlag: testProjectId,
globalflags.RegionFlag: testRegion,
}
for _, mod := range mods {
mod(flagValues)
Expand Down Expand Up @@ -107,23 +107,23 @@ func TestParseInput(t *testing.T) {
description: "project id missing",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
delete(flagValues, projectIdFlag)
delete(flagValues, globalflags.ProjectIdFlag)
}),
isValid: false,
},
{
description: "project id invalid 1",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = ""
flagValues[globalflags.ProjectIdFlag] = ""
}),
isValid: false,
},
{
description: "project id invalid 2",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = "invalid-uuid"
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
}),
isValid: false,
},
Expand Down
20 changes: 10 additions & 10 deletions internal/cmd/object-storage/bucket/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)

var projectIdFlag = globalflags.ProjectIdFlag
var regionFlag = globalflags.RegionFlag

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
var testClient = &objectstorage.APIClient{}
var testProjectId = uuid.NewString()
var testRegion = "eu01"
var testBucketName = "my-bucket"

const (
testRegion = "eu01"
testBucketName = "my-bucket"
)

func fixtureArgValues(mods ...func(argValues []string)) []string {
argValues := []string{
Expand All @@ -36,8 +36,8 @@ func fixtureArgValues(mods ...func(argValues []string)) []string {

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
projectIdFlag: testProjectId,
regionFlag: testRegion,
globalflags.ProjectIdFlag: testProjectId,
globalflags.RegionFlag: testRegion,
}
for _, mod := range mods {
mod(flagValues)
Expand Down Expand Up @@ -105,23 +105,23 @@ func TestParseInput(t *testing.T) {
description: "project id missing",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
delete(flagValues, projectIdFlag)
delete(flagValues, globalflags.ProjectIdFlag)
}),
isValid: false,
},
{
description: "project id invalid 1",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = ""
flagValues[globalflags.ProjectIdFlag] = ""
}),
isValid: false,
},
{
description: "project id invalid 2",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = "invalid-uuid"
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
}),
isValid: false,
},
Expand Down
20 changes: 10 additions & 10 deletions internal/cmd/object-storage/bucket/describe/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)

var projectIdFlag = globalflags.ProjectIdFlag
var regionFlag = globalflags.RegionFlag

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
var testClient = &objectstorage.APIClient{}
var testProjectId = uuid.NewString()
var testRegion = "eu01"
var testBucketName = "my-bucket"

const (
testRegion = "eu01"
testBucketName = "my-bucket"
)

func fixtureArgValues(mods ...func(argValues []string)) []string {
argValues := []string{
Expand All @@ -38,8 +38,8 @@ func fixtureArgValues(mods ...func(argValues []string)) []string {

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
projectIdFlag: testProjectId,
regionFlag: testRegion,
globalflags.ProjectIdFlag: testProjectId,
globalflags.RegionFlag: testRegion,
}
for _, mod := range mods {
mod(flagValues)
Expand Down Expand Up @@ -107,23 +107,23 @@ func TestParseInput(t *testing.T) {
description: "project id missing",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
delete(flagValues, projectIdFlag)
delete(flagValues, globalflags.ProjectIdFlag)
}),
isValid: false,
},
{
description: "project id invalid 1",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = ""
flagValues[globalflags.ProjectIdFlag] = ""
}),
isValid: false,
},
{
description: "project id invalid 2",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = "invalid-uuid"
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
}),
isValid: false,
},
Expand Down
24 changes: 13 additions & 11 deletions internal/cmd/object-storage/bucket/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,20 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
if err != nil {
return fmt.Errorf("get Object Storage buckets: %w", err)
}
if resp.Buckets == nil || len(*resp.Buckets) == 0 {
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
}
params.Printer.Info("No buckets found for project %s\n", projectLabel)
return nil
buckets := resp.GetBuckets()

projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
}
buckets := *resp.Buckets

// Truncate output
if model.Limit != nil && len(buckets) > int(*model.Limit) {
buckets = buckets[:*model.Limit]
}

return outputResult(params.Printer, model.OutputFormat, buckets)
return outputResult(params.Printer, model.OutputFormat, projectLabel, buckets)
},
}

Expand Down Expand Up @@ -120,12 +117,17 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstora
return req
}

func outputResult(p *print.Printer, outputFormat string, buckets []objectstorage.Bucket) error {
func outputResult(p *print.Printer, outputFormat, projectLabel string, buckets []objectstorage.Bucket) error {
if buckets == nil {
return fmt.Errorf("buckets is empty")
}

return p.OutputResult(outputFormat, buckets, func() error {
if len(buckets) == 0 {
p.Outputf("No buckets found for project %s\n", projectLabel)
return nil
}

table := tables.NewTable()
table.SetHeader("NAME", "REGION", "URL (PATH STYLE)", "URL (VIRTUAL HOSTED STYLE)")
for i := range buckets {
Expand Down
18 changes: 8 additions & 10 deletions internal/cmd/object-storage/bucket/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)

var projectIdFlag = globalflags.ProjectIdFlag
var regionFlag = globalflags.RegionFlag

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
Expand All @@ -28,9 +25,9 @@ var testRegion = "eu01"

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
projectIdFlag: testProjectId,
limitFlag: "10",
regionFlag: testRegion,
globalflags.ProjectIdFlag: testProjectId,
limitFlag: "10",
globalflags.RegionFlag: testRegion,
}
for _, mod := range mods {
mod(flagValues)
Expand Down Expand Up @@ -83,21 +80,21 @@ func TestParseInput(t *testing.T) {
{
description: "project id missing",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
delete(flagValues, projectIdFlag)
delete(flagValues, globalflags.ProjectIdFlag)
}),
isValid: false,
},
{
description: "project id invalid 1",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = ""
flagValues[globalflags.ProjectIdFlag] = ""
}),
isValid: false,
},
{
description: "project id invalid 2",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = "invalid-uuid"
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
}),
isValid: false,
},
Expand Down Expand Up @@ -155,6 +152,7 @@ func TestBuildRequest(t *testing.T) {
func TestOutputResult(t *testing.T) {
type args struct {
outputFormat string
projectLabel string
buckets []objectstorage.Bucket
}
tests := []struct {
Expand All @@ -179,7 +177,7 @@ func TestOutputResult(t *testing.T) {
p.Cmd = NewCmd(&params.CmdParams{Printer: p})
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := outputResult(p, tt.args.outputFormat, tt.args.buckets); (err != nil) != tt.wantErr {
if err := outputResult(p, tt.args.outputFormat, tt.args.projectLabel, tt.args.buckets); (err != nil) != tt.wantErr {
t.Errorf("outputResult() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ import (
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)

var projectIdFlag = globalflags.ProjectIdFlag
var regionFlag = globalflags.RegionFlag

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
var testClient = &objectstorage.APIClient{}
var testProjectId = uuid.NewString()
var testCredentialsGroupName = "test-name"
var testRegion = "eu01"

const (
testCredentialsGroupName = "test-name"
testRegion = "eu01"
)

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
projectIdFlag: testProjectId,
credentialsGroupNameFlag: testCredentialsGroupName,
regionFlag: testRegion,
globalflags.ProjectIdFlag: testProjectId,
credentialsGroupNameFlag: testCredentialsGroupName,
globalflags.RegionFlag: testRegion,
}
for _, mod := range mods {
mod(flagValues)
Expand Down Expand Up @@ -95,21 +95,21 @@ func TestParseInput(t *testing.T) {
{
description: "project id missing",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
delete(flagValues, projectIdFlag)
delete(flagValues, globalflags.ProjectIdFlag)
}),
isValid: false,
},
{
description: "project id invalid 1",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = ""
flagValues[globalflags.ProjectIdFlag] = ""
}),
isValid: false,
},
{
description: "project id invalid 2",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = "invalid-uuid"
flagValues[globalflags.ProjectIdFlag] = "invalid-uuid"
}),
isValid: false,
},
Expand Down
Loading
Loading