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 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud" |
Samuel A. Falvo II | 43d8353 | 2014-07-31 14:34:48 -0700 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/acceptance/tools" |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 12 | "github.com/rackspace/gophercloud/openstack" |
Jamie Hannaford | c8fc6ea | 2014-09-10 13:59:58 +0200 | [diff] [blame] | 13 | "github.com/rackspace/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 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 32 | func waitForStatus(client *gophercloud.ServiceClient, server *servers.Server, status string) error { |
| 33 | return tools.WaitFor(func() (bool, error) { |
Ash Wilson | 89734d0 | 2014-09-25 13:50:08 -0400 | [diff] [blame] | 34 | latest, err := servers.Get(client, server.ID).Extract() |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 35 | if err != nil { |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 36 | return false, err |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 39 | if latest.Status == status { |
| 40 | // Success! |
| 41 | return true, nil |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 42 | } |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 43 | |
| 44 | return false, nil |
| 45 | }) |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 46 | } |
| 47 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 48 | // ComputeChoices contains image and flavor selections for use by the acceptance tests. |
| 49 | type ComputeChoices struct { |
| 50 | // ImageID contains the ID of a valid image. |
| 51 | ImageID string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 52 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 53 | // FlavorID contains the ID of a valid flavor. |
| 54 | FlavorID string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 55 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 56 | // FlavorIDResize contains the ID of a different flavor available on the same OpenStack installation, that is distinct |
| 57 | // from FlavorID. |
| 58 | FlavorIDResize string |
Samuel A. Falvo II | f370dc7 | 2014-02-13 15:05:34 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 61 | // ComputeChoicesFromEnv populates a ComputeChoices struct from environment variables. |
| 62 | // If any required state is missing, an `error` will be returned that enumerates the missing properties. |
| 63 | func ComputeChoicesFromEnv() (*ComputeChoices, error) { |
| 64 | imageID := os.Getenv("OS_IMAGE_ID") |
| 65 | flavorID := os.Getenv("OS_FLAVOR_ID") |
| 66 | flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE") |
| 67 | |
| 68 | missing := make([]string, 0, 3) |
| 69 | if imageID == "" { |
| 70 | missing = append(missing, "OS_IMAGE_ID") |
| 71 | } |
| 72 | if flavorID == "" { |
| 73 | missing = append(missing, "OS_FLAVOR_ID") |
| 74 | } |
| 75 | if flavorIDResize == "" { |
| 76 | missing = append(missing, "OS_FLAVOR_ID_RESIZE") |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 79 | notDistinct := "" |
| 80 | if flavorID == flavorIDResize { |
| 81 | notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct." |
| 82 | } |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 83 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 84 | if len(missing) > 0 || notDistinct != "" { |
| 85 | text := "You're missing some important setup:\n" |
| 86 | if len(missing) > 0 { |
| 87 | text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n" |
| 88 | } |
| 89 | if notDistinct != "" { |
| 90 | text += " * " + notDistinct + "\n" |
Samuel A. Falvo II | db02088 | 2014-02-13 15:37:57 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 93 | return nil, fmt.Errorf(text) |
Samuel A. Falvo II | 38c6ad0 | 2014-05-06 18:09:46 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Ash Wilson | fd56648 | 2014-09-23 15:47:35 -0400 | [diff] [blame] | 96 | return &ComputeChoices{ImageID: imageID, FlavorID: flavorID, FlavorIDResize: flavorIDResize}, nil |
Samuel A. Falvo II | 808bb63 | 2014-03-12 00:07:50 -0700 | [diff] [blame] | 97 | } |