/
provider.go
183 lines (157 loc) · 5.5 KB
/
provider.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package vsphere
import (
stdcontext "context"
"net/url"
"github.com/juju/errors"
"github.com/juju/jsonschema"
"github.com/juju/loggo"
"golang.org/x/net/context"
"github.com/juju/juju/cloud"
"github.com/juju/juju/environs"
environscloudspec "github.com/juju/juju/environs/cloudspec"
"github.com/juju/juju/environs/config"
callcontext "github.com/juju/juju/environs/context"
)
var logger = loggo.GetLogger("juju.provider.vmware")
const (
// provider version 1 organises VMs into folders.
providerVersion1 = 1
currentProviderVersion = providerVersion1
)
type environProvider struct {
environProviderCredentials
dial DialFunc
}
var _ config.ConfigSchemaSource = (*environProvider)(nil)
// EnvironProviderConfig contains configuration for the EnvironProvider.
type EnvironProviderConfig struct {
// Dial is a function used for dialing connections to vCenter/ESXi.
Dial DialFunc
}
// NewEnvironProvider returns a new environs.EnvironProvider that will
// dial vSphere connections with the given dial function.
func NewEnvironProvider(config EnvironProviderConfig) environs.CloudEnvironProvider {
return &environProvider{
dial: config.Dial,
}
}
// Version implements environs.EnvironProvider.
func (p *environProvider) Version() int {
return currentProviderVersion
}
// Open implements environs.EnvironProvider.
func (p *environProvider) Open(_ stdcontext.Context, args environs.OpenParams) (environs.Environ, error) {
if err := validateCloudSpec(args.Cloud); err != nil {
return nil, errors.Annotate(err, "validating cloud spec")
}
env, err := newEnviron(p, args.Cloud, args.Config)
return env, errors.Trace(err)
}
var cloudSchema = &jsonschema.Schema{
Type: []jsonschema.Type{jsonschema.ObjectType},
Required: []string{cloud.EndpointKey, cloud.AuthTypesKey, cloud.RegionsKey},
Order: []string{cloud.EndpointKey, cloud.AuthTypesKey, cloud.RegionsKey},
Properties: map[string]*jsonschema.Schema{
cloud.EndpointKey: {
Singular: "the vCenter address or URL",
Type: []jsonschema.Type{jsonschema.StringType},
Format: jsonschema.FormatURI,
},
cloud.AuthTypesKey: {
// don't need a prompt, since there's only one choice.
Type: []jsonschema.Type{jsonschema.ArrayType},
Enum: []interface{}{[]string{string(cloud.UserPassAuthType)}},
},
cloud.RegionsKey: {
Type: []jsonschema.Type{jsonschema.ObjectType},
Singular: "datacenter",
Plural: "datacenters",
AdditionalProperties: &jsonschema.Schema{
Type: []jsonschema.Type{jsonschema.ObjectType},
MaxProperties: jsonschema.Int(0),
},
},
},
}
// CloudSchema returns the schema for adding new clouds of this type.
func (p *environProvider) CloudSchema() *jsonschema.Schema {
return cloudSchema
}
const failedLoginMsg = "ServerFaultCode: Cannot complete login due to an incorrect user name or password."
// Ping tests the connection to the cloud, to verify the endpoint is valid.
func (p *environProvider) Ping(callCtx callcontext.ProviderCallContext, endpoint string) error {
// try to be smart and not punish people for adding or forgetting http
u, err := url.Parse(endpoint)
if err != nil {
return errors.New("Invalid endpoint format, please give a full url or IP/hostname.")
}
switch u.Scheme {
case "http", "https":
// good!
case "":
u, err = url.Parse("https://" + endpoint + "/sdk")
if err != nil {
return errors.New("Invalid endpoint format, please give a full url or IP/hostname.")
}
default:
return errors.New("Invalid endpoint format, please use an http or https URL.")
}
// Set a user, to force the dial function to perform a login. The login
// should fail, since there's no password set.
u.User = url.User("juju")
ctx := context.Background()
client, err := p.dial(ctx, u, "")
if err != nil {
if err.Error() == failedLoginMsg {
// This is our expected error for trying to log into
// vSphere without any creds, so return nil.
return nil
}
logger.Errorf("Unexpected error dialing vSphere connection: %v", err)
return errors.Errorf("No vCenter/ESXi available at %s", endpoint)
}
defer client.Close(ctx)
// We shouldn't get here, since we haven't set a password, but it is
// theoretically possible to have user="juju", password="".
return nil
}
// PrepareConfig implements environs.EnvironProvider.
func (p *environProvider) PrepareConfig(args environs.PrepareConfigParams) (*config.Config, error) {
if err := validateCloudSpec(args.Cloud); err != nil {
return nil, errors.Annotate(err, "validating cloud spec")
}
return args.Config, nil
}
// Validate implements environs.EnvironProvider.
func (*environProvider) Validate(cfg, old *config.Config) (valid *config.Config, err error) {
if old == nil {
ecfg, err := newValidConfig(cfg)
if err != nil {
return nil, errors.Annotate(err, "invalid config")
}
return ecfg.Config, nil
}
ecfg, err := newValidConfig(old)
if err != nil {
return nil, errors.Annotate(err, "invalid base config")
}
if err := ecfg.update(cfg); err != nil {
return nil, errors.Annotate(err, "invalid config change")
}
return ecfg.Config, nil
}
func validateCloudSpec(spec environscloudspec.CloudSpec) error {
if err := spec.Validate(); err != nil {
return errors.Trace(err)
}
// TODO(axw) add validation of endpoint/region.
if spec.Credential == nil {
return errors.NotValidf("missing credential")
}
if authType := spec.Credential.AuthType(); authType != cloud.UserPassAuthType {
return errors.NotSupportedf("%q auth-type", authType)
}
return nil
}