github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/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  	ControllerUUID     string              `yaml:"uuid" json:"uuid"`
    37  	APIEndpoints       []string            `yaml:"api-endpoints,flow" json:"api-endpoints"`
    38  	CACert             string              `yaml:"ca-cert" json:"ca-cert"`
    39  	Cloud              string              `yaml:"cloud" json:"cloud"`
    40  	CloudRegion        string              `yaml:"region,omitempty" json:"region,omitempty"`
    41  	AgentVersion       string              `yaml:"agent-version,omitempty" json:"agent-version,omitempty"`
    42  	ModelCount         *int                `yaml:"model-count,omitempty" json:"model-count,omitempty"`
    43  	MachineCount       *int                `yaml:"machine-count,omitempty" json:"machine-count,omitempty"`
    44  	ControllerMachines *ControllerMachines `yaml:"controller-machines,omitempty" json:"controller-machins,omitempty"`
    45  }
    46  
    47  // convertControllerDetails takes a map of Controllers and
    48  // the recently used model for each and creates a list of
    49  // amalgamated controller and model details.
    50  func (c *listControllersCommand) convertControllerDetails(storeControllers map[string]jujuclient.ControllerDetails) (map[string]ControllerItem, []string) {
    51  	if len(storeControllers) == 0 {
    52  		return nil, nil
    53  	}
    54  
    55  	errs := []string{}
    56  	addError := func(msg, controllerName string, err error) {
    57  		logger.Errorf(fmt.Sprintf("getting current %s for controller %s: %v", msg, controllerName, err))
    58  		errs = append(errs, msg)
    59  	}
    60  
    61  	controllers := map[string]ControllerItem{}
    62  	for controllerName, details := range storeControllers {
    63  		serverName := ""
    64  		// The most recently connected-to address
    65  		// is the first in the list.
    66  		if len(details.APIEndpoints) > 0 {
    67  			serverName = details.APIEndpoints[0]
    68  		}
    69  
    70  		var userName, access string
    71  		accountDetails, err := c.store.AccountDetails(controllerName)
    72  		if err != nil {
    73  			if !errors.IsNotFound(err) {
    74  				addError("account details", controllerName, err)
    75  				continue
    76  			}
    77  		} else {
    78  			userName = accountDetails.User
    79  			access = accountDetails.LastKnownAccess
    80  		}
    81  
    82  		var modelName string
    83  		currentModel, err := c.store.CurrentModel(controllerName)
    84  		if err != nil {
    85  			if !errors.IsNotFound(err) {
    86  				addError("model", controllerName, err)
    87  				continue
    88  			}
    89  		} else {
    90  			modelName = currentModel
    91  			if userName != "" {
    92  				// There's a user logged in, so display the
    93  				// model name relative to that user.
    94  				if unqualifiedModelName, owner, err := jujuclient.SplitModelName(modelName); err == nil {
    95  					user := names.NewUserTag(userName)
    96  					modelName = common.OwnerQualifiedModelName(unqualifiedModelName, owner, user)
    97  				}
    98  			}
    99  		}
   100  
   101  		item := ControllerItem{
   102  			ModelName:      modelName,
   103  			User:           userName,
   104  			Access:         access,
   105  			Server:         serverName,
   106  			APIEndpoints:   details.APIEndpoints,
   107  			ControllerUUID: details.ControllerUUID,
   108  			CACert:         details.CACert,
   109  			Cloud:          details.Cloud,
   110  			CloudRegion:    details.CloudRegion,
   111  			AgentVersion:   details.AgentVersion,
   112  		}
   113  		if details.MachineCount != nil && *details.MachineCount > 0 {
   114  			item.MachineCount = details.MachineCount
   115  		}
   116  		if details.ModelCount != nil && *details.ModelCount > 0 {
   117  			item.ModelCount = details.ModelCount
   118  		}
   119  		if details.ControllerMachineCount > 0 {
   120  			item.ControllerMachines = &ControllerMachines{
   121  				Total:  details.ControllerMachineCount,
   122  				Active: details.ActiveControllerMachineCount,
   123  			}
   124  		}
   125  		controllers[controllerName] = item
   126  	}
   127  	return controllers, errs
   128  }