blob: 15b5163e603731ae2c242dd7674dbed31e1202a7 [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) {
Ash Wilson89734d02014-09-25 13:50:08 -040035 latest, err := servers.Get(client, server.ID).Extract()
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 }
39
Ash Wilsonfd566482014-09-23 15:47:35 -040040 if latest.Status == status {
41 // Success!
42 return true, nil
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080043 }
Ash Wilsonfd566482014-09-23 15:47:35 -040044
45 return false, nil
46 })
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080047}
48
Ash Wilsonfd566482014-09-23 15:47:35 -040049// ComputeChoices contains image and flavor selections for use by the acceptance tests.
50type ComputeChoices struct {
51 // ImageID contains the ID of a valid image.
52 ImageID string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080053
Ash Wilsonfd566482014-09-23 15:47:35 -040054 // FlavorID contains the ID of a valid flavor.
55 FlavorID string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080056
Ash Wilsonfd566482014-09-23 15:47:35 -040057 // FlavorIDResize contains the ID of a different flavor available on the same OpenStack installation, that is distinct
58 // from FlavorID.
59 FlavorIDResize string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080060}
61
Ash Wilsonfd566482014-09-23 15:47:35 -040062// ComputeChoicesFromEnv populates a ComputeChoices struct from environment variables.
63// If any required state is missing, an `error` will be returned that enumerates the missing properties.
64func ComputeChoicesFromEnv() (*ComputeChoices, error) {
65 imageID := os.Getenv("OS_IMAGE_ID")
66 flavorID := os.Getenv("OS_FLAVOR_ID")
67 flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE")
68
69 missing := make([]string, 0, 3)
70 if imageID == "" {
71 missing = append(missing, "OS_IMAGE_ID")
72 }
73 if flavorID == "" {
74 missing = append(missing, "OS_FLAVOR_ID")
75 }
76 if flavorIDResize == "" {
77 missing = append(missing, "OS_FLAVOR_ID_RESIZE")
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080078 }
79
Ash Wilsonfd566482014-09-23 15:47:35 -040080 notDistinct := ""
81 if flavorID == flavorIDResize {
82 notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct."
83 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080084
Ash Wilsonfd566482014-09-23 15:47:35 -040085 if len(missing) > 0 || notDistinct != "" {
86 text := "You're missing some important setup:\n"
87 if len(missing) > 0 {
88 text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n"
89 }
90 if notDistinct != "" {
91 text += " * " + notDistinct + "\n"
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080092 }
93
Ash Wilsonfd566482014-09-23 15:47:35 -040094 return nil, fmt.Errorf(text)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -070095 }
96
Ash Wilsonfd566482014-09-23 15:47:35 -040097 return &ComputeChoices{ImageID: imageID, FlavorID: flavorID, FlavorIDResize: flavorIDResize}, nil
Samuel A. Falvo II808bb632014-03-12 00:07:50 -070098}