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
4 changes: 2 additions & 2 deletions docs/stackit_postgresflex_instance_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ stackit postgresflex instance create [flags]

```
Create a PostgreSQL Flex instance with name "my-instance", ACL 0.0.0.0/0 (open access) and specify flavor by CPU and RAM. Other parameters are set to default values
$ stackit postgresflex instance create --name my-instance --cpu 1 --ram 4 --acl 0.0.0.0/0
$ stackit postgresflex instance create --name my-instance --cpu 2 --ram 4 --acl 0.0.0.0/0

Create a PostgreSQL Flex instance with name "my-instance", ACL 0.0.0.0/0 (open access) and specify flavor by ID. Other parameters are set to default values
$ stackit postgresflex instance create --name my-instance --flavor-id xxx --acl 0.0.0.0/0

Create a PostgreSQL Flex instance with name "my-instance", allow access to a specific range of IP addresses, specify flavor by CPU and RAM and set storage size to 20 GB. Other parameters are set to default values
$ stackit postgresflex instance create --name my-instance --cpu 1 --ram 4 --acl 1.2.3.0/24 --storage-size 20
$ stackit postgresflex instance create --name my-instance --cpu 2 --ram 4 --acl 1.2.3.0/24 --storage-size 20
```

### Options
Expand Down
8 changes: 7 additions & 1 deletion docs/stackit_postgresflex_instance_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Deletes a PostgreSQL Flex instance
### Synopsis

Deletes a PostgreSQL Flex instance.
By default, instances will be kept in a delayed deleted state for 7 days before being permanently deleted.
Use the --force flag to force the immediate deletion of a delayed deleted instance.

```
stackit postgresflex instance delete INSTANCE_ID [flags]
Expand All @@ -15,12 +17,16 @@ stackit postgresflex instance delete INSTANCE_ID [flags]
```
Delete a PostgreSQL Flex instance with ID "xxx"
$ stackit postgresflex instance delete xxx

Force the deletion of a delayed deleted PostgreSQL Flex instance with ID "xxx"
$ stackit postgresflex instance delete xxx --force
```

### Options

```
-h, --help Help for "stackit postgresflex instance delete"
-f, --force Force deletion of a delayed deleted instance
-h, --help Help for "stackit postgresflex instance delete"
```

### Options inherited from parent commands
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/stackitcloud/stackit-sdk-go/services/dns v0.8.4
github.com/stackitcloud/stackit-sdk-go/services/mongodbflex v0.11.1
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.10.1
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.11.0
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.12.0
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.7.7
github.com/stackitcloud/stackit-sdk-go/services/secretsmanager v0.6.0
github.com/stackitcloud/stackit-sdk-go/services/serviceaccount v0.3.6
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ github.com/stackitcloud/stackit-sdk-go/services/objectstorage v0.8.6 h1:+mcoBKs6
github.com/stackitcloud/stackit-sdk-go/services/objectstorage v0.8.6/go.mod h1:W9BML8bqZb2dOZe1K+M+qBBs8/QNirr3jA0xxy9tNRY=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.10.1 h1:LKic8dXtXKsRst2+wY9dNjjkMyJ05QIDpOJuRmVb410=
github.com/stackitcloud/stackit-sdk-go/services/opensearch v0.10.1/go.mod h1:g1o1bmqtTliy9UkFlRV/6bn6GQk+hkvnny3UjMI69S0=
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.11.0 h1:w3vUPJcPE81nItkkbPs1pxm+QF4c0YIbPyY0dd6qI2w=
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.11.0/go.mod h1:P0YyvgwIsVKJijdWGVJVOp/ac7PVX99Oj+dr4v1zECc=
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.12.0 h1:W2WSYUyhKaHQ+BZfmyRw9PKv5q7ihGRyNhNgIlyM+Y8=
github.com/stackitcloud/stackit-sdk-go/services/postgresflex v0.12.0/go.mod h1:P0YyvgwIsVKJijdWGVJVOp/ac7PVX99Oj+dr4v1zECc=
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.10.0 h1:Fle394socpyf662g3jMrtZpZaWVgBMBIEFnh4fnGock=
github.com/stackitcloud/stackit-sdk-go/services/rabbitmq v0.10.0/go.mod h1:JvqOSrTCiynS0x6Y9OsK54yvdB6AtIWLwXDEjoCkAIg=
github.com/stackitcloud/stackit-sdk-go/services/redis v0.10.1 h1:/tRad17HUcGRm448l8XyX6uhnnHVfj3VdUQquIwNq2Q=
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/postgresflex/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ func NewCmd() *cobra.Command {
Example: examples.Build(
examples.NewExample(
`Create a PostgreSQL Flex instance with name "my-instance", ACL 0.0.0.0/0 (open access) and specify flavor by CPU and RAM. Other parameters are set to default values`,
`$ stackit postgresflex instance create --name my-instance --cpu 1 --ram 4 --acl 0.0.0.0/0`),
`$ stackit postgresflex instance create --name my-instance --cpu 2 --ram 4 --acl 0.0.0.0/0`),
examples.NewExample(
`Create a PostgreSQL Flex instance with name "my-instance", ACL 0.0.0.0/0 (open access) and specify flavor by ID. Other parameters are set to default values`,
`$ stackit postgresflex instance create --name my-instance --flavor-id xxx --acl 0.0.0.0/0`),
examples.NewExample(
`Create a PostgreSQL Flex instance with name "my-instance", allow access to a specific range of IP addresses, specify flavor by CPU and RAM and set storage size to 20 GB. Other parameters are set to default values`,
`$ stackit postgresflex instance create --name my-instance --cpu 1 --ram 4 --acl 1.2.3.0/24 --storage-size 20`),
`$ stackit postgresflex instance create --name my-instance --cpu 2 --ram 4 --acl 1.2.3.0/24 --storage-size 20`),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
Expand Down
109 changes: 94 additions & 15 deletions internal/cmd/postgresflex/instance/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"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/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client"
postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils"
Expand All @@ -21,23 +22,33 @@ import (

const (
instanceIdArg = "INSTANCE_ID"

forceDeleteFlag = "force"
)

type inputModel struct {
*globalflags.GlobalFlagModel
InstanceId string
InstanceId string
ForceDelete bool
}

func NewCmd() *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("delete %s", instanceIdArg),
Short: "Deletes a PostgreSQL Flex instance",
Long: "Deletes a PostgreSQL Flex instance.",
Args: args.SingleArg(instanceIdArg, utils.ValidateUUID),
Long: fmt.Sprintf("%s\n%s\n%s",
"Deletes a PostgreSQL Flex instance.",
"By default, instances will be kept in a delayed deleted state for 7 days before being permanently deleted.",
"Use the --force flag to force the immediate deletion of a delayed deleted instance.",
),
Args: args.SingleArg(instanceIdArg, utils.ValidateUUID),
Example: examples.Build(
examples.NewExample(
`Delete a PostgreSQL Flex instance with ID "xxx"`,
"$ stackit postgresflex instance delete xxx"),
examples.NewExample(
`Force the deletion of a delayed deleted PostgreSQL Flex instance with ID "xxx"`,
"$ stackit postgresflex instance delete xxx --force"),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
Expand Down Expand Up @@ -65,35 +76,74 @@ func NewCmd() *cobra.Command {
}
}

// Call API
req := buildRequest(ctx, model, apiClient)
err = req.Execute()
toDelete, toForceDelete, err := getNextOperations(ctx, model, apiClient)
if err != nil {
return fmt.Errorf("delete PostgreSQL Flex instance: %w", err)
return err
}

// Wait for async operation, if async mode not enabled
if !model.Async {
s := spinner.New(cmd)
s.Start("Deleting instance")
_, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx)
if toDelete {
// Call API
delReq := buildDeleteRequest(ctx, model, apiClient)
err = delReq.Execute()
if err != nil {
return fmt.Errorf("wait for PostgreSQL Flex instance deletion: %w", err)
return fmt.Errorf("delete PostgreSQL Flex instance: %w", err)
}

// Wait for async operation, if async mode not enabled
if !model.Async {
s := spinner.New(cmd)
s.Start("Deleting instance")
_, err = wait.DeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("wait for PostgreSQL Flex instance deletion: %w", err)
}
s.Stop()
}
}

if toForceDelete {
// Call API
forceDelReq := buildForceDeleteRequest(ctx, model, apiClient)
err = forceDelReq.Execute()
if err != nil {
return fmt.Errorf("force delete PostgreSQL Flex instance: %w", err)
}

// Wait for async operation, if async mode not enabled
if !model.Async {
s := spinner.New(cmd)
s.Start("Forcing deletion of instance")
_, err = wait.ForceDeleteInstanceWaitHandler(ctx, apiClient, model.ProjectId, model.InstanceId).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("wait for PostgreSQL Flex instance force deletion: %w", err)
}
s.Stop()
}
s.Stop()
}

operationState := "Deleted"
if toForceDelete {
operationState = "Forcefully deleted"
}
if model.Async {
operationState = "Triggered deletion of"
if toForceDelete {
operationState = "Triggered forced deletion of"
}
}

cmd.Printf("%s instance %q\n", operationState, instanceLabel)
return nil
},
}
configureFlags(cmd)
return cmd
}

func configureFlags(cmd *cobra.Command) {
cmd.Flags().BoolP(forceDeleteFlag, "f", false, "Force deletion of a delayed deleted instance")
}

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

Expand All @@ -105,10 +155,39 @@ func parseInput(cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
return &inputModel{
GlobalFlagModel: globalFlags,
InstanceId: instanceId,
ForceDelete: flags.FlagToBoolValue(cmd, forceDeleteFlag),
}, nil
}

func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresflex.APIClient) postgresflex.ApiDeleteInstanceRequest {
func buildDeleteRequest(ctx context.Context, model *inputModel, apiClient *postgresflex.APIClient) postgresflex.ApiDeleteInstanceRequest {
req := apiClient.DeleteInstance(ctx, model.ProjectId, model.InstanceId)
return req
}

func buildForceDeleteRequest(ctx context.Context, model *inputModel, apiClient *postgresflex.APIClient) postgresflex.ApiForceDeleteInstanceRequest {
req := apiClient.ForceDeleteInstance(ctx, model.ProjectId, model.InstanceId)
return req
}

type PostgreSQLFlexClient interface {
GetInstanceExecute(ctx context.Context, projectId, instanceId string) (*postgresflex.InstanceResponse, error)
ListVersionsExecute(ctx context.Context, projectId string) (*postgresflex.ListVersionsResponse, error)
GetUserExecute(ctx context.Context, projectId, instanceId, userId string) (*postgresflex.GetUserResponse, error)
}

func getNextOperations(ctx context.Context, model *inputModel, apiClient PostgreSQLFlexClient) (toDelete, toForceDelete bool, err error) {
instanceStatus, err := postgresflexUtils.GetInstanceStatus(ctx, apiClient, model.ProjectId, model.InstanceId)
if err != nil {
return false, false, fmt.Errorf("get PostgreSQL Flex instance status: %w", err)
}

if instanceStatus == wait.InstanceStateDeleted {
if !model.ForceDelete {
return false, false, fmt.Errorf("instance is already deleted, use --force to force the deletion of a delayed deleted instance")
}

return false, model.ForceDelete, nil
}

return true, model.ForceDelete, nil
}
Loading