Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -36,4 +36,5 @@ stackit beta sfs [flags]
* [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
* [stackit beta sfs snapshot-policy](./stackit_beta_sfs_snapshot-policy.md) - Provides functionality for SFS snapshot policies

35 changes: 35 additions & 0 deletions docs/stackit_beta_sfs_snapshot-policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
## stackit beta sfs snapshot-policy

Provides functionality for SFS snapshot policies

### Synopsis

Provides functionality for SFS snapshot policies.

```
stackit beta sfs snapshot-policy [flags]
```

### Options

```
-h, --help Help for "stackit beta sfs snapshot-policy"
```

### 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 snapshot-policy describe](./stackit_beta_sfs_snapshot-policy_describe.md) - Shows details of a snapshot policy
* [stackit beta sfs snapshot-policy list](./stackit_beta_sfs_snapshot-policy_list.md) - Lists all snapshot policies of a project

40 changes: 40 additions & 0 deletions docs/stackit_beta_sfs_snapshot-policy_describe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## stackit beta sfs snapshot-policy describe

Shows details of a snapshot policy

### Synopsis

Shows details of a snapshot policy.

```
stackit beta sfs snapshot-policy describe SNAPSHOT_POLICY_ID [flags]
```

### Examples

```
Describe a snapshot policy with ID "xxx"
$ stackit beta sfs snapshot-policy describe xxx
```

### Options

```
-h, --help Help for "stackit beta sfs snapshot-policy 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 snapshot-policy](./stackit_beta_sfs_snapshot-policy.md) - Provides functionality for SFS snapshot policies

48 changes: 48 additions & 0 deletions docs/stackit_beta_sfs_snapshot-policy_list.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## stackit beta sfs snapshot-policy list

Lists all snapshot policies of a project

### Synopsis

Lists all snapshot policies of a project.

```
stackit beta sfs snapshot-policy list [flags]
```

### Examples

```
List all snapshot policies
$ stackit beta sfs snapshot-policy list

List all immutable snapshot policies
$ stackit beta sfs snapshot-policy list --immutable

List up to 10 snapshot policies
$ stackit beta sfs snapshot-policy list --limit 10
```

### Options

```
-h, --help Help for "stackit beta sfs snapshot-policy list"
--immutable Immutable snapshot policy
--limit int Maximum number of entries to list
```

### 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 snapshot-policy](./stackit_beta_sfs_snapshot-policy.md) - Provides functionality for SFS snapshot policies

2 changes: 2 additions & 0 deletions internal/cmd/beta/sfs/sfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
resourcepool "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sfs/resource-pool"
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/sfs/share"
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/sfs/snapshot"
snapshotpolicy "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sfs/snapshot-policy"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
Expand All @@ -31,6 +32,7 @@ func addSubcommands(cmd *cobra.Command, params *types.CmdParams) {
cmd.AddCommand(share.NewCmd(params))
cmd.AddCommand(exportpolicy.NewCmd(params))
cmd.AddCommand(snapshot.NewCmd(params))
cmd.AddCommand(snapshotpolicy.NewCmd(params))
cmd.AddCommand(performanceclass.NewCmd(params))
cmd.AddCommand(projectlock.NewCmd(params))
}
149 changes: 149 additions & 0 deletions internal/cmd/beta/sfs/snapshot-policy/describe/describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package describe

import (
"context"
"fmt"

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

"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"
)

const snapshotPolicyIdArg = "SNAPSHOT_POLICY_ID"

type inputModel struct {
*globalflags.GlobalFlagModel
SnapshotPolicyId string
}

func NewCmd(params *types.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("describe %s", snapshotPolicyIdArg),
Short: "Shows details of a snapshot policy",
Long: "Shows details of a snapshot policy.",
Args: args.SingleArg(snapshotPolicyIdArg, utils.ValidateUUID),
Example: examples.Build(
examples.NewExample(
`Describe a snapshot policy with ID "xxx"`,
"$ stackit beta sfs snapshot-policy describe xxx",
),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return fmt.Errorf("unable to parse input: %w", err)
}

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

// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("read snapshot policy: %w", err)
}

// Get projectLabel
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
}

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

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

globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}

model := inputModel{
GlobalFlagModel: globalFlags,
SnapshotPolicyId: snapshotPolicyId,
}

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

func buildRequest(ctx context.Context, model *inputModel, apiClient *sfs.APIClient) sfs.ApiGetSnapshotPolicyRequest {
return apiClient.DefaultAPI.GetSnapshotPolicy(ctx, model.ProjectId, model.SnapshotPolicyId)
}

func outputResult(p *print.Printer, outputFormat, snapshotPolicyId, projectLabel string, snapshotPolicy *sfs.GetSnapshotPolicyResponse) error {
return p.OutputResult(outputFormat, snapshotPolicy, func() error {
if snapshotPolicy == nil || snapshotPolicy.SnapshotPolicy == nil {
p.Outputf("Snapshot policy %q not found in project %q", snapshotPolicyId, projectLabel)
return nil
}

var content []tables.Table

table := tables.NewTable()
table.SetTitle("Snapshot Policy")
policy := snapshotPolicy.SnapshotPolicy

table.AddRow("ID", utils.PtrString(policy.Id))
table.AddSeparator()
table.AddRow("NAME", utils.PtrString(policy.Name))
table.AddSeparator()
table.AddRow("ENABLED", utils.PtrString(policy.Enabled))
table.AddSeparator()
table.AddRow("COMMENT", utils.PtrString(policy.Comment))
table.AddSeparator()
table.AddRow("CREATED AT", utils.ConvertTimePToDateTimeString(policy.CreatedAt))

content = append(content, table)

if len(policy.SnapshotSchedules) > 0 {
snapshotSchedulesTable := tables.NewTable()
snapshotSchedulesTable.SetTitle("Snapshot Schedules")

snapshotSchedulesTable.SetHeader("ID", "NAME", "INTERVAL", "PREFIX", "RETENTION COUNT", "RETENTION PERIOD", "CREATED AT")

for _, snapshotSchedule := range policy.SnapshotSchedules {
snapshotSchedulesTable.AddRow(
utils.PtrString(snapshotSchedule.Id),
utils.PtrString(snapshotSchedule.Name),
utils.PtrString(snapshotSchedule.Interval),
utils.PtrString(snapshotSchedule.Prefix),
utils.PtrString(snapshotSchedule.RetentionCount),
utils.PtrString(snapshotSchedule.RetentionPeriod),
utils.ConvertTimePToDateTimeString(snapshotSchedule.CreatedAt),
)
snapshotSchedulesTable.AddSeparator()
}

content = append(content, snapshotSchedulesTable)
}

if err := tables.DisplayTables(p, content); err != nil {
return fmt.Errorf("render tables: %w", err)
}
return nil
})
}
Loading
Loading