blob: 33e49fea9bb1ed6c76afef4310d99c59f21a6158 [file] [log] [blame]
Jamie Hannaford8ed4fe72014-11-20 12:01:28 +01001// +build acceptance common
Samuel A. Falvo IIb5d93f22014-02-21 15:00:20 -08002
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"
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080014)
15
Ash Wilsonfd566482014-09-23 15:47:35 -040016func newClient() (*gophercloud.ServiceClient, error) {
Jamie Hannaford390555a2014-10-22 17:04:03 +020017 ao, err := openstack.AuthOptionsFromEnv()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080018 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -040019 return nil, err
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080020 }
21
Ash Wilsonfd566482014-09-23 15:47:35 -040022 client, err := openstack.AuthenticatedClient(ao)
23 if err != nil {
24 return nil, err
25 }
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080026
Ash Wilsonfd566482014-09-23 15:47:35 -040027 return openstack.NewComputeV2(client, gophercloud.EndpointOpts{
28 Region: os.Getenv("OS_REGION_NAME"),
29 })
30}
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080031
Ash Wilsonfd566482014-09-23 15:47:35 -040032func waitForStatus(client *gophercloud.ServiceClient, server *servers.Server, status string) error {
33 return tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -040034 latest, err := servers.Get(client, server.ID).Extract()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080035 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -040036 return false, err
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080037 }
38
Ash Wilsonfd566482014-09-23 15:47:35 -040039 if latest.Status == status {
40 // Success!
41 return true, nil
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080042 }
Ash Wilsonfd566482014-09-23 15:47:35 -040043
44 return false, nil
45 })
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080046}
47
Ash Wilsonfd566482014-09-23 15:47:35 -040048// ComputeChoices contains image and flavor selections for use by the acceptance tests.
49type ComputeChoices struct {
50 // ImageID contains the ID of a valid image.
51 ImageID string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080052
Ash Wilsonfd566482014-09-23 15:47:35 -040053 // FlavorID contains the ID of a valid flavor.
54 FlavorID string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080055
Ash Wilsonfd566482014-09-23 15:47:35 -040056 // 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 IIf370dc72014-02-13 15:05:34 -080059}
60
Ash Wilsonfd566482014-09-23 15:47:35 -040061// 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.
63func 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 IIdb020882014-02-13 15:37:57 -080077 }
78
Ash Wilsonfd566482014-09-23 15:47:35 -040079 notDistinct := ""
80 if flavorID == flavorIDResize {
81 notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct."
82 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080083
Ash Wilsonfd566482014-09-23 15:47:35 -040084 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 IIdb020882014-02-13 15:37:57 -080091 }
92
Ash Wilsonfd566482014-09-23 15:47:35 -040093 return nil, fmt.Errorf(text)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070094 }
95
Ash Wilsonfd566482014-09-23 15:47:35 -040096 return &ComputeChoices{ImageID: imageID, FlavorID: flavorID, FlavorIDResize: flavorIDResize}, nil
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070097}