-
Notifications
You must be signed in to change notification settings - Fork 2k
/
acl_policy_apply.go
111 lines (90 loc) · 2.31 KB
/
acl_policy_apply.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
package command
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/hashicorp/nomad/api"
"github.com/posener/complete"
)
type ACLPolicyApplyCommand struct {
Meta
}
func (c *ACLPolicyApplyCommand) Help() string {
helpText := `
Usage: nomad acl policy apply [options] <name> <path>
Apply is used to create or update an ACL policy. The policy is
sourced from <path> or from stdin if path is "-".
General Options:
` + generalOptionsUsage() + `
Apply Options:
-description
Specifies a human readable description for the policy.
`
return strings.TrimSpace(helpText)
}
func (c *ACLPolicyApplyCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{})
}
func (c *ACLPolicyApplyCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
func (c *ACLPolicyApplyCommand) Synopsis() string {
return "Create or update an ACL policy"
}
func (c *ACLPolicyApplyCommand) Run(args []string) int {
var description string
flags := c.Meta.FlagSet("acl policy apply", FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
flags.StringVar(&description, "description", "", "")
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got two arguments
args = flags.Args()
if l := len(args); l != 2 {
c.Ui.Error(c.Help())
return 1
}
// Get the policy name
policyName := args[0]
// Read the file contents
file := args[1]
var rawPolicy []byte
var err error
if file == "-" {
rawPolicy, err = ioutil.ReadAll(os.Stdin)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to read stdin: %v", err))
return 1
}
} else {
rawPolicy, err = ioutil.ReadFile(file)
if err != nil {
c.Ui.Error(fmt.Sprintf("Failed to read file: %v", err))
return 1
}
}
// Construct the policy
ap := &api.ACLPolicy{
Name: policyName,
Description: description,
Rules: string(rawPolicy),
}
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
// Upsert the policy
_, err = client.ACLPolicies().Upsert(ap, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error writing ACL policy: %s", err))
return 1
}
c.Ui.Output(fmt.Sprintf("Successfully wrote %q ACL policy!",
policyName))
return 0
}