forked from hashicorp/nomad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
namespace_status.go
143 lines (114 loc) · 3.38 KB
/
namespace_status.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package command
import (
"fmt"
"strings"
"github.com/hashicorp/nomad/api"
"github.com/posener/complete"
)
type NamespaceStatusCommand struct {
Meta
}
func (c *NamespaceStatusCommand) Help() string {
helpText := `
Usage: nomad namespace status [options] <namespace>
Status is used to view the status of a particular namespace.
General Options:
` + generalOptionsUsage()
return strings.TrimSpace(helpText)
}
func (c *NamespaceStatusCommand) AutocompleteFlags() complete.Flags {
return c.Meta.AutocompleteFlags(FlagSetClient)
}
func (c *NamespaceStatusCommand) AutocompleteArgs() complete.Predictor {
return NamespacePredictor(c.Meta.Client, nil)
}
func (c *NamespaceStatusCommand) Synopsis() string {
return "Display a namespace's status"
}
func (c *NamespaceStatusCommand) Name() string { return "namespace status" }
func (c *NamespaceStatusCommand) Run(args []string) int {
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got one arguments
args = flags.Args()
if l := len(args); l != 1 {
c.Ui.Error("This command takes one argument: <namespace>")
c.Ui.Error(commandErrorText(c))
return 1
}
name := 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
}
// Do a prefix lookup
ns, possible, err := getNamespace(client.Namespaces(), name)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving namespaces: %s", err))
return 1
}
if len(possible) != 0 {
c.Ui.Error(fmt.Sprintf("Prefix matched multiple namespaces\n\n%s", formatNamespaces(possible)))
return 1
}
c.Ui.Output(formatNamespaceBasics(ns))
if ns.Quota != "" {
quotas := client.Quotas()
spec, _, err := quotas.Info(ns.Quota, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error retrieving quota spec: %s", err))
return 1
}
// Get the quota usages
usages, failures := quotaUsages(spec, quotas)
// Format the limits
c.Ui.Output(c.Colorize().Color("\n[bold]Quota Limits[reset]"))
c.Ui.Output(formatQuotaLimits(spec, usages))
// Display any failures
if len(failures) != 0 {
c.Ui.Error(c.Colorize().Color("\n[bold][red]Lookup Failures[reset]"))
for region, failure := range failures {
c.Ui.Error(fmt.Sprintf(" * Failed to retrieve quota usage for region %q: %v", region, failure))
return 1
}
}
}
return 0
}
// formatNamespaceBasics formats the basic information of the namespace
func formatNamespaceBasics(ns *api.Namespace) string {
basic := []string{
fmt.Sprintf("Name|%s", ns.Name),
fmt.Sprintf("Description|%s", ns.Description),
fmt.Sprintf("Quota|%s", ns.Quota),
}
return formatKV(basic)
}
func getNamespace(client *api.Namespaces, ns string) (match *api.Namespace, possible []*api.Namespace, err error) {
// Do a prefix lookup
namespaces, _, err := client.PrefixList(ns, nil)
if err != nil {
return nil, nil, err
}
l := len(namespaces)
switch {
case l == 0:
return nil, nil, fmt.Errorf("Namespace %q matched no namespaces", ns)
case l == 1:
return namespaces[0], nil, nil
default:
// search for an exact match in the returned namespaces
for _, namespace := range namespaces {
if namespace.Name == ns {
return namespace, nil, nil
}
}
// if not found, return the fuzzy matches.
return nil, namespaces, nil
}
}