/
list.go
214 lines (186 loc) · 5.67 KB
/
list.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package resource
import (
"sort"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/names/v5"
"github.com/juju/juju/api/client/resources"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/modelcmd"
coreresources "github.com/juju/juju/core/resources"
)
// ListClient has the API client methods needed by ListCommand.
type ListClient interface {
// ListResources returns info about resources for applications in the model.
ListResources(applications []string) ([]coreresources.ApplicationResources, error)
// Close closes the connection.
Close() error
}
// ListCommand discovers and lists application or unit resources.
type ListCommand struct {
modelcmd.ModelCommandBase
newClient func() (ListClient, error)
details bool
out cmd.Output
target string
}
// NewListCommand returns a new command that lists resources defined
// by a charm.
func NewListCommand() modelcmd.ModelCommand {
c := &ListCommand{}
c.newClient = func() (ListClient, error) {
apiRoot, err := c.NewAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
return resources.NewClient(apiRoot)
}
return modelcmd.Wrap(c)
}
// Info implements cmd.Command.Info.
func (c *ListCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "resources",
Aliases: []string{"list-resources"},
Args: "<application or unit>",
Purpose: "Show the resources for an application or unit.",
SeeAlso: []string{
"attach-resource",
"charm-resources",
},
Doc: `
This command shows the resources required by and those in use by an existing
application or unit in your model. When run for an application, it will also show any
updates available for resources from a store.
`,
})
}
// SetFlags implements cmd.Command.SetFlags.
func (c *ListCommand) SetFlags(f *gnuflag.FlagSet) {
c.ModelCommandBase.SetFlags(f)
const defaultFormat = "tabular"
c.out.AddFlags(f, defaultFormat, map[string]cmd.Formatter{
defaultFormat: FormatAppTabular,
"yaml": cmd.FormatYaml,
"json": cmd.FormatJson,
})
f.BoolVar(&c.details, "details", false, "show detailed information about resources used by each unit.")
}
// Init implements cmd.Command.Init. It will return an error satisfying
// errors.BadRequest if you give it an incorrect number of arguments.
func (c *ListCommand) Init(args []string) error {
if len(args) == 0 {
return errors.NewBadRequest(nil, "missing application or unit name")
}
c.target = args[0]
if err := cmd.CheckEmpty(args[1:]); err != nil {
return errors.NewBadRequest(err, "")
}
return nil
}
// Run implements cmd.Command.Run.
func (c *ListCommand) Run(ctx *cmd.Context) error {
apiclient, err := c.newClient()
if err != nil {
return errors.Trace(err)
}
defer apiclient.Close()
var unit string
var application string
if names.IsValidApplication(c.target) {
application = c.target
} else {
application, err = names.UnitApplication(c.target)
if err != nil {
return errors.Errorf("%q is neither an application nor a unit", c.target)
}
unit = c.target
}
vals, err := apiclient.ListResources([]string{application})
if err != nil {
return errors.Trace(err)
}
if len(vals) != 1 {
return errors.Errorf("bad data returned from server")
}
v := vals[0]
// It's a lot easier to read and to digest a list of resources
// when they are ordered.
sort.Sort(charmResourceList(v.CharmStoreResources))
sort.Sort(resourceList(v.Resources))
for _, u := range v.UnitResources {
sort.Sort(resourceList(u.Resources))
}
if unit == "" {
return c.formatApplicationResources(ctx, v)
}
return c.formatUnitResources(ctx, unit, application, v)
}
const noResources = "No resources to display."
func (c *ListCommand) formatApplicationResources(ctx *cmd.Context, sr coreresources.ApplicationResources) error {
if c.details {
formatted, err := FormatApplicationDetails(sr)
if err != nil {
return errors.Trace(err)
}
if len(formatted.Resources) == 0 && len(formatted.Updates) == 0 {
ctx.Infof(noResources)
return nil
}
return c.out.Write(ctx, formatted)
}
formatted, err := formatApplicationResources(sr)
if err != nil {
return errors.Trace(err)
}
if len(formatted.Resources) == 0 && len(formatted.Updates) == 0 {
ctx.Infof(noResources)
return nil
}
return c.out.Write(ctx, formatted)
}
func (c *ListCommand) formatUnitResources(ctx *cmd.Context, unit, application string, sr coreresources.ApplicationResources) error {
if len(sr.Resources) == 0 && len(sr.UnitResources) == 0 {
ctx.Infof(noResources)
return nil
}
if c.details {
formatted := detailedResources(unit, sr)
return c.out.Write(ctx, FormattedUnitDetails(formatted))
}
resources := unitResources(unit, application, sr)
res := make([]FormattedAppResource, len(sr.Resources))
for i, r := range sr.Resources {
if unitResource, ok := resources[r.ID]; ok {
// Unit has this application resource,
// so use unit's version.
r = unitResource
} else {
// Unit does not have this application resource.
// Have to set it to -1 since revision 0 is still a valid revision.
// All other information is inherited from application resource.
r.Revision = -1
}
res[i] = FormatAppResource(r)
}
return c.out.Write(ctx, res)
}
func unitResources(unit, application string, sr coreresources.ApplicationResources) map[string]coreresources.Resource {
var res []coreresources.Resource
for _, r := range sr.UnitResources {
if r.Tag.Id() == unit {
res = r.Resources
}
}
if len(res) == 0 {
return nil
}
unitResourcesById := make(map[string]coreresources.Resource)
for _, r := range res {
unitResourcesById[r.ID] = r
}
return unitResourcesById
}