Skip to content

Commit 892faf5

Browse files
committed
feat(sfs): onboard snapshot policy commands
relates to STACKITCLI-395
1 parent 64ff998 commit 892faf5

10 files changed

Lines changed: 877 additions & 0 deletions

docs/stackit_beta_sfs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ stackit beta sfs [flags]
3535
* [stackit beta sfs resource-pool](./stackit_beta_sfs_resource-pool.md) - Provides functionality for SFS resource pools
3636
* [stackit beta sfs share](./stackit_beta_sfs_share.md) - Provides functionality for SFS shares
3737
* [stackit beta sfs snapshot](./stackit_beta_sfs_snapshot.md) - Provides functionality for SFS snapshots
38+
* [stackit beta sfs snapshot-policy](./stackit_beta_sfs_snapshot-policy.md) - Provides functionality for SFS snapshot policies
3839

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## stackit beta sfs snapshot-policy
2+
3+
Provides functionality for SFS snapshot policies
4+
5+
### Synopsis
6+
7+
Provides functionality for SFS snapshot policies.
8+
9+
```
10+
stackit beta sfs snapshot-policy [flags]
11+
```
12+
13+
### Options
14+
15+
```
16+
-h, --help Help for "stackit beta sfs snapshot-policy"
17+
```
18+
19+
### Options inherited from parent commands
20+
21+
```
22+
-y, --assume-yes If set, skips all confirmation prompts
23+
--async If set, runs the command asynchronously
24+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
25+
-p, --project-id string Project ID
26+
--region string Target region for region-specific requests
27+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
28+
```
29+
30+
### SEE ALSO
31+
32+
* [stackit beta sfs](./stackit_beta_sfs.md) - Provides functionality for SFS (stackit file storage)
33+
* [stackit beta sfs snapshot-policy describe](./stackit_beta_sfs_snapshot-policy_describe.md) - Shows details of a snapshot policy
34+
* [stackit beta sfs snapshot-policy list](./stackit_beta_sfs_snapshot-policy_list.md) - Lists all snapshot policies of a project
35+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
## stackit beta sfs snapshot-policy describe
2+
3+
Shows details of a snapshot policy
4+
5+
### Synopsis
6+
7+
Shows details of a snapshot policy.
8+
9+
```
10+
stackit beta sfs snapshot-policy describe SNAPSHOT_POLICY_ID [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
Describe a snapshot policy with ID "xxx"
17+
$ stackit beta sfs snapshot-policy describe xxx
18+
```
19+
20+
### Options
21+
22+
```
23+
-h, --help Help for "stackit beta sfs snapshot-policy describe"
24+
```
25+
26+
### Options inherited from parent commands
27+
28+
```
29+
-y, --assume-yes If set, skips all confirmation prompts
30+
--async If set, runs the command asynchronously
31+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
32+
-p, --project-id string Project ID
33+
--region string Target region for region-specific requests
34+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
35+
```
36+
37+
### SEE ALSO
38+
39+
* [stackit beta sfs snapshot-policy](./stackit_beta_sfs_snapshot-policy.md) - Provides functionality for SFS snapshot policies
40+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## stackit beta sfs snapshot-policy list
2+
3+
Lists all snapshot policies of a project
4+
5+
### Synopsis
6+
7+
Lists all snapshot policies of a project.
8+
9+
```
10+
stackit beta sfs snapshot-policy list [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
List all snapshot policies
17+
$ stackit beta sfs snapshot-policy list
18+
19+
List all immutable snapshot policies
20+
$ stackit beta sfs snapshot-policy list --immutable
21+
22+
List up to 10 snapshot policies
23+
$ stackit beta sfs snapshot-policy list --limit 10
24+
```
25+
26+
### Options
27+
28+
```
29+
-h, --help Help for "stackit beta sfs snapshot-policy list"
30+
--immutable Immutable snapshot policy
31+
--limit int Maximum number of entries to list
32+
```
33+
34+
### Options inherited from parent commands
35+
36+
```
37+
-y, --assume-yes If set, skips all confirmation prompts
38+
--async If set, runs the command asynchronously
39+
-o, --output-format string Output format, one of ["json" "pretty" "none" "yaml"]
40+
-p, --project-id string Project ID
41+
--region string Target region for region-specific requests
42+
--verbosity string Verbosity of the CLI, one of ["debug" "info" "warning" "error"] (default "info")
43+
```
44+
45+
### SEE ALSO
46+
47+
* [stackit beta sfs snapshot-policy](./stackit_beta_sfs_snapshot-policy.md) - Provides functionality for SFS snapshot policies
48+

internal/cmd/beta/sfs/sfs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
resourcepool "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sfs/resource-pool"
77
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/sfs/share"
88
"github.com/stackitcloud/stackit-cli/internal/cmd/beta/sfs/snapshot"
9+
snapshotpolicy "github.com/stackitcloud/stackit-cli/internal/cmd/beta/sfs/snapshot-policy"
910
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1011
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
1112
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
@@ -30,5 +31,6 @@ func addSubcommands(cmd *cobra.Command, params *types.CmdParams) {
3031
cmd.AddCommand(share.NewCmd(params))
3132
cmd.AddCommand(exportpolicy.NewCmd(params))
3233
cmd.AddCommand(snapshot.NewCmd(params))
34+
cmd.AddCommand(snapshotpolicy.NewCmd(params))
3335
cmd.AddCommand(performanceclass.NewCmd(params))
3436
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package describe
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
sfs "github.com/stackitcloud/stackit-sdk-go/services/sfs/v1api"
9+
10+
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
11+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
12+
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
13+
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
15+
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
16+
"github.com/stackitcloud/stackit-cli/internal/pkg/services/sfs/client"
17+
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
18+
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
19+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
20+
)
21+
22+
const snapshotPolicyIdArg = "SNAPSHOT_POLICY_ID"
23+
24+
type inputModel struct {
25+
*globalflags.GlobalFlagModel
26+
SnapshotPolicyId string
27+
}
28+
29+
func NewCmd(params *types.CmdParams) *cobra.Command {
30+
cmd := &cobra.Command{
31+
Use: fmt.Sprintf("describe %s", snapshotPolicyIdArg),
32+
Short: "Shows details of a snapshot policy",
33+
Long: "Shows details of a snapshot policy.",
34+
Args: args.SingleArg(snapshotPolicyIdArg, utils.ValidateUUID),
35+
Example: examples.Build(
36+
examples.NewExample(
37+
`Describe a snapshot policy with ID "xxx"`,
38+
"$ stackit beta sfs snapshot-policy describe xxx",
39+
),
40+
),
41+
RunE: func(cmd *cobra.Command, args []string) error {
42+
ctx := context.Background()
43+
model, err := parseInput(params.Printer, cmd, args)
44+
if err != nil {
45+
return fmt.Errorf("unable to parse input: %w", err)
46+
}
47+
48+
// Configure API client
49+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
50+
if err != nil {
51+
return err
52+
}
53+
54+
// Call API
55+
req := buildRequest(ctx, model, apiClient)
56+
resp, err := req.Execute()
57+
if err != nil {
58+
return fmt.Errorf("read snapshot policy: %w", err)
59+
}
60+
61+
// Get projectLabel
62+
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
63+
if err != nil {
64+
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
65+
projectLabel = model.ProjectId
66+
} else if projectLabel == "" {
67+
projectLabel = model.ProjectId
68+
}
69+
70+
return outputResult(params.Printer, model.OutputFormat, model.SnapshotPolicyId, projectLabel, resp)
71+
},
72+
}
73+
return cmd
74+
}
75+
76+
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
77+
snapshotPolicyId := inputArgs[0]
78+
79+
globalFlags := globalflags.Parse(p, cmd)
80+
if globalFlags.ProjectId == "" {
81+
return nil, &errors.ProjectIdError{}
82+
}
83+
84+
model := inputModel{
85+
GlobalFlagModel: globalFlags,
86+
SnapshotPolicyId: snapshotPolicyId,
87+
}
88+
89+
p.DebugInputModel(model)
90+
return &model, nil
91+
}
92+
93+
func buildRequest(ctx context.Context, model *inputModel, apiClient *sfs.APIClient) sfs.ApiGetSnapshotPolicyRequest {
94+
return apiClient.DefaultAPI.GetSnapshotPolicy(ctx, model.ProjectId, model.SnapshotPolicyId)
95+
}
96+
97+
func outputResult(p *print.Printer, outputFormat, snapshotPolicyId, projectLabel string, snapshotPolicy *sfs.GetSnapshotPolicyResponse) error {
98+
return p.OutputResult(outputFormat, snapshotPolicy, func() error {
99+
if snapshotPolicy == nil || snapshotPolicy.SnapshotPolicy == nil {
100+
p.Outputf("Snapshot policy %q not found in project %q", snapshotPolicyId, projectLabel)
101+
return nil
102+
}
103+
104+
var content []tables.Table
105+
106+
table := tables.NewTable()
107+
table.SetTitle("Snapshot Policy")
108+
policy := snapshotPolicy.SnapshotPolicy
109+
110+
table.AddRow("ID", utils.PtrString(policy.Id))
111+
table.AddSeparator()
112+
table.AddRow("NAME", utils.PtrString(policy.Name))
113+
table.AddSeparator()
114+
table.AddRow("ENABLED", utils.PtrString(policy.Enabled))
115+
table.AddSeparator()
116+
table.AddRow("COMMENT", utils.PtrString(policy.Comment))
117+
table.AddSeparator()
118+
table.AddRow("CREATED AT", utils.ConvertTimePToDateTimeString(policy.CreatedAt))
119+
120+
content = append(content, table)
121+
122+
if len(policy.SnapshotSchedules) > 0 {
123+
snapshotSchedulesTable := tables.NewTable()
124+
snapshotSchedulesTable.SetTitle("Snapshot Schedules")
125+
126+
snapshotSchedulesTable.SetHeader("ID", "NAME", "INTERVAL", "PREFIX", "RETENTION COUNT", "RETENTION PERIOD", "CREATED AT")
127+
128+
for _, snapshotSchedule := range policy.SnapshotSchedules {
129+
snapshotSchedulesTable.AddRow(
130+
utils.PtrString(snapshotSchedule.Id),
131+
utils.PtrString(snapshotSchedule.Name),
132+
utils.PtrString(snapshotSchedule.Interval),
133+
utils.PtrString(snapshotSchedule.Prefix),
134+
utils.PtrString(snapshotSchedule.RetentionCount),
135+
utils.PtrString(snapshotSchedule.RetentionPeriod),
136+
utils.ConvertTimePToDateTimeString(snapshotSchedule.CreatedAt),
137+
)
138+
snapshotSchedulesTable.AddSeparator()
139+
}
140+
141+
content = append(content, snapshotSchedulesTable)
142+
}
143+
144+
if err := tables.DisplayTables(p, content); err != nil {
145+
return fmt.Errorf("render tables: %w", err)
146+
}
147+
return nil
148+
})
149+
}

0 commit comments

Comments
 (0)