github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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 11 "github.com/juju/juju/jujuclient" 12 ) 13 14 // ControllerSet contains the set of controllers known to the client, 15 // and name of the current controller. 16 type ControllerSet struct { 17 Controllers map[string]ControllerItem `yaml:"controllers" json:"controllers"` 18 CurrentController string `yaml:"current-controller" json:"current-controller"` 19 } 20 21 // ControllerItem defines the serialization behaviour of controller information. 22 type ControllerItem struct { 23 ModelName string `yaml:"current-model,omitempty" json:"current-model,omitempty"` 24 User string `yaml:"user,omitempty" json:"user,omitempty"` 25 Server string `yaml:"recent-server,omitempty" json:"recent-server,omitempty"` 26 ControllerUUID string `yaml:"uuid" json:"uuid"` 27 APIEndpoints []string `yaml:"api-endpoints,flow" json:"api-endpoints"` 28 CACert string `yaml:"ca-cert" json:"ca-cert"` 29 } 30 31 // convertControllerDetails takes a map of Controllers and 32 // the recently used model for each and creates a list of 33 // amalgamated controller and model details. 34 func (c *listControllersCommand) convertControllerDetails(storeControllers map[string]jujuclient.ControllerDetails) (map[string]ControllerItem, []string) { 35 if len(storeControllers) == 0 { 36 return nil, nil 37 } 38 39 errs := []string{} 40 addError := func(msg, controllerName string, err error) { 41 logger.Errorf(fmt.Sprintf("getting current %s for controller %s: %v", msg, controllerName, err)) 42 errs = append(errs, msg) 43 } 44 45 controllers := map[string]ControllerItem{} 46 for controllerName, details := range storeControllers { 47 serverName := "" 48 // The most recently connected-to address 49 // is the first in the list. 50 if len(details.APIEndpoints) > 0 { 51 serverName = details.APIEndpoints[0] 52 } 53 54 var userName, modelName string 55 accountName, err := c.store.CurrentAccount(controllerName) 56 if err != nil { 57 if !errors.IsNotFound(err) { 58 addError("account name", controllerName, err) 59 continue 60 } 61 } else { 62 currentAccount, err := c.store.AccountByName(controllerName, accountName) 63 if err != nil { 64 addError("account details", controllerName, err) 65 continue 66 } 67 userName = currentAccount.User 68 69 currentModel, err := c.store.CurrentModel(controllerName, accountName) 70 if err != nil { 71 if !errors.IsNotFound(err) { 72 addError("model", controllerName, err) 73 continue 74 } 75 } 76 modelName = currentModel 77 } 78 79 controllers[controllerName] = ControllerItem{ 80 ModelName: modelName, 81 User: userName, 82 Server: serverName, 83 APIEndpoints: details.APIEndpoints, 84 ControllerUUID: details.ControllerUUID, 85 CACert: details.CACert, 86 } 87 } 88 return controllers, errs 89 }