github.com/adityamillind98/nomad@v0.11.8/command/acl_token_create.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/hashicorp/nomad/api"
     8  	"github.com/posener/complete"
     9  )
    10  
    11  type ACLTokenCreateCommand struct {
    12  	Meta
    13  }
    14  
    15  func (c *ACLTokenCreateCommand) Help() string {
    16  	helpText := `
    17  Usage: nomad acl token create [options]
    18  
    19    Create is used to issue new ACL tokens. Requires a management token.
    20  
    21  General Options:
    22  
    23    ` + generalOptionsUsage() + `
    24  
    25  Create Options:
    26  
    27    -name=""
    28      Sets the human readable name for the ACL token.
    29  
    30    -type="client"
    31      Sets the type of token. Must be one of "client" (default), or "management".
    32  
    33    -global=false
    34      Toggles the global mode of the token. Global tokens are replicated to all regions.
    35  
    36    -policy=""
    37      Specifies a policy to associate with the token. Can be specified multiple times,
    38      but only with client type tokens.
    39  `
    40  	return strings.TrimSpace(helpText)
    41  }
    42  
    43  func (c *ACLTokenCreateCommand) 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 *ACLTokenCreateCommand) AutocompleteArgs() complete.Predictor {
    54  	return complete.PredictNothing
    55  }
    56  
    57  func (c *ACLTokenCreateCommand) Synopsis() string {
    58  	return "Create a new ACL token"
    59  }
    60  
    61  func (c *ACLTokenCreateCommand) Name() string { return "acl token create" }
    62  
    63  func (c *ACLTokenCreateCommand) 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 no arguments
    81  	args = flags.Args()
    82  	if l := len(args); l != 0 {
    83  		c.Ui.Error("This command takes no arguments")
    84  		c.Ui.Error(commandErrorText(c))
    85  		return 1
    86  	}
    87  
    88  	// Setup the token
    89  	tk := &api.ACLToken{
    90  		Name:     name,
    91  		Type:     tokenType,
    92  		Policies: policies,
    93  		Global:   global,
    94  	}
    95  
    96  	// Get the HTTP client
    97  	client, err := c.Meta.Client()
    98  	if err != nil {
    99  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
   100  		return 1
   101  	}
   102  
   103  	// Create the bootstrap token
   104  	token, _, err := client.ACLTokens().Create(tk, nil)
   105  	if err != nil {
   106  		c.Ui.Error(fmt.Sprintf("Error creating token: %s", err))
   107  		return 1
   108  	}
   109  
   110  	// Format the output
   111  	c.Ui.Output(formatKVACLToken(token))
   112  	return 0
   113  }