-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcreatePolicy.go
More file actions
117 lines (98 loc) · 2.86 KB
/
createPolicy.go
File metadata and controls
117 lines (98 loc) · 2.86 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
package main
import (
"fmt"
"io"
"net/http"
"github.com/kosli-dev/cli/internal/requests"
"github.com/spf13/cobra"
)
const createPolicyShortDesc = `Create or update a Kosli policy.`
const createPolicyExample = `
# create a Kosli policy:
kosli create policy yourPolicyName \
--description yourPolicyDescription \
--type environment \
--api-token yourAPIToken \
--org yourOrgName
# update a Kosli policy:
kosli create policy yourFlowName \
--description yourPolicyDescription \
--type environment \
--comment yourChangeComment \
--api-token yourAPIToken \
--org yourOrgName
`
type createPolicyOptions struct {
payload PolicyPayload
}
type PolicyPayload struct {
Name string `json:"name"`
Description string `json:"description"`
Comment string `json:"comment"`
Type string `json:"type"`
}
func newCreatePolicyCmd(out io.Writer) *cobra.Command {
o := new(createPolicyOptions)
cmd := &cobra.Command{
Use: "policy POLICY-NAME POLICY-FILE-PATH",
Short: createPolicyShortDesc,
Long: createPolicyShortDesc,
Example: createPolicyExample,
Hidden: true,
Args: cobra.ExactArgs(2),
PreRunE: func(cmd *cobra.Command, args []string) error {
err := RequireGlobalFlags(global, []string{"Org", "ApiToken"})
if err != nil {
return ErrorBeforePrintingUsage(cmd, err.Error())
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return o.run(args)
},
}
cmd.Flags().StringVar(&o.payload.Description, "description", "", policyDescriptionFlag)
cmd.Flags().StringVar(&o.payload.Comment, "comment", "", policyCommentFlag)
cmd.Flags().StringVar(&o.payload.Type, "type", "env", policyTypeFlag)
addDryRunFlag(cmd)
return cmd
}
func (o *createPolicyOptions) run(args []string) error {
var reqParams *requests.RequestParams
var url string
o.payload.Name = args[0]
policyFile := args[1]
url = fmt.Sprintf("%s/api/v2/policies/%s", global.Host, global.Org)
form, err := newPolicyForm(o.payload, policyFile)
if err != nil {
return err
}
reqParams = &requests.RequestParams{
Method: http.MethodPut,
URL: url,
Form: form,
DryRun: global.DryRun,
Password: global.ApiToken,
}
res, err := kosliClient.Do(reqParams)
if err == nil && global.DryRun == "false" {
verb := "created"
if res.Resp.StatusCode == 200 {
verb = "updated"
}
logger.Info("policy '%s' was %s", o.payload.Name, verb)
}
return err
}
// newPolicyForm constructs a list of FormItems for a policy with a policy file
// form submission.
func newPolicyForm(payload interface{}, policyFile string) ([]requests.FormItem, error) {
if policyFile == "" {
return []requests.FormItem{}, fmt.Errorf("cannot create a policy form without a policy file")
}
form := []requests.FormItem{
{Type: "field", FieldName: "payload", Content: payload},
{Type: "file", FieldName: "policy_file", Content: policyFile},
}
return form, nil
}