/
connection_impl.go
210 lines (184 loc) · 6.75 KB
/
connection_impl.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
package openstack
import (
"context"
"fmt"
"github.com/dihedron/steampipe-plugin-utils/utils"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)
const (
// defaults currently referring to Train
DefaultComputeV2Microversion = "2.79"
DefaultIdentityV3Microversion = "3.13"
DefaultBlockStorageV3Microversion = "3.59"
DefaultImageServiceV2Microversion = "2.9"
)
type ServiceType string
const (
// AuthenticatedClient is the cache key for the openStack authenticated client.
AuthenticatedClient = "openstack_authenticated_client"
// IdentityV3 identifies the OpenStack Identity V3 service (Keystone).
IdentityV3 ServiceType = "openstack_identity_v3"
// Compute identifies the penStack Compute V2 service (Nova).
ComputeV2 = "openstack_compute_v2"
// NetworkV2 identifies the OpenStack Network V2 service (Neutron).
NetworkV2 = "openstack_network_v2"
// BlockStorageV3 identifies the OpenStack Block Storage V3 service (Cinder).
BlockStorageV3 = "openstack_blockstorage_v3"
// ImageServiceV2 identifies the OpenStack Image Service V2 service (Glance).
ImageServiceV2 = "openstack_imageservice_v2"
)
type serviceConfig struct {
newClient func(client *gophercloud.ProviderClient, eo gophercloud.EndpointOpts) (*gophercloud.ServiceClient, error)
getMicroversion func(config *openstackConfig) string
}
var serviceConfigMap = map[ServiceType]serviceConfig{
IdentityV3: {
newClient: openstack.NewIdentityV3,
getMicroversion: func(config *openstackConfig) string {
microversion := DefaultIdentityV3Microversion
if config.IdentityV3Microversion != nil {
microversion = *config.IdentityV3Microversion
}
return microversion
},
},
ComputeV2: {
newClient: openstack.NewComputeV2,
getMicroversion: func(config *openstackConfig) string {
microversion := DefaultComputeV2Microversion
if config.ComputeV2Microversion != nil {
microversion = *config.ComputeV2Microversion
}
return microversion
},
},
NetworkV2: {
newClient: openstack.NewNetworkV2,
getMicroversion: func(config *openstackConfig) string {
// TODO: check if we need to leverage/support micro-versions
return ""
},
},
BlockStorageV3: {
newClient: openstack.NewBlockStorageV3,
getMicroversion: func(config *openstackConfig) string {
microversion := DefaultBlockStorageV3Microversion
if config.BlockStorageV3Microversion != nil {
microversion = *config.BlockStorageV3Microversion
}
return microversion
},
},
ImageServiceV2: {
newClient: openstack.NewImageServiceV2,
getMicroversion: func(config *openstackConfig) string {
microversion := DefaultImageServiceV2Microversion
if config.ImageServiceV2Microversion != nil {
microversion = *config.ImageServiceV2Microversion
}
return microversion
},
},
}
func getServiceClient(ctx context.Context, d *plugin.QueryData, key ServiceType) (*gophercloud.ServiceClient, error) {
plugin.Logger(ctx).Debug("returning service client", "type", key)
// load connection from cache, which preserves throttling protection etc
if cachedData, ok := d.ConnectionManager.Cache.Get(string(key)); ok {
plugin.Logger(ctx).Debug("returning service client from cache")
return cachedData.(*gophercloud.ServiceClient), nil
}
if _, ok := serviceConfigMap[key]; !ok {
plugin.Logger(ctx).Error("invalid service client type", "type", key)
panic(fmt.Sprintf("invalid service type: %q", key))
}
plugin.Logger(ctx).Info("creating new service client", "type", key)
api, err := getAuthenticatedClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("no valid authenticated provider client available", "error", err)
return nil, err
}
openstackConfig := GetConfig(d.Connection)
region := ""
if openstackConfig.Region != nil {
region = *openstackConfig.Region
}
client, err := serviceConfigMap[key].newClient(api, gophercloud.EndpointOpts{Region: region})
if err != nil {
plugin.Logger(ctx).Error("error creating service client", "type", key, "error", err)
return nil, err
}
client.Microversion = serviceConfigMap[key].getMicroversion(&openstackConfig)
// save to cache
plugin.Logger(ctx).Debug("saving service client to cache", "type", key)
d.ConnectionManager.Cache.Set(string(key), client)
return client, nil
}
// Create the OpenStack REST API client.
func getAuthenticatedClient(ctx context.Context, d *plugin.QueryData) (*gophercloud.ProviderClient, error) {
// load connection from cache, which preserves throttling protection etc
if cachedData, ok := d.ConnectionManager.Cache.Get(AuthenticatedClient); ok {
plugin.Logger(ctx).Debug("returning the authenticated client from cache")
return cachedData.(*gophercloud.ProviderClient), nil
}
plugin.Logger(ctx).Info("creating new authenticated client")
// try with the environment first
auth, err := openstack.AuthOptionsFromEnv()
if err != nil {
plugin.Logger(ctx).Info("no auth info available in environment, filling with defaults", "error", err)
// fill the auth info from the configuration
auth.AllowReauth = true
openstackConfig := GetConfig(d.Connection)
plugin.Logger(ctx).Info("configuration", "info", utils.ToPrettyJSON(openstackConfig))
if openstackConfig.EndpointUrl != nil {
auth.IdentityEndpoint = *openstackConfig.EndpointUrl
}
if openstackConfig.UserID != nil {
auth.UserID = *openstackConfig.UserID
}
if openstackConfig.Username != nil {
auth.Username = *openstackConfig.Username
}
if openstackConfig.Password != nil {
auth.Password = *openstackConfig.Password
}
if openstackConfig.ProjectID != nil {
auth.TenantID = *openstackConfig.ProjectID
}
if openstackConfig.ProjectName != nil {
auth.TenantName = *openstackConfig.ProjectName
}
if openstackConfig.DomainID != nil {
auth.DomainID = *openstackConfig.DomainID
}
if openstackConfig.DomainName != nil {
auth.DomainName = *openstackConfig.DomainName
}
if openstackConfig.AccessToken != nil {
auth.TokenID = *openstackConfig.AccessToken
}
if openstackConfig.AppCredentialID != nil {
auth.ApplicationCredentialID = *openstackConfig.AppCredentialID
}
if openstackConfig.AppCredentialSecret != nil {
auth.ApplicationCredentialSecret = *openstackConfig.AppCredentialSecret
}
if openstackConfig.AllowReauth != nil {
auth.AllowReauth = *openstackConfig.AllowReauth
}
}
//
// IMPORTANT NOTE: when using App Credentials, it is necessary
// that all other fields except the endpoint URL be left blank!
//
client, err := openstack.AuthenticatedClient(auth)
if err != nil {
plugin.Logger(ctx).Error("error creating authenticated client", "error", err)
return nil, err
}
// save to cache
plugin.Logger(ctx).Debug("saving authenticated client to cache")
d.ConnectionManager.Cache.Set(AuthenticatedClient, client)
return client, nil
}