github.com/hernad/nomad@v1.6.112/command/acl_role_create.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/hernad/nomad/api"
    11  	"github.com/mitchellh/cli"
    12  	"github.com/posener/complete"
    13  )
    14  
    15  // Ensure ACLRoleCreateCommand satisfies the cli.Command interface.
    16  var _ cli.Command = &ACLRoleCreateCommand{}
    17  
    18  // ACLRoleCreateCommand implements cli.Command.
    19  type ACLRoleCreateCommand struct {
    20  	Meta
    21  
    22  	name        string
    23  	description string
    24  	policyNames []string
    25  	json        bool
    26  	tmpl        string
    27  }
    28  
    29  // Help satisfies the cli.Command Help function.
    30  func (a *ACLRoleCreateCommand) Help() string {
    31  	helpText := `
    32  Usage: nomad acl role create [options]
    33  
    34    Create is used to create new ACL roles. Use requires a management token.
    35  
    36  General Options:
    37  
    38    ` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
    39  
    40  ACL Create Options:
    41  
    42    -name
    43      Sets the human readable name for the ACL role. The name must be between
    44      1-128 characters and is a required parameter.
    45  
    46    -description
    47      A free form text description of the role that must not exceed 256
    48      characters.
    49  
    50    -policy
    51      Specifies a policy to associate with the role identified by their name. This
    52      flag can be specified multiple times and must be specified at least once.
    53  
    54    -json
    55      Output the ACL role in a JSON format.
    56  
    57    -t
    58      Format and display the ACL role using a Go template.
    59  `
    60  	return strings.TrimSpace(helpText)
    61  }
    62  
    63  func (a *ACLRoleCreateCommand) AutocompleteFlags() complete.Flags {
    64  	return mergeAutocompleteFlags(a.Meta.AutocompleteFlags(FlagSetClient),
    65  		complete.Flags{
    66  			"-name":        complete.PredictAnything,
    67  			"-description": complete.PredictAnything,
    68  			"-policy":      complete.PredictAnything,
    69  			"-json":        complete.PredictNothing,
    70  			"-t":           complete.PredictAnything,
    71  		})
    72  }
    73  
    74  func (a *ACLRoleCreateCommand) AutocompleteArgs() complete.Predictor { return complete.PredictNothing }
    75  
    76  // Synopsis satisfies the cli.Command Synopsis function.
    77  func (a *ACLRoleCreateCommand) Synopsis() string { return "Create a new ACL role" }
    78  
    79  // Name returns the name of this command.
    80  func (a *ACLRoleCreateCommand) Name() string { return "acl role create" }
    81  
    82  // Run satisfies the cli.Command Run function.
    83  func (a *ACLRoleCreateCommand) Run(args []string) int {
    84  
    85  	flags := a.Meta.FlagSet(a.Name(), FlagSetClient)
    86  	flags.Usage = func() { a.Ui.Output(a.Help()) }
    87  	flags.StringVar(&a.name, "name", "", "")
    88  	flags.StringVar(&a.description, "description", "", "")
    89  	flags.Var((funcVar)(func(s string) error {
    90  		a.policyNames = append(a.policyNames, s)
    91  		return nil
    92  	}), "policy", "")
    93  	flags.BoolVar(&a.json, "json", false, "")
    94  	flags.StringVar(&a.tmpl, "t", "", "")
    95  	if err := flags.Parse(args); err != nil {
    96  		return 1
    97  	}
    98  
    99  	// Check that we got no arguments.
   100  	if len(flags.Args()) != 0 {
   101  		a.Ui.Error("This command takes no arguments")
   102  		a.Ui.Error(commandErrorText(a))
   103  		return 1
   104  	}
   105  
   106  	// Perform some basic validation on the submitted role information to avoid
   107  	// sending API and RPC requests which will fail basic validation.
   108  	if a.name == "" {
   109  		a.Ui.Error("ACL role name must be specified using the -name flag")
   110  		return 1
   111  	}
   112  	if len(a.policyNames) < 1 {
   113  		a.Ui.Error("At least one policy name must be specified using the -policy flag")
   114  		return 1
   115  	}
   116  
   117  	// Set up the ACL with the passed parameters.
   118  	aclRole := api.ACLRole{
   119  		Name:        a.name,
   120  		Description: a.description,
   121  		Policies:    aclRolePolicyNamesToPolicyLinks(a.policyNames),
   122  	}
   123  
   124  	// Get the HTTP client.
   125  	client, err := a.Meta.Client()
   126  	if err != nil {
   127  		a.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
   128  		return 1
   129  	}
   130  
   131  	// Create the ACL role via the API.
   132  	role, _, err := client.ACLRoles().Create(&aclRole, nil)
   133  	if err != nil {
   134  		a.Ui.Error(fmt.Sprintf("Error creating ACL role: %s", err))
   135  		return 1
   136  	}
   137  
   138  	if a.json || len(a.tmpl) > 0 {
   139  		out, err := Format(a.json, a.tmpl, role)
   140  		if err != nil {
   141  			a.Ui.Error(err.Error())
   142  			return 1
   143  		}
   144  
   145  		a.Ui.Output(out)
   146  		return 0
   147  	}
   148  
   149  	a.Ui.Output(formatACLRole(role))
   150  	return 0
   151  }