forked from juju/juju
/
show_service.go
190 lines (162 loc) · 5.09 KB
/
show_service.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cmd
import (
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/resource"
)
// ShowServiceClient has the API client methods needed by ShowServiceCommand.
type ShowServiceClient interface {
// ListResources returns info about resources for applications in the model.
ListResources(services []string) ([]resource.ServiceResources, error)
// Close closes the connection.
Close() error
}
// ShowServiceDeps is a type that contains external functions that ShowService
// depends on to function.
type ShowServiceDeps struct {
// NewClient returns the value that wraps the API for showing application
// resources from the server.
NewClient func(*ShowServiceCommand) (ShowServiceClient, error)
}
// ShowServiceCommand implements the upload command.
type ShowServiceCommand struct {
modelcmd.ModelCommandBase
details bool
deps ShowServiceDeps
out cmd.Output
target string
}
// NewShowServiceCommand returns a new command that lists resources defined
// by a charm.
func NewShowServiceCommand(deps ShowServiceDeps) *ShowServiceCommand {
return &ShowServiceCommand{deps: deps}
}
// Info implements cmd.Command.Info.
func (c *ShowServiceCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "resources",
Aliases: []string{"list-resources"},
Args: "application-or-unit",
Purpose: "Show the resources for an application or unit.",
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 the charmstore.
`,
}
}
// SetFlags implements cmd.Command.SetFlags.
func (c *ShowServiceCommand) SetFlags(f *gnuflag.FlagSet) {
c.ModelCommandBase.SetFlags(f)
const defaultFormat = "tabular"
c.out.AddFlags(f, defaultFormat, map[string]cmd.Formatter{
defaultFormat: FormatSvcTabular,
"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 *ShowServiceCommand) Init(args []string) error {
if len(args) == 0 {
return errors.NewBadRequest(nil, "missing application 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 *ShowServiceCommand) Run(ctx *cmd.Context) error {
apiclient, err := c.deps.NewClient(c)
if err != nil {
return errors.Annotatef(err, "can't connect to %s", c.ConnectionName())
}
defer apiclient.Close()
var unit string
var service string
if names.IsValidApplication(c.target) {
service = c.target
} else {
service, 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{service})
if err != nil {
return errors.Trace(err)
}
if len(vals) != 1 {
return errors.Errorf("bad data returned from server")
}
v := vals[0]
if unit == "" {
return c.formatServiceResources(ctx, v)
}
return c.formatUnitResources(ctx, unit, service, v)
}
const noResources = "No resources to display."
func (c *ShowServiceCommand) formatServiceResources(ctx *cmd.Context, sr resource.ServiceResources) error {
if c.details {
formatted, err := FormatServiceDetails(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 := formatServiceResources(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 *ShowServiceCommand) formatUnitResources(ctx *cmd.Context, unit, service string, sr resource.ServiceResources) error {
if len(sr.UnitResources) == 0 {
ctx.Infof(noResources)
return nil
}
if c.details {
formatted, err := detailedResources(unit, sr)
if err != nil {
return errors.Trace(err)
}
return c.out.Write(ctx, FormattedUnitDetails(formatted))
}
resources, err := unitResources(unit, service, sr)
if err != nil {
return errors.Trace(err)
}
res := make([]FormattedUnitResource, len(resources))
for i, r := range resources {
res[i] = FormattedUnitResource(FormatSvcResource(r))
}
return c.out.Write(ctx, res)
}
func unitResources(unit, service string, v resource.ServiceResources) ([]resource.Resource, error) {
for _, res := range v.UnitResources {
if res.Tag.Id() == unit {
return res.Resources, nil
}
}
// TODO(natefinch): we need to differentiate between a unit with no
// resources and a unit that doesn't exist. This requires a serverside
// change.
return nil, nil
}