-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathcreate.go
More file actions
150 lines (131 loc) · 5.32 KB
/
Copy pathcreate.go
File metadata and controls
150 lines (131 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package create
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/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/sfs/client"
sfsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/sfs/utils"
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
)
const (
resourcePoolIdFlag = "resource-pool-id"
nameFlag = "name"
commentFlag = "comment"
snaplockRetentionHoursFlag = "snaplock-retention-hours"
)
type inputModel struct {
*globalflags.GlobalFlagModel
ResourcePoolId string
Name string
Comment *string
SnaplockRetentionHours *int32
}
func NewCmd(params *types.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates a new snapshot of a resource pool",
Long: "Creates a new snapshot of a resource pool.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create a new snapshot with name "snapshot-name" of a resource pool with ID "xxx"`,
"$ stackit beta sfs snapshot create --name snapshot-name --resource-pool-id xxx",
),
examples.NewExample(
`Create a new snapshot with name "snapshot-name" and comment "snapshot-comment" of a resource pool with ID "xxx"`,
`$ stackit beta sfs snapshot create --name snapshot-name --resource-pool-id xxx --comment "snapshot-comment"`,
),
examples.NewExample(
`Create a new snapshot with name "snapshot-name" and snaplock retention hours "24" of a resource pool with ID "xxx"`,
`$ stackit beta sfs snapshot create --name snapshot-name --resource-pool-id xxx --snaplock-retention-hours 24`,
),
),
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
}
resourcePoolLabel, err := sfsUtils.GetResourcePoolName(ctx, apiClient.DefaultAPI, model.ProjectId, model.Region, model.ResourcePoolId)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get resource pool name: %v", err)
resourcePoolLabel = model.ResourcePoolId
} else if resourcePoolLabel == "" {
resourcePoolLabel = model.ResourcePoolId
}
prompt := fmt.Sprintf("Are you sure you want to create a snapshot for resource pool %q?", resourcePoolLabel)
err = params.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
}
// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("create snapshot: %w", err)
}
return outputResult(params.Printer, model.OutputFormat, model.Name, resourcePoolLabel, resp)
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
cmd.Flags().String(nameFlag, "", "Snapshot name")
cmd.Flags().String(commentFlag, "", "A comment to add more information to the snapshot")
cmd.Flags().Int32(snaplockRetentionHoursFlag, 0, "Retention hours for the snaplock")
cmd.Flags().Var(flags.UUIDFlag(), resourcePoolIdFlag, "The resource pool from which the snapshot should be created")
err := flags.MarkFlagsRequired(cmd, resourcePoolIdFlag, nameFlag)
cobra.CheckErr(err)
}
func buildRequest(ctx context.Context, model *inputModel, apiClient *sfs.APIClient) sfs.ApiCreateResourcePoolSnapshotRequest {
req := apiClient.DefaultAPI.CreateResourcePoolSnapshot(ctx, model.ProjectId, model.Region, model.ResourcePoolId)
req = req.CreateResourcePoolSnapshotPayload(sfs.CreateResourcePoolSnapshotPayload{
Name: utils.Ptr(model.Name),
Comment: *sfs.NewNullableString(model.Comment),
SnaplockRetentionHours: *sfs.NewNullableInt32(model.SnaplockRetentionHours),
})
return req
}
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,
Name: flags.FlagToStringValue(p, cmd, nameFlag),
ResourcePoolId: flags.FlagToStringValue(p, cmd, resourcePoolIdFlag),
Comment: flags.FlagToStringPointer(p, cmd, commentFlag),
SnaplockRetentionHours: flags.FlagToInt32Pointer(p, cmd, snaplockRetentionHoursFlag),
}
p.DebugInputModel(model)
return &model, nil
}
func outputResult(p *print.Printer, outputFormat, snapshotLabel, resourcePoolLabel string, resp *sfs.CreateResourcePoolSnapshotResponse) error {
return p.OutputResult(outputFormat, resp, func() error {
if resp == nil || resp.ResourcePoolSnapshot == nil {
p.Outputln("SFS snapshot response is empty")
return nil
}
p.Outputf(
"Created snapshot %q for resource pool %q.\n",
snapshotLabel,
resourcePoolLabel,
)
return nil
})
}