blob: b76cfd1e1ba803293dfa88147e67a98a1800ed8b [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
Joe Topjianb720d842016-07-24 02:11:46 +000032func newIdentityClient() (*gophercloud.ServiceClient, error) {
33 ao, err := openstack.AuthOptionsFromEnv()
34 if err != nil {
35 return nil, err
36 }
37
38 client, err := openstack.AuthenticatedClient(ao)
39 if err != nil {
40 return nil, err
41 }
42
43 return openstack.NewIdentityV2(client, gophercloud.EndpointOpts{
44 Region: os.Getenv("OS_REGION_NAME"),
45 })
46}
47
48func newBlockClient() (*gophercloud.ServiceClient, error) {
49 ao, err := openstack.AuthOptionsFromEnv()
50 if err != nil {
51 return nil, err
52 }
53
54 client, err := openstack.AuthenticatedClient(ao)
55 if err != nil {
56 return nil, err
57 }
58
59 return openstack.NewBlockStorageV1(client, gophercloud.EndpointOpts{
60 Region: os.Getenv("OS_REGION_NAME"),
61 })
62}
63
Ash Wilsonfd566482014-09-23 15:47:35 -040064func waitForStatus(client *gophercloud.ServiceClient, server *servers.Server, status string) error {
65 return tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -040066 latest, err := servers.Get(client, server.ID).Extract()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080067 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -040068 return false, err
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080069 }
70
Ash Wilsonfd566482014-09-23 15:47:35 -040071 if latest.Status == status {
72 // Success!
73 return true, nil
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080074 }
Ash Wilsonfd566482014-09-23 15:47:35 -040075
Joe Topjianb720d842016-07-24 02:11:46 +000076 if latest.Status == "ERROR" {
77 return false, fmt.Errorf("Instance in ERROR state")
78 }
79
Ash Wilsonfd566482014-09-23 15:47:35 -040080 return false, nil
81 })
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080082}
83
Ash Wilsonfd566482014-09-23 15:47:35 -040084// ComputeChoices contains image and flavor selections for use by the acceptance tests.
85type ComputeChoices struct {
86 // ImageID contains the ID of a valid image.
87 ImageID string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080088
Ash Wilsonfd566482014-09-23 15:47:35 -040089 // FlavorID contains the ID of a valid flavor.
90 FlavorID string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080091
Ash Wilsonfd566482014-09-23 15:47:35 -040092 // FlavorIDResize contains the ID of a different flavor available on the same OpenStack installation, that is distinct
93 // from FlavorID.
94 FlavorIDResize string
Joe Topjianb4395c72015-02-24 02:47:23 +000095
Joe Topjianb720d842016-07-24 02:11:46 +000096 // FloatingIPPool contains the name of the pool from where to obtain floating IPs.
97 FloatingIPPoolName string
98
Joe Topjianb4395c72015-02-24 02:47:23 +000099 // NetworkName is the name of a network to launch the instance on.
100 NetworkName string
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -0800101}
102
Ash Wilsonfd566482014-09-23 15:47:35 -0400103// ComputeChoicesFromEnv populates a ComputeChoices struct from environment variables.
104// If any required state is missing, an `error` will be returned that enumerates the missing properties.
105func ComputeChoicesFromEnv() (*ComputeChoices, error) {
106 imageID := os.Getenv("OS_IMAGE_ID")
107 flavorID := os.Getenv("OS_FLAVOR_ID")
108 flavorIDResize := os.Getenv("OS_FLAVOR_ID_RESIZE")
Joe Topjianb4395c72015-02-24 02:47:23 +0000109 networkName := os.Getenv("OS_NETWORK_NAME")
Joe Topjianb720d842016-07-24 02:11:46 +0000110 floatingIPPoolName := os.Getenv("OS_POOL_NAME")
Ash Wilsonfd566482014-09-23 15:47:35 -0400111
112 missing := make([]string, 0, 3)
113 if imageID == "" {
114 missing = append(missing, "OS_IMAGE_ID")
115 }
116 if flavorID == "" {
117 missing = append(missing, "OS_FLAVOR_ID")
118 }
119 if flavorIDResize == "" {
120 missing = append(missing, "OS_FLAVOR_ID_RESIZE")
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800121 }
Joe Topjianb720d842016-07-24 02:11:46 +0000122 if floatingIPPoolName == "" {
123 missing = append(missing, "OS_POOL_NAME")
124 }
Joe Topjianb4395c72015-02-24 02:47:23 +0000125 if networkName == "" {
Joe Topjianb720d842016-07-24 02:11:46 +0000126 networkName = "private"
Joe Topjianb4395c72015-02-24 02:47:23 +0000127 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800128
Ash Wilsonfd566482014-09-23 15:47:35 -0400129 notDistinct := ""
130 if flavorID == flavorIDResize {
131 notDistinct = "OS_FLAVOR_ID and OS_FLAVOR_ID_RESIZE must be distinct."
132 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800133
Ash Wilsonfd566482014-09-23 15:47:35 -0400134 if len(missing) > 0 || notDistinct != "" {
135 text := "You're missing some important setup:\n"
136 if len(missing) > 0 {
137 text += " * These environment variables must be provided: " + strings.Join(missing, ", ") + "\n"
138 }
139 if notDistinct != "" {
140 text += " * " + notDistinct + "\n"
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800141 }
142
Ash Wilsonfd566482014-09-23 15:47:35 -0400143 return nil, fmt.Errorf(text)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700144 }
145
Joe Topjianb720d842016-07-24 02:11:46 +0000146 return &ComputeChoices{ImageID: imageID, FlavorID: flavorID, FlavorIDResize: flavorIDResize, FloatingIPPoolName: floatingIPPoolName, NetworkName: networkName}, nil
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700147}