github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/model/dumpdb.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package model 5 6 import ( 7 "github.com/juju/cmd" 8 "github.com/juju/errors" 9 "github.com/juju/gnuflag" 10 "gopkg.in/juju/names.v2" 11 12 "github.com/juju/juju/cmd/modelcmd" 13 "github.com/juju/juju/cmd/output" 14 ) 15 16 // NewDumpDBCommand returns a fully constructed dump-db command. 17 func NewDumpDBCommand() cmd.Command { 18 return modelcmd.WrapController(&dumpDBCommand{}) 19 } 20 21 type dumpDBCommand struct { 22 modelcmd.ControllerCommandBase 23 out cmd.Output 24 api DumpDBAPI 25 26 model string 27 } 28 29 const dumpDBHelpDoc = ` 30 dump-db returns all that is stored in the database for the specified model. 31 32 Examples: 33 34 juju dump-db 35 juju dump-db mymodel 36 37 See also: 38 models 39 ` 40 41 // Info implements Command. 42 func (c *dumpDBCommand) Info() *cmd.Info { 43 return &cmd.Info{ 44 Name: "dump-db", 45 Args: "[model-name]", 46 Purpose: "Displays the mongo documents for of the model.", 47 Doc: dumpDBHelpDoc, 48 } 49 } 50 51 // SetFlags implements Command. 52 func (c *dumpDBCommand) SetFlags(f *gnuflag.FlagSet) { 53 c.ControllerCommandBase.SetFlags(f) 54 c.out.AddFlags(f, "yaml", output.DefaultFormatters) 55 } 56 57 // Init implements Command. 58 func (c *dumpDBCommand) Init(args []string) error { 59 if len(args) == 1 { 60 c.model = args[0] 61 return nil 62 } 63 return cmd.CheckEmpty(args) 64 } 65 66 // DumpDBAPI specifies the used function calls of the ModelManager. 67 type DumpDBAPI interface { 68 Close() error 69 DumpModelDB(names.ModelTag) (map[string]interface{}, error) 70 } 71 72 func (c *dumpDBCommand) getAPI() (DumpDBAPI, error) { 73 if c.api != nil { 74 return c.api, nil 75 } 76 return c.NewModelManagerAPIClient() 77 } 78 79 // Run implements Command. 80 func (c *dumpDBCommand) Run(ctx *cmd.Context) error { 81 client, err := c.getAPI() 82 if err != nil { 83 return err 84 } 85 defer client.Close() 86 87 store := c.ClientStore() 88 if c.model == "" { 89 c.model, err = store.CurrentModel(c.ControllerName()) 90 if err != nil { 91 return err 92 } 93 } 94 95 modelDetails, err := store.ModelByName( 96 c.ControllerName(), 97 c.model, 98 ) 99 if err != nil { 100 return errors.Annotate(err, "getting model details") 101 } 102 103 modelTag := names.NewModelTag(modelDetails.ModelUUID) 104 results, err := client.DumpModelDB(modelTag) 105 if err != nil { 106 return err 107 } 108 109 return c.out.Write(ctx, results) 110 }