~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/model/dump.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "gopkg.in/juju/names.v2"
 
10
        "launchpad.net/gnuflag"
 
11
 
 
12
        "github.com/juju/juju/api/modelmanager"
 
13
        "github.com/juju/juju/cmd/modelcmd"
 
14
)
 
15
 
 
16
// NewDumpCommand returns a fully constructed dump-model command.
 
17
func NewDumpCommand() cmd.Command {
 
18
        return modelcmd.Wrap(&dumpCommand{})
 
19
}
 
20
 
 
21
type dumpCommand struct {
 
22
        modelcmd.ModelCommandBase
 
23
        out cmd.Output
 
24
        api DumpModelAPI
 
25
}
 
26
 
 
27
const dumpModelHelpDoc = `
 
28
Calls export on the model's database representation and writes the
 
29
resulting YAML to stdout.
 
30
 
 
31
Examples:
 
32
 
 
33
    juju dump-model
 
34
    juju dump-model -m mymodel
 
35
 
 
36
See also:
 
37
    models
 
38
`
 
39
 
 
40
// Info implements Command.
 
41
func (c *dumpCommand) Info() *cmd.Info {
 
42
        return &cmd.Info{
 
43
                Name:    "dump-model",
 
44
                Purpose: "Displays the database agnostic representation of the model.",
 
45
                Doc:     dumpModelHelpDoc,
 
46
        }
 
47
}
 
48
 
 
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,
 
54
        })
 
55
}
 
56
 
 
57
// Init implements Command.
 
58
func (c *dumpCommand) Init(args []string) (err error) {
 
59
        return cmd.CheckEmpty(args)
 
60
}
 
61
 
 
62
// DumpModelAPI specifies the used function calls of the ModelManager.
 
63
type DumpModelAPI interface {
 
64
        Close() error
 
65
        DumpModel(names.ModelTag) (map[string]interface{}, error)
 
66
}
 
67
 
 
68
func (c *dumpCommand) getAPI() (DumpModelAPI, error) {
 
69
        if c.api != nil {
 
70
                return c.api, nil
 
71
        }
 
72
        root, err := c.NewAPIRoot()
 
73
        if err != nil {
 
74
                return nil, errors.Trace(err)
 
75
        }
 
76
        return modelmanager.NewClient(root), nil
 
77
}
 
78
 
 
79
// Run implements Command.
 
80
func (c *dumpCommand) 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
        modelDetails, err := store.ModelByName(
 
89
                c.ControllerName(),
 
90
                c.ModelName(),
 
91
        )
 
92
        if err != nil {
 
93
                return errors.Annotate(err, "getting model details")
 
94
        }
 
95
 
 
96
        modelTag := names.NewModelTag(modelDetails.ModelUUID)
 
97
        results, err := client.DumpModel(modelTag)
 
98
        if err != nil {
 
99
                return err
 
100
        }
 
101
 
 
102
        return c.out.Write(ctx, results)
 
103
}