-
Notifications
You must be signed in to change notification settings - Fork 491
/
list.go
406 lines (360 loc) · 10.6 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cloud
import (
"io"
"sort"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/loggo"
"gopkg.in/juju/names.v3"
cloudapi "github.com/juju/juju/api/cloud"
jujucloud "github.com/juju/juju/cloud"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/common"
"github.com/juju/juju/cmd/modelcmd"
"github.com/juju/juju/cmd/output"
"github.com/juju/juju/jujuclient"
)
var logger = loggo.GetLogger("juju.cmd.juju.cloud")
type listCloudsCommand struct {
modelcmd.OptionalControllerCommand
out cmd.Output
listCloudsAPIFunc func() (ListCloudsAPI, error)
all bool
showAllMessage bool
}
// listCloudsDoc is multi-line since we need to use ` to denote
// commands for ease in markdown.
var listCloudsDoc = "" +
"Display the fundamental properties for each cloud known to Juju:\n" +
"name, number of regions, number of registered credentials, default region, type, etc...\n" +
"\n" +
"Clouds known to this client are the clouds known to Juju out of the box \n" +
"along with any which have been added with `add-cloud --client`. These clouds can be\n" +
"used to create a controller and can be displayed using --client option.\n" +
"\n" +
"Clouds may be listed that are co-hosted with the Juju client. When the LXD hypervisor\n" +
"is detected, the 'localhost' cloud is made available. When a microk8s installation is\n" +
"detected, the 'microk8s' cloud is displayed.\n" +
"\n" +
"Use --controller option to list clouds from a controller. \n" +
"Use --client option to list clouds from this client. \n" +
"This command's default output format is 'tabular'. Use 'json' and 'yaml' for\n" +
"machine-readable output.\n" +
"\n" +
"Cloud metadata sometimes changes, e.g. providers add regions. Use the `update-public-clouds`\n" +
"command to update public clouds or `update-cloud` to update other clouds.\n" +
"\n" +
"Use the `regions` command to list a cloud's regions.\n" +
"\n" +
"Use the `show-cloud` command to get more detail, such as regions and endpoints.\n" +
"\n" +
"Further reading:\n " +
"\n" +
" Documentation: https://jaas.ai/docs/clouds\n" +
" microk8s: https://microk8s.io/\n" +
" LXD hypervisor: https://linuxcontainers.org/lxd/\n" +
listCloudsDocExamples
var listCloudsDocExamples = `
Examples:
juju clouds
juju clouds --format yaml
juju clouds --controller mycontroller
juju clouds --controller mycontroller --client
juju clouds --client
See also:
add-cloud
credentials
controllers
regions
default-credential
default-region
show-cloud
update-cloud
update-public-clouds
`
type ListCloudsAPI interface {
Clouds() (map[names.CloudTag]jujucloud.Cloud, error)
Close() error
}
// NewListCloudsCommand returns a command to list cloud information.
func NewListCloudsCommand() cmd.Command {
store := jujuclient.NewFileClientStore()
c := &listCloudsCommand{
OptionalControllerCommand: modelcmd.OptionalControllerCommand{
Store: store,
ReadOnly: true,
},
}
c.listCloudsAPIFunc = c.cloudAPI
return modelcmd.WrapBase(c)
}
func (c *listCloudsCommand) cloudAPI() (ListCloudsAPI, error) {
root, err := c.NewAPIRoot(c.Store, c.ControllerName, "")
if err != nil {
return nil, errors.Trace(err)
}
return cloudapi.NewClient(root), nil
}
func (c *listCloudsCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "clouds",
Purpose: "Lists all clouds available to Juju.",
Doc: listCloudsDoc,
Aliases: []string{"list-clouds"},
})
}
func (c *listCloudsCommand) SetFlags(f *gnuflag.FlagSet) {
c.OptionalControllerCommand.SetFlags(f)
f.BoolVar(&c.all, "all", false, "Show all available clouds")
c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
"json": cmd.FormatJson,
"tabular": formatCloudsTabular,
})
}
func (c *listCloudsCommand) getCloudList(ctxt *cmd.Context) (*cloudList, error) {
var returnErr error
warn := func(anErr error) {
ctxt.Warningf("%v", anErr)
returnErr = cmd.ErrSilent
}
details := newCloudList()
if c.Client {
var err error
if details, err = listLocalCloudDetails(c.Store); err != nil {
warn(err)
}
}
if c.ControllerName != "" {
remotes := func() error {
api, err := c.listCloudsAPIFunc()
if err != nil {
return errors.Trace(err)
}
defer api.Close()
controllerClouds, err := api.Clouds()
if err != nil {
return errors.Trace(err)
}
for _, cloud := range controllerClouds {
cloudDetails := makeCloudDetails(c.Store, cloud)
details.remote[cloud.Name] = cloudDetails
}
return nil
}
if err := remotes(); err != nil {
warn(err)
}
}
c.showAllMessage = details.filter(c.all)
return details, returnErr
}
func (c *listCloudsCommand) Run(ctxt *cmd.Context) error {
// TODO: is serial states that it's machine serialisable. We shouldn't
// prompt in those situations. In an ideal world we would move this into
// MaybePrompt, but the changes may have unintended consequences without
// proper understanding.
// Should fix this for Juju 3.0
c.ReadOnly = ctxt.IsSerial()
if err := c.MaybePrompt(ctxt, "list clouds from"); err != nil {
return errors.Trace(err)
}
details, err := c.getCloudList(ctxt)
if err != nil {
return errors.Trace(err)
}
if c.showAllMessage {
if details.len() != 0 {
ctxt.Infof("Only clouds with registered credentials are shown.")
} else {
ctxt.Infof("No clouds with registered credentials to show.")
}
ctxt.Infof("There are more clouds, use --all to see them.")
}
var result interface{}
switch c.out.Name() {
case "yaml", "json":
clouds := details.all()
for _, cloud := range clouds {
cloud.CloudType = displayCloudType(cloud.CloudType)
}
result = clouds
default:
result = details
}
return c.out.Write(ctxt, result)
}
type cloudList struct {
public map[string]*CloudDetails
builtin map[string]*CloudDetails
personal map[string]*CloudDetails
remote map[string]*CloudDetails
}
func newCloudList() *cloudList {
return &cloudList{
make(map[string]*CloudDetails),
make(map[string]*CloudDetails),
make(map[string]*CloudDetails),
make(map[string]*CloudDetails),
}
}
func (c *cloudList) len() int {
return len(c.personal) + len(c.builtin) + len(c.public) + len(c.remote)
}
func (c *cloudList) all() map[string]*CloudDetails {
if len(c.personal) == 0 && len(c.builtin) == 0 && len(c.public) == 0 && len(c.remote) == 0 {
return nil
}
result := make(map[string]*CloudDetails)
addAll := func(someClouds map[string]*CloudDetails) {
for name, cloud := range someClouds {
result[name] = cloud
}
}
addAll(c.public)
addAll(c.builtin)
addAll(c.personal)
addAll(c.remote)
return result
}
func (c *cloudList) local() map[string]*CloudDetails {
if len(c.personal) == 0 && len(c.builtin) == 0 && len(c.public) == 0 {
return nil
}
result := make(map[string]*CloudDetails)
addAll := func(someClouds map[string]*CloudDetails) {
for name, cloud := range someClouds {
result[name] = cloud
}
}
addAll(c.public)
addAll(c.builtin)
addAll(c.personal)
return result
}
func (c *cloudList) filter(all bool) bool {
if all {
return false
}
if len(c.personal) == 0 && len(c.builtin) == 0 && len(c.public) == 0 && len(c.remote) == 0 {
return false
}
result := false
examine := func(someClouds map[string]*CloudDetails) {
for name, cloud := range someClouds {
if cloud.CredentialCount == 0 {
result = true
delete(someClouds, name)
}
}
}
examine(c.public)
return result
}
func clientPublicClouds() (map[string]jujucloud.Cloud, error) {
clouds, _, err := jujucloud.PublicCloudMetadata(jujucloud.JujuPublicCloudsPath())
if err != nil {
return nil, errors.Trace(err)
}
return clouds, nil
}
func listLocalCloudDetails(store jujuclient.CredentialGetter) (*cloudList, error) {
clouds, err := clientPublicClouds()
if err != nil {
return nil, errors.Trace(err)
}
details := newCloudList()
for name, cloud := range clouds {
cloudDetails := makeCloudDetails(store, cloud)
details.public[name] = cloudDetails
}
// Add in built in clouds like localhost (lxd).
builtinClouds, err := common.BuiltInClouds()
if err != nil {
return nil, errors.Trace(err)
}
for name, cloud := range builtinClouds {
cloudDetails := makeCloudDetails(store, cloud)
cloudDetails.Source = "built-in"
details.builtin[name] = cloudDetails
}
personalClouds, err := jujucloud.PersonalCloudMetadata()
if err != nil {
return nil, errors.Trace(err)
}
for name, cloud := range personalClouds {
cloudDetails := makeCloudDetails(store, cloud)
cloudDetails.Source = "local"
details.personal[name] = cloudDetails
// Delete any built-in or public clouds with same name.
delete(details.builtin, name)
delete(details.public, name)
}
return details, nil
}
// formatCloudsTabular writes a tabular summary of cloud information.
func formatCloudsTabular(writer io.Writer, value interface{}) error {
clouds, ok := value.(*cloudList)
if !ok {
return errors.Errorf("expected value of type %T, got %T", clouds, value)
}
tw := output.TabWriter(writer)
w := output.Wrapper{tw}
w.SetColumnAlignRight(1)
cloudNamesSorted := func(someClouds map[string]*CloudDetails) []string {
var cloudNames []string
for name := range someClouds {
cloudNames = append(cloudNames, name)
}
sort.Strings(cloudNames)
return cloudNames
}
printClouds := func(someClouds map[string]*CloudDetails, showTail bool) {
cloudNames := cloudNamesSorted(someClouds)
for _, name := range cloudNames {
info := someClouds[name]
defaultRegion := info.DefaultRegion
if defaultRegion == "" {
if len(info.Regions) > 0 {
defaultRegion = info.RegionsMap[info.Regions[0].Key.(string)].Name
}
}
description := info.CloudDescription
if len(description) > 40 {
description = description[:39]
}
w.Print(name, len(info.Regions), defaultRegion, displayCloudType(info.CloudType))
if showTail {
w.Println(info.CredentialCount, info.Source, description)
} else {
w.Println()
}
}
}
var hasRemotes bool
if len(clouds.remote) > 0 {
w.Println("\nClouds available on the controller:")
w.Println("Cloud", "Regions", "Default", "Type")
printClouds(clouds.remote, false)
hasRemotes = true
}
if localClouds := clouds.local(); len(localClouds) > 0 {
if !hasRemotes {
w.Println("You can bootstrap a new controller using one of these clouds...")
}
w.Println("\nClouds available on the client:")
w.Println("Cloud", "Regions", "Default", "Type", "Credentials", "Source", "Description")
printClouds(localClouds, true)
}
tw.Flush()
return nil
}
func displayCloudType(in string) string {
if in == "kubernetes" {
return "k8s"
}
return in
}