github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/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/names"
    12  	"launchpad.net/gnuflag"
    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  )
    19  
    20  const showModelCommandDoc = `Show information about the current or specified model`
    21  
    22  func NewShowCommand() cmd.Command {
    23  	return modelcmd.Wrap(&showModelCommand{})
    24  }
    25  
    26  // showModelCommand shows all the users with access to the current model.
    27  type showModelCommand struct {
    28  	modelcmd.ModelCommandBase
    29  	out cmd.Output
    30  	api ShowModelAPI
    31  }
    32  
    33  // ShowModelAPI defines the methods on the client API that the
    34  // users command calls.
    35  type ShowModelAPI interface {
    36  	Close() error
    37  	ModelInfo([]names.ModelTag) ([]params.ModelInfoResult, error)
    38  }
    39  
    40  func (c *showModelCommand) getAPI() (ShowModelAPI, error) {
    41  	if c.api != nil {
    42  		return c.api, nil
    43  	}
    44  	api, err := c.NewAPIRoot()
    45  	if err != nil {
    46  		return nil, errors.Trace(err)
    47  	}
    48  	return modelmanager.NewClient(api), nil
    49  }
    50  
    51  // Info implements Command.Info.
    52  func (c *showModelCommand) Info() *cmd.Info {
    53  	return &cmd.Info{
    54  		Name:    "show-model",
    55  		Purpose: "shows information about the current or specified model",
    56  		Doc:     showModelCommandDoc,
    57  	}
    58  }
    59  
    60  // SetFlags implements Command.SetFlags.
    61  func (c *showModelCommand) SetFlags(f *gnuflag.FlagSet) {
    62  	c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
    63  		"yaml": cmd.FormatYaml,
    64  		"json": cmd.FormatJson,
    65  	})
    66  }
    67  
    68  // Run implements Command.Run.
    69  func (c *showModelCommand) Run(ctx *cmd.Context) (err error) {
    70  	api, err := c.getAPI()
    71  	if err != nil {
    72  		return err
    73  	}
    74  	defer api.Close()
    75  
    76  	store := c.ClientStore()
    77  	modelDetails, err := store.ModelByName(
    78  		c.ControllerName(),
    79  		c.AccountName(),
    80  		c.ModelName(),
    81  	)
    82  	if err != nil {
    83  		return errors.Annotate(err, "getting model details")
    84  	}
    85  
    86  	modelTag := names.NewModelTag(modelDetails.ModelUUID)
    87  	results, err := api.ModelInfo([]names.ModelTag{modelTag})
    88  	if err != nil {
    89  		return err
    90  	}
    91  	if results[0].Error != nil {
    92  		return results[0].Error
    93  	}
    94  	infoMap, err := c.apiModelInfoToModelInfoMap([]params.ModelInfo{*results[0].Result})
    95  	if err != nil {
    96  		return errors.Trace(err)
    97  	}
    98  	return c.out.Write(ctx, infoMap)
    99  }
   100  
   101  func (c *showModelCommand) apiModelInfoToModelInfoMap(modelInfo []params.ModelInfo) (map[string]common.ModelInfo, error) {
   102  	now := time.Now()
   103  	output := make(map[string]common.ModelInfo)
   104  	for _, info := range modelInfo {
   105  		out, err := common.ModelInfoFromParams(info, now)
   106  		if err != nil {
   107  			return nil, errors.Trace(err)
   108  		}
   109  		output[out.Name] = out
   110  	}
   111  	return output, nil
   112  }