forked from juju/juju
/
list.go
258 lines (226 loc) · 6.44 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package block
import (
"fmt"
"io"
"sort"
"strings"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/api"
"github.com/juju/juju/api/controller"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/cmd/output"
)
// NewListCommand returns the command that lists the disabled
// commands for the model.
func NewListCommand() cmd.Command {
return modelcmd.Wrap(&listCommand{
apiFunc: func(c newAPIRoot) (blockListAPI, error) {
return getBlockAPI(c)
},
controllerAPIFunc: func(c newControllerAPIRoot) (controllerListAPI, error) {
return getControllerAPI(c)
},
})
}
const listCommandDoc = `
List disabled commands for the model.
` + commandSets + `
See also:
disable-command
enable-command
`
// listCommand list blocks.
type listCommand struct {
modelcmd.ModelCommandBase
apiFunc func(newAPIRoot) (blockListAPI, error)
controllerAPIFunc func(newControllerAPIRoot) (controllerListAPI, error)
all bool
out cmd.Output
}
// Init implements Command.Init.
func (c *listCommand) Init(args []string) (err error) {
return cmd.CheckEmpty(args)
}
// Info implements Command.Info.
func (c *listCommand) Info() *cmd.Info {
return &cmd.Info{
Name: "disabled-commands",
Purpose: "List disabled commands.",
Doc: listCommandDoc,
Aliases: []string{"list-disabled-commands"},
}
}
// SetFlags implements Command.SetFlags.
func (c *listCommand) SetFlags(f *gnuflag.FlagSet) {
c.ModelCommandBase.SetFlags(f)
f.BoolVar(&c.all, "all", false, "Lists for all models (administrative users only)")
c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
"json": cmd.FormatJson,
"tabular": c.formatter,
})
}
// Run implements Command.Run.
func (c *listCommand) Run(ctx *cmd.Context) (err error) {
if c.all {
return c.listForController(ctx)
}
return c.listForModel(ctx)
}
const noBlocks = "No commands are currently disabled."
func (c *listCommand) listForModel(ctx *cmd.Context) (err error) {
api, err := c.apiFunc(c)
if err != nil {
return errors.Trace(err)
}
defer api.Close()
result, err := api.List()
if err != nil {
return errors.Trace(err)
}
if len(result) == 0 && c.out.Name() == "tabular" {
ctx.Infof(noBlocks)
return nil
}
return c.out.Write(ctx, formatBlockInfo(result))
}
func (c *listCommand) listForController(ctx *cmd.Context) (err error) {
api, err := c.controllerAPIFunc(c)
if err != nil {
return errors.Trace(err)
}
defer api.Close()
result, err := api.ListBlockedModels()
if err != nil {
return errors.Trace(err)
}
if len(result) == 0 && c.out.Name() == "tabular" {
ctx.Infof(noBlocks)
return nil
}
info, err := FormatModelBlockInfo(result)
if err != nil {
return errors.Trace(err)
}
return c.out.Write(ctx, info)
}
func (c *listCommand) formatter(writer io.Writer, value interface{}) error {
if c.all {
return FormatTabularBlockedModels(writer, value)
}
return formatBlocks(writer, value)
}
// blockListAPI defines the client API methods that block list command uses.
type blockListAPI interface {
Close() error
List() ([]params.Block, error)
}
// controllerListAPI defines the methods on the controller API endpoint
// that the blocks command calls.
type controllerListAPI interface {
Close() error
ListBlockedModels() ([]params.ModelBlockInfo, error)
}
// BlockInfo defines the serialization behaviour of the block information.
type BlockInfo struct {
Commands string `yaml:"command-set" json:"command-set"`
Message string `yaml:"message,omitempty" json:"message,omitempty"`
}
// formatBlockInfo takes a set of Block and creates a
// mapping to information structures.
func formatBlockInfo(all []params.Block) []BlockInfo {
output := make([]BlockInfo, len(all))
for i, one := range all {
set, ok := toCmdValue[one.Type]
if !ok {
set = "<unknown>"
}
output[i] = BlockInfo{
Commands: set,
Message: one.Message,
}
}
return output
}
// formatBlocks writes block list representation.
func formatBlocks(writer io.Writer, value interface{}) error {
blocks, ok := value.([]BlockInfo)
if !ok {
return errors.Errorf("expected value of type %T, got %T", blocks, value)
}
if len(blocks) == 0 {
fmt.Fprintf(writer, "No commands are currently disabled.")
return nil
}
tw := output.TabWriter(writer)
w := output.Wrapper{tw}
w.Println("Disabled commands", "Message")
for _, info := range blocks {
w.Println(info.Commands, info.Message)
}
tw.Flush()
return nil
}
type newControllerAPIRoot interface {
NewControllerAPIRoot() (api.Connection, error)
}
// getControllerAPI returns a block api for block manipulation.
func getControllerAPI(c newControllerAPIRoot) (*controller.Client, error) {
root, err := c.NewControllerAPIRoot()
if err != nil {
return nil, errors.Trace(err)
}
return controller.NewClient(root), nil
}
type modelBlockInfo struct {
Name string `yaml:"name" json:"name"`
UUID string `yaml:"model-uuid" json:"model-uuid"`
Owner string `yaml:"owner" json:"owner"`
CommandSets []string `yaml:"disabled-commands,omitempty" json:"disabled-commands,omitempty"`
}
func FormatModelBlockInfo(all []params.ModelBlockInfo) ([]modelBlockInfo, error) {
output := make([]modelBlockInfo, len(all))
for i, one := range all {
tag, err := names.ParseUserTag(one.OwnerTag)
if err != nil {
return nil, errors.Trace(err)
}
output[i] = modelBlockInfo{
Name: one.Name,
UUID: one.UUID,
Owner: tag.Id(),
CommandSets: blocksToStr(one.Blocks),
}
}
return output, nil
}
// FormatTabularBlockedModels writes out tabular format for blocked models.
// This method is exported as it is also used by destroy-model.
func FormatTabularBlockedModels(writer io.Writer, value interface{}) error {
models, ok := value.([]modelBlockInfo)
if !ok {
return errors.Errorf("expected value of type %T, got %T", models, value)
}
tw := output.TabWriter(writer)
w := output.Wrapper{tw}
w.Println("Name", "Model UUID", "Owner", "Disabled commands")
for _, model := range models {
w.Println(model.Name, model.UUID, model.Owner, strings.Join(model.CommandSets, ", "))
}
tw.Flush()
return nil
}
func blocksToStr(blocks []string) []string {
result := make([]string, len(blocks))
for i, val := range blocks {
result[i] = operationFromType(val)
}
sort.Strings(result)
return result
}