1
// Copyright 2014 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
11
"github.com/juju/errors"
12
"launchpad.net/gnuflag"
14
"github.com/juju/juju/cmd/envcmd"
15
"github.com/juju/juju/environs/configstore"
18
// APIInfoCommand returns the fields used to connect to an API server.
19
type APIInfoCommand struct {
33
Print the field values used to connect to the environment's API servers"
35
The exact fields to output can be specified on the command line. The
43
If "password" is included as a field, or the --password option is given, the
44
password value will be shown.
50
environ-uuid: 373b309b-4a86-4f13-88e2-c213d97075b8
55
ca-cert: '-----BEGIN CERTIFICATE-----
57
-----END CERTIFICATE-----
63
$ juju api-info user password
70
func (c *APIInfoCommand) Info() *cmd.Info {
74
Purpose: "print the field values used to connect to the environment's API servers",
79
func (c *APIInfoCommand) Init(args []string) error {
91
for _, name := range args {
106
unknown = append(unknown, fmt.Sprintf("%q", name))
109
if len(unknown) > 0 {
110
return errors.Errorf("unknown fields: %s", strings.Join(unknown, ", "))
116
func (c *APIInfoCommand) SetFlags(f *gnuflag.FlagSet) {
117
c.out.AddFlags(f, "default", map[string]cmd.Formatter{
119
"yaml": cmd.FormatYaml,
120
"json": cmd.FormatJson,
122
f.BoolVar(&c.refresh, "refresh", false, "connect to the API to ensure an up-to-date endpoint location")
123
f.BoolVar(&c.password, "password", false, "include the password in the output fields")
126
func connectionEndpoint(c envcmd.EnvCommandBase, refresh bool) (configstore.APIEndpoint, error) {
127
return c.ConnectionEndpoint(refresh)
130
func connectionCredentials(c envcmd.EnvCommandBase) (configstore.APICredentials, error) {
131
return c.ConnectionCredentials()
135
endpoint = connectionEndpoint
136
creds = connectionCredentials
139
// Print out the addresses of the API server endpoints.
140
func (c *APIInfoCommand) Run(ctx *cmd.Context) error {
141
apiendpoint, err := endpoint(c.EnvCommandBase, c.refresh)
145
credentials, err := creds(c.EnvCommandBase)
152
result.User = credentials.User
155
result.Password = credentials.Password
158
result.EnvironUUID = apiendpoint.EnvironUUID
161
result.StateServers = apiendpoint.Addresses
164
result.CACert = apiendpoint.CACert
167
result.ServerUUID = apiendpoint.ServerUUID
170
return c.out.Write(ctx, result)
173
func (c *APIInfoCommand) format(value interface{}) ([]byte, error) {
174
if len(c.fields) == 1 {
175
data := value.(InfoData)
176
field, err := data.field(c.fields[0])
180
switch value := field.(type) {
182
return []byte(strings.Join(value, "\n")), nil
184
return []byte(value), nil
186
return nil, errors.Errorf("Unsupported type %T", field)
190
return cmd.FormatYaml(value)
193
type InfoData struct {
194
User string `json:"user,omitempty" yaml:",omitempty"`
195
Password string `json:"password,omitempty" yaml:",omitempty"`
196
EnvironUUID string `json:"environ-uuid,omitempty" yaml:"environ-uuid,omitempty"`
197
ServerUUID string `json:"server-uuid,omitempty" yaml:"server-uuid,omitempty"`
198
StateServers []string `json:"state-servers,omitempty" yaml:"state-servers,omitempty"`
199
CACert string `json:"ca-cert,omitempty" yaml:"ca-cert,omitempty"`
202
func (i *InfoData) field(name string) (interface{}, error) {
207
return i.Password, nil
209
return i.EnvironUUID, nil
210
case "state-servers":
211
return i.StateServers, nil
215
return i.ServerUUID, nil
217
return "", errors.Errorf("unknown field %q", name)