Skip to content

Commit

Permalink
Review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
apollo13 committed Jan 11, 2022
1 parent d504449 commit 8e1f152
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 56 deletions.
69 changes: 30 additions & 39 deletions command/namespace_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ func (c *NamespaceApplyCommand) Help() string {
helpText := `
Usage: nomad namespace apply [options] <input>
Apply is used to create or update a namespace. The specification file
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.
Alternatively It takes the namespace name to create or update as its only argument.
Instead of a file, you may instead pass the namespace name to create
or update as the only argument.
If ACLs are enabled, this command requires a management ACL token.
Expand Down Expand Up @@ -100,7 +101,20 @@ func (c *NamespaceApplyCommand) Run(args []string) int {
file := args[0]
var rawNamespace []byte
var err error
var namespace *api.Namespace

// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}

if _, err = os.Stat(file); file == "-" || err == nil {
if quota != nil || description != nil {
c.Ui.Warn("Flags are ignored when a file is specified!")
}

if file == "-" {
rawNamespace, err = ioutil.ReadAll(os.Stdin)
if err != nil {
Expand All @@ -114,39 +128,23 @@ func (c *NamespaceApplyCommand) Run(args []string) int {
return 1
}
}
var spec *api.Namespace
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
}
spec = &jsonSpec
namespace = &jsonSpec
} else {
hclSpec, err := parseNamespaceSpec(rawNamespace)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing quota specification: %s", err))
return 1
}

spec = hclSpec
}
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}

_, err = client.Namespaces().Register(spec, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error applying namespace specification: %s", err))
return 1
namespace = hclSpec
}

c.Ui.Output(fmt.Sprintf("Successfully applied namespace specification %q!", spec.Name))

} else {
name := args[0]

Expand All @@ -156,42 +154,35 @@ func (c *NamespaceApplyCommand) Run(args []string) int {
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
}

// Lookup the given namespace
ns, _, err := client.Namespaces().Info(name, nil)
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 ns == nil {
ns = &api.Namespace{
if namespace == nil {
namespace = &api.Namespace{
Name: name,
}
}

// Add what is set
if description != nil {
ns.Description = *description
namespace.Description = *description
}
if quota != nil {
ns.Quota = *quota
namespace.Quota = *quota
}
}
_, err = client.Namespaces().Register(namespace, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error applying namespace: %s", err))
return 1
}

_, err = client.Namespaces().Register(ns, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error applying namespace: %s", err))
return 1
}
c.Ui.Output(fmt.Sprintf("Successfully applied namespace %q!", namespace.Name))

c.Ui.Output(fmt.Sprintf("Successfully applied namespace %q!", name))
}
return 0
}

Expand Down
18 changes: 3 additions & 15 deletions command/namespace_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,11 @@ func formatNamespaces(namespaces []*api.Namespace) string {
sort.Slice(namespaces, func(i, j int) bool { return namespaces[i].Name < namespaces[j].Name })

rows := make([]string, len(namespaces)+1)
rows[0] = "Name|Description|EnabledDrivers|DisabledDrivers"
rows[0] = "Name|Description"
for i, ns := range namespaces {
enabled_drivers := "*"
disabled_drivers := ""
if ns.Capabilities != nil {
if len(ns.Capabilities.EnabledTaskDrivers) != 0 {
enabled_drivers = strings.Join(ns.Capabilities.EnabledTaskDrivers, ",")
}
if len(ns.Capabilities.DisabledTaskDrivers) != 0 {
disabled_drivers = strings.Join(ns.Capabilities.DisabledTaskDrivers, ",")
}
}
rows[i+1] = fmt.Sprintf("%s|%s|%s|%s",
rows[i+1] = fmt.Sprintf("%s|%s",
ns.Name,
ns.Description,
enabled_drivers,
disabled_drivers)
ns.Description)
}
return formatList(rows)
}
6 changes: 4 additions & 2 deletions nomad/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5015,8 +5015,10 @@ func (n *Namespace) Copy() *Namespace {
nc.Hash = make([]byte, len(n.Hash))
if n.Capabilities != nil {