github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/command/acl_token_update.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/posener/complete" 8 ) 9 10 type ACLTokenUpdateCommand struct { 11 Meta 12 } 13 14 func (c *ACLTokenUpdateCommand) Help() string { 15 helpText := ` 16 Usage: nomad acl token update <token_accessor_id> 17 18 Update is used to update an existing ACL token. Requires a management token. 19 20 General Options: 21 22 ` + generalOptionsUsage() + ` 23 24 Update 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 43 func (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 53 func (c *ACLTokenUpdateCommand) AutocompleteArgs() complete.Predictor { 54 return complete.PredictNothing 55 } 56 57 func (c *ACLTokenUpdateCommand) Synopsis() string { 58 return "Update an existing ACL token" 59 } 60 61 func (c *ACLTokenUpdateCommand) Run(args []string) int { 62 var name, tokenType string 63 var global bool 64 var policies []string 65 flags := c.Meta.FlagSet("acl token update", FlagSetClient) 66 flags.Usage = func() { c.Ui.Output(c.Help()) } 67 flags.StringVar(&name, "name", "", "") 68 flags.StringVar(&tokenType, "type", "client", "") 69 flags.BoolVar(&global, "global", false, "") 70 flags.Var((funcVar)(func(s string) error { 71 policies = append(policies, s) 72 return nil 73 }), "policy", "") 74 if err := flags.Parse(args); err != nil { 75 return 1 76 } 77 78 // Check that we got exactly one argument 79 args = flags.Args() 80 if l := len(args); l != 1 { 81 c.Ui.Error(c.Help()) 82 return 1 83 } 84 85 tokenAccessorID := args[0] 86 87 // Get the HTTP client 88 client, err := c.Meta.Client() 89 if err != nil { 90 c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err)) 91 return 1 92 } 93 94 // Get the specified token 95 token, _, err := client.ACLTokens().Info(tokenAccessorID, nil) 96 if err != nil { 97 c.Ui.Error(fmt.Sprintf("Error fetching token: %s", err)) 98 return 1 99 } 100 101 // Create the updated token 102 if name != "" { 103 token.Name = name 104 } 105 106 if tokenType != "" { 107 token.Type = tokenType 108 } 109 110 // This will default to false if the user does not specify it 111 if global != token.Global { 112 token.Global = global 113 } 114 115 if len(policies) != 0 { 116 token.Policies = policies 117 } 118 119 // Update the token 120 updatedToken, _, err := client.ACLTokens().Update(token, nil) 121 if err != nil { 122 c.Ui.Error(fmt.Sprintf("Error updating token: %s", err)) 123 return 1 124 } 125 126 // Format the output 127 c.Ui.Output(formatKVACLToken(updatedToken)) 128 return 0 129 }