blob: 9efa901c1f8e9f08368540172e7c8156c1040a75 [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
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080032}
33
Joe Topjian1c15e3f2016-08-08 10:48:38 -060034// AcceptanceTestChoicesFromEnv populates a ComputeChoices struct from environment variables.
Ash Wilsonfd566482014-09-23 15:47:35 -040035// If any required state is missing, an `error` will be returned that enumerates the missing properties.
Joe Topjian1c15e3f2016-08-08 10:48:38 -060036func AcceptanceTestChoicesFromEnv() (*AcceptanceTestChoices, error) {
Ash Wilsonfd566482014-09-23 15:47:35 -040037 imageID := os.Getenv("OS_IMAGE_ID")
38 flavorID := os.Getenv("OS_FLAVOR_ID")
39 flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE")
Joe Topjianb4395c72015-02-24 02:47:23 +000040 networkName := os.Getenv("OS_NETWORK_NAME")
Joe Topjianb720d842016-07-24 02:11:46 +000041 floatingIPPoolName := os.Getenv("OS_POOL_NAME")
Ash Wilsonfd566482014-09-23 15:47:35 -040042
43 missing := make([]string, 0, 3)
44 if imageID == "" {
45 missing = append(missing, "OS_IMAGE_ID")
46 }
47 if flavorID == "" {
48 missing = append(missing, "OS_FLAVOR_ID")
49 }
50 if flavorIDResize == "" {
51 missing = append(missing, "OS_FLAVOR_ID_RESIZE")
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080052 }
Joe Topjianb720d842016-07-24 02:11:46 +000053 if floatingIPPoolName == "" {
54 missing = append(missing, "OS_POOL_NAME")
55 }
Joe Topjianb4395c72015-02-24 02:47:23 +000056 if networkName == "" {
Joe Topjianb720d842016-07-24 02:11:46 +000057 networkName = "private"
Joe Topjianb4395c72015-02-24 02:47:23 +000058 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080059
Ash Wilsonfd566482014-09-23 15:47:35 -040060 notDistinct := ""
61 if flavorID == flavorIDResize {
62 notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct."
63 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080064
Ash Wilsonfd566482014-09-23 15:47:35 -040065 if len(missing) > 0 || notDistinct != "" {
66 text := "You're missing some important setup:\n"
67 if len(missing) > 0 {
68 text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n"
69 }
70 if notDistinct != "" {
71 text += " * " + notDistinct + "\n"
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080072 }
73
Ash Wilsonfd566482014-09-23 15:47:35 -040074 return nil, fmt.Errorf(text)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070075 }
76
Joe Topjian1c15e3f2016-08-08 10:48:38 -060077 return &AcceptanceTestChoices{ImageID: imageID, FlavorID: flavorID, FlavorIDResize: flavorIDResize, FloatingIPPoolName: floatingIPPoolName, NetworkName: networkName}, nil
78}
79
80// NewBlockStorageV1Client returns a *ServiceClient for making calls
81// to the OpenStack Block Storage v1 API. An error will be returned
82// if authentication or client creation was not possible.
83func NewBlockStorageV1Client() (*gophercloud.ServiceClient, error) {
84 ao, err := openstack.AuthOptionsFromEnv()
85 if err != nil {
86 return nil, err
87 }
88
89 client, err := openstack.AuthenticatedClient(ao)
90 if err != nil {
91 return nil, err
92 }
93
94 return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
95 Region: os.Getenv("OS_REGION_NAME"),
96 })
97}
98
Joe Topjian68bed5f2016-08-10 15:30:57 -060099// NewBlockStorageV2Client returns a *ServiceClient for making calls
100// to the OpenStack Block Storage v2 API. An error will be returned
101// if authentication or client creation was not possible.
102func NewBlockStorageV2Client() (*gophercloud.ServiceClient, error) {
103 ao, err := openstack.AuthOptionsFromEnv()
104 if err != nil {
105 return nil, err
106 }
107
108 client, err := openstack.AuthenticatedClient(ao)
109 if err != nil {
110 return nil, err
111 }
112
113 return openstack.NewBlockStorageV2(client, gophercloud.EndpointOpts{
114 Region: os.Getenv("OS_REGION_NAME"),
115 })
116}
117
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600118// NewComputeV2Client returns a *ServiceClient for making calls
119// to the OpenStack Compute v2 API. An error will be returned
120// if authentication or client creation was not possible.
121func NewComputeV2Client() (*gophercloud.ServiceClient, error) {
122 ao, err := openstack.AuthOptionsFromEnv()
123 if err != nil {
124 return nil, err
125 }
126
127 client, err := openstack.AuthenticatedClient(ao)
128 if err != nil {
129 return nil, err
130 }
131
132 return openstack.NewComputeV2(client, gophercloud.EndpointOpts{
133 Region: os.Getenv("OS_REGION_NAME"),
134 })
135}
136
137// NewIdentityV2Client returns a *ServiceClient for making calls
138// to the OpenStack Identity v2 API. An error will be returned
139// if authentication or client creation was not possible.
140func NewIdentityV2Client() (*gophercloud.ServiceClient, error) {
141 ao, err := openstack.AuthOptionsFromEnv()
142 if err != nil {
143 return nil, err
144 }
145
146 client, err := openstack.AuthenticatedClient(ao)
147 if err != nil {
148 return nil, err
149 }
150
151 return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{
152 Region: os.Getenv("OS_REGION_NAME"),
153 })
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700154}
Joe Topjian918f5732016-08-15 08:47:08 -0600155
156// NewIdentityV2AdminClient returns a *ServiceClient for making calls
157// to the Admin Endpoint of the OpenStack Identity v2 API. An error
158// will be returned if authentication or client creation was not possible.
159func NewIdentityV2AdminClient() (*gophercloud.ServiceClient, error) {
160 ao, err := openstack.AuthOptionsFromEnv()
161 if err != nil {
162 return nil, err
163 }
164
165 client, err := openstack.AuthenticatedClient(ao)
166 if err != nil {
167 return nil, err
168 }
169
170 return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{
171 Region: os.Getenv("OS_REGION_NAME"),
172 Availability: gophercloud.AvailabilityAdmin,
173 })
174}
175
176// NewIdentityV2UnauthenticatedClient returns an unauthenticated *ServiceClient
177// for the OpenStack Identity v2 API. An error will be returned if
178// authentication or client creation was not possible.
179func NewIdentityV2UnauthenticatedClient() (*gophercloud.ServiceClient, error) {
180 ao, err := openstack.AuthOptionsFromEnv()
181 if err != nil {
182 return nil, err
183 }
184
185 client, err := openstack.NewClient(ao.IdentityEndpoint)
186 if err != nil {
187 return nil, err
188 }
189
190 return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{})
191}
192
193// NewIdentityV3Client returns a *ServiceClient for making calls
194// to the OpenStack Identity v3 API. An error will be returned
195// if authentication or client creation was not possible.
196func NewIdentityV3Client() (*gophercloud.ServiceClient, error) {
197 ao, err := openstack.AuthOptionsFromEnv()
198 if err != nil {
199 return nil, err
200 }
201
202 client, err := openstack.AuthenticatedClient(ao)
203 if err != nil {
204 return nil, err
205 }
206
207 return openstack.NewIdentityV3(client, gophercloud.EndpointOpts{
208 Region: os.Getenv("OS_REGION_NAME"),
209 })
210}
211
212// NewIdentityV3UnauthenticatedClient returns an unauthenticated *ServiceClient
213// for the OpenStack Identity v3 API. An error will be returned if
214// authentication or client creation was not possible.
215func NewIdentityV3UnauthenticatedClient() (*gophercloud.ServiceClient, error) {
216 ao, err := openstack.AuthOptionsFromEnv()
217 if err != nil {
218 return nil, err
219 }
220
221 client, err := openstack.NewClient(ao.IdentityEndpoint)
222 if err != nil {
223 return nil, err
224 }
225
226 return openstack.NewIdentityV3(client, gophercloud.EndpointOpts{})
227}