open-nomad/command/acl_policy_delete.go

80 lines
1.7 KiB
Go
Raw Normal View History

2017-09-17 04:36:08 +00:00
package command
import (
"fmt"
"strings"
"github.com/posener/complete"
)
type ACLPolicyDeleteCommand struct {
Meta
}
func (c *ACLPolicyDeleteCommand) Help() string {
helpText := `
2017-09-17 05:15:25 +00:00
Usage: nomad acl policy delete <name>
2017-09-17 04:36:08 +00:00
2017-10-13 21:36:02 +00:00
Delete is used to delete an existing ACL policy.
2017-09-17 04:36:08 +00:00
This command requires a management ACL token.
2017-09-17 04:36:08 +00:00
General Options:
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace)
2017-09-17 04:36:08 +00:00
return strings.TrimSpace(helpText)
}
func (c *ACLPolicyDeleteCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{})
}
func (c *ACLPolicyDeleteCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictNothing
}
func (c *ACLPolicyDeleteCommand) Synopsis() string {
return "Delete an existing ACL policy"
}
func (c *ACLPolicyDeleteCommand) Name() string { return "acl policy delete" }
2017-09-17 04:36:08 +00:00
func (c *ACLPolicyDeleteCommand) Run(args []string) int {
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
2017-09-17 04:36:08 +00:00
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got exactly one argument
args = flags.Args()
if l := len(args); l != 1 {
c.Ui.Error("This command takes one argument: <name>")
c.Ui.Error(commandErrorText(c))
2017-09-17 04:36:08 +00:00
return 1
}
// Get the policy name
policyName := args[0]
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
2017-09-17 05:15:25 +00:00
// Delete the policy
2017-09-17 04:36:08 +00:00
_, err = client.ACLPolicies().Delete(policyName, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error deleting ACL policy: %s", err))
return 1
}
c.Ui.Output(fmt.Sprintf("Successfully deleted %s policy!",
policyName))
return 0
}