github.com/cloud-green/juju@v0.0.0-20151002100041-a00291338d3d/cmd/juju/system/listblocks.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package system
     5  
     6  import (
     7  	"github.com/juju/cmd"
     8  	"github.com/juju/errors"
     9  	"launchpad.net/gnuflag"
    10  
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/cmd/envcmd"
    13  )
    14  
    15  // ListBlocksCommand lists all blocks for environments within the system.
    16  type ListBlocksCommand struct {
    17  	envcmd.SysCommandBase
    18  	out    cmd.Output
    19  	api    listBlocksAPI
    20  	apierr error
    21  }
    22  
    23  var listBlocksDoc = `List all blocks for environments within the specified system`
    24  
    25  // listBlocksAPI defines the methods on the system manager API endpoint
    26  // that the list-blocks command calls.
    27  type listBlocksAPI interface {
    28  	Close() error
    29  	ListBlockedEnvironments() ([]params.EnvironmentBlockInfo, error)
    30  }
    31  
    32  // Info implements Command.Info.
    33  func (c *ListBlocksCommand) Info() *cmd.Info {
    34  	return &cmd.Info{
    35  		Name:    "list-blocks",
    36  		Purpose: "list all blocks within the system",
    37  		Doc:     listBlocksDoc,
    38  	}
    39  }
    40  
    41  // SetFlags implements Command.SetFlags.
    42  func (c *ListBlocksCommand) SetFlags(f *gnuflag.FlagSet) {
    43  	c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{
    44  		"yaml":    cmd.FormatYaml,
    45  		"json":    cmd.FormatJson,
    46  		"tabular": formatTabularBlockedEnvironments,
    47  	})
    48  }
    49  
    50  func (c *ListBlocksCommand) getAPI() (listBlocksAPI, error) {
    51  	if c.api != nil {
    52  		return c.api, c.apierr
    53  	}
    54  	return c.NewSystemManagerAPIClient()
    55  }
    56  
    57  // Run implements Command.Run
    58  func (c *ListBlocksCommand) Run(ctx *cmd.Context) error {
    59  	api, err := c.getAPI()
    60  	if err != nil {
    61  		return errors.Annotate(err, "cannot connect to the API")
    62  	}
    63  	defer api.Close()
    64  
    65  	envs, err := api.ListBlockedEnvironments()
    66  	if err != nil {
    67  		logger.Errorf("Unable to list blocked environments: %s", err)
    68  		return err
    69  	}
    70  	return c.out.Write(ctx, envs)
    71  }