github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/controller/listcontrollersconverters.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package controller 5 6 import ( 7 "fmt" 8 9 "github.com/juju/errors" 10 "gopkg.in/juju/names.v2" 11 12 "github.com/juju/juju/cmd/juju/common" 13 "github.com/juju/juju/jujuclient" 14 ) 15 16 // ControllerSet contains the set of controllers known to the client, 17 // and name of the current controller. 18 type ControllerSet struct { 19 Controllers map[string]ControllerItem `yaml:"controllers" json:"controllers"` 20 CurrentController string `yaml:"current-controller" json:"current-controller"` 21 } 22 23 // ControllerMachines holds the total number of controller 24 // machines and the number of active ones. 25 type ControllerMachines struct { 26 Active int `yaml:"active"` 27 Total int `yaml:"total"` 28 } 29 30 // ControllerItem defines the serialization behaviour of controller information. 31 type ControllerItem struct { 32 ModelName string `yaml:"current-model,omitempty" json:"current-model,omitempty"` 33 User string `yaml:"user,omitempty" json:"user,omitempty"` 34 Access string `yaml:"access,omitempty" json:"access,omitempty"` 35 Server string `yaml:"recent-server,omitempty" json:"recent-server,omitempty"` 36 // TODO(anastasiamac 2018-08-10) This is a deprecated property, see lp#1596607. 37 // It was added for backward compatibility, lp#1786061, to be removed for Juju 3. 38 OldControllerUUID string `yaml:"uuid" json:"-"` 39 ControllerUUID string `yaml:"controller-uuid" json:"uuid"` 40 APIEndpoints []string `yaml:"api-endpoints,flow" json:"api-endpoints"` 41 CACert string `yaml:"ca-cert" json:"ca-cert"` 42 Cloud string `yaml:"cloud" json:"cloud"` 43 CloudRegion string `yaml:"region,omitempty" json:"region,omitempty"` 44 AgentVersion string `yaml:"agent-version,omitempty" json:"agent-version,omitempty"` 45 ModelCount *int `yaml:"model-count,omitempty" json:"model-count,omitempty"` 46 MachineCount *int `yaml:"machine-count,omitempty" json:"machine-count,omitempty"` 47 ControllerMachines *ControllerMachines `yaml:"controller-machines,omitempty" json:"controller-machines,omitempty"` 48 } 49 50 // convertControllerDetails takes a map of Controllers and 51 // the recently used model for each and creates a list of 52 // amalgamated controller and model details. 53 func (c *listControllersCommand) convertControllerDetails(storeControllers map[string]jujuclient.ControllerDetails) (map[string]ControllerItem, []string) { 54 if len(storeControllers) == 0 { 55 return nil, nil 56 } 57 58 errs := []string{} 59 addError := func(msg, controllerName string, err error) { 60 logger.Errorf(fmt.Sprintf("getting current %s for controller %s: %v", msg, controllerName, err)) 61 errs = append(errs, msg) 62 } 63 64 controllers := map[string]ControllerItem{} 65 for controllerName, details := range storeControllers { 66 serverName := "" 67 // The most recently connected-to address 68 // is the first in the list. 69 if len(details.APIEndpoints) > 0 { 70 serverName = details.APIEndpoints[0] 71 } 72 73 var userName, access string 74 accountDetails, err := c.store.AccountDetails(controllerName) 75 if err != nil { 76 if !errors.IsNotFound(err) { 77 addError("account details", controllerName, err) 78 continue 79 } 80 } else { 81 userName = accountDetails.User 82 access = accountDetails.LastKnownAccess 83 } 84 85 var modelName string 86 currentModel, err := c.store.CurrentModel(controllerName) 87 if err != nil { 88 if !errors.IsNotFound(err) { 89 addError("model", controllerName, err) 90 continue 91 } 92 } else { 93 modelName = currentModel 94 if userName != "" { 95 // There's a user logged in, so display the 96 // model name relative to that user. 97 if unqualifiedModelName, owner, err := jujuclient.SplitModelName(modelName); err == nil { 98 user := names.NewUserTag(userName) 99 modelName = common.OwnerQualifiedModelName(unqualifiedModelName, owner, user) 100 } 101 } 102 } 103 models, err := c.store.AllModels(controllerName) 104 if err != nil && !errors.IsNotFound(err) { 105 addError("models", controllerName, err) 106 } 107 modelCount := len(models) 108 109 item := ControllerItem{ 110 ModelName: modelName, 111 User: userName, 112 Access: access, 113 Server: serverName, 114 APIEndpoints: details.APIEndpoints, 115 ControllerUUID: details.ControllerUUID, 116 OldControllerUUID: details.ControllerUUID, 117 CACert: details.CACert, 118 Cloud: details.Cloud, 119 CloudRegion: details.CloudRegion, 120 AgentVersion: details.AgentVersion, 121 } 122 if details.MachineCount != nil && *details.MachineCount > 0 { 123 item.MachineCount = details.MachineCount 124 } 125 if modelCount > 0 { 126 item.ModelCount = &modelCount 127 } 128 if details.ControllerMachineCount > 0 { 129 item.ControllerMachines = &ControllerMachines{ 130 Total: details.ControllerMachineCount, 131 Active: details.ActiveControllerMachineCount, 132 } 133 } 134 controllers[controllerName] = item 135 } 136 return controllers, errs 137 }