Skip to content

Commit 46a5d86

Browse files
authored
Add Object Storage bucket commands (#97)
* Implement object-storage bucket commands * Update docs
1 parent 4561274 commit 46a5d86

16 files changed

+1512
-0
lines changed

docs/stackit_object-storage.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ stackit object-storage [flags]
2828
### SEE ALSO
2929

3030
* [stackit](./stackit.md) - Manage STACKIT resources using the command line
31+
* [stackit object-storage bucket](./stackit_object-storage_bucket.md) - Provides functionality for Object Storage buckets
3132
* [stackit object-storage disable](./stackit_object-storage_disable.md) - Disables Object Storage for a project
3233
* [stackit object-storage enable](./stackit_object-storage_enable.md) - Enables Object Storage for a project
3334

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## stackit object-storage bucket
2+
3+
Provides functionality for Object Storage buckets
4+
5+
### Synopsis
6+
7+
Provides functionality for Object Storage buckets.
8+
9+
```
10+
stackit object-storage bucket [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Help for "stackit object-storage bucket"
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```
22+
-y, --assume-yes If set, skips all confirmation prompts
23+
--async If set, runs the command asynchronously
24+
-o, --output-format string Output format, one of ["json" "pretty"]
25+
-p, --project-id string Project ID
26+
```
27+
28+
### SEE ALSO
29+
30+
* [stackit object-storage](./stackit_object-storage.md) - Provides functionality regarding Object Storage
31+
* [stackit object-storage bucket create](./stackit_object-storage_bucket_create.md) - Creates an Object Storage bucket
32+
* [stackit object-storage bucket delete](./stackit_object-storage_bucket_delete.md) - Deletes an Object Storage bucket
33+
* [stackit object-storage bucket describe](./stackit_object-storage_bucket_describe.md) - Shows details of an Object Storage bucket
34+
* [stackit object-storage bucket list](./stackit_object-storage_bucket_list.md) - Lists all Object Storage buckets
35+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## stackit object-storage bucket create
2+
3+
Creates an Object Storage bucket
4+
5+
### Synopsis
6+
7+
Creates an Object Storage bucket.
8+
9+
```
10+
stackit object-storage bucket create BUCKET_NAME [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Create an Object Storage bucket with name "my-bucket"
17+
$ stackit object-storage bucket create my-bucket
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit object-storage bucket create"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty"]
32+
-p, --project-id string Project ID
33+
```
34+
35+
### SEE ALSO
36+
37+
* [stackit object-storage bucket](./stackit_object-storage_bucket.md) - Provides functionality for Object Storage buckets
38+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## stackit object-storage bucket delete
2+
3+
Deletes an Object Storage bucket
4+
5+
### Synopsis
6+
7+
Deletes an Object Storage bucket.
8+
9+
```
10+
stackit object-storage bucket delete BUCKET_NAME [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Delete an Object Storage bucket with name "my-bucket"
17+
$ stackit object-storage bucket delete my-bucket
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit object-storage bucket delete"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty"]
32+
-p, --project-id string Project ID
33+
```
34+
35+
### SEE ALSO
36+
37+
* [stackit object-storage bucket](./stackit_object-storage_bucket.md) - Provides functionality for Object Storage buckets
38+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
## stackit object-storage bucket describe
2+
3+
Shows details of an Object Storage bucket
4+
5+
### Synopsis
6+
7+
Shows details of an Object Storage bucket.
8+
9+
```
10+
stackit object-storage bucket describe BUCKET_NAME [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Get details of an Object Storage bucket with name "my-bucket"
17+
$ stackit object-storage bucket describe my-bucket
18+
19+
Get details of an Object Storage bucket with name "my-bucket" in a table format
20+
$ stackit object-storage bucket describe my-bucket --output-format pretty
21+
```
22+
23+
### Options
24+
25+
```
26+
-h, --help Help for "stackit object-storage bucket describe"
27+
```
28+
29+
### Options inherited from parent commands
30+
31+
```
32+
-y, --assume-yes If set, skips all confirmation prompts
33+
--async If set, runs the command asynchronously
34+
-o, --output-format string Output format, one of ["json" "pretty"]
35+
-p, --project-id string Project ID
36+
```
37+
38+
### SEE ALSO
39+
40+
* [stackit object-storage bucket](./stackit_object-storage_bucket.md) - Provides functionality for Object Storage buckets
41+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## stackit object-storage bucket list
2+
3+
Lists all Object Storage buckets
4+
5+
### Synopsis
6+
7+
Lists all Object Storage buckets.
8+
9+
```
10+
stackit object-storage bucket list [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
List all Object Storage buckets
17+
$ stackit object-storage bucket list
18+
19+
List all Object Storage buckets in JSON format
20+
$ stackit object-storage bucket list --output-format json
21+
22+
List up to 10 Object Storage buckets
23+
$ stackit object-storage bucket list --limit 10
24+
```
25+
26+
### Options
27+
28+
```
29+
-h, --help Help for "stackit object-storage bucket list"
30+
--limit int Maximum number of entries to list
31+
```
32+
33+
### Options inherited from parent commands
34+
35+
```
36+
-y, --assume-yes If set, skips all confirmation prompts
37+
--async If set, runs the command asynchronously
38+
-o, --output-format string Output format, one of ["json" "pretty"]
39+
-p, --project-id string Project ID
40+
```
41+
42+
### SEE ALSO
43+
44+
* [stackit object-storage bucket](./stackit_object-storage_bucket.md) - Provides functionality for Object Storage buckets
45+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package bucket
2+
3+
import (
4+
"github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/bucket/create"
5+
"github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/bucket/delete"
6+
"github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/bucket/describe"
7+
"github.com/stackitcloud/stackit-cli/internal/cmd/object-storage/bucket/list"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
10+
11+
"github.com/spf13/cobra"
12+
)
13+
14+
func NewCmd() *cobra.Command {
15+
cmd := &cobra.Command{
16+
Use: "bucket",
17+
Short: "Provides functionality for Object Storage buckets",
18+
Long: "Provides functionality for Object Storage buckets.",
19+
Args: args.NoArgs,
20+
Run: utils.CmdHelp,
21+
}
22+
addSubcommands(cmd)
23+
return cmd
24+
}
25+
26+
func addSubcommands(cmd *cobra.Command) {
27+
cmd.AddCommand(delete.NewCmd())
28+
cmd.AddCommand(describe.NewCmd())
29+
cmd.AddCommand(create.NewCmd())
30+
cmd.AddCommand(list.NewCmd())
31+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package create
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
8+
"github.com/stackitcloud/stackit-cli/internal/pkg/confirm"
9+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
14+
15+
"github.com/spf13/cobra"
16+
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
17+
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage/wait"
18+
)
19+
20+
const (
21+
bucketNameArg = "BUCKET_NAME"
22+
)
23+
24+
type inputModel struct {
25+
*globalflags.GlobalFlagModel
26+
BucketName string
27+
}
28+
29+
func NewCmd() *cobra.Command {
30+
cmd := &cobra.Command{
31+
Use: fmt.Sprintf("create %s", bucketNameArg),
32+
Short: "Creates an Object Storage bucket",
33+
Long: "Creates an Object Storage bucket.",
34+
Args: args.SingleArg(bucketNameArg, nil),
35+
Example: examples.Build(
36+
examples.NewExample(
37+
`Create an Object Storage bucket with name "my-bucket"`,
38+
"$ stackit object-storage bucket create my-bucket"),
39+
),
40+
RunE: func(cmd *cobra.Command, args []string) error {
41+
ctx := context.Background()
42+
model, err := parseInput(cmd, args)
43+
if err != nil {
44+
return err
45+
}
46+
47+
// Configure API client
48+
apiClient, err := client.ConfigureClient(cmd)
49+
if err != nil {
50+
return err
51+
}
52+
53+
if !model.AssumeYes {
54+
prompt := fmt.Sprintf("Are you sure you want to create bucket %s? (This cannot be undone)", model.BucketName)
55+
err = confirm.PromptForConfirmation(cmd, prompt)
56+
if err != nil {
57+
return err
58+
}
59+
}
60+
61+
// Call API
62+
req := buildRequest(ctx, model, apiClient)
63+
_, err = req.Execute()
64+
if err != nil {
65+
return fmt.Errorf("create Object Storage bucket: %w", err)
66+
}
67+
68+
// Wait for async operation, if async mode not enabled
69+
if !model.Async {
70+
s := spinner.New(cmd)
71+
s.Start("Creating bucket")
72+
_, err = wait.CreateBucketWaitHandler(ctx, apiClient, model.ProjectId, model.BucketName).WaitWithContext(ctx)
73+
if err != nil {
74+
return fmt.Errorf("wait for Object Storage bucket creation: %w", err)
75+
}
76+
s.Stop()
77+
}
78+
79+
operationState := "Created"
80+
if model.Async {
81+
operationState = "Triggered creation of"
82+
}
83+
cmd.Printf("%s bucket %s\n", operationState, model.BucketName)
84+
return nil
85+
},
86+
}
87+
return cmd
88+
}
89+
90+
func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
91+
bucketName := inputArgs[0]
92+
93+
globalFlags := globalflags.Parse(cmd)
94+
if globalFlags.ProjectId == "" {
95+
return nil, &errors.ProjectIdError{}
96+
}
97+
98+
return &inputModel{
99+
GlobalFlagModel: globalFlags,
100+
BucketName: bucketName,
101+
}, nil
102+
}
103+
104+
func buildRequest(ctx context.Context, model *inputModel, apiClient *objectstorage.APIClient) objectstorage.ApiCreateBucketRequest {
105+
req := apiClient.CreateBucket(ctx, model.ProjectId, model.BucketName)
106+
return req
107+
}

0 commit comments

Comments
 (0)