Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement new Object Storage enable and disable commands #96

Merged
merged 2 commits into from
Feb 26, 2024
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
1 change: 1 addition & 0 deletions docs/stackit.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ stackit [flags]
* [stackit logme](./stackit_logme.md) - Provides functionality for LogMe
* [stackit mariadb](./stackit_mariadb.md) - Provides functionality for MariaDB
* [stackit mongodbflex](./stackit_mongodbflex.md) - Provides functionality for MongoDB Flex
* [stackit object-storage](./stackit_object-storage.md) - Provides functionality regarding Object Storage
* [stackit opensearch](./stackit_opensearch.md) - Provides functionality for OpenSearch
* [stackit organization](./stackit_organization.md) - Provides functionality regarding organizations
* [stackit postgresflex](./stackit_postgresflex.md) - Provides functionality for PostgreSQL Flex
Expand Down
1 change: 1 addition & 0 deletions docs/stackit_config_set.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ stackit config set [flags]
--logme-custom-endpoint string LogMe API base URL, used in calls to this API
--mariadb-custom-endpoint string MariaDB API base URL, used in calls to this API
--mongodbflex-custom-endpoint string MongoDB Flex API base URL, used in calls to this API
--object-storage-custom-endpoint string Object Storage API base URL, used in calls to this API
--opensearch-custom-endpoint string OpenSearch API base URL, used in calls to this API
--postgresflex-custom-endpoint string PostgreSQL Flex API base URL, used in calls to this API
--rabbitmq-custom-endpoint string RabbitMQ API base URL, used in calls to this API
Expand Down
1 change: 1 addition & 0 deletions docs/stackit_config_unset.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ stackit config unset [flags]
--logme-custom-endpoint LogMe API base URL. If unset, uses the default base URL
--mariadb-custom-endpoint MariaDB API base URL. If unset, uses the default base URL
--mongodbflex-custom-endpoint MongoDB Flex API base URL. If unset, uses the default base URL
--object-storage-custom-endpoint Object Storage API base URL. If unset, uses the default base URL
--opensearch-custom-endpoint OpenSearch API base URL. If unset, uses the default base URL
--output-format Output format
--postgresflex-custom-endpoint PostgreSQL Flex API base URL. If unset, uses the default base URL
Expand Down
33 changes: 33 additions & 0 deletions docs/stackit_object-storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## stackit object-storage

Provides functionality regarding Object Storage

### Synopsis

Provides functionality regarding Object Storage.

```
stackit object-storage [flags]
```

### Options

```
-h, --help Help for "stackit object-storage"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty"]
-p, --project-id string Project ID
```

### SEE ALSO

* [stackit](./stackit.md) - Manage STACKIT resources using the command line
* [stackit object-storage disable](./stackit_object-storage_disable.md) - Disables Object Storage for a project
* [stackit object-storage enable](./stackit_object-storage_enable.md) - Enables Object Storage for a project

38 changes: 38 additions & 0 deletions docs/stackit_object-storage_disable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## stackit object-storage disable

Disables Object Storage for a project

### Synopsis

Disables Object Storage for a project. It will delete all associated buckets.

```
stackit object-storage disable [flags]
```

### Examples

```
Disable Object Storage functionality for your project, deleting all associated buckets
$ stackit object-storage disable
```

### Options

```
-h, --help Help for "stackit object-storage disable"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty"]
-p, --project-id string Project ID
```

### SEE ALSO

* [stackit object-storage](./stackit_object-storage.md) - Provides functionality regarding Object Storage

38 changes: 38 additions & 0 deletions docs/stackit_object-storage_enable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
## stackit object-storage enable

Enables Object Storage for a project

### Synopsis

Enables Object Storage for a project.

```
stackit object-storage enable [flags]
```

### Examples

```
Enable Object Storage functionality for your project
$ stackit object-storage enable
```

### Options

```
-h, --help Help for "stackit object-storage enable"
```

### Options inherited from parent commands

```
-y, --assume-yes If set, skips all confirmation prompts
--async If set, runs the command asynchronously
-o, --output-format string Output format, one of ["json" "pretty"]
-p, --project-id string Project ID
```

### SEE ALSO

* [stackit object-storage](./stackit_object-storage.md) - Provides functionality regarding Object Storage

92 changes: 92 additions & 0 deletions internal/cmd/object-storage/disable/disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package disable

import (
"context"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/confirm"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client"

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

type InputModel struct {
*globalflags.GlobalFlagModel
}

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "disable",
Short: "Disables Object Storage for a project",
Long: "Disables Object Storage for a project. It will delete all associated buckets.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Disable Object Storage functionality for your project, deleting all associated buckets`,
"$ stackit object-storage disable"),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(cmd)
if err != nil {
return err
}

// Configure API client
apiClient, err := client.ConfigureClient(cmd)
if err != nil {
return err
}

projectLabel, err := projectname.GetProjectName(ctx, cmd)
if err != nil {
projectLabel = model.ProjectId
}

if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to disable Object Storage for project %s? (This will delete all associated clusters)", projectLabel)
err = confirm.PromptForConfirmation(cmd, prompt)
if err != nil {
return err
}
}

// Call API
req := buildRequest(ctx, model, apiClient)
_, err = req.Execute()
if err != nil {
return fmt.Errorf("disable Object Storage: %w", err)
}

operationState := "Disabled"
if model.Async {
operationState = "Triggered disablement of"
}
cmd.Printf("%s Object Storage for project %s\n", operationState, projectLabel)
return nil
},
}
return cmd
}

func parseInput(cmd *cobra.Command) (*InputModel, error) {
globalFlags := globalflags.Parse(cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}

return &InputModel{
GlobalFlagModel: globalFlags,
}, nil
}

func buildRequest(ctx context.Context, model *InputModel, apiClient *objectstorage.APIClient) objectstorage.ApiDisableServiceRequest {
req := apiClient.DisableService(ctx, model.ProjectId)
return req
}
166 changes: 166 additions & 0 deletions internal/cmd/object-storage/disable/disable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package disable

import (
"context"
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"

"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)

var projectIdFlag = globalflags.ProjectIdFlag

type testCtxKey struct{}

var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
var testClient = &objectstorage.APIClient{}
var testProjectId = uuid.NewString()

func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
flagValues := map[string]string{
projectIdFlag: testProjectId,
}
for _, mod := range mods {
mod(flagValues)
}
return flagValues
}

func fixtureInputModel(mods ...func(model *InputModel)) *InputModel {
model := &InputModel{
GlobalFlagModel: &globalflags.GlobalFlagModel{
ProjectId: testProjectId,
},
}
for _, mod := range mods {
mod(model)
}
return model
}

func fixtureRequest(mods ...func(request *objectstorage.ApiDisableServiceRequest)) objectstorage.ApiDisableServiceRequest {
request := testClient.DisableService(testCtx, testProjectId)
for _, mod := range mods {
mod(&request)
}
return request
}

func TestParseInput(t *testing.T) {
tests := []struct {
description string
flagValues map[string]string
isValid bool
expectedModel *InputModel
}{
{
description: "base",
flagValues: fixtureFlagValues(),
isValid: true,
expectedModel: fixtureInputModel(),
},
{
description: "no values",
flagValues: map[string]string{},
isValid: false,
},
{
description: "project id missing",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
delete(flagValues, projectIdFlag)
}),
isValid: false,
},
{
description: "project id invalid 1",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = ""
}),
isValid: false,
},
{
description: "project id invalid 2",
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
flagValues[projectIdFlag] = "invalid-uuid"
}),
isValid: false,
},
}

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
cmd := &cobra.Command{}
err := globalflags.Configure(cmd.Flags())
if err != nil {
t.Fatalf("configure global flags: %v", err)
}

for flag, value := range tt.flagValues {
err := cmd.Flags().Set(flag, value)
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("setting flag --%s=%s: %v", flag, value, err)
}
}

err = cmd.ValidateRequiredFlags()
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("error validating flags: %v", err)
}

model, err := parseInput(cmd)
if err != nil {
if !tt.isValid {
return
}
t.Fatalf("error parsing flags: %v", err)
}

if !tt.isValid {
t.Fatalf("did not fail on invalid input")
}
diff := cmp.Diff(model, tt.expectedModel)
if diff != "" {
t.Fatalf("Data does not match: %s", diff)
}
})
}
}

func TestBuildRequest(t *testing.T) {
tests := []struct {
description string
model *InputModel
expectedRequest objectstorage.ApiDisableServiceRequest
}{
{
description: "base",
model: fixtureInputModel(),
expectedRequest: fixtureRequest(),
},
}

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
request := buildRequest(testCtx, tt.model, testClient)

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