~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/storage/volume.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
package storage
5
5
 
6
6
import (
 
7
        "fmt"
 
8
 
7
9
        "github.com/juju/cmd"
8
10
        "github.com/juju/errors"
9
11
        "github.com/juju/names"
10
12
 
11
13
        "github.com/juju/juju/apiserver/params"
12
 
        jujucmd "github.com/juju/juju/cmd"
13
14
        "github.com/juju/juju/cmd/juju/common"
 
15
        "github.com/juju/juju/status"
14
16
)
15
17
 
16
 
const volumeCmdDoc = `
17
 
"juju storage volume" is used to manage storage volumes in
18
 
 the Juju model.
19
 
`
20
 
 
21
 
const volumeCmdPurpose = "manage storage volumes"
22
 
 
23
 
// newVolumeSuperCommand creates the storage volume super subcommand and
24
 
// registers the subcommands that it supports.
25
 
func newVolumeSuperCommand() cmd.Command {
26
 
        supercmd := jujucmd.NewSubSuperCommand(cmd.SuperCommandParams{
27
 
                Name:        "volume",
28
 
                Doc:         volumeCmdDoc,
29
 
                UsagePrefix: "juju storage",
30
 
                Purpose:     volumeCmdPurpose,
31
 
        })
32
 
        supercmd.Register(newVolumeListCommand())
33
 
        return supercmd
34
 
}
35
 
 
36
 
// VolumeCommandBase is a helper base structure for volume commands.
37
 
type VolumeCommandBase struct {
38
 
        StorageCommandBase
39
 
}
40
 
 
41
18
// VolumeInfo defines the serialization behaviour for storage volume.
42
19
type VolumeInfo struct {
43
20
        // from params.Volume. This is provider-supplied unique volume id.
64
41
}
65
42
 
66
43
type EntityStatus struct {
67
 
        Current params.Status `json:"current,omitempty" yaml:"current,omitempty"`
 
44
        Current status.Status `json:"current,omitempty" yaml:"current,omitempty"`
68
45
        Message string        `json:"message,omitempty" yaml:"message,omitempty"`
69
46
        Since   string        `json:"since,omitempty" yaml:"since,omitempty"`
70
47
}
82
59
        // TODO(axw) add machine volume attachment status when we have it
83
60
}
84
61
 
 
62
//generateListVolumeOutput returns a map of volume info
 
63
func (c *listCommand) generateListVolumeOutput(ctx *cmd.Context, api StorageListAPI) (output interface{}, err error) {
 
64
 
 
65
        results, err := api.ListVolumes(c.ids)
 
66
        if err != nil {
 
67
                return nil, err
 
68
        }
 
69
        // filter out valid output, if any
 
70
        var valid []params.VolumeDetails
 
71
        for _, result := range results {
 
72
                if result.Error == nil {
 
73
                        valid = append(valid, result.Result...)
 
74
                        continue
 
75
                }
 
76
                // display individual error
 
77
                fmt.Fprintf(ctx.Stderr, "%v\n", result.Error)
 
78
        }
 
79
        if len(valid) == 0 {
 
80
                return nil, nil
 
81
        }
 
82
        info, err := convertToVolumeInfo(valid)
 
83
        if err != nil {
 
84
                return nil, err
 
85
        }
 
86
        switch c.out.Name() {
 
87
        case "yaml", "json":
 
88
                output = map[string]map[string]VolumeInfo{"volumes": info}
 
89
        default:
 
90
                output = info
 
91
        }
 
92
        return output, nil
 
93
}
 
94
 
85
95
// convertToVolumeInfo returns a map of volume IDs to volume info.
86
96
func convertToVolumeInfo(all []params.VolumeDetails) (map[string]VolumeInfo, error) {
87
97
        result := make(map[string]VolumeInfo)