Skip to content
Open
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_beta_sfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ stackit beta sfs [flags]
* [stackit beta](./stackit_beta.md) - Contains beta STACKIT CLI commands
* [stackit beta sfs export-policy](./stackit_beta_sfs_export-policy.md) - Provides functionality for SFS export policies
* [stackit beta sfs performance-class](./stackit_beta_sfs_performance-class.md) - Provides functionality for SFS performance classes
* [stackit beta sfs project-lock](./stackit_beta_sfs_project-lock.md) - Provides functionality for SFS project locks
* [stackit beta sfs resource-pool](./stackit_beta_sfs_resource-pool.md) - Provides functionality for SFS resource pools
* [stackit beta sfs share](./stackit_beta_sfs_share.md) - Provides functionality for SFS shares
* [stackit beta sfs snapshot](./stackit_beta_sfs_snapshot.md) - Provides functionality for SFS snapshots
Expand Down
36 changes: 36 additions & 0 deletions docs/stackit_beta_sfs_project-lock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## stackit beta sfs project-lock

Provides functionality for SFS project locks

### Synopsis

Provides functionality for SFS project locks.

```
stackit beta sfs project-lock [flags]
```

### Options

```
-h, --help Help for "stackit beta sfs project-lock"
```

### 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" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit beta sfs](./stackit_beta_sfs.md) - Provides functionality for SFS (stackit file storage)
* [stackit beta sfs project-lock describe](./stackit_beta_sfs_project-lock_describe.md) - Get lock status for a project
* [stackit beta sfs project-lock lock](./stackit_beta_sfs_project-lock_lock.md) - Enables lock for a project
* [stackit beta sfs project-lock unlock](./stackit_beta_sfs_project-lock_unlock.md) - Clean up lock for a project

40 changes: 40 additions & 0 deletions docs/stackit_beta_sfs_project-lock_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit beta sfs project-lock describe

Get lock status for a project

### Synopsis

Get lock status for a project.

```
stackit beta sfs project-lock describe [flags]
```

### Examples

```
Get lock status for project
$ stackit beta sfs project-lock describe
```

### Options

```
-h, --help Help for "stackit beta sfs project-lock describe"
```

### 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" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit beta sfs project-lock](./stackit_beta_sfs_project-lock.md) - Provides functionality for SFS project locks

40 changes: 40 additions & 0 deletions docs/stackit_beta_sfs_project-lock_lock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit beta sfs project-lock lock

Enables lock for a project

### Synopsis

Enables lock for a project. Necessary for immutable snapshots and to prevent accidental deletion of resources.

```
stackit beta sfs project-lock lock [flags]
```

### Examples

```
Enable lock for project
$ stackit beta sfs project-lock lock
```

### Options

```
-h, --help Help for "stackit beta sfs project-lock lock"
```

### 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" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit beta sfs project-lock](./stackit_beta_sfs_project-lock.md) - Provides functionality for SFS project locks

40 changes: 40 additions & 0 deletions docs/stackit_beta_sfs_project-lock_unlock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit beta sfs project-lock unlock

Clean up lock for a project

### Synopsis

Clean up lock for a project.

```
stackit beta sfs project-lock unlock [flags]
```

### Examples

```
Disable lock for project
$ stackit beta sfs project-lock unlock
```

### Options

```
-h, --help Help for "stackit beta sfs project-lock unlock"
```

### 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" "none" "yaml"]
-p, --project-id string Project ID
--region string Target region for region-specific requests
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
```

### SEE ALSO

* [stackit beta sfs project-lock](./stackit_beta_sfs_project-lock.md) - Provides functionality for SFS project locks

115 changes: 115 additions & 0 deletions internal/cmd/beta/sfs/project-lock/describe/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package describe

import (
"context"
"fmt"
"net/http"

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

"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"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/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/sfs/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

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

type inputModel struct {
*globalflags.GlobalFlagModel
}

func NewCmd(params *types.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "describe",
Short: "Get lock status for a project",
Long: "Get lock status for a project.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Get lock status for project`,
"$ stackit beta sfs project-lock describe"),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return err
}

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

projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
} else if projectLabel == "" {
projectLabel = model.ProjectId
}

// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
oapiErr, _ := err.(*oapierror.GenericOpenAPIError) //nolint:errorlint //complaining that error.As should be used to catch wrapped errors, but this error should not be wrapped
if oapiErr.StatusCode == http.StatusNotFound {
fmt.Printf("No active lock found for project %s\n", projectLabel)
return nil
}
return fmt.Errorf("get lock status for project: %w", err)
}

return outputResult(params.Printer, model.OutputFormat, resp)
},
}
return cmd
}

func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}

model := inputModel{
GlobalFlagModel: globalFlags,
}

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

func buildRequest(ctx context.Context, model *inputModel, apiClient *sfs.APIClient) sfs.ApiGetLockRequest {
req := apiClient.DefaultAPI.GetLock(ctx, model.Region, model.ProjectId)
return req
}

func outputResult(p *print.Printer, outputFormat string, resp *sfs.GetLockResponse) error {
return p.OutputResult(outputFormat, resp, func() error {
if resp == nil {
return fmt.Errorf("response is empty")
}

table := tables.NewTable()
table.AddRow("LOCK ID", utils.PtrString(resp.LockId))
table.AddSeparator()

err := table.Display(p)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
})
}
Loading
Loading