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 |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 34 | // AcceptanceTestChoicesFromEnv populates a ComputeChoices struct from environment variables. |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 35 | // 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] | 36 | func AcceptanceTestChoicesFromEnv() (*AcceptanceTestChoices, error) { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 37 | imageID := os.Getenv("OS_IMAGE_ID") |
| 38 | flavorID := os.Getenv("OS_FLAVOR_ID") |
| 39 | flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE") |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 40 | networkName := os.Getenv("OS_NETWORK_NAME") |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 41 | floatingIPPoolName := os.Getenv("OS_POOL_NAME") |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 42 | |
| 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 II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 52 | } |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 53 | if floatingIPPoolName == "" { |
| 54 | missing = append(missing, "OS_POOL_NAME") |
| 55 | } |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 56 | if networkName == "" { |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 57 | networkName = "private" |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 58 | } |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 59 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 60 | notDistinct := "" |
| 61 | if flavorID == flavorIDResize { |
| 62 | notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct." |
| 63 | } |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 64 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 65 | 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 II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 74 | return nil, fmt.Errorf(text) |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 77 | 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. |
| 83 | func 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 Topjian | 68bed5f | 2016-08-10 15:30:57 -0600 | [diff] [blame] | 99 | // 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. |
| 102 | func 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 Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 118 | // 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. |
| 121 | func 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. |
| 140 | func 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 II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 154 | } |