Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 1 | // 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. |
| 4 | package clients |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 5 | |
| 6 | import ( |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 7 | "fmt" |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 8 | "os" |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 9 | "strings" |
Jamie Hannaford | c8fc6ea | 2014-09-10 13:59:58 +0200 | [diff] [blame] | 10 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 11 | "github.com/gophercloud/gophercloud" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 12 | "github.com/gophercloud/gophercloud/openstack" |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 13 | ) |
| 14 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 15 | // AcceptanceTestChoices contains image and flavor selections for use by the acceptance tests. |
| 16 | type AcceptanceTestChoices struct { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 17 | // ImageID contains the ID of a valid image. |
| 18 | ImageID string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 19 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 20 | // FlavorID contains the ID of a valid flavor. |
| 21 | FlavorID string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 22 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 23 | // FlavorIDResize contains the ID of a different flavor available on the same OpenStack installation, that is distinct |
| 24 | // from FlavorID. |
| 25 | FlavorIDResize string |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 26 | |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 27 | // FloatingIPPool contains the name of the pool from where to obtain floating IPs. |
| 28 | FloatingIPPoolName string |
| 29 | |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 30 | // NetworkName is the name of a network to launch the instance on. |
| 31 | NetworkName string |
Joe Topjian | 7c8dd02 | 2016-09-01 12:02:04 -0600 | [diff] [blame] | 32 | |
| 33 | // ExternalNetworkID is the network ID of the external network. |
| 34 | ExternalNetworkID string |
Mikko Valkonen | 9368c00 | 2017-01-16 18:31:39 +0200 | [diff] [blame] | 35 | |
| 36 | // ShareNetworkID is the Manila Share network ID |
| 37 | ShareNetworkID string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 40 | // AcceptanceTestChoicesFromEnv populates a ComputeChoices struct from environment variables. |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 41 | // If any required state is missing, an `error` will be returned that enumerates the missing properties. |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 42 | func AcceptanceTestChoicesFromEnv() (*AcceptanceTestChoices, error) { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 43 | imageID := os.Getenv("OS_IMAGE_ID") |
| 44 | flavorID := os.Getenv("OS_FLAVOR_ID") |
| 45 | flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE") |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 46 | networkName := os.Getenv("OS_NETWORK_NAME") |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 47 | floatingIPPoolName := os.Getenv("OS_POOL_NAME") |
Joe Topjian | 7c8dd02 | 2016-09-01 12:02:04 -0600 | [diff] [blame] | 48 | externalNetworkID := os.Getenv("OS_EXTGW_ID") |
Mikko Valkonen | 9368c00 | 2017-01-16 18:31:39 +0200 | [diff] [blame] | 49 | shareNetworkID := os.Getenv("OS_SHARE_NETWORK_ID") |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 50 | |
| 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 II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 60 | } |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 61 | if floatingIPPoolName == "" { |
| 62 | missing = append(missing, "OS_POOL_NAME") |
| 63 | } |
Joe Topjian | 7c8dd02 | 2016-09-01 12:02:04 -0600 | [diff] [blame] | 64 | if externalNetworkID == "" { |
| 65 | missing = append(missing, "OS_EXTGW_ID") |
| 66 | } |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 67 | if networkName == "" { |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 68 | networkName = "private" |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 69 | } |
Mikko Valkonen | 9368c00 | 2017-01-16 18:31:39 +0200 | [diff] [blame] | 70 | if shareNetworkID == "" { |
| 71 | missing = append(missing, "OS_SHARE_NETWORK_ID") |
| 72 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 73 | notDistinct := "" |
| 74 | if flavorID == flavorIDResize { |
| 75 | notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct." |
| 76 | } |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 77 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 78 | 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 II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 85 | } |
| 86 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 87 | return nil, fmt.Errorf(text) |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Mikko Valkonen | 9368c00 | 2017-01-16 18:31:39 +0200 | [diff] [blame] | 90 | 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 Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 99 | } |
| 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. |
| 104 | func 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 Topjian | 68bed5f | 2016-08-10 15:30:57 -0600 | [diff] [blame] | 120 | // 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. |
| 123 | func 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 | |
ehdou | 10f1f85 | 2016-10-14 20:58:23 +0300 | [diff] [blame] | 139 | // 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. |
| 142 | func 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 Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 158 | // 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. |
| 161 | func 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. |
| 180 | func 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 II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 194 | } |
Joe Topjian | 918f573 | 2016-08-15 08:47:08 -0600 | [diff] [blame] | 195 | |
| 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. |
| 199 | func 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. |
| 219 | func 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. |
| 236 | func 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. |
| 255 | func 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 Topjian | 7c8dd02 | 2016-09-01 12:02:04 -0600 | [diff] [blame] | 268 | |
Joe Topjian | 929e60b | 2017-02-20 15:31:15 -0700 | [diff] [blame] | 269 | // 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. |
| 272 | func 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 Topjian | 7c8dd02 | 2016-09-01 12:02:04 -0600 | [diff] [blame] | 288 | // 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. |
| 291 | func 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 | } |