Jamie Hannaford | 8ed4fe7 | 2014-11-20 12:01:28 +0100 | [diff] [blame] | 1 | // +build acceptance common |
Samuel A. Falvo II | b5d93f2 | 2014-02-21 15:00:20 -0800 | [diff] [blame] | 2 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 3 | package v2 |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 4 | |
| 5 | import ( |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 6 | "fmt" |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 7 | "os" |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 8 | "strings" |
Jamie Hannaford | c8fc6ea | 2014-09-10 13:59:58 +0200 | [diff] [blame] | 9 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud" |
| 11 | "github.com/gophercloud/gophercloud/acceptance/tools" |
| 12 | "github.com/gophercloud/gophercloud/openstack" |
| 13 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 14 | ) |
| 15 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 16 | func newClient() (*gophercloud.ServiceClient, error) { |
Jamie Hannaford | 390555a | 2014-10-22 17:04:03 +0200 | [diff] [blame] | 17 | ao, err := openstack.AuthOptionsFromEnv() |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 18 | if err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 19 | return nil, err |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 20 | } |
| 21 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 22 | client, err := openstack.AuthenticatedClient(ao) |
| 23 | if err != nil { |
| 24 | return nil, err |
| 25 | } |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 26 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 27 | return openstack.NewComputeV2(client, gophercloud.EndpointOpts{ |
| 28 | Region: os.Getenv("OS_REGION_NAME"), |
| 29 | }) |
| 30 | } |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 31 | |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 32 | func newIdentityClient() (*gophercloud.ServiceClient, error) { |
| 33 | ao, err := openstack.AuthOptionsFromEnv() |
| 34 | if err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | |
| 38 | client, err := openstack.AuthenticatedClient(ao) |
| 39 | if err != nil { |
| 40 | return nil, err |
| 41 | } |
| 42 | |
| 43 | return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{ |
| 44 | Region: os.Getenv("OS_REGION_NAME"), |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | func newBlockClient() (*gophercloud.ServiceClient, error) { |
| 49 | ao, err := openstack.AuthOptionsFromEnv() |
| 50 | if err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 | |
| 54 | client, err := openstack.AuthenticatedClient(ao) |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | |
| 59 | return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{ |
| 60 | Region: os.Getenv("OS_REGION_NAME"), |
| 61 | }) |
| 62 | } |
| 63 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 64 | func waitForStatus(client *gophercloud.ServiceClient, server *servers.Server, status string) error { |
| 65 | return tools.WaitFor(func() (bool, error) { |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 66 | latest, err := servers.Get(client, server.ID).Extract() |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 67 | if err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 68 | return false, err |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 71 | if latest.Status == status { |
| 72 | // Success! |
| 73 | return true, nil |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 74 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 75 | |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 76 | if latest.Status == "ERROR" { |
| 77 | return false, fmt.Errorf("Instance in ERROR state") |
| 78 | } |
| 79 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 80 | return false, nil |
| 81 | }) |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 84 | // ComputeChoices contains image and flavor selections for use by the acceptance tests. |
| 85 | type ComputeChoices struct { |
| 86 | // ImageID contains the ID of a valid image. |
| 87 | ImageID string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 88 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 89 | // FlavorID contains the ID of a valid flavor. |
| 90 | FlavorID string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 91 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 92 | // FlavorIDResize contains the ID of a different flavor available on the same OpenStack installation, that is distinct |
| 93 | // from FlavorID. |
| 94 | FlavorIDResize string |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 95 | |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 96 | // FloatingIPPool contains the name of the pool from where to obtain floating IPs. |
| 97 | FloatingIPPoolName string |
| 98 | |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 99 | // NetworkName is the name of a network to launch the instance on. |
| 100 | NetworkName string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 103 | // ComputeChoicesFromEnv populates a ComputeChoices struct from environment variables. |
| 104 | // If any required state is missing, an `error` will be returned that enumerates the missing properties. |
| 105 | func ComputeChoicesFromEnv() (*ComputeChoices, error) { |
| 106 | imageID := os.Getenv("OS_IMAGE_ID") |
| 107 | flavorID := os.Getenv("OS_FLAVOR_ID") |
| 108 | flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE") |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 109 | networkName := os.Getenv("OS_NETWORK_NAME") |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 110 | floatingIPPoolName := os.Getenv("OS_POOL_NAME") |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 111 | |
| 112 | missing := make([]string, 0, 3) |
| 113 | if imageID == "" { |
| 114 | missing = append(missing, "OS_IMAGE_ID") |
| 115 | } |
| 116 | if flavorID == "" { |
| 117 | missing = append(missing, "OS_FLAVOR_ID") |
| 118 | } |
| 119 | if flavorIDResize == "" { |
| 120 | missing = append(missing, "OS_FLAVOR_ID_RESIZE") |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 121 | } |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 122 | if floatingIPPoolName == "" { |
| 123 | missing = append(missing, "OS_POOL_NAME") |
| 124 | } |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 125 | if networkName == "" { |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 126 | networkName = "private" |
Joe Topjian | b4395c7 | 2015-02-24 02:47:23 +0000 | [diff] [blame] | 127 | } |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 128 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 129 | notDistinct := "" |
| 130 | if flavorID == flavorIDResize { |
| 131 | notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct." |
| 132 | } |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 133 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 134 | if len(missing) > 0 || notDistinct != "" { |
| 135 | text := "You're missing some important setup:\n" |
| 136 | if len(missing) > 0 { |
| 137 | text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n" |
| 138 | } |
| 139 | if notDistinct != "" { |
| 140 | text += " * " + notDistinct + "\n" |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 143 | return nil, fmt.Errorf(text) |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Joe Topjian | b720d84 | 2016-07-24 02:11:46 +0000 | [diff] [blame] | 146 | return &ComputeChoices{ImageID: imageID, FlavorID: flavorID, FlavorIDResize: flavorIDResize, FloatingIPPoolName: floatingIPPoolName, NetworkName: networkName}, nil |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 147 | } |