-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
310 lines (264 loc) · 8.32 KB
/
config.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package openstack
import (
"crypto/tls"
"crypto/x509"
"fmt"
"log"
"net/http"
"os"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/objectstorage/v1/swauth"
"github.com/gophercloud/utils/openstack/clientconfig"
"github.com/hashicorp/terraform/helper/pathorcontents"
"github.com/hashicorp/terraform/terraform"
)
type Config struct {
CACertFile string
ClientCertFile string
ClientKeyFile string
Cloud string
DefaultDomain string
DomainID string
DomainName string
EndpointType string
IdentityEndpoint string
Insecure *bool
Password string
ProjectDomainName string
ProjectDomainID string
Region string
Swauth bool
TenantID string
TenantName string
Token string
UserDomainName string
UserDomainID string
Username string
UserID string
useOctavia bool
OsClient *gophercloud.ProviderClient
}
func (c *Config) LoadAndValidate() error {
// Make sure at least one of auth_url or cloud was specified.
if c.IdentityEndpoint == "" && c.Cloud == "" {
return fmt.Errorf("One of 'auth_url' or 'cloud' must be specified")
}
validEndpoint := false
validEndpoints := []string{
"internal", "internalURL",
"admin", "adminURL",
"public", "publicURL",
"",
}
for _, endpoint := range validEndpoints {
if c.EndpointType == endpoint {
validEndpoint = true
}
}
if !validEndpoint {
return fmt.Errorf("Invalid endpoint type provided")
}
clientOpts := new(clientconfig.ClientOpts)
// If a cloud entry was given, base AuthOptions on a clouds.yaml file.
if c.Cloud != "" {
clientOpts.Cloud = c.Cloud
cloud, err := clientconfig.GetCloudFromYAML(clientOpts)
if err != nil {
return err
}
if c.Region == "" && cloud.RegionName != "" {
c.Region = cloud.RegionName
}
if c.CACertFile == "" && cloud.CACertFile != "" {
c.CACertFile = cloud.CACertFile
}
if c.ClientCertFile == "" && cloud.ClientCertFile != "" {
c.ClientCertFile = cloud.ClientCertFile
}
if c.ClientKeyFile == "" && cloud.ClientKeyFile != "" {
c.ClientKeyFile = cloud.ClientKeyFile
}
if c.Insecure == nil && cloud.Verify != nil {
v := (!*cloud.Verify)
c.Insecure = &v
}
} else {
authInfo := &clientconfig.AuthInfo{
AuthURL: c.IdentityEndpoint,
DefaultDomain: c.DefaultDomain,
DomainID: c.DomainID,
DomainName: c.DomainName,
Password: c.Password,
ProjectDomainID: c.ProjectDomainID,
ProjectDomainName: c.ProjectDomainName,
ProjectID: c.TenantID,
ProjectName: c.TenantName,
Token: c.Token,
UserDomainID: c.UserDomainID,
UserDomainName: c.UserDomainName,
Username: c.Username,
UserID: c.UserID,
}
clientOpts.AuthInfo = authInfo
}
ao, err := clientconfig.AuthOptions(clientOpts)
if err != nil {
return err
}
client, err := openstack.NewClient(ao.IdentityEndpoint)
if err != nil {
return err
}
// Set UserAgent
client.UserAgent.Prepend(terraform.UserAgentString())
config := &tls.Config{}
if c.CACertFile != "" {
caCert, _, err := pathorcontents.Read(c.CACertFile)
if err != nil {
return fmt.Errorf("Error reading CA Cert: %s", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM([]byte(caCert))
config.RootCAs = caCertPool
}
if c.Insecure == nil {
config.InsecureSkipVerify = false
} else {
config.InsecureSkipVerify = *c.Insecure
}
if c.ClientCertFile != "" && c.ClientKeyFile != "" {
clientCert, _, err := pathorcontents.Read(c.ClientCertFile)
if err != nil {
return fmt.Errorf("Error reading Client Cert: %s", err)
}
clientKey, _, err := pathorcontents.Read(c.ClientKeyFile)
if err != nil {
return fmt.Errorf("Error reading Client Key: %s", err)
}
cert, err := tls.X509KeyPair([]byte(clientCert), []byte(clientKey))
if err != nil {
return err
}
config.Certificates = []tls.Certificate{cert}
config.BuildNameToCertificate()
}
// if OS_DEBUG is set, log the requests and responses
var osDebug bool
if os.Getenv("OS_DEBUG") != "" {
osDebug = true
}
transport := &http.Transport{Proxy: http.ProxyFromEnvironment, TLSClientConfig: config}
client.HTTPClient = http.Client{
Transport: &LogRoundTripper{
Rt: transport,
OsDebug: osDebug,
},
}
// If using Swift Authentication, there's no need to validate authentication normally.
if !c.Swauth {
err = openstack.Authenticate(client, *ao)
if err != nil {
return err
}
}
c.OsClient = client
return nil
}
func (c *Config) determineRegion(region string) string {
// If a resource-level region was not specified, and a provider-level region was set,
// use the provider-level region.
if region == "" && c.Region != "" {
region = c.Region
}
log.Printf("[DEBUG] OpenStack Region is: %s", region)
return region
}
func (c *Config) blockStorageV1Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewBlockStorageV1(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) blockStorageV2Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewBlockStorageV2(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) blockStorageV3Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewBlockStorageV3(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) computeV2Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewComputeV2(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) dnsV2Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewDNSV2(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) identityV3Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewIdentityV3(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) imageV2Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewImageServiceV2(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) networkingV2Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewNetworkV2(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) objectStorageV1Client(region string) (*gophercloud.ServiceClient, error) {
// If Swift Authentication is being used, return a swauth client.
if c.Swauth {
return swauth.NewObjectStorageV1(c.OsClient, swauth.AuthOpts{
User: c.Username,
Key: c.Password,
})
}
return openstack.NewObjectStorageV1(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) loadBalancerV2Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewLoadBalancerV2(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) databaseV1Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewDBV1(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) containerInfraV1Client(region string) (*gophercloud.ServiceClient, error) {
return openstack.NewContainerInfraV1(c.OsClient, gophercloud.EndpointOpts{
Region: c.determineRegion(region),
Availability: c.getEndpointType(),
})
}
func (c *Config) getEndpointType() gophercloud.Availability {
if c.EndpointType == "internal" || c.EndpointType == "internalURL" {
return gophercloud.AvailabilityInternal
}
if c.EndpointType == "admin" || c.EndpointType == "adminURL" {
return gophercloud.AvailabilityAdmin
}
return gophercloud.AvailabilityPublic
}