forked from juju/juju
/
show.go
196 lines (174 loc) · 6.08 KB
/
show.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package cloud
import (
"fmt"
"github.com/juju/cmd"
"github.com/juju/errors"
"github.com/juju/gnuflag"
"gopkg.in/yaml.v2"
jujucloud "github.com/juju/juju/cloud"
jujucmd "github.com/juju/juju/cmd"
"github.com/juju/juju/cmd/juju/common"
)
type showCloudCommand struct {
cmd.CommandBase
out cmd.Output
CloudName string
includeConfig bool
}
var showCloudDoc = `
Provided information includes 'defined' (public, built-in), 'type',
'auth-type', 'regions', 'endpoints', and cloud specific configuration
options.
If ‘--include-config’ is used, additional configuration (key, type, and
description) specific to the cloud are displayed if available.
Examples:
juju show-cloud google
juju show-cloud azure-china --output ~/azure_cloud_details.txt
See also:
clouds
update-clouds
`
// NewShowCloudCommand returns a command to list cloud information.
func NewShowCloudCommand() cmd.Command {
return &showCloudCommand{}
}
func (c *showCloudCommand) SetFlags(f *gnuflag.FlagSet) {
c.CommandBase.SetFlags(f)
// We only support yaml for display purposes.
c.out.AddFlags(f, "yaml", map[string]cmd.Formatter{
"yaml": cmd.FormatYaml,
})
f.BoolVar(&c.includeConfig, "include-config", false, "Print available config option details specific to the specified cloud")
}
func (c *showCloudCommand) Init(args []string) error {
switch len(args) {
case 1:
c.CloudName = args[0]
default:
return errors.New("no cloud specified")
}
return cmd.CheckEmpty(args[1:])
}
func (c *showCloudCommand) Info() *cmd.Info {
return jujucmd.Info(&cmd.Info{
Name: "show-cloud",
Args: "<cloud name>",
Purpose: "Shows detailed information on a cloud.",
Doc: showCloudDoc,
})
}
func (c *showCloudCommand) Run(ctxt *cmd.Context) error {
details, err := GetAllCloudDetails()
if err != nil {
return err
}
cloud, ok := details[c.CloudName]
if !ok {
return errors.NotFoundf("cloud %q", c.CloudName)
}
if err = c.out.Write(ctxt, cloud); err != nil {
return err
}
if c.includeConfig {
config := getCloudConfigDetails(cloud.CloudType)
if len(config) > 0 {
fmt.Fprintln(ctxt.Stdout, fmt.Sprintf("\nThe available config options specific to %s clouds are:", cloud.CloudType))
return c.out.Write(ctxt, config)
}
}
return nil
}
// RegionDetails holds region details.
type RegionDetails struct {
Name string `yaml:"-" json:"-"`
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`
IdentityEndpoint string `yaml:"identity-endpoint,omitempty" json:"identity-endpoint,omitempty"`
StorageEndpoint string `yaml:"storage-endpoint,omitempty" json:"storage-endpoint,omitempty"`
}
// CloudDetails holds cloud details.
type CloudDetails struct {
Source string `yaml:"defined,omitempty" json:"defined,omitempty"`
CloudType string `yaml:"type" json:"type"`
CloudDescription string `yaml:"description" json:"description"`
AuthTypes []string `yaml:"auth-types,omitempty,flow" json:"auth-types,omitempty"`
Endpoint string `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`
IdentityEndpoint string `yaml:"identity-endpoint,omitempty" json:"identity-endpoint,omitempty"`
StorageEndpoint string `yaml:"storage-endpoint,omitempty" json:"storage-endpoint,omitempty"`
// Regions is for when we want to print regions in order for yaml output.
Regions yaml.MapSlice `yaml:"regions,omitempty" json:"-"`
// Regions map is for json marshalling where format is important but not order.
RegionsMap map[string]RegionDetails `yaml:"-" json:"regions,omitempty"`
Config map[string]interface{} `yaml:"config,omitempty" json:"config,omitempty"`
RegionConfig jujucloud.RegionConfig `yaml:"region-config,omitempty" json:"region-config,omitempty"`
CACredentials []string `yaml:"ca-credentials,omitempty" json:"ca-credentials,omitempty"`
}
func makeCloudDetails(cloud jujucloud.Cloud) *CloudDetails {
result := &CloudDetails{
Source: "public",
CloudType: cloud.Type,
Endpoint: cloud.Endpoint,
IdentityEndpoint: cloud.IdentityEndpoint,
StorageEndpoint: cloud.StorageEndpoint,
Config: cloud.Config,
RegionConfig: cloud.RegionConfig,
CloudDescription: cloud.Description,
CACredentials: cloud.CACertificates,
}
result.AuthTypes = make([]string, len(cloud.AuthTypes))
for i, at := range cloud.AuthTypes {
result.AuthTypes[i] = string(at)
}
result.RegionsMap = make(map[string]RegionDetails)
for _, region := range cloud.Regions {
r := RegionDetails{Name: region.Name}
if region.Endpoint != result.Endpoint {
r.Endpoint = region.Endpoint
}
if region.IdentityEndpoint != result.IdentityEndpoint {
r.IdentityEndpoint = region.IdentityEndpoint
}
if region.StorageEndpoint != result.StorageEndpoint {
r.StorageEndpoint = region.StorageEndpoint
}
result.Regions = append(result.Regions, yaml.MapItem{r.Name, r})
result.RegionsMap[region.Name] = r
}
return result
}
func getCloudConfigDetails(cloudType string) map[string]interface{} {
// providerSchema has all config options, including their descriptions
// and types.
providerSchema, err := common.CloudSchemaByType(cloudType)
if err != nil {
// Some providers do not implement the ProviderSchema interface.
return nil
}
specifics := make(map[string]interface{})
ps, err := common.ProviderConfigSchemaSourceByType(cloudType)
if err != nil {
// Some providers do not implement the ConfigSchema interface.
return nil
}
// ps.ConfigSchema() returns the provider specific config option names, but no
// description etc.
for attr := range ps.ConfigSchema() {
if providerSchema[attr].Secret {
continue
}
specifics[attr] = common.PrintConfigSchema{
Description: providerSchema[attr].Description,
Type: fmt.Sprintf("%s", providerSchema[attr].Type),
}
}
return specifics
}
// GetAllCloudDetails returns a list of all cloud details.
func GetAllCloudDetails() (map[string]*CloudDetails, error) {
result, err := listCloudDetails()
if err != nil {
return nil, errors.Trace(err)
}
return result.all(), nil
}