1package command
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/posener/complete"
8)
9
10type ACLTokenUpdateCommand struct {
11	Meta
12}
13
14func (c *ACLTokenUpdateCommand) Help() string {
15	helpText := `
16Usage: nomad acl token update <token_accessor_id>
17
18  Update is used to update an existing ACL token. Requires a management token.
19
20General Options:
21
22  ` + generalOptionsUsage() + `
23
24Update Options:
25
26  -name=""
27    Sets the human readable name for the ACL token.
28
29  -type="client"
30    Sets the type of token. Must be one of "client" (default), or "management".
31
32  -global=false
33    Toggles the global mode of the token. Global tokens are replicated to all regions.
34
35  -policy=""
36    Specifies a policy to associate with the token. Can be specified multiple times,
37    but only with client type tokens.
38`
39
40	return strings.TrimSpace(helpText)
41}
42
43func (c *ACLTokenUpdateCommand) AutocompleteFlags() complete.Flags {
44	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
45		complete.Flags{
46			"name":   complete.PredictAnything,
47			"type":   complete.PredictAnything,
48			"global": complete.PredictNothing,
49			"policy": complete.PredictAnything,
50		})
51}
52
53func (c *ACLTokenUpdateCommand) AutocompleteArgs() complete.Predictor {
54	return complete.PredictNothing
55}
56
57func (c *ACLTokenUpdateCommand) Synopsis() string {
58	return "Update an existing ACL token"
59}
60
61func (*ACLTokenUpdateCommand) Name() string { return "acl token update" }
62
63func (c *ACLTokenUpdateCommand) Run(args []string) int {
64	var name, tokenType string
65	var global bool
66	var policies []string
67	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
68	flags.Usage = func() { c.Ui.Output(c.Help()) }
69	flags.StringVar(&name, "name", "", "")
70	flags.StringVar(&tokenType, "type", "client", "")
71	flags.BoolVar(&global, "global", false, "")
72	flags.Var((funcVar)(func(s string) error {
73		policies = append(policies, s)
74		return nil
75	}), "policy", "")
76	if err := flags.Parse(args); err != nil {
77		return 1
78	}
79
80	// Check that we got exactly one argument
81	args = flags.Args()
82	if l := len(args); l != 1 {
83		c.Ui.Error("This command takes one argument: <token_accessor_id>")
84		c.Ui.Error(commandErrorText(c))
85		return 1
86	}
87
88	tokenAccessorID := args[0]
89
90	// Get the HTTP client
91	client, err := c.Meta.Client()
92	if err != nil {
93		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
94		return 1
95	}
96
97	// Get the specified token
98	token, _, err := client.ACLTokens().Info(tokenAccessorID, nil)
99	if err != nil {
100		c.Ui.Error(fmt.Sprintf("Error fetching token: %s", err))
101		return 1
102	}
103
104	// Create the updated token
105	if name != "" {
106		token.Name = name
107	}
108
109	if tokenType != "" {
110		token.Type = tokenType
111	}
112
113	// This will default to false if the user does not specify it
114	if global != token.Global {
115		token.Global = global
116	}
117
118	if len(policies) != 0 {
119		token.Policies = policies
120	}
121
122	// Update the token
123	updatedToken, _, err := client.ACLTokens().Update(token, nil)
124	if err != nil {
125		c.Ui.Error(fmt.Sprintf("Error updating token: %s", err))
126		return 1
127	}
128
129	// Format the output
130	c.Ui.Output(formatKVACLToken(updatedToken))
131	return 0
132}
133