-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathupdate.go
More file actions
170 lines (146 loc) · 5.51 KB
/
update.go
File metadata and controls
170 lines (146 loc) · 5.51 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package update
import (
"context"
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"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/projectname"
"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"
"github.com/stackitcloud/stackit-sdk-go/services/sfs"
)
const (
exportPolicyArg = "EXPORT_POLICY_ID"
rulesFlag = "rules"
removeRulesFlag = "remove-rules"
)
type inputModel struct {
*globalflags.GlobalFlagModel
ExportPolicyId string
Rules *[]sfs.UpdateShareExportPolicyBodyRule
}
func NewCmd(params *types.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: fmt.Sprintf("update %s", exportPolicyArg),
Short: "Updates a export policy",
Long: "Updates a export policy.",
Args: args.SingleArg(exportPolicyArg, utils.ValidateUUID),
Example: examples.Build(
examples.NewExample(
`Update a export policy with ID "xxx" and with rules from file "./rules.json"`,
"$ stackit beta sfs export-policy update xxx --rules @./rules.json",
),
examples.NewExample(
`Update a export policy with ID "xxx" and remove the rules`,
"$ stackit beta sfs export-policy update XXX --remove-rules",
),
),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
model, err := parseInput(params.Printer, cmd, args)
if err != nil {
return fmt.Errorf("unable to parse input: %w", err)
}
// Configure client
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
exportPolicyLabel, err := sfsUtils.GetExportPolicyName(ctx, apiClient, model.ProjectId, model.Region, model.ExportPolicyId)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get export policy name: %v", err)
exportPolicyLabel = model.ExportPolicyId
} else if exportPolicyLabel == "" {
exportPolicyLabel = model.ExportPolicyId
}
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
}
prompt := fmt.Sprintf("Are you sure you want to update export policy %q for project %q?", exportPolicyLabel, projectLabel)
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("update export policy: %w", err)
}
return outputResult(params.Printer, model.OutputFormat, projectLabel, exportPolicyLabel, resp)
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
cmd.Flags().Var(flags.ReadFromFileFlag(), rulesFlag, "Rules of the export policy")
cmd.Flags().Bool(removeRulesFlag, false, "Remove the export policy rules")
rulesFlags := []string{rulesFlag, removeRulesFlag}
cmd.MarkFlagsMutuallyExclusive(rulesFlags...)
cmd.MarkFlagsOneRequired(rulesFlags...) // Because the update endpoint supports only rules at the moment, one of the flags must be required
}
func buildRequest(ctx context.Context, model *inputModel, apiClient *sfs.APIClient) sfs.ApiUpdateShareExportPolicyRequest {
req := apiClient.UpdateShareExportPolicy(ctx, model.ProjectId, model.Region, model.ExportPolicyId)
payload := sfs.UpdateShareExportPolicyPayload{
Rules: model.Rules,
}
return req.UpdateShareExportPolicyPayload(payload)
}
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
exportPolicyId := inputArgs[0]
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}
var rules *[]sfs.UpdateShareExportPolicyBodyRule
noRulesErr := fmt.Errorf("no rules specified")
if rulesString := flags.FlagToStringPointer(p, cmd, rulesFlag); rulesString != nil {
var r []sfs.UpdateShareExportPolicyBodyRule
err := json.Unmarshal([]byte(*rulesString), &r)
if err != nil {
return nil, fmt.Errorf("could not parse rules: %w", err)
}
if r == nil {
return nil, noRulesErr
}
rules = &r
}
if removeRules := flags.FlagToBoolPointer(p, cmd, removeRulesFlag); removeRules != nil {
// Create an empty slice for the patch request
rules = &[]sfs.UpdateShareExportPolicyBodyRule{}
}
// Because the update endpoint supports only rules at the moment, this should not be empty
if rules == nil {
return nil, noRulesErr
}
model := inputModel{
GlobalFlagModel: globalFlags,
ExportPolicyId: exportPolicyId,
Rules: rules,
}
p.DebugInputModel(model)
return &model, nil
}
func outputResult(p *print.Printer, outputFormat, projectLabel, exportPolicyLabel string, resp *sfs.UpdateShareExportPolicyResponse) error {
return p.OutputResult(outputFormat, resp, func() error {
if resp == nil {
p.Outputln("Empty export policy response")
return nil
}
p.Outputf("Updated export policy %q for project %q\n", exportPolicyLabel, projectLabel)
return nil
})
}