1package command
2
3import (
4	"fmt"
5	"strings"
6
7	"github.com/hashicorp/nomad/api"
8	flaghelper "github.com/hashicorp/nomad/helper/flag-helpers"
9	"github.com/posener/complete"
10)
11
12type NamespaceApplyCommand struct {
13	Meta
14}
15
16func (c *NamespaceApplyCommand) Help() string {
17	helpText := `
18Usage: nomad namespace apply [options] <namespace>
19
20  Apply is used to create or update a namespace. It takes the namespace name to
21  create or update as its only argument.
22
23General Options:
24
25  ` + generalOptionsUsage() + `
26
27Apply Options:
28
29  -quota
30    The quota to attach to the namespace.
31
32  -description
33    An optional description for the namespace.
34`
35	return strings.TrimSpace(helpText)
36}
37
38func (c *NamespaceApplyCommand) AutocompleteFlags() complete.Flags {
39	return mergeAutocompleteFlags(c.Meta.AutocompleteFlags(FlagSetClient),
40		complete.Flags{
41			"-description": complete.PredictAnything,
42			"-quota":       QuotaPredictor(c.Meta.Client),
43		})
44}
45
46func (c *NamespaceApplyCommand) AutocompleteArgs() complete.Predictor {
47	return NamespacePredictor(c.Meta.Client, nil)
48}
49
50func (c *NamespaceApplyCommand) Synopsis() string {
51	return "Create or update a namespace"
52}
53
54func (c *NamespaceApplyCommand) Name() string { return "namespace apply" }
55
56func (c *NamespaceApplyCommand) Run(args []string) int {
57	var description, quota *string
58
59	flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
60	flags.Usage = func() { c.Ui.Output(c.Help()) }
61	flags.Var((flaghelper.FuncVar)(func(s string) error {
62		description = &s
63		return nil
64	}), "description", "")
65	flags.Var((flaghelper.FuncVar)(func(s string) error {
66		quota = &s
67		return nil
68	}), "quota", "")
69
70	if err := flags.Parse(args); err != nil {
71		return 1
72	}
73
74	// Check that we get exactly one argument
75	args = flags.Args()
76	if l := len(args); l != 1 {
77		c.Ui.Error("This command takes one argument: <namespace>")
78		c.Ui.Error(commandErrorText(c))
79		return 1
80	}
81
82	name := args[0]
83
84	// Validate we have at-least a name
85	if name == "" {
86		c.Ui.Error("Namespace name required")
87		return 1
88	}
89
90	// Get the HTTP client
91	client, err := c.Meta.Client()
92	if err != nil {
93		c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
94		return 1
95	}
96
97	// Lookup the given namespace
98	ns, _, err := client.Namespaces().Info(name, nil)
99	if err != nil && !strings.Contains(err.Error(), "404") {
100		c.Ui.Error(fmt.Sprintf("Error looking up namespace: %s", err))
101		return 1
102	}
103
104	if ns == nil {
105		ns = &api.Namespace{
106			Name: name,
107		}
108	}
109
110	// Add what is set
111	if description != nil {
112		ns.Description = *description
113	}
114	if quota != nil {
115		ns.Quota = *quota
116	}
117
118	_, err = client.Namespaces().Register(ns, nil)
119	if err != nil {
120		c.Ui.Error(fmt.Sprintf("Error applying namespace: %s", err))
121		return 1
122	}
123
124	c.Ui.Output(fmt.Sprintf("Successfully applied namespace %q!", name))
125	return 0
126}
127