open-nomad/command/namespace_apply.go

129 lines
2.8 KiB
Go
Raw Normal View History

2017-09-07 23:56:15 +00:00
package command
import (
"fmt"
"strings"
"github.com/hashicorp/nomad/api"
flaghelper "github.com/hashicorp/nomad/helper/flags"
2017-09-07 23:56:15 +00:00
"github.com/posener/complete"
)
type NamespaceApplyCommand struct {
Meta
}
func (c *NamespaceApplyCommand) Help() string {
helpText := `
2017-09-19 14:47:10 +00:00
Usage: nomad namespace apply [options] <namespace>
2017-09-07 23:56:15 +00:00
2017-10-13 21:36:02 +00:00
Apply is used to create or update a namespace. It takes the namespace name to
create or update as its only argument.
2017-09-07 23:56:15 +00:00
If ACLs are enabled, this command requires a management ACL token.
2017-09-07 23:56:15 +00:00
General Options:
` + generalOptionsUsage(usageOptsDefault|usageOptsNoNamespace) + `
2017-09-07 23:56:15 +00:00
Apply Options:
2017-10-13 21:36:02 +00:00
-quota
The quota to attach to the namespace.
2017-09-07 23:56:15 +00:00
-description
An optional description for the namespace.
`
return strings.TrimSpace(helpText)
}
func (c *NamespaceApplyCommand) AutocompleteFlags() complete.Flags {
return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
complete.Flags{
"-description": complete.PredictAnything,
2017-10-13 21:36:02 +00:00
"-quota": QuotaPredictor(c.Meta.Client),
2017-09-07 23:56:15 +00:00
})
}
func (c *NamespaceApplyCommand) AutocompleteArgs() complete.Predictor {
2017-10-13 21:36:02 +00:00
return NamespacePredictor(c.Meta.Client, nil)
2017-09-07 23:56:15 +00:00
}
func (c *NamespaceApplyCommand) Synopsis() string {
return "Create or update a namespace"
}
func (c *NamespaceApplyCommand) Name() string { return "namespace apply" }
2017-09-07 23:56:15 +00:00
func (c *NamespaceApplyCommand) Run(args []string) int {
2017-10-13 21:36:02 +00:00
var description, quota *string
2017-09-07 23:56:15 +00:00
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
2017-09-07 23:56:15 +00:00
flags.Usage = func() { c.Ui.Output(c.Help()) }
2017-10-13 21:36:02 +00:00
flags.Var((flaghelper.FuncVar)(func(s string) error {
description = &s
return nil
}), "description", "")
flags.Var((flaghelper.FuncVar)(func(s string) error {
quota = &s
return nil
}), "quota", "")
2017-09-07 23:56:15 +00:00
if err := flags.Parse(args); err != nil {
return 1
}
2017-09-19 14:47:10 +00:00
// Check that we get exactly one argument
2017-09-07 23:56:15 +00:00
args = flags.Args()
2017-09-19 14:47:10 +00:00
if l := len(args); l != 1 {
c.Ui.Error("This command takes one argument: <namespace>")
c.Ui.Error(commandErrorText(c))
2017-09-07 23:56:15 +00:00
return 1
}
2017-09-19 14:47:10 +00:00
name := args[0]
2017-09-07 23:56:15 +00:00
// Validate we have at-least a name
if name == "" {
c.Ui.Error("Namespace name required")
return 1
}
// 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-10-13 21:36:02 +00:00
// Lookup the given namespace
ns, _, err := client.Namespaces().Info(name, nil)
if err != nil && !strings.Contains(err.Error(), "404") {
c.Ui.Error(fmt.Sprintf("Error looking up namespace: %s", err))
return 1
}
if ns == nil {
ns = &api.Namespace{
Name: name,
}
}
// Add what is set
if description != nil {
ns.Description = *description
}
if quota != nil {
ns.Quota = *quota
2017-09-07 23:56:15 +00:00
}
_, err = client.Namespaces().Register(ns, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error applying namespace: %s", err))
return 1
}
2017-09-19 14:47:10 +00:00
c.Ui.Output(fmt.Sprintf("Successfully applied namespace %q!", name))
2017-09-07 23:56:15 +00:00
return 0
}