blob: 8bf4aa37f4036bef3045f45a9c69363cb6fa5ffe [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
Jon Perritt27249f42016-02-18 10:35:59 -060011 "github.com/gophercloud/gophercloud"
Jon Perritt27249f42016-02-18 10:35:59 -060012 "github.com/gophercloud/gophercloud/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
177// NewIdentityV2Client returns a *ServiceClient for making calls
178// to the OpenStack Identity v2 API. An error will be returned
179// if authentication or client creation was not possible.
180func NewIdentityV2Client() (*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.NewIdentityV2(client, gophercloud.EndpointOpts{
192 Region: os.Getenv("OS_REGION_NAME"),
193 })
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700194}
Joe Topjian918f5732016-08-15 08:47:08 -0600195
196// NewIdentityV2AdminClient returns a *ServiceClient for making calls
197// to the Admin Endpoint of the OpenStack Identity v2 API. An error
198// will be returned if authentication or client creation was not possible.
199func NewIdentityV2AdminClient() (*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 Availability: gophercloud.AvailabilityAdmin,
213 })
214}
215
216// NewIdentityV2UnauthenticatedClient returns an unauthenticated *ServiceClient
217// for the OpenStack Identity v2 API. An error will be returned if
218// authentication or client creation was not possible.
219func NewIdentityV2UnauthenticatedClient() (*gophercloud.ServiceClient, error) {
220 ao, err := openstack.AuthOptionsFromEnv()
221 if err != nil {
222 return nil, err
223 }
224
225 client, err := openstack.NewClient(ao.IdentityEndpoint)
226 if err != nil {
227 return nil, err
228 }
229
230 return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{})
231}
232
233// NewIdentityV3Client returns a *ServiceClient for making calls
234// to the OpenStack Identity v3 API. An error will be returned
235// if authentication or client creation was not possible.
236func NewIdentityV3Client() (*gophercloud.ServiceClient, error) {
237 ao, err := openstack.AuthOptionsFromEnv()
238 if err != nil {
239 return nil, err
240 }
241
242 client, err := openstack.AuthenticatedClient(ao)
243 if err != nil {
244 return nil, err
245 }
246
247 return openstack.NewIdentityV3(client, gophercloud.EndpointOpts{
248 Region: os.Getenv("OS_REGION_NAME"),
249 })
250}
251
252// NewIdentityV3UnauthenticatedClient returns an unauthenticated *ServiceClient
253// for the OpenStack Identity v3 API. An error will be returned if
254// authentication or client creation was not possible.
255func NewIdentityV3UnauthenticatedClient() (*gophercloud.ServiceClient, error) {
256 ao, err := openstack.AuthOptionsFromEnv()
257 if err != nil {
258 return nil, err
259 }
260
261 client, err := openstack.NewClient(ao.IdentityEndpoint)
262 if err != nil {
263 return nil, err
264 }
265
266 return openstack.NewIdentityV3(client, gophercloud.EndpointOpts{})
267}
Joe Topjian7c8dd022016-09-01 12:02:04 -0600268
Joe Topjian929e60b2017-02-20 15:31:15 -0700269// NewImageServiceV2Client returns a *ServiceClient for making calls to the
270// OpenStack Image v2 API. An error will be returned if authentication or
271// client creation was not possible.
272func NewImageServiceV2Client() (*gophercloud.ServiceClient, error) {
273 ao, err := openstack.AuthOptionsFromEnv()
274 if err != nil {
275 return nil, err
276 }
277
278 client, err := openstack.AuthenticatedClient(ao)
279 if err != nil {
280 return nil, err
281 }
282
283 return openstack.NewImageServiceV2(client, gophercloud.EndpointOpts{
284 Region: os.Getenv("OS_REGION_NAME"),
285 })
286}
287
Joe Topjian7c8dd022016-09-01 12:02:04 -0600288// NewNetworkV2Client returns a *ServiceClient for making calls to the
289// OpenStack Networking v2 API. An error will be returned if authentication
290// or client creation was not possible.
291func NewNetworkV2Client() (*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.NewNetworkV2(client, gophercloud.EndpointOpts{
303 Region: os.Getenv("OS_REGION_NAME"),
304 })
305}