blob: f5d0a6572d8bec9f87c9eae7b3fb8041010c494f [file] [log] [blame]
Samuel A. Falvo IIb5d93f22014-02-21 15:00:20 -08001// +build acceptance
2
Ash Wilsonfd566482014-09-23 15:47:35 -04003package v2
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -08004
5import (
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -08006 "fmt"
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -08007 "os"
Ash Wilsonfd566482014-09-23 15:47:35 -04008 "strings"
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +02009
Ash Wilsonfd566482014-09-23 15:47:35 -040010 "github.com/rackspace/gophercloud"
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070011 "github.com/rackspace/gophercloud/acceptance/tools"
Ash Wilsonfd566482014-09-23 15:47:35 -040012 "github.com/rackspace/gophercloud/openstack"
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +020013 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
Ash Wilsonfd566482014-09-23 15:47:35 -040014 "github.com/rackspace/gophercloud/openstack/utils"
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080015)
16
Ash Wilsonfd566482014-09-23 15:47:35 -040017func newClient() (*gophercloud.ServiceClient, error) {
18 ao, err := utils.AuthOptions()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080019 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -040020 return nil, err
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080021 }
22
Ash Wilsonfd566482014-09-23 15:47:35 -040023 client, err := openstack.AuthenticatedClient(ao)
24 if err != nil {
25 return nil, err
26 }
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080027
Ash Wilsonfd566482014-09-23 15:47:35 -040028 return openstack.NewComputeV2(client, gophercloud.EndpointOpts{
29 Region: os.Getenv("OS_REGION_NAME"),
30 })
31}
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080032
Ash Wilsonfd566482014-09-23 15:47:35 -040033func waitForStatus(client *gophercloud.ServiceClient, server *servers.Server, status string) error {
34 return tools.WaitFor(func() (bool, error) {
35 response, err := servers.Get(client, server.ID)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080036 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -040037 return false, err
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080038 }
Ash Wilsonfd566482014-09-23 15:47:35 -040039 latest, err := servers.ExtractServer(response)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080040 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -040041 return false, err
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080042 }
43
Ash Wilsonfd566482014-09-23 15:47:35 -040044 if latest.Status == status {
45 // Success!
46 return true, nil
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080047 }
Ash Wilsonfd566482014-09-23 15:47:35 -040048
49 return false, nil
50 })
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080051}
52
Ash Wilsonfd566482014-09-23 15:47:35 -040053// ComputeChoices contains image and flavor selections for use by the acceptance tests.
54type ComputeChoices struct {
55 // ImageID contains the ID of a valid image.
56 ImageID string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080057
Ash Wilsonfd566482014-09-23 15:47:35 -040058 // FlavorID contains the ID of a valid flavor.
59 FlavorID string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080060
Ash Wilsonfd566482014-09-23 15:47:35 -040061 // FlavorIDResize contains the ID of a different flavor available on the same OpenStack installation, that is distinct
62 // from FlavorID.
63 FlavorIDResize string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080064}
65
Ash Wilsonfd566482014-09-23 15:47:35 -040066// ComputeChoicesFromEnv populates a ComputeChoices struct from environment variables.
67// If any required state is missing, an `error` will be returned that enumerates the missing properties.
68func ComputeChoicesFromEnv() (*ComputeChoices, error) {
69 imageID := os.Getenv("OS_IMAGE_ID")
70 flavorID := os.Getenv("OS_FLAVOR_ID")
71 flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE")
72
73 missing := make([]string, 0, 3)
74 if imageID == "" {
75 missing = append(missing, "OS_IMAGE_ID")
76 }
77 if flavorID == "" {
78 missing = append(missing, "OS_FLAVOR_ID")
79 }
80 if flavorIDResize == "" {
81 missing = append(missing, "OS_FLAVOR_ID_RESIZE")
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080082 }
83
Ash Wilsonfd566482014-09-23 15:47:35 -040084 notDistinct := ""
85 if flavorID == flavorIDResize {
86 notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct."
87 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080088
Ash Wilsonfd566482014-09-23 15:47:35 -040089 if len(missing) > 0 || notDistinct != "" {
90 text := "You're missing some important setup:\n"
91 if len(missing) > 0 {
92 text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n"
93 }
94 if notDistinct != "" {
95 text += " * " + notDistinct + "\n"
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080096 }
97
Ash Wilsonfd566482014-09-23 15:47:35 -040098 return nil, fmt.Errorf(text)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070099 }
100
Ash Wilsonfd566482014-09-23 15:47:35 -0400101 return &ComputeChoices{ImageID: imageID, FlavorID: flavorID, FlavorIDResize: flavorIDResize}, nil
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700102}