github.com/ferranbt/nomad@v0.9.3-0.20190607002617-85c449b7667c/command/acl_policy_apply.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/nomad/api"
    10  	"github.com/posener/complete"
    11  )
    12  
    13  type ACLPolicyApplyCommand struct {
    14  	Meta
    15  }
    16  
    17  func (c *ACLPolicyApplyCommand) Help() string {
    18  	helpText := `
    19  Usage: nomad acl policy apply [options] <name> <path>
    20  
    21    Apply is used to create or update an ACL policy. The policy is
    22    sourced from <path> or from stdin if path is "-".
    23  
    24  General Options:
    25  
    26    ` + generalOptionsUsage() + `
    27  
    28  Apply Options:
    29  
    30    -description
    31      Specifies a human readable description for the policy.
    32  
    33  `
    34  	return strings.TrimSpace(helpText)
    35  }
    36  
    37  func (c *ACLPolicyApplyCommand) AutocompleteFlags() complete.Flags {
    38  	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
    39  		complete.Flags{})
    40  }
    41  
    42  func (c *ACLPolicyApplyCommand) AutocompleteArgs() complete.Predictor {
    43  	return complete.PredictNothing
    44  }
    45  
    46  func (c *ACLPolicyApplyCommand) Synopsis() string {
    47  	return "Create or update an ACL policy"
    48  }
    49  
    50  func (c *ACLPolicyApplyCommand) Name() string { return "acl policy apply" }
    51  
    52  func (c *ACLPolicyApplyCommand) Run(args []string) int {
    53  	var description string
    54  	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
    55  	flags.Usage = func() { c.Ui.Output(c.Help()) }
    56  	flags.StringVar(&description, "description", "", "")
    57  	if err := flags.Parse(args); err != nil {
    58  		return 1
    59  	}
    60  
    61  	// Check that we got two arguments
    62  	args = flags.Args()
    63  	if l := len(args); l != 2 {
    64  		c.Ui.Error("This command takes two arguments: <name> and <path>")
    65  		c.Ui.Error(commandErrorText(c))
    66  		return 1
    67  	}
    68  
    69  	// Get the policy name
    70  	policyName := args[0]
    71  
    72  	// Read the file contents
    73  	file := args[1]
    74  	var rawPolicy []byte
    75  	var err error
    76  	if file == "-" {
    77  		rawPolicy, err = ioutil.ReadAll(os.Stdin)
    78  		if err != nil {
    79  			c.Ui.Error(fmt.Sprintf("Failed to read stdin: %v", err))
    80  			return 1
    81  		}
    82  	} else {
    83  		rawPolicy, err = ioutil.ReadFile(file)
    84  		if err != nil {
    85  			c.Ui.Error(fmt.Sprintf("Failed to read file: %v", err))
    86  			return 1
    87  		}
    88  	}
    89  
    90  	// Construct the policy
    91  	ap := &api.ACLPolicy{
    92  		Name:        policyName,
    93  		Description: description,
    94  		Rules:       string(rawPolicy),
    95  	}
    96  
    97  	// Get the HTTP client
    98  	client, err := c.Meta.Client()
    99  	if err != nil {
   100  		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
   101  		return 1
   102  	}
   103  
   104  	// Upsert the policy
   105  	_, err = client.ACLPolicies().Upsert(ap, nil)
   106  	if err != nil {
   107  		c.Ui.Error(fmt.Sprintf("Error writing ACL policy: %s", err))
   108  		return 1
   109  	}
   110  
   111  	c.Ui.Output(fmt.Sprintf("Successfully wrote %q ACL policy!",
   112  		policyName))
   113  	return 0
   114  }