forked from mantl/consul-cli
/
acl_create.go
112 lines (90 loc) · 2.58 KB
/
acl_create.go
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
package commands
import (
"fmt"
consulapi "github.com/hashicorp/consul/api"
"github.com/spf13/cobra"
)
type AclCreateOptions struct {
IsManagement bool
Name string
ConfigRules []*ConfigRule
}
func (a *Acl) AddCreateSub(c *cobra.Command) {
aco := &AclCreateOptions{}
createCmd := &cobra.Command{
Use: "create",
Short: "Create an ACL. Requires a management token.",
Long: "Create an ACL. Requires a management token.",
RunE: func(cmd *cobra.Command, args []string) error {
return a.Create(args, aco)
},
}
oldCreateCmd := &cobra.Command{
Use: "acl-create",
Short: "Create an ACL. Requires a management token.",
Long: "Create an ACL. Requires a management token.",
Deprecated: "Use acl create",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
return a.Create(args, aco)
},
}
createCmd.Flags().BoolVar(&aco.IsManagement, "management", false, "Create a management token")
createCmd.Flags().StringVar(&aco.Name, "name", "", "Name of the ACL")
createCmd.Flags().Var((funcVar)(func(s string) error {
t, err := a.ParseRuleConfig(s)
if err != nil {
return err
}
if aco.ConfigRules == nil {
aco.ConfigRules = make([]*ConfigRule, 0, 1)
}
aco.ConfigRules = append(aco.ConfigRules, t)
return nil
}), "rule", "Rule to create. Can be multiple rules on a command line. Format is type:path:policy")
oldCreateCmd.Flags().BoolVar(&aco.IsManagement, "management", false, "Create a management token")
oldCreateCmd.Flags().StringVar(&aco.Name, "name", "", "Name of the ACL")
oldCreateCmd.Flags().Var((funcVar)(func(s string) error {
t, err := a.ParseRuleConfig(s)
if err != nil {
return err
}
if aco.ConfigRules == nil {
aco.ConfigRules = make([]*ConfigRule, 0, 1)
}
aco.ConfigRules = append(aco.ConfigRules, t)
return nil
}), "rule", "Rule to create. Can be multiple rules on a command line. Format is type:path:policy")
c.AddCommand(createCmd)
a.AddCommand(oldCreateCmd)
}
func (a *Acl) Create(args []string, aco *AclCreateOptions) error {
client, err := a.ACL()
if err != nil {
return err
}
var entry *consulapi.ACLEntry
if aco.IsManagement {
entry = &consulapi.ACLEntry{
Name: aco.Name,
Type: consulapi.ACLManagementType,
}
} else {
rules, err := a.GetRulesString(aco.ConfigRules)
if err != nil {
return err
}
entry = &consulapi.ACLEntry{
Name: aco.Name,
Type: consulapi.ACLClientType,
Rules: rules,
}
}
writeOpts := a.WriteOptions()
id, _, err := client.Create(entry, writeOpts)
if err != nil {
return err
}
fmt.Fprintln(a.Out, id)
return nil
}