~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/cloud/show.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 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package cloud
 
5
 
 
6
import (
 
7
        "github.com/juju/cmd"
 
8
        "github.com/juju/errors"
 
9
        "gopkg.in/yaml.v2"
 
10
        "launchpad.net/gnuflag"
 
11
 
 
12
        jujucloud "github.com/juju/juju/cloud"
 
13
)
 
14
 
 
15
type showCloudCommand struct {
 
16
        cmd.CommandBase
 
17
        out cmd.Output
 
18
 
 
19
        CloudName string
 
20
}
 
21
 
 
22
var showCloudDoc = `
 
23
Provided information includes 'defined' (public, built-in), 'type',
 
24
'auth-type', 'regions', and 'endpoints'.
 
25
 
 
26
Examples:
 
27
 
 
28
    juju show-cloud google
 
29
    juju show-cloud azure-china --output ~/azure_cloud_details.txt
 
30
 
 
31
See also: clouds
 
32
          update-clouds
 
33
`
 
34
 
 
35
// NewShowCloudCommand returns a command to list cloud information.
 
36
func NewShowCloudCommand() cmd.Command {
 
37
        return &showCloudCommand{}
 
38
}
 
39
 
 
40
func (c *showCloudCommand) SetFlags(f *gnuflag.FlagSet) {
 
41
        // We only support yaml for display purposes.
 
42
        c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
 
43
                "yaml": cmd.FormatYaml,
 
44
        })
 
45
}
 
46
 
 
47
func (c *showCloudCommand) Init(args []string) error {
 
48
        switch len(args) {
 
49
        case 1:
 
50
                c.CloudName = args[0]
 
51
        default:
 
52
                return errors.New("no cloud specified")
 
53
        }
 
54
        return cmd.CheckEmpty(args[1:])
 
55
}
 
56
 
 
57
func (c *showCloudCommand) Info() *cmd.Info {
 
58
        return &cmd.Info{
 
59
                Name:    "show-cloud",
 
60
                Args:    "<cloud name>",
 
61
                Purpose: "Shows detailed information on a cloud.",
 
62
                Doc:     showCloudDoc,
 
63
        }
 
64
}
 
65
 
 
66
func (c *showCloudCommand) Run(ctxt *cmd.Context) error {
 
67
        details, err := getCloudDetails()
 
68
        if err != nil {
 
69
                return err
 
70
        }
 
71
        cloud, ok := details[c.CloudName]
 
72
        if !ok {
 
73
                return errors.NotFoundf("cloud %q", c.CloudName)
 
74
        }
 
75
        return c.out.Write(ctxt, cloud)
 
76
}
 
77
 
 
78
type regionDetails struct {
 
79
        Name            string `yaml:"-" json:"-"`
 
80
        Endpoint        string `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`
 
81
        StorageEndpoint string `yaml:"storage-endpoint,omitempty" json:"storage-endpoint,omitempty"`
 
82
}
 
83
 
 
84
type cloudDetails struct {
 
85
        Source          string   `yaml:"defined,omitempty" json:"defined,omitempty"`
 
86
        CloudType       string   `yaml:"type" json:"type"`
 
87
        AuthTypes       []string `yaml:"auth-types,omitempty,flow" json:"auth-types,omitempty"`
 
88
        Endpoint        string   `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`
 
89
        StorageEndpoint string   `yaml:"storage-endpoint,omitempty" json:"storage-endpoint,omitempty"`
 
90
        // Regions is for when we want to print regions in order for yaml or tabular output.
 
91
        Regions yaml.MapSlice `yaml:"regions,omitempty" json:"-"`
 
92
        // Regions map is for json marshalling where format is important but not order.
 
93
        RegionsMap map[string]regionDetails `yaml:"-" json:"regions,omitempty"`
 
94
        Config     map[string]interface{}   `yaml:"config,omitempty" json:"config,omitempty"`
 
95
}
 
96
 
 
97
func makeCloudDetails(cloud jujucloud.Cloud) *cloudDetails {
 
98
        result := &cloudDetails{
 
99
                Source:          "public",
 
100
                CloudType:       cloud.Type,
 
101
                Endpoint:        cloud.Endpoint,
 
102
                StorageEndpoint: cloud.StorageEndpoint,
 
103
                Config:          cloud.Config,
 
104
        }
 
105
        result.AuthTypes = make([]string, len(cloud.AuthTypes))
 
106
        for i, at := range cloud.AuthTypes {
 
107
                result.AuthTypes[i] = string(at)
 
108
        }
 
109
        result.RegionsMap = make(map[string]regionDetails)
 
110
        for _, region := range cloud.Regions {
 
111
                r := regionDetails{Name: region.Name}
 
112
                if region.Endpoint != result.Endpoint {
 
113
                        r.Endpoint = region.Endpoint
 
114
                }
 
115
                if region.StorageEndpoint != result.StorageEndpoint {
 
116
                        r.StorageEndpoint = region.StorageEndpoint
 
117
                }
 
118
                result.Regions = append(result.Regions, yaml.MapItem{r.Name, r})
 
119
                result.RegionsMap[region.Name] = r
 
120
        }
 
121
        return result
 
122
}
 
123
 
 
124
func getCloudDetails() (map[string]*cloudDetails, error) {
 
125
        result, err := listCloudDetails()
 
126
        if err != nil {
 
127
                return nil, errors.Trace(err)
 
128
        }
 
129
        return result.all(), nil
 
130
}