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
6 changes: 5 additions & 1 deletion docs/stackit_object-storage_bucket_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@ stackit object-storage bucket create BUCKET_NAME [flags]
```
Create an Object Storage bucket with name "my-bucket"
$ stackit object-storage bucket create my-bucket
Create an Object Storage bucket with enabled object-lock
$ stackit object-storage bucket create my-bucket --object-lock-enabled
```

### Options

```
-h, --help Help for "stackit object-storage bucket create"
-h, --help Help for "stackit object-storage bucket create"
--object-lock-enabled is the object-lock enabled for the bucket
```

### Options inherited from parent commands
Expand Down
22 changes: 17 additions & 5 deletions internal/cmd/object-storage/bucket/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
Expand All @@ -21,12 +22,14 @@ import (
)

const (
bucketNameArg = "BUCKET_NAME"
bucketNameArg = "BUCKET_NAME"
objectLockEnabledFlag = "object-lock-enabled"
)

type inputModel struct {
*globalflags.GlobalFlagModel
BucketName string
BucketName string
ObjectLockEnabled bool
}

func NewCmd(params *types.CmdParams) *cobra.Command {
Expand All @@ -39,6 +42,9 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
examples.NewExample(
`Create an Object Storage bucket with name "my-bucket"`,
"$ stackit object-storage bucket create my-bucket"),
examples.NewExample(
`Create an Object Storage bucket with enabled object-lock`,
`$ stackit object-storage bucket create my-bucket --object-lock-enabled`),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
Expand Down Expand Up @@ -91,9 +97,14 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
return outputResult(params.Printer, model.OutputFormat, model.Async, model.BucketName, resp)
},
}
configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
cmd.Flags().Bool(objectLockEnabledFlag, false, "is the object-lock enabled for the bucket")
}

func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
bucketName := inputArgs[0]

Expand All @@ -103,16 +114,17 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu
}

model := inputModel{
GlobalFlagModel: globalFlags,
BucketName: bucketName,
GlobalFlagModel: globalFlags,
BucketName: bucketName,
ObjectLockEnabled: flags.FlagToBoolValue(p, cmd, objectLockEnabledFlag),
}

p.DebugInputModel(model)
return &model, nil
}

func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstorage.APIClient) objectstorage.ApiCreateBucketRequest {
req := apiClient.DefaultAPI.CreateBucket(ctx, model.ProjectId, model.Region, model.BucketName)
req := apiClient.DefaultAPI.CreateBucket(ctx, model.ProjectId, model.Region, model.BucketName).ObjectLockEnabled(model.ObjectLockEnabled)
return req
}

Expand Down
29 changes: 25 additions & 4 deletions internal/cmd/object-storage/bucket/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,19 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
Verbosity: globalflags.VerbosityDefault,
Region: testRegion,
},
BucketName: testBucketName,
BucketName: testBucketName,
ObjectLockEnabled: false,
}
for _, mod := range mods {
mod(model)
}
return model
}

func fixtureRequest(mods ...func(request *objectstorage.ApiCreateBucketRequest)) objectstorage.ApiCreateBucketRequest {
request := testClient.DefaultAPI.CreateBucket(testCtx, testProjectId, testRegion, testBucketName)
func fixtureRequest(mods ...func(request objectstorage.ApiCreateBucketRequest) objectstorage.ApiCreateBucketRequest) objectstorage.ApiCreateBucketRequest {
request := testClient.DefaultAPI.CreateBucket(testCtx, testProjectId, testRegion, testBucketName).ObjectLockEnabled(false)
for _, mod := range mods {
mod(&request)
request = mod(request)
}
return request
}
Expand Down Expand Up @@ -134,6 +135,17 @@ func TestParseInput(t *testing.T) {
flagValues: fixtureFlagValues(),
isValid: false,
},
{
description: "enable object-lock",
argValues: fixtureArgValues(),
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[objectLockEnabledFlag] = "true"
}),
expectedModel: fixtureInputModel(func(model *inputModel) {
model.ObjectLockEnabled = true
}),
isValid: true,
},
}

for _, tt := range tests {
Expand All @@ -154,6 +166,15 @@ func TestBuildRequest(t *testing.T) {
model: fixtureInputModel(),
expectedRequest: fixtureRequest(),
},
{
description: "object-lock enabled",
model: fixtureInputModel(func(model *inputModel) {
model.ObjectLockEnabled = true
}),
expectedRequest: fixtureRequest(func(request objectstorage.ApiCreateBucketRequest) objectstorage.ApiCreateBucketRequest {
return request.ObjectLockEnabled(true)
}),
},
}

for _, tt := range tests {
Expand Down
2 changes: 2 additions & 0 deletions internal/cmd/object-storage/bucket/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func outputResult(p *print.Printer, outputFormat string, resp *objectstorage.Get
table.AddSeparator()
table.AddRow("URL (Virtual Hosted Style)", resp.Bucket.UrlVirtualHostedStyle)
table.AddSeparator()
table.AddRow("Object Lock Enabled", resp.Bucket.ObjectLockEnabled)
table.AddSeparator()
err := table.Display(p)
if err != nil {
return fmt.Errorf("render table: %w", err)
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/object-storage/bucket/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,15 @@ func outputResult(p *print.Printer, outputFormat, projectLabel string, buckets [
}

table := tables.NewTable()
table.SetHeader("NAME", "REGION", "URL (PATH STYLE)", "URL (VIRTUAL HOSTED STYLE)")
table.SetHeader("NAME", "REGION", "URL (PATH STYLE)", "URL (VIRTUAL HOSTED STYLE)", "OBJECT LOCK ENABLED")
for i := range buckets {
bucket := buckets[i]
table.AddRow(
bucket.Name,
bucket.Region,
bucket.UrlPathStyle,
bucket.UrlVirtualHostedStyle,
bucket.ObjectLockEnabled,
)
}
err := table.Display(p)
Expand Down
Loading