Skip to content

Check for service enablement before bucket creation #398

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

Merged
merged 4 commits into from
Jul 2, 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
12 changes: 12 additions & 0 deletions internal/cmd/object-storage/bucket/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/object-storage/utils"
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -60,6 +61,17 @@ func NewCmd(p *print.Printer) *cobra.Command {
}
}

// Check if the project is enabled before trying to create
enabled, err := utils.ProjectEnabled(ctx, apiClient, model.ProjectId)
if err != nil {
return fmt.Errorf("check if Object Storage is enabled: %w", err)
}
if !enabled {
return &errors.ServiceDisabledError{
Service: "object-storage",
}
}

// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
Expand Down
6 changes: 4 additions & 2 deletions internal/cmd/ske/cluster/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,15 @@ func NewCmd(p *print.Printer) *cobra.Command {
}
}

// Check if SKE is enabled for this project
// Check if the project is enabled before trying to create
enabled, err := skeUtils.ProjectEnabled(ctx, apiClient, model.ProjectId)
if err != nil {
return err
}
if !enabled {
return fmt.Errorf("SKE isn't enabled for this project, please run 'stackit ske enable'")
return &errors.ServiceDisabledError{
Service: "ske",
}
}

// Check if cluster exists
Expand Down
13 changes: 13 additions & 0 deletions internal/pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ The profile name can only contain lowercase letters, numbers, and "-" and cannot

USAGE_TIP = `For usage help, run:
$ %s --help`

SERVICE_DISABLED = `This service isn't enabled for the current project.

To enable it, run:
$ stackit %s enable`
)

type ProjectIdError struct{}
Expand Down Expand Up @@ -364,3 +369,11 @@ type InvalidProfileNameError struct {
func (e *InvalidProfileNameError) Error() string {
return fmt.Sprintf(INVALID_PROFILE_NAME, e.Profile)
}

type ServiceDisabledError struct {
Service string
}

func (e *ServiceDisabledError) Error() string {
return fmt.Sprintf(SERVICE_DISABLED, e.Service)
}
18 changes: 18 additions & 0 deletions internal/pkg/services/object-storage/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,33 @@ package utils
import (
"context"
"fmt"
"net/http"

"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)

type ObjectStorageClient interface {
GetServiceStatusExecute(ctx context.Context, projectId string) (*objectstorage.ProjectStatus, error)
ListCredentialsGroupsExecute(ctx context.Context, projectId string) (*objectstorage.ListCredentialsGroupsResponse, error)
ListAccessKeys(ctx context.Context, projectId string) objectstorage.ApiListAccessKeysRequest
}

func ProjectEnabled(ctx context.Context, apiClient ObjectStorageClient, projectId string) (bool, error) {
_, err := apiClient.GetServiceStatusExecute(ctx, projectId)
if err != nil {
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
if !ok {
return false, err
}
if oapiErr.StatusCode == http.StatusNotFound {
return false, nil
}
return false, err
}
return true, nil
}

func GetCredentialsGroupName(ctx context.Context, apiClient ObjectStorageClient, projectId, credentialsGroupId string) (string, error) {
resp, err := apiClient.ListCredentialsGroupsExecute(ctx, projectId)
if err != nil {
Expand Down
65 changes: 65 additions & 0 deletions internal/pkg/services/object-storage/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/uuid"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/services/objectstorage"
)

Expand All @@ -27,11 +28,23 @@ const (
)

type objectStorageClientMocked struct {
serviceDisabled bool
getServiceStatusFails bool
listCredentialsGroupsFails bool
listCredentialsGroupsResp *objectstorage.ListCredentialsGroupsResponse
listAccessKeysReq objectstorage.ApiListAccessKeysRequest
}

func (m *objectStorageClientMocked) GetServiceStatusExecute(_ context.Context, _ string) (*objectstorage.ProjectStatus, error) {
if m.getServiceStatusFails {
return nil, fmt.Errorf("could not get service status")
}
if m.serviceDisabled {
return nil, &oapierror.GenericOpenAPIError{StatusCode: 404}
}
return &objectstorage.ProjectStatus{}, nil
}

func (m *objectStorageClientMocked) ListCredentialsGroupsExecute(_ context.Context, _ string) (*objectstorage.ListCredentialsGroupsResponse, error) {
if m.listCredentialsGroupsFails {
return nil, fmt.Errorf("could not list credentials groups")
Expand All @@ -43,6 +56,58 @@ func (m *objectStorageClientMocked) ListAccessKeys(_ context.Context, _ string)
return m.listAccessKeysReq
}

func TestProjectEnabled(t *testing.T) {
tests := []struct {
description string
serviceDisabled bool
getProjectFails bool
isValid bool
expectedOutput bool
}{
{
description: "project enabled",
isValid: true,
expectedOutput: true,
},
{
description: "project disabled (404)",
serviceDisabled: true,
isValid: true,
expectedOutput: false,
},
{
description: "get project fails",
getProjectFails: true,
isValid: false,
},
}

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
client := &objectStorageClientMocked{
serviceDisabled: tt.serviceDisabled,
getServiceStatusFails: tt.getProjectFails,
}

output, err := ProjectEnabled(context.Background(), client, testProjectId)

if tt.isValid && err != nil {
fmt.Printf("failed on valid input: %v", err)
t.Errorf("failed on valid input")
}
if !tt.isValid && err == nil {
t.Errorf("did not fail on invalid input")
}
if !tt.isValid {
return
}
if output != tt.expectedOutput {
t.Errorf("expected output to be %t, got %t", tt.expectedOutput, output)
}
})
}
}

func TestGetCredentialsGroupName(t *testing.T) {
tests := []struct {
description string
Expand Down
11 changes: 10 additions & 1 deletion internal/pkg/services/ske/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package utils
import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"

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

"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/services/ske"
"golang.org/x/mod/semver"
)
Expand Down Expand Up @@ -37,7 +39,14 @@ type SKEClient interface {
func ProjectEnabled(ctx context.Context, apiClient SKEClient, projectId string) (bool, error) {
project, err := apiClient.GetServiceStatusExecute(ctx, projectId)
if err != nil {
return false, fmt.Errorf("get SKE status: %w", err)
oapiErr, ok := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
if !ok {
return false, err
}
if oapiErr.StatusCode == http.StatusNotFound {
return false, nil
}
return false, err
}
return *project.State == ske.PROJECTSTATE_CREATED, nil
}
Expand Down
13 changes: 13 additions & 0 deletions internal/pkg/services/ske/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
"github.com/stackitcloud/stackit-sdk-go/services/ske"
)

Expand All @@ -23,6 +24,7 @@ const (
)

type skeClientMocked struct {
serviceDisabled bool
getServiceStatusFails bool
getServiceStatusResp *ske.ProjectResponse
listClustersFails bool
Expand All @@ -35,6 +37,9 @@ func (m *skeClientMocked) GetServiceStatusExecute(_ context.Context, _ string) (
if m.getServiceStatusFails {
return nil, fmt.Errorf("could not get service status")
}
if m.serviceDisabled {
return nil, &oapierror.GenericOpenAPIError{StatusCode: 404}
}
return m.getServiceStatusResp, nil
}

Expand All @@ -55,6 +60,7 @@ func (m *skeClientMocked) ListProviderOptionsExecute(_ context.Context) (*ske.Pr
func TestProjectEnabled(t *testing.T) {
tests := []struct {
description string
serviceDisabled bool
getProjectFails bool
getProjectResp *ske.ProjectResponse
isValid bool
Expand All @@ -66,6 +72,12 @@ func TestProjectEnabled(t *testing.T) {
isValid: true,
expectedOutput: true,
},
{
description: "project disabled (404)",
serviceDisabled: true,
isValid: true,
expectedOutput: false,
},
{
description: "project disabled 1",
getProjectResp: &ske.ProjectResponse{State: ske.PROJECTSTATE_CREATING.Ptr()},
Expand All @@ -88,6 +100,7 @@ func TestProjectEnabled(t *testing.T) {
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
client := &skeClientMocked{
serviceDisabled: tt.serviceDisabled,
getServiceStatusFails: tt.getProjectFails,
getServiceStatusResp: tt.getProjectResp,
}
Expand Down