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