2017-09-07 23:56:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2022-02-24 14:27:32 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2017-09-07 23:56:15 +00:00
|
|
|
"fmt"
|
2022-02-24 14:27:32 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-09-07 23:56:15 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-02-24 14:27:32 +00:00
|
|
|
"github.com/hashicorp/hcl"
|
|
|
|
"github.com/hashicorp/hcl/hcl/ast"
|
2017-09-07 23:56:15 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2020-12-14 16:07:27 +00:00
|
|
|
flaghelper "github.com/hashicorp/nomad/helper/flags"
|
2022-02-24 14:27:32 +00:00
|
|
|
"github.com/mitchellh/mapstructure"
|
2017-09-07 23:56:15 +00:00
|
|
|
"github.com/posener/complete"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NamespaceApplyCommand struct {
|
|
|
|
Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NamespaceApplyCommand) Help() string {
|
|
|
|
helpText := `
|
2022-02-24 14:27:32 +00:00
|
|
|
Usage: nomad namespace apply [options] <input>
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2022-02-24 14:27:32 +00:00
|
|
|
Apply is used to create or update a namespace. The specification file
|
|
|
|
will be read from stdin by specifying "-", otherwise a path to the file is
|
|
|
|
expected.
|
2022-04-21 18:53:45 +00:00
|
|
|
|
2022-02-24 14:27:32 +00:00
|
|
|
Instead of a file, you may instead pass the namespace name to create
|
|
|
|
or update as the only argument.
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2020-11-19 21:38:08 +00:00
|
|
|
If ACLs are enabled, this command requires a management ACL token.
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
General Options:
|
|
|
|
|
2020-11-19 16:15:23 +00:00
|
|
|
` + 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.
|
2022-02-24 14:27:32 +00:00
|
|
|
|
|
|
|
-json
|
|
|
|
Parse the input as a JSON namespace specification.
|
2017-09-07 23:56:15 +00:00
|
|
|
`
|
|
|
|
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),
|
2022-02-24 14:27:32 +00:00
|
|
|
"-json": complete.PredictNothing,
|
2017-09-07 23:56:15 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NamespaceApplyCommand) AutocompleteArgs() complete.Predictor {
|
2022-03-04 20:58:17 +00:00
|
|
|
return complete.PredictOr(
|
|
|
|
NamespacePredictor(c.Meta.Client, nil),
|
|
|
|
complete.PredictFiles("*.hcl"),
|
|
|
|
complete.PredictFiles("*.json"),
|
|
|
|
)
|
2017-09-07 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *NamespaceApplyCommand) Synopsis() string {
|
|
|
|
return "Create or update a namespace"
|
|
|
|
}
|
|
|
|
|
2018-04-18 16:02:11 +00:00
|
|
|
func (c *NamespaceApplyCommand) Name() string { return "namespace apply" }
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
func (c *NamespaceApplyCommand) Run(args []string) int {
|
2022-02-24 14:27:32 +00:00
|
|
|
var jsonInput bool
|
2017-10-13 21:36:02 +00:00
|
|
|
var description, quota *string
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2018-04-18 16:02:11 +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", "")
|
2022-02-24 14:27:32 +00:00
|
|
|
flags.BoolVar(&jsonInput, "json", false, "")
|
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 {
|
2022-02-24 14:27:32 +00:00
|
|
|
c.Ui.Error("This command takes one argument: <input>")
|
2018-04-18 16:02:11 +00:00
|
|
|
c.Ui.Error(commandErrorText(c))
|
2017-09-07 23:56:15 +00:00
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-02-24 14:27:32 +00:00
|
|
|
file := args[0]
|
|
|
|
var rawNamespace []byte
|
|
|
|
var err error
|
|
|
|
var namespace *api.Namespace
|
2017-09-07 23:56:15 +00:00
|
|
|
|
|
|
|
// Get the HTTP client
|
|
|
|
client, err := c.Meta.Client()
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-04-21 18:53:45 +00:00
|
|
|
if fi, err := os.Stat(file); (file == "-" || err == nil) && !fi.IsDir() {
|
2022-02-24 14:27:32 +00:00
|
|
|
if quota != nil || description != nil {
|
|
|
|
c.Ui.Warn("Flags are ignored when a file is specified!")
|
|
|
|
}
|
2017-10-13 21:36:02 +00:00
|
|
|
|
2022-02-24 14:27:32 +00:00
|
|
|
if file == "-" {
|
|
|
|
rawNamespace, err = ioutil.ReadAll(os.Stdin)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to read stdin: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rawNamespace, err = ioutil.ReadFile(file)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to read file: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
2017-10-13 21:36:02 +00:00
|
|
|
}
|
2022-02-24 14:27:32 +00:00
|
|
|
if jsonInput {
|
|
|
|
var jsonSpec api.Namespace
|
|
|
|
dec := json.NewDecoder(bytes.NewBuffer(rawNamespace))
|
|
|
|
if err := dec.Decode(&jsonSpec); err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Failed to parse quota: %v", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
namespace = &jsonSpec
|
|
|
|
} else {
|
|
|
|
hclSpec, err := parseNamespaceSpec(rawNamespace)
|
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error parsing quota specification: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace = hclSpec
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
name := args[0]
|
2017-10-13 21:36:02 +00:00
|
|
|
|
2022-02-24 14:27:32 +00:00
|
|
|
// Validate we have at-least a name
|
|
|
|
if name == "" {
|
|
|
|
c.Ui.Error("Namespace name required")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup the given namespace
|
|
|
|
namespace, _, 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 namespace == nil {
|
|
|
|
namespace = &api.Namespace{
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
}
|
2017-09-07 23:56:15 +00:00
|
|
|
|
2022-02-24 14:27:32 +00:00
|
|
|
// Add what is set
|
|
|
|
if description != nil {
|
|
|
|
namespace.Description = *description
|
|
|
|
}
|
|
|
|
if quota != nil {
|
|
|
|
namespace.Quota = *quota
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = client.Namespaces().Register(namespace, nil)
|
2017-09-07 23:56:15 +00:00
|
|
|
if err != nil {
|
|
|
|
c.Ui.Error(fmt.Sprintf("Error applying namespace: %s", err))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-02-24 14:27:32 +00:00
|
|
|
c.Ui.Output(fmt.Sprintf("Successfully applied namespace %q!", namespace.Name))
|
|
|
|
|
2017-09-07 23:56:15 +00:00
|
|
|
return 0
|
|
|
|
}
|
2022-02-24 14:27:32 +00:00
|
|
|
|
|
|
|
// parseNamespaceSpec is used to parse the namespace specification from HCL
|
|
|
|
func parseNamespaceSpec(input []byte) (*api.Namespace, error) {
|
|
|
|
root, err := hcl.ParseBytes(input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Top-level item should be a list
|
|
|
|
list, ok := root.Node.(*ast.ObjectList)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("error parsing: root should be an object")
|
|
|
|
}
|
|
|
|
|
|
|
|
var spec api.Namespace
|
|
|
|
if err := parseNamespaceSpecImpl(&spec, list); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &spec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseNamespaceSpec parses the quota namespace taking as input the AST tree
|
|
|
|
func parseNamespaceSpecImpl(result *api.Namespace, list *ast.ObjectList) error {
|
|
|
|
// Decode the full thing into a map[string]interface for ease
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, list); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(m, "capabilities")
|
2022-02-27 08:09:10 +00:00
|
|
|
delete(m, "meta")
|
2022-02-24 14:27:32 +00:00
|
|
|
|
|
|
|
// Decode the rest
|
|
|
|
if err := mapstructure.WeakDecode(m, result); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cObj := list.Filter("capabilities")
|
|
|
|
if len(cObj.Items) > 0 {
|
|
|
|
for _, o := range cObj.Elem().Items {
|
|
|
|
ot, ok := o.Val.(*ast.ObjectType)
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
var opts *api.NamespaceCapabilities
|
|
|
|
if err := hcl.DecodeObject(&opts, ot.List); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
result.Capabilities = opts
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 08:09:10 +00:00
|
|
|
if metaO := list.Filter("meta"); len(metaO.Items) > 0 {
|
|
|
|
for _, o := range metaO.Elem().Items {
|
|
|
|
var m map[string]interface{}
|
|
|
|
if err := hcl.DecodeObject(&m, o.Val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := mapstructure.WeakDecode(m, &result.Meta); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 14:27:32 +00:00
|
|
|
return nil
|
|
|
|
}
|