1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
8
"github.com/juju/errors"
9
"gopkg.in/juju/names.v2"
10
"launchpad.net/gnuflag"
12
"github.com/juju/juju/api/modelmanager"
13
"github.com/juju/juju/cmd/modelcmd"
16
// NewDumpCommand returns a fully constructed dump-model command.
17
func NewDumpCommand() cmd.Command {
18
return modelcmd.Wrap(&dumpCommand{})
21
type dumpCommand struct {
22
modelcmd.ModelCommandBase
27
const dumpModelHelpDoc = `
28
Calls export on the model's database representation and writes the
29
resulting YAML to stdout.
34
juju dump-model -m mymodel
40
// Info implements Command.
41
func (c *dumpCommand) Info() *cmd.Info {
44
Purpose: "Displays the database agnostic representation of the model.",
45
Doc: dumpModelHelpDoc,
49
// SetFlags implements Command.
50
func (c *dumpCommand) SetFlags(f *gnuflag.FlagSet) {
51
c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
52
"yaml": cmd.FormatYaml,
53
"json": cmd.FormatJson,
57
// Init implements Command.
58
func (c *dumpCommand) Init(args []string) (err error) {
59
return cmd.CheckEmpty(args)
62
// DumpModelAPI specifies the used function calls of the ModelManager.
63
type DumpModelAPI interface {
65
DumpModel(names.ModelTag) (map[string]interface{}, error)
68
func (c *dumpCommand) getAPI() (DumpModelAPI, error) {
72
root, err := c.NewAPIRoot()
74
return nil, errors.Trace(err)
76
return modelmanager.NewClient(root), nil
79
// Run implements Command.
80
func (c *dumpCommand) Run(ctx *cmd.Context) error {
81
client, err := c.getAPI()
87
store := c.ClientStore()
88
modelDetails, err := store.ModelByName(
93
return errors.Annotate(err, "getting model details")
96
modelTag := names.NewModelTag(modelDetails.ModelUUID)
97
results, err := client.DumpModel(modelTag)
102
return c.out.Write(ctx, results)