github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/model/dump.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 // NewDumpCommand returns a fully constructed dump-model command. 17 func NewDumpCommand() cmd.Command { 18 return modelcmd.WrapController(&dumpCommand{}) 19 } 20 21 type dumpCommand struct { 22 modelcmd.ControllerCommandBase 23 out cmd.Output 24 api DumpModelAPI 25 26 model string 27 } 28 29 const dumpModelHelpDoc = ` 30 Calls export on the model's database representation and writes the 31 resulting YAML to stdout. 32 33 Examples: 34 35 juju dump-model 36 juju dump-model mymodel 37 38 See also: 39 models 40 ` 41 42 // Info implements Command. 43 func (c *dumpCommand) Info() *cmd.Info { 44 return &cmd.Info{ 45 Name: "dump-model", 46 Args: "[model-name]", 47 Purpose: "Displays the database agnostic representation of the model.", 48 Doc: dumpModelHelpDoc, 49 } 50 } 51 52 // SetFlags implements Command. 53 func (c *dumpCommand) SetFlags(f *gnuflag.FlagSet) { 54 c.ControllerCommandBase.SetFlags(f) 55 c.out.AddFlags(f, "yaml", output.DefaultFormatters) 56 } 57 58 // Init implements Command. 59 func (c *dumpCommand) Init(args []string) error { 60 if len(args) == 1 { 61 c.model = args[0] 62 return nil 63 } 64 return cmd.CheckEmpty(args) 65 } 66 67 // DumpModelAPI specifies the used function calls of the ModelManager. 68 type DumpModelAPI interface { 69 Close() error 70 DumpModel(names.ModelTag) (map[string]interface{}, error) 71 } 72 73 func (c *dumpCommand) getAPI() (DumpModelAPI, error) { 74 if c.api != nil { 75 return c.api, nil 76 } 77 return c.NewModelManagerAPIClient() 78 } 79 80 // Run implements Command. 81 func (c *dumpCommand) Run(ctx *cmd.Context) error { 82 client, err := c.getAPI() 83 if err != nil { 84 return err 85 } 86 defer client.Close() 87 88 store := c.ClientStore() 89 if c.model == "" { 90 c.model, err = store.CurrentModel(c.ControllerName()) 91 if err != nil { 92 return err 93 } 94 } 95 96 modelDetails, err := store.ModelByName( 97 c.ControllerName(), 98 c.model, 99 ) 100 if err != nil { 101 return errors.Annotate(err, "getting model details") 102 } 103 104 modelTag := names.NewModelTag(modelDetails.ModelUUID) 105 results, err := client.DumpModel(modelTag) 106 if err != nil { 107 return err 108 } 109 110 return c.out.Write(ctx, results) 111 }