1
// Copyright 2015 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
8
"github.com/juju/errors"
10
"launchpad.net/gnuflag"
12
jujucloud "github.com/juju/juju/cloud"
15
type showCloudCommand struct {
23
Provided information includes 'defined' (public, built-in), 'type',
24
'auth-type', 'regions', and 'endpoints'.
28
juju show-cloud google
29
juju show-cloud azure-china --output ~/azure_cloud_details.txt
35
// NewShowCloudCommand returns a command to list cloud information.
36
func NewShowCloudCommand() cmd.Command {
37
return &showCloudCommand{}
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,
47
func (c *showCloudCommand) Init(args []string) error {
52
return errors.New("no cloud specified")
54
return cmd.CheckEmpty(args[1:])
57
func (c *showCloudCommand) Info() *cmd.Info {
61
Purpose: "Shows detailed information on a cloud.",
66
func (c *showCloudCommand) Run(ctxt *cmd.Context) error {
67
details, err := getCloudDetails()
71
cloud, ok := details[c.CloudName]
73
return errors.NotFoundf("cloud %q", c.CloudName)
75
return c.out.Write(ctxt, cloud)
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"`
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"`
97
func makeCloudDetails(cloud jujucloud.Cloud) *cloudDetails {
98
result := &cloudDetails{
100
CloudType: cloud.Type,
101
Endpoint: cloud.Endpoint,
102
StorageEndpoint: cloud.StorageEndpoint,
103
Config: cloud.Config,
105
result.AuthTypes = make([]string, len(cloud.AuthTypes))
106
for i, at := range cloud.AuthTypes {
107
result.AuthTypes[i] = string(at)
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
115
if region.StorageEndpoint != result.StorageEndpoint {
116
r.StorageEndpoint = region.StorageEndpoint
118
result.Regions = append(result.Regions, yaml.MapItem{r.Name, r})
119
result.RegionsMap[region.Name] = r
124
func getCloudDetails() (map[string]*cloudDetails, error) {
125
result, err := listCloudDetails()
127
return nil, errors.Trace(err)
129
return result.all(), nil