blob: 9f0b64fb43f9959b700ccb67871e715903273bd9 [file] [log] [blame]
Joe Topjian1c15e3f2016-08-08 10:48:38 -06001// Package clients contains functions for creating OpenStack service clients
2// for use in acceptance tests. It also manages the required environment
3// variables to run the tests.
4package clients
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -08005
6import (
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -08007 "fmt"
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -08008 "os"
Ash Wilsonfd566482014-09-23 15:47:35 -04009 "strings"
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +020010
Krzysztof Szukiełojć3f41d082017-05-07 14:43:06 +020011 "gerrit.mcp.mirantis.net/debian/gophercloud.git"
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +020012 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack"
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080013)
14
Joe Topjian1c15e3f2016-08-08 10:48:38 -060015// AcceptanceTestChoices contains image and flavor selections for use by the acceptance tests.
16type AcceptanceTestChoices struct {
Ash Wilsonfd566482014-09-23 15:47:35 -040017 // ImageID contains the ID of a valid image.
18 ImageID string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080019
Ash Wilsonfd566482014-09-23 15:47:35 -040020 // FlavorID contains the ID of a valid flavor.
21 FlavorID string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080022
Ash Wilsonfd566482014-09-23 15:47:35 -040023 // FlavorIDResize contains the ID of a different flavor available on the same OpenStack installation, that is distinct
24 // from FlavorID.
25 FlavorIDResize string
Joe Topjianb4395c72015-02-24 02:47:23 +000026
Joe Topjianb720d842016-07-24 02:11:46 +000027 // FloatingIPPool contains the name of the pool from where to obtain floating IPs.
28 FloatingIPPoolName string
29
Joe Topjianb4395c72015-02-24 02:47:23 +000030 // NetworkName is the name of a network to launch the instance on.
31 NetworkName string
Joe Topjian7c8dd022016-09-01 12:02:04 -060032
33 // ExternalNetworkID is the network ID of the external network.
34 ExternalNetworkID string
Mikko Valkonen9368c002017-01-16 18:31:39 +020035
36 // ShareNetworkID is the Manila Share network ID
37 ShareNetworkID string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080038}
39
Joe Topjian1c15e3f2016-08-08 10:48:38 -060040// AcceptanceTestChoicesFromEnv populates a ComputeChoices struct from environment variables.
Ash Wilsonfd566482014-09-23 15:47:35 -040041// If any required state is missing, an `error` will be returned that enumerates the missing properties.
Joe Topjian1c15e3f2016-08-08 10:48:38 -060042func AcceptanceTestChoicesFromEnv() (*AcceptanceTestChoices, error) {
Ash Wilsonfd566482014-09-23 15:47:35 -040043 imageID := os.Getenv("OS_IMAGE_ID")
44 flavorID := os.Getenv("OS_FLAVOR_ID")
45 flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE")
Joe Topjianb4395c72015-02-24 02:47:23 +000046 networkName := os.Getenv("OS_NETWORK_NAME")
Joe Topjianb720d842016-07-24 02:11:46 +000047 floatingIPPoolName := os.Getenv("OS_POOL_NAME")
Joe Topjian7c8dd022016-09-01 12:02:04 -060048 externalNetworkID := os.Getenv("OS_EXTGW_ID")
Mikko Valkonen9368c002017-01-16 18:31:39 +020049 shareNetworkID := os.Getenv("OS_SHARE_NETWORK_ID")
Ash Wilsonfd566482014-09-23 15:47:35 -040050
51 missing := make([]string, 0, 3)
52 if imageID == "" {
53 missing = append(missing, "OS_IMAGE_ID")
54 }
55 if flavorID == "" {
56 missing = append(missing, "OS_FLAVOR_ID")
57 }
58 if flavorIDResize == "" {
59 missing = append(missing, "OS_FLAVOR_ID_RESIZE")
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080060 }
Joe Topjianb720d842016-07-24 02:11:46 +000061 if floatingIPPoolName == "" {
62 missing = append(missing, "OS_POOL_NAME")
63 }
Joe Topjian7c8dd022016-09-01 12:02:04 -060064 if externalNetworkID == "" {
65 missing = append(missing, "OS_EXTGW_ID")
66 }
Joe Topjianb4395c72015-02-24 02:47:23 +000067 if networkName == "" {
Joe Topjianb720d842016-07-24 02:11:46 +000068 networkName = "private"
Joe Topjianb4395c72015-02-24 02:47:23 +000069 }
Mikko Valkonen9368c002017-01-16 18:31:39 +020070 if shareNetworkID == "" {
71 missing = append(missing, "OS_SHARE_NETWORK_ID")
72 }
Ash Wilsonfd566482014-09-23 15:47:35 -040073 notDistinct := ""
74 if flavorID == flavorIDResize {
75 notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct."
76 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080077
Ash Wilsonfd566482014-09-23 15:47:35 -040078 if len(missing) > 0 || notDistinct != "" {
79 text := "You're missing some important setup:\n"
80 if len(missing) > 0 {
81 text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n"
82 }
83 if notDistinct != "" {
84 text += " * " + notDistinct + "\n"
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080085 }
86
Ash Wilsonfd566482014-09-23 15:47:35 -040087 return nil, fmt.Errorf(text)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070088 }
89
Mikko Valkonen9368c002017-01-16 18:31:39 +020090 return &AcceptanceTestChoices{
91 ImageID: imageID,
92 FlavorID: flavorID,
93 FlavorIDResize: flavorIDResize,
94 FloatingIPPoolName: floatingIPPoolName,
95 NetworkName: networkName,
96 ExternalNetworkID: externalNetworkID,
97 ShareNetworkID: shareNetworkID,
98 }, nil
Joe Topjian1c15e3f2016-08-08 10:48:38 -060099}
100
101// NewBlockStorageV1Client returns a *ServiceClient for making calls
102// to the OpenStack Block Storage v1 API. An error will be returned
103// if authentication or client creation was not possible.
104func NewBlockStorageV1Client() (*gophercloud.ServiceClient, error) {
105 ao, err := openstack.AuthOptionsFromEnv()
106 if err != nil {
107 return nil, err
108 }
109
110 client, err := openstack.AuthenticatedClient(ao)
111 if err != nil {
112 return nil, err
113 }
114
115 return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
116 Region: os.Getenv("OS_REGION_NAME"),
117 })
118}
119
Joe Topjian68bed5f2016-08-10 15:30:57 -0600120// NewBlockStorageV2Client returns a *ServiceClient for making calls
121// to the OpenStack Block Storage v2 API. An error will be returned
122// if authentication or client creation was not possible.
123func NewBlockStorageV2Client() (*gophercloud.ServiceClient, error) {
124 ao, err := openstack.AuthOptionsFromEnv()
125 if err != nil {
126 return nil, err
127 }
128
129 client, err := openstack.AuthenticatedClient(ao)
130 if err != nil {
131 return nil, err
132 }
133
134 return openstack.NewBlockStorageV2(client, gophercloud.EndpointOpts{
135 Region: os.Getenv("OS_REGION_NAME"),
136 })
137}
138
ehdou10f1f852016-10-14 20:58:23 +0300139// NewSharedFileSystemV2Client returns a *ServiceClient for making calls
140// to the OpenStack Shared File System v2 API. An error will be returned
141// if authentication or client creation was not possible.
142func NewSharedFileSystemV2Client() (*gophercloud.ServiceClient, error) {
143 ao, err := openstack.AuthOptionsFromEnv()
144 if err != nil {
145 return nil, err
146 }
147
148 client, err := openstack.AuthenticatedClient(ao)
149 if err != nil {
150 return nil, err
151 }
152
153 return openstack.NewSharedFileSystemV2(client, gophercloud.EndpointOpts{
154 Region: os.Getenv("OS_REGION_NAME"),
155 })
156}
157
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600158// NewComputeV2Client returns a *ServiceClient for making calls
159// to the OpenStack Compute v2 API. An error will be returned
160// if authentication or client creation was not possible.
161func NewComputeV2Client() (*gophercloud.ServiceClient, error) {
162 ao, err := openstack.AuthOptionsFromEnv()
163 if err != nil {
164 return nil, err
165 }
166
167 client, err := openstack.AuthenticatedClient(ao)
168 if err != nil {
169 return nil, err
170 }
171
172 return openstack.NewComputeV2(client, gophercloud.EndpointOpts{
173 Region: os.Getenv("OS_REGION_NAME"),
174 })
175}
176
Joe Topjian71b85bd2017-03-09 18:55:36 -0700177// NewDNSV2Client returns a *ServiceClient for making calls
178// to the OpenStack Compute v2 API. An error will be returned
179// if authentication or client creation was not possible.
180func NewDNSV2Client() (*gophercloud.ServiceClient, error) {
181 ao, err := openstack.AuthOptionsFromEnv()
182 if err != nil {
183 return nil, err
184 }
185
186 client, err := openstack.AuthenticatedClient(ao)
187 if err != nil {
188 return nil, err
189 }
190
191 return openstack.NewDNSV2(client, gophercloud.EndpointOpts{
192 Region: os.Getenv("OS_REGION_NAME"),
193 })
194}
195
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600196// NewIdentityV2Client returns a *ServiceClient for making calls
197// to the OpenStack Identity v2 API. An error will be returned
198// if authentication or client creation was not possible.
199func NewIdentityV2Client() (*gophercloud.ServiceClient, error) {
200 ao, err := openstack.AuthOptionsFromEnv()
201 if err != nil {
202 return nil, err
203 }
204
205 client, err := openstack.AuthenticatedClient(ao)
206 if err != nil {
207 return nil, err
208 }
209
210 return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{
211 Region: os.Getenv("OS_REGION_NAME"),
212 })
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700213}
Joe Topjian918f5732016-08-15 08:47:08 -0600214
215// NewIdentityV2AdminClient returns a *ServiceClient for making calls
216// to the Admin Endpoint of the OpenStack Identity v2 API. An error
217// will be returned if authentication or client creation was not possible.
218func NewIdentityV2AdminClient() (*gophercloud.ServiceClient, error) {
219 ao, err := openstack.AuthOptionsFromEnv()
220 if err != nil {
221 return nil, err
222 }
223
224 client, err := openstack.AuthenticatedClient(ao)
225 if err != nil {
226 return nil, err
227 }
228
229 return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{
230 Region: os.Getenv("OS_REGION_NAME"),
231 Availability: gophercloud.AvailabilityAdmin,
232 })
233}
234
235// NewIdentityV2UnauthenticatedClient returns an unauthenticated *ServiceClient
236// for the OpenStack Identity v2 API. An error will be returned if
237// authentication or client creation was not possible.
238func NewIdentityV2UnauthenticatedClient() (*gophercloud.ServiceClient, error) {
239 ao, err := openstack.AuthOptionsFromEnv()
240 if err != nil {
241 return nil, err
242 }
243
244 client, err := openstack.NewClient(ao.IdentityEndpoint)
245 if err != nil {
246 return nil, err
247 }
248
249 return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{})
250}
251
252// NewIdentityV3Client returns a *ServiceClient for making calls
253// to the OpenStack Identity v3 API. An error will be returned
254// if authentication or client creation was not possible.
255func NewIdentityV3Client() (*gophercloud.ServiceClient, error) {
256 ao, err := openstack.AuthOptionsFromEnv()
257 if err != nil {
258 return nil, err
259 }
260
261 client, err := openstack.AuthenticatedClient(ao)
262 if err != nil {
263 return nil, err
264 }
265
266 return openstack.NewIdentityV3(client, gophercloud.EndpointOpts{
267 Region: os.Getenv("OS_REGION_NAME"),
268 })
269}
270
271// NewIdentityV3UnauthenticatedClient returns an unauthenticated *ServiceClient
272// for the OpenStack Identity v3 API. An error will be returned if
273// authentication or client creation was not possible.
274func NewIdentityV3UnauthenticatedClient() (*gophercloud.ServiceClient, error) {
275 ao, err := openstack.AuthOptionsFromEnv()
276 if err != nil {
277 return nil, err
278 }
279
280 client, err := openstack.NewClient(ao.IdentityEndpoint)
281 if err != nil {
282 return nil, err
283 }
284
285 return openstack.NewIdentityV3(client, gophercloud.EndpointOpts{})
286}
Joe Topjian7c8dd022016-09-01 12:02:04 -0600287
Joe Topjian929e60b2017-02-20 15:31:15 -0700288// NewImageServiceV2Client returns a *ServiceClient for making calls to the
289// OpenStack Image v2 API. An error will be returned if authentication or
290// client creation was not possible.
291func NewImageServiceV2Client() (*gophercloud.ServiceClient, error) {
292 ao, err := openstack.AuthOptionsFromEnv()
293 if err != nil {
294 return nil, err
295 }
296
297 client, err := openstack.AuthenticatedClient(ao)
298 if err != nil {
299 return nil, err
300 }
301
302 return openstack.NewImageServiceV2(client, gophercloud.EndpointOpts{
303 Region: os.Getenv("OS_REGION_NAME"),
304 })
305}
306
Joe Topjian7c8dd022016-09-01 12:02:04 -0600307// NewNetworkV2Client returns a *ServiceClient for making calls to the
308// OpenStack Networking v2 API. An error will be returned if authentication
309// or client creation was not possible.
310func NewNetworkV2Client() (*gophercloud.ServiceClient, error) {
311 ao, err := openstack.AuthOptionsFromEnv()
312 if err != nil {
313 return nil, err
314 }
315
316 client, err := openstack.AuthenticatedClient(ao)
317 if err != nil {
318 return nil, err
319 }
320
321 return openstack.NewNetworkV2(client, gophercloud.EndpointOpts{
322 Region: os.Getenv("OS_REGION_NAME"),
323 })
324}