github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/model/show.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for infos.
     3  
     4  package model
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/errors"
    11  	"github.com/juju/gnuflag"
    12  	"gopkg.in/juju/names.v2"
    13  
    14  	"github.com/juju/juju/api/modelmanager"
    15  	"github.com/juju/juju/apiserver/params"
    16  	"github.com/juju/juju/cmd/juju/common"
    17  	"github.com/juju/juju/cmd/modelcmd"
    18  	"github.com/juju/juju/cmd/output"
    19  )
    20  
    21  const showModelCommandDoc = `Show information about the current or specified model`
    22  
    23  func NewShowCommand() cmd.Command {
    24  	return modelcmd.Wrap(&showModelCommand{}, modelcmd.WrapSkipModelFlags)
    25  }
    26  
    27  // showModelCommand shows all the users with access to the current model.
    28  type showModelCommand struct {
    29  	modelcmd.ModelCommandBase
    30  	out cmd.Output
    31  	api ShowModelAPI
    32  }
    33  
    34  // ShowModelAPI defines the methods on the client API that the
    35  // users command calls.
    36  type ShowModelAPI interface {
    37  	Close() error
    38  	ModelInfo([]names.ModelTag) ([]params.ModelInfoResult, error)
    39  }
    40  
    41  func (c *showModelCommand) getAPI() (ShowModelAPI, error) {
    42  	if c.api != nil {
    43  		return c.api, nil
    44  	}
    45  	api, err := c.NewControllerAPIRoot()
    46  	if err != nil {
    47  		return nil, errors.Trace(err)
    48  	}
    49  	return modelmanager.NewClient(api), nil
    50  }
    51  
    52  // Info implements Command.Info.
    53  func (c *showModelCommand) Info() *cmd.Info {
    54  	return &cmd.Info{
    55  		Name:    "show-model",
    56  		Args:    "<model name>",
    57  		Purpose: "Shows information about the current or specified model.",
    58  		Doc:     showModelCommandDoc,
    59  	}
    60  }
    61  
    62  // SetFlags implements Command.SetFlags.
    63  func (c *showModelCommand) SetFlags(f *gnuflag.FlagSet) {
    64  	c.ModelCommandBase.SetFlags(f)
    65  	c.out.AddFlags(f, "yaml", output.DefaultFormatters)
    66  }
    67  
    68  // Init implements Command.Init.
    69  func (c *showModelCommand) Init(args []string) error {
    70  	if len(args) > 0 {
    71  		c.SetModelName(args[0])
    72  		args = args[1:]
    73  	}
    74  	if err := c.ModelCommandBase.Init(args); err != nil {
    75  		return err
    76  	}
    77  	if c.ModelName() == "" {
    78  		defaultModel, err := modelcmd.GetCurrentModel(c.ClientStore())
    79  		if err != nil {
    80  			return err
    81  		}
    82  		c.SetModelName(defaultModel)
    83  	}
    84  	return nil
    85  }
    86  
    87  // Run implements Command.Run.
    88  func (c *showModelCommand) Run(ctx *cmd.Context) (err error) {
    89  	api, err := c.getAPI()
    90  	if err != nil {
    91  		return err
    92  	}
    93  	defer api.Close()
    94  
    95  	store := c.ClientStore()
    96  	modelDetails, err := store.ModelByName(
    97  		c.ControllerName(),
    98  		c.ModelName(),
    99  	)
   100  	if err != nil {
   101  		return errors.Annotate(err, "getting model details")
   102  	}
   103  
   104  	modelTag := names.NewModelTag(modelDetails.ModelUUID)
   105  	results, err := api.ModelInfo([]names.ModelTag{modelTag})
   106  	if err != nil {
   107  		return err
   108  	}
   109  	if results[0].Error != nil {
   110  		return results[0].Error
   111  	}
   112  	infoMap, err := c.apiModelInfoToModelInfoMap([]params.ModelInfo{*results[0].Result})
   113  	if err != nil {
   114  		return errors.Trace(err)
   115  	}
   116  	return c.out.Write(ctx, infoMap)
   117  }
   118  
   119  func (c *showModelCommand) apiModelInfoToModelInfoMap(modelInfo []params.ModelInfo) (map[string]common.ModelInfo, error) {
   120  	// TODO(perrito666) 2016-05-02 lp:1558657
   121  	now := time.Now()
   122  	output := make(map[string]common.ModelInfo)
   123  	for _, info := range modelInfo {
   124  		out, err := common.ModelInfoFromParams(info, now)
   125  		if err != nil {
   126  			return nil, errors.Trace(err)
   127  		}
   128  		out.ControllerName = c.ControllerName()
   129  		output[out.Name] = out
   130  	}
   131  	return output, nil
   132  }