blob: 86871aa26bb354df145502480a09ae2034f065c6 [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
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080035}
36
Joe Topjian1c15e3f2016-08-08 10:48:38 -060037// AcceptanceTestChoicesFromEnv populates a ComputeChoices struct from environment variables.
Ash Wilsonfd566482014-09-23 15:47:35 -040038// If any required state is missing, an `error` will be returned that enumerates the missing properties.
Joe Topjian1c15e3f2016-08-08 10:48:38 -060039func AcceptanceTestChoicesFromEnv() (*AcceptanceTestChoices, error) {
Ash Wilsonfd566482014-09-23 15:47:35 -040040 imageID := os.Getenv("OS_IMAGE_ID")
41 flavorID := os.Getenv("OS_FLAVOR_ID")
42 flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE")
Joe Topjianb4395c72015-02-24 02:47:23 +000043 networkName := os.Getenv("OS_NETWORK_NAME")
Joe Topjianb720d842016-07-24 02:11:46 +000044 floatingIPPoolName := os.Getenv("OS_POOL_NAME")
Joe Topjian7c8dd022016-09-01 12:02:04 -060045 externalNetworkID := os.Getenv("OS_EXTGW_ID")
Ash Wilsonfd566482014-09-23 15:47:35 -040046
47 missing := make([]string, 0, 3)
48 if imageID == "" {
49 missing = append(missing, "OS_IMAGE_ID")
50 }
51 if flavorID == "" {
52 missing = append(missing, "OS_FLAVOR_ID")
53 }
54 if flavorIDResize == "" {
55 missing = append(missing, "OS_FLAVOR_ID_RESIZE")
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080056 }
Joe Topjianb720d842016-07-24 02:11:46 +000057 if floatingIPPoolName == "" {
58 missing = append(missing, "OS_POOL_NAME")
59 }
Joe Topjian7c8dd022016-09-01 12:02:04 -060060 if externalNetworkID == "" {
61 missing = append(missing, "OS_EXTGW_ID")
62 }
Joe Topjianb4395c72015-02-24 02:47:23 +000063 if networkName == "" {
Joe Topjianb720d842016-07-24 02:11:46 +000064 networkName = "private"
Joe Topjianb4395c72015-02-24 02:47:23 +000065 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080066
Ash Wilsonfd566482014-09-23 15:47:35 -040067 notDistinct := ""
68 if flavorID == flavorIDResize {
69 notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct."
70 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080071
Ash Wilsonfd566482014-09-23 15:47:35 -040072 if len(missing) > 0 || notDistinct != "" {
73 text := "You're missing some important setup:\n"
74 if len(missing) > 0 {
75 text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n"
76 }
77 if notDistinct != "" {
78 text += " * " + notDistinct + "\n"
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080079 }
80
Ash Wilsonfd566482014-09-23 15:47:35 -040081 return nil, fmt.Errorf(text)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070082 }
83
Joe Topjian7c8dd022016-09-01 12:02:04 -060084 return &AcceptanceTestChoices{ImageID: imageID, FlavorID: flavorID, FlavorIDResize: flavorIDResize, FloatingIPPoolName: floatingIPPoolName, NetworkName: networkName, ExternalNetworkID: externalNetworkID}, nil
Joe Topjian1c15e3f2016-08-08 10:48:38 -060085}
86
87// NewBlockStorageV1Client returns a *ServiceClient for making calls
88// to the OpenStack Block Storage v1 API. An error will be returned
89// if authentication or client creation was not possible.
90func NewBlockStorageV1Client() (*gophercloud.ServiceClient, error) {
91 ao, err := openstack.AuthOptionsFromEnv()
92 if err != nil {
93 return nil, err
94 }
95
96 client, err := openstack.AuthenticatedClient(ao)
97 if err != nil {
98 return nil, err
99 }
100
101 return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
102 Region: os.Getenv("OS_REGION_NAME"),
103 })
104}
105
Joe Topjian68bed5f2016-08-10 15:30:57 -0600106// NewBlockStorageV2Client returns a *ServiceClient for making calls
107// to the OpenStack Block Storage v2 API. An error will be returned
108// if authentication or client creation was not possible.
109func NewBlockStorageV2Client() (*gophercloud.ServiceClient, error) {
110 ao, err := openstack.AuthOptionsFromEnv()
111 if err != nil {
112 return nil, err
113 }
114
115 client, err := openstack.AuthenticatedClient(ao)
116 if err != nil {
117 return nil, err
118 }
119
120 return openstack.NewBlockStorageV2(client, gophercloud.EndpointOpts{
121 Region: os.Getenv("OS_REGION_NAME"),
122 })
123}
124
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600125// NewComputeV2Client returns a *ServiceClient for making calls
126// to the OpenStack Compute v2 API. An error will be returned
127// if authentication or client creation was not possible.
128func NewComputeV2Client() (*gophercloud.ServiceClient, error) {
129 ao, err := openstack.AuthOptionsFromEnv()
130 if err != nil {
131 return nil, err
132 }
133
134 client, err := openstack.AuthenticatedClient(ao)
135 if err != nil {
136 return nil, err
137 }
138
139 return openstack.NewComputeV2(client, gophercloud.EndpointOpts{
140 Region: os.Getenv("OS_REGION_NAME"),
141 })
142}
143
144// NewIdentityV2Client returns a *ServiceClient for making calls
145// to the OpenStack Identity v2 API. An error will be returned
146// if authentication or client creation was not possible.
147func NewIdentityV2Client() (*gophercloud.ServiceClient, error) {
148 ao, err := openstack.AuthOptionsFromEnv()
149 if err != nil {
150 return nil, err
151 }
152
153 client, err := openstack.AuthenticatedClient(ao)
154 if err != nil {
155 return nil, err
156 }
157
158 return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{
159 Region: os.Getenv("OS_REGION_NAME"),
160 })
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700161}
Joe Topjian918f5732016-08-15 08:47:08 -0600162
163// NewIdentityV2AdminClient returns a *ServiceClient for making calls
164// to the Admin Endpoint of the OpenStack Identity v2 API. An error
165// will be returned if authentication or client creation was not possible.
166func NewIdentityV2AdminClient() (*gophercloud.ServiceClient, error) {
167 ao, err := openstack.AuthOptionsFromEnv()
168 if err != nil {
169 return nil, err
170 }
171
172 client, err := openstack.AuthenticatedClient(ao)
173 if err != nil {
174 return nil, err
175 }
176
177 return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{
178 Region: os.Getenv("OS_REGION_NAME"),
179 Availability: gophercloud.AvailabilityAdmin,
180 })
181}
182
183// NewIdentityV2UnauthenticatedClient returns an unauthenticated *ServiceClient
184// for the OpenStack Identity v2 API. An error will be returned if
185// authentication or client creation was not possible.
186func NewIdentityV2UnauthenticatedClient() (*gophercloud.ServiceClient, error) {
187 ao, err := openstack.AuthOptionsFromEnv()
188 if err != nil {
189 return nil, err
190 }
191
192 client, err := openstack.NewClient(ao.IdentityEndpoint)
193 if err != nil {
194 return nil, err
195 }
196
197 return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{})
198}
199
200// NewIdentityV3Client returns a *ServiceClient for making calls
201// to the OpenStack Identity v3 API. An error will be returned
202// if authentication or client creation was not possible.
203func NewIdentityV3Client() (*gophercloud.ServiceClient, error) {
204 ao, err := openstack.AuthOptionsFromEnv()
205 if err != nil {
206 return nil, err
207 }
208
209 client, err := openstack.AuthenticatedClient(ao)
210 if err != nil {
211 return nil, err
212 }
213
214 return openstack.NewIdentityV3(client, gophercloud.EndpointOpts{
215 Region: os.Getenv("OS_REGION_NAME"),
216 })
217}
218
219// NewIdentityV3UnauthenticatedClient returns an unauthenticated *ServiceClient
220// for the OpenStack Identity v3 API. An error will be returned if
221// authentication or client creation was not possible.
222func NewIdentityV3UnauthenticatedClient() (*gophercloud.ServiceClient, error) {
223 ao, err := openstack.AuthOptionsFromEnv()
224 if err != nil {
225 return nil, err
226 }
227
228 client, err := openstack.NewClient(ao.IdentityEndpoint)
229 if err != nil {
230 return nil, err
231 }
232
233 return openstack.NewIdentityV3(client, gophercloud.EndpointOpts{})
234}
Joe Topjian7c8dd022016-09-01 12:02:04 -0600235
236// NewNetworkV2Client returns a *ServiceClient for making calls to the
237// OpenStack Networking v2 API. An error will be returned if authentication
238// or client creation was not possible.
239func NewNetworkV2Client() (*gophercloud.ServiceClient, error) {
240 ao, err := openstack.AuthOptionsFromEnv()
241 if err != nil {
242 return nil, err
243 }
244
245 client, err := openstack.AuthenticatedClient(ao)
246 if err != nil {
247 return nil, err
248 }
249
250 return openstack.NewNetworkV2(client, gophercloud.EndpointOpts{
251 Region: os.Getenv("OS_REGION_NAME"),
252 })
253}