github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/controller/listblocks.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package controller 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/modelcmd" 13 ) 14 15 // NewListBlocksCommand returns a command to list the blocks in a controller. 16 func NewListBlocksCommand() cmd.Command { 17 return modelcmd.WrapController(&listBlocksCommand{}) 18 } 19 20 // listBlocksCommand lists all blocks for environments within the controller. 21 type listBlocksCommand struct { 22 modelcmd.ControllerCommandBase 23 out cmd.Output 24 api listBlocksAPI 25 apierr error 26 } 27 28 var listBlocksDoc = `List all blocks for models within the specified controller` 29 30 // listBlocksAPI defines the methods on the controller API endpoint 31 // that the list-blocks command calls. 32 type listBlocksAPI interface { 33 Close() error 34 ListBlockedModels() ([]params.ModelBlockInfo, error) 35 } 36 37 // Info implements Command.Info. 38 func (c *listBlocksCommand) Info() *cmd.Info { 39 return &cmd.Info{ 40 Name: "list-all-blocks", 41 Purpose: "list all blocks within the controller", 42 Doc: listBlocksDoc, 43 } 44 } 45 46 // SetFlags implements Command.SetFlags. 47 func (c *listBlocksCommand) SetFlags(f *gnuflag.FlagSet) { 48 c.out.AddFlags(f, "tabular", map[string]cmd.Formatter{ 49 "yaml": cmd.FormatYaml, 50 "json": cmd.FormatJson, 51 "tabular": formatTabularBlockedModels, 52 }) 53 } 54 55 func (c *listBlocksCommand) getAPI() (listBlocksAPI, error) { 56 if c.api != nil { 57 return c.api, c.apierr 58 } 59 return c.NewControllerAPIClient() 60 } 61 62 // Run implements Command.Run 63 func (c *listBlocksCommand) Run(ctx *cmd.Context) error { 64 api, err := c.getAPI() 65 if err != nil { 66 return errors.Annotate(err, "cannot connect to the API") 67 } 68 defer api.Close() 69 70 envs, err := api.ListBlockedModels() 71 if err != nil { 72 logger.Errorf("Unable to list blocked models: %s", err) 73 return err 74 } 75 return c.out.Write(ctx, envs) 76 }