-
Notifications
You must be signed in to change notification settings - Fork 491
/
listmodels.go
479 lines (432 loc) · 15.2 KB
/
listmodels.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package controller
import (
"fmt"
"io"
"strings"
"time"
"github.com/juju/ansiterm"
"github.com/juju/cmd/v3"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"github.com/juju/names/v5"
"github.com/juju/juju/api/base"
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/core/life"
"github.com/juju/juju/core/model"
"github.com/juju/juju/jujuclient"
"github.com/juju/juju/rpc/params"
)
// NewListModelsCommand returns a command to list models.
func NewListModelsCommand() cmd.Command {
return modelcmd.WrapController(&modelsCommand{})
}
// ModelManagerAPI defines the methods on the model manager API that
// the models command calls.
type ModelManagerAPI interface {
Close() error
ListModels(user string) ([]base.UserModel, error)
ListModelSummaries(user string, all bool) ([]base.UserModelSummary, error)
ModelInfo([]names.ModelTag) ([]params.ModelInfoResult, error)
}
// ModelsSysAPI defines the methods on the controller manager API that the
// list models command calls.
type ModelsSysAPI interface {
Close() error
AllModels() ([]base.UserModel, error)
}
// modelsCommand returns the list of all the models the
// current user can access on the current controller.
type modelsCommand struct {
modelcmd.ControllerCommandBase
out cmd.Output
all bool
loggedInUser string
user string
listUUID bool
exactTime bool
modelAPI ModelManagerAPI
sysAPI ModelsSysAPI
runVars modelsRunValues
}
// Info implements Command.Info
func (c *modelsCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "models",
Purpose: "Lists models a user can access on a controller.",
Doc: listModelsDoc,
Aliases: []string{"list-models"},
Examples: listModelsExamples,
SeeAlso: []string{
"add-model",
},
})
}
// SetFlags implements Command.SetFlags.
func (c *modelsCommand) SetFlags(f *gnuflag.FlagSet) {
c.ControllerCommandBase.SetFlags(f)
f.StringVar(&c.user, "user", "", "The user to list models for (administrative users only)")
f.BoolVar(&c.all, "all", false, "Lists all models, regardless of user accessibility (administrative users only)")
f.BoolVar(&c.listUUID, "uuid", false, "Display UUID for models")
f.BoolVar(&c.exactTime, "exact-time", false, "Use full timestamps")
c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
"json": cmd.FormatJson,
"tabular": c.formatTabular,
})
}
// Run implements Command.Run
func (c *modelsCommand) Run(ctx *cmd.Context) error {
controllerName, err := c.ControllerName()
if err != nil {
return errors.Trace(err)
}
accountDetails, err := c.CurrentAccountDetails()
if err != nil {
return err
}
c.loggedInUser = accountDetails.User
if c.user == "" {
c.user = accountDetails.User
}
if !names.IsValidUser(c.user) {
return errors.NotValidf("user %q", c.user)
}
c.runVars = modelsRunValues{
currentUser: names.NewUserTag(c.user),
controllerName: controllerName,
}
// TODO(perrito666) 2016-05-02 lp:1558657
now := time.Now()
modelmanagerAPI, err := c.getModelManagerAPI()
if err != nil {
return errors.Trace(err)
}
defer modelmanagerAPI.Close()
haveModels, err := c.getModelSummaries(ctx, modelmanagerAPI, now)
if err != nil {
return err
}
if !haveModels && c.out.Name() == "tabular" {
// When the output is tabular, we inform the user when there
// are no models available, and tell them how to go about
// creating or granting access to them.
fmt.Fprintln(ctx.Stderr, noModelsMessage)
}
return nil
}
func (c *modelsCommand) currentModelName() (qualified, name string) {
current, err := c.ClientStore().CurrentModel(c.runVars.controllerName)
if err == nil {
qualified, name = current, current
if c.user != "" {
unqualifiedModelName, owner, err := jujuclient.SplitModelName(current)
if err == nil {
// If current model's owner is this user, un-qualify model name.
name = common.OwnerQualifiedModelName(
unqualifiedModelName, owner, c.runVars.currentUser,
)
}
}
}
return
}
func (c *modelsCommand) getModelManagerAPI() (ModelManagerAPI, error) {
if c.modelAPI != nil {
return c.modelAPI, nil
}
return c.NewModelManagerAPIClient()
}
func (c *modelsCommand) getModelSummaries(ctx *cmd.Context, client ModelManagerAPI, now time.Time) (bool, error) {
results, err := client.ListModelSummaries(c.user, c.all)
if err != nil {
return false, errors.Trace(err)
}
summaries := []ModelSummary{}
modelsToStore := map[string]jujuclient.ModelDetails{}
for _, result := range results {
// Since we do not want to throw away all results if we have an
// an issue with a model, we will display errors in Stderr
// and will continue processing the rest.
if result.Error != nil {
ctx.Infof(result.Error.Error())
continue
}
model, err := c.modelSummaryFromParams(result, now)
if err != nil {
ctx.Infof(err.Error())
continue
}
model.ControllerName = c.runVars.controllerName
summaries = append(summaries, model)
modelsToStore[model.Name] = jujuclient.ModelDetails{ModelUUID: model.UUID, ModelType: model.Type}
}
found := len(summaries) > 0
if err := c.ClientStore().SetModels(c.runVars.controllerName, modelsToStore); err != nil {
return found, errors.Trace(err)
}
// Identifying current model has to be done after models in client store have been updated
// since that call determines/updates current model information.
modelSummarySet := ModelSummarySet{Models: summaries}
modelSummarySet.CurrentModelQualified, modelSummarySet.CurrentModel = c.currentModelName()
if err := c.out.Write(ctx, modelSummarySet); err != nil {
return found, err
}
return found, err
}
// ModelSummarySet contains the set of summaries for models.
type ModelSummarySet struct {
Models []ModelSummary `yaml:"models" json:"models"`
// CurrentModel is the name of the current model, qualified for the
// user for which we're listing models. i.e. for the user admin,
// and the model admin/foo, this field will contain "foo"; for
// bob and the same model, the field will contain "admin/foo".
CurrentModel string `yaml:"current-model,omitempty" json:"current-model,omitempty"`
// CurrentModelQualified is the fully qualified name for the current
// model, i.e. having the format $owner/$model.
CurrentModelQualified string `yaml:"-" json:"-"`
}
// ModelSummary contains a summary of some information about a model.
type ModelSummary struct {
// Name is a fully qualified model name, i.e. having the format $owner/$model.
Name string `json:"name" yaml:"name"`
// ShortName is un-qualified model name.
ShortName string `json:"short-name" yaml:"short-name"`
UUID string `json:"model-uuid" yaml:"model-uuid"`
Type model.ModelType `json:"model-type" yaml:"model-type"`
ControllerUUID string `json:"controller-uuid" yaml:"controller-uuid"`
ControllerName string `json:"controller-name" yaml:"controller-name"`
IsController bool `json:"is-controller" yaml:"is-controller"`
Owner string `json:"owner" yaml:"owner"`
Cloud string `json:"cloud" yaml:"cloud"`
CloudRegion string `json:"region,omitempty" yaml:"region,omitempty"`
CloudCredential *common.ModelCredential `json:"credential,omitempty" yaml:"credential,omitempty"`
ProviderType string `json:"type,omitempty" yaml:"type,omitempty"`
Life life.Value `json:"life" yaml:"life"`
Status *common.ModelStatus `json:"status,omitempty" yaml:"status,omitempty"`
UserAccess string `yaml:"access" json:"access"`
UserLastConnection string `yaml:"last-connection" json:"last-connection"`
// Counts is the map of different counts where key is the entity that was counted
// and value is the number, for e.g. {"machines":10,"cores":3, "units:4}.
Counts map[string]int64 `json:"-" yaml:"-"`
SLA string `json:"sla,omitempty" yaml:"sla,omitempty"`
SLAOwner string `json:"sla-owner,omitempty" yaml:"sla-owner,omitempty"`
AgentVersion string `json:"agent-version,omitempty" yaml:"agent-version,omitempty"`
}
func (c *modelsCommand) modelSummaryFromParams(apiSummary base.UserModelSummary, now time.Time) (ModelSummary, error) {
var statusSince string
if c.exactTime {
statusSince = apiSummary.Status.Since.String()
} else {
statusSince = common.FriendlyDuration(apiSummary.Status.Since, now)
}
summary := ModelSummary{
ShortName: apiSummary.Name,
Name: jujuclient.JoinOwnerModelName(names.NewUserTag(apiSummary.Owner), apiSummary.Name),
UUID: apiSummary.UUID,
Type: apiSummary.Type,
ControllerUUID: apiSummary.ControllerUUID,
IsController: apiSummary.IsController,
Owner: apiSummary.Owner,
Life: apiSummary.Life,
Cloud: apiSummary.Cloud,
CloudRegion: apiSummary.CloudRegion,
UserAccess: apiSummary.ModelUserAccess,
Status: &common.ModelStatus{
Current: apiSummary.Status.Status,
Message: apiSummary.Status.Info,
Since: statusSince,
},
}
if apiSummary.AgentVersion != nil {
summary.AgentVersion = apiSummary.AgentVersion.String()
}
if apiSummary.Migration != nil {
status := summary.Status
if status == nil {
status = &common.ModelStatus{}
summary.Status = status
}
status.Migration = apiSummary.Migration.Status
status.MigrationStart = common.FriendlyDuration(apiSummary.Migration.StartTime, now)
status.MigrationEnd = common.FriendlyDuration(apiSummary.Migration.EndTime, now)
}
if apiSummary.ProviderType != "" {
summary.ProviderType = apiSummary.ProviderType
}
if apiSummary.CloudCredential != "" {
if !names.IsValidCloudCredential(apiSummary.CloudCredential) {
return ModelSummary{}, errors.NotValidf("cloud credential ID %q", apiSummary.CloudCredential)
}
credTag := names.NewCloudCredentialTag(apiSummary.CloudCredential)
summary.CloudCredential = &common.ModelCredential{
Name: credTag.Name(),
Owner: credTag.Owner().Id(),
Cloud: credTag.Cloud().Id(),
}
}
if apiSummary.UserLastConnection != nil {
if c.exactTime {
summary.UserLastConnection = apiSummary.UserLastConnection.String()
} else {
summary.UserLastConnection = common.UserFriendlyDuration(*apiSummary.UserLastConnection, now)
}
} else {
summary.UserLastConnection = "never connected"
}
if apiSummary.SLA != nil {
summary.SLA = apiSummary.SLA.Level
summary.SLAOwner = apiSummary.SLA.Owner
}
summary.Counts = map[string]int64{}
for _, v := range apiSummary.Counts {
summary.Counts[v.Entity] = v.Count
}
// If hasMachinesCounts is not yet set, check if we should set it based on this model summary.
if !c.runVars.hasMachinesCount {
if _, ok := summary.Counts[string(params.Machines)]; ok {
c.runVars.hasMachinesCount = true
}
}
// If hasCoresCounts is not yet set, check if we should set it based on this model summary.
if !c.runVars.hasCoresCount {
if _, ok := summary.Counts[string(params.Cores)]; ok {
c.runVars.hasCoresCount = true
}
}
// If hasUnitsCounts is not yet set, check if we should set it based on this model summary.
if !c.runVars.hasUnitsCount {
if _, ok := summary.Counts[string(params.Units)]; ok {
c.runVars.hasUnitsCount = true
}
}
return summary, nil
}
// These values are specific to an individual Run() of the model command.
type modelsRunValues struct {
currentUser names.UserTag
controllerName string
hasMachinesCount bool
hasCoresCount bool
hasUnitsCount bool
}
// ModelSet contains the set of models known to the client,
// and UUID of the current model.
// (anastasiamac 2017-23-11) This is old, pre juju 2.3 implementation.
type ModelSet struct {
Models []common.ModelInfo `yaml:"models" json:"models"`
// CurrentModel is the name of the current model, qualified for the
// user for which we're listing models. i.e. for the user admin,
// and the model admin/foo, this field will contain "foo"; for
// bob and the same model, the field will contain "admin/foo".
CurrentModel string `yaml:"current-model,omitempty" json:"current-model,omitempty"`
// CurrentModelQualified is the fully qualified name for the current
// model, i.e. having the format $owner/$model.
CurrentModelQualified string `yaml:"-" json:"-"`
}
// formatTabular takes an interface{} to adhere to the cmd.Formatter interface
func (c *modelsCommand) formatTabular(writer io.Writer, value interface{}) error {
summariesSet, ok := value.(ModelSummarySet)
if !ok {
return errors.Errorf("expected value of type ModelSummarySet, got %T", value)
}
return c.tabularSummaries(writer, summariesSet)
}
func (c *modelsCommand) tabularColumns(tw *ansiterm.TabWriter, w output.Wrapper) {
w.Println("Controller: " + c.runVars.controllerName)
w.Println()
w.Print("Model")
if c.listUUID {
w.Print("UUID")
}
w.Print("Cloud/Region", "Type", "Status")
printColumnHeader := func(columnName string, columnNumber int) {
w.Print(columnName)
offset := 0
if c.listUUID {
offset++
}
tw.SetColumnAlignRight(columnNumber + offset)
}
if c.runVars.hasMachinesCount {
printColumnHeader("Machines", 4)
}
if c.runVars.hasCoresCount {
printColumnHeader("Cores", 5)
}
if c.runVars.hasUnitsCount {
printColumnHeader("Units", 5)
}
w.Println("Access", "Last connection")
}
// tabularSummaries takes model summaries set to adhere to the cmd.Formatter interface
func (c *modelsCommand) tabularSummaries(writer io.Writer, modelSet ModelSummarySet) error {
tw := output.TabWriter(writer)
w := output.Wrapper{tw}
c.tabularColumns(tw, w)
for _, model := range modelSet.Models {
cloudRegion := strings.Trim(model.Cloud+"/"+model.CloudRegion, "/")
owner := names.NewUserTag(model.Owner)
name := model.Name
if c.runVars.currentUser == owner {
// No need to display fully qualified model name to its owner.
name = model.ShortName
}
if model.Name == modelSet.CurrentModelQualified {
name += "*"
w.PrintColor(output.CurrentHighlight, name)
} else {
w.Print(name)
}
if c.listUUID {
w.Print(model.UUID)
}
status := "-"
if model.Status != nil && model.Status.Current.String() != "" {
status = model.Status.Current.String()
}
w.Print(cloudRegion, model.ProviderType, status)
if c.runVars.hasMachinesCount {
if v, ok := model.Counts[string(params.Machines)]; ok {
w.Print(v)
} else {
w.Print(0)
}
}
if c.runVars.hasCoresCount {
if v, ok := model.Counts[string(params.Cores)]; ok {
w.Print(v)
} else {
w.Print("-")
}
}
if c.runVars.hasUnitsCount {
if v, ok := model.Counts[string(params.Units)]; ok {
w.Print(v)
} else {
w.Print("-")
}
}
access := model.UserAccess
if access == "" {
access = "-"
}
w.Println(access, model.UserLastConnection)
}
tw.Flush()
return nil
}
var listModelsDoc = `
The models listed here are either models you have created yourself, or
models which have been shared with you. Default values for user and
controller are, respectively, the current user and the current controller.
The active model is denoted by an asterisk.
`
const listModelsExamples = `
juju models
juju models --user bob
`