blob: 83b0e35e6839a1a7b2c212e4809ae92eea47e401 [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
Jon Perritt27249f42016-02-18 10:35:59 -060010 "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 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
Joe Topjianb4395c72015-02-24 02:47:23 +000059
60 // NetworkName is the name of a network to launch the instance on.
61 NetworkName string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080062}
63
Ash Wilsonfd566482014-09-23 15:47:35 -040064// ComputeChoicesFromEnv populates a ComputeChoices struct from environment variables.
65// If any required state is missing, an `error` will be returned that enumerates the missing properties.
66func ComputeChoicesFromEnv() (*ComputeChoices, error) {
67 imageID := os.Getenv("OS_IMAGE_ID")
68 flavorID := os.Getenv("OS_FLAVOR_ID")
69 flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE")
Joe Topjianb4395c72015-02-24 02:47:23 +000070 networkName := os.Getenv("OS_NETWORK_NAME")
Ash Wilsonfd566482014-09-23 15:47:35 -040071
72 missing := make([]string, 0, 3)
73 if imageID == "" {
74 missing = append(missing, "OS_IMAGE_ID")
75 }
76 if flavorID == "" {
77 missing = append(missing, "OS_FLAVOR_ID")
78 }
79 if flavorIDResize == "" {
80 missing = append(missing, "OS_FLAVOR_ID_RESIZE")
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080081 }
Joe Topjianb4395c72015-02-24 02:47:23 +000082 if networkName == "" {
83 networkName = "public"
84 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080085
Ash Wilsonfd566482014-09-23 15:47:35 -040086 notDistinct := ""
87 if flavorID == flavorIDResize {
88 notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct."
89 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080090
Ash Wilsonfd566482014-09-23 15:47:35 -040091 if len(missing) > 0 || notDistinct != "" {
92 text := "You're missing some important setup:\n"
93 if len(missing) > 0 {
94 text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n"
95 }
96 if notDistinct != "" {
97 text += " * " + notDistinct + "\n"
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080098 }
99
Ash Wilsonfd566482014-09-23 15:47:35 -0400100 return nil, fmt.Errorf(text)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700101 }
102
Joe Topjianb4395c72015-02-24 02:47:23 +0000103 return &ComputeChoices{ImageID: imageID, FlavorID: flavorID, FlavorIDResize: flavorIDResize, NetworkName: networkName}, nil
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700104}