github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/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) Run(args []string) int {
    62  	var name, tokenType string
    63  	var global bool
    64  	var policies []string
    65  	flags := c.Meta.FlagSet("acl token create", 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 no arguments
    79  	args = flags.Args()
    80  	if l := len(args); l != 0 {
    81  		c.Ui.Error(c.Help())
    82  		return 1
    83  	}
    84  
    85  	// Setup the token
    86  	tk := &api.ACLToken{
    87  		Name:     name,
    88  		Type:     tokenType,
    89  		Policies: policies,
    90  		Global:   global,
    91  	}
    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  	// Create the bootstrap token
   101  	token, _, err := client.ACLTokens().Create(tk, nil)
   102  	if err != nil {
   103  		c.Ui.Error(fmt.Sprintf("Error creating token: %s", err))
   104  		return 1
   105  	}
   106  
   107  	// Format the output
   108  	c.Ui.Output(formatKVACLToken(token))
   109  	return 0
   110  }