blob: 26b7bfe16ae66971aad29c4ea1511c8d8ed92c23 [file] [log] [blame]
Joe Topjiandee32222015-02-09 23:56:26 +00001// +build acceptance compute servers
2
3package v2
4
5import (
Joe Topjiandee32222015-02-09 23:56:26 +00006 "testing"
7
Joe Topjian1c15e3f2016-08-08 10:48:38 -06008 "github.com/gophercloud/gophercloud/acceptance/clients"
Joe Topjian66a046c2017-01-19 22:07:26 -07009 "github.com/gophercloud/gophercloud/acceptance/tools"
Joe Topjian48209e32016-07-25 16:31:06 +000010 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips"
Jon Perritt27249f42016-02-18 10:35:59 -060011 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Joe Topjiandee32222015-02-09 23:56:26 +000012)
13
Joe Topjian48209e32016-07-25 16:31:06 +000014func TestFloatingIPsList(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060015 client, err := clients.NewComputeV2Client()
Joe Topjian48209e32016-07-25 16:31:06 +000016 if err != nil {
17 t.Fatalf("Unable to create a compute client: %v", err)
18 }
19
20 allPages, err := floatingips.List(client).AllPages()
21 if err != nil {
22 t.Fatalf("Unable to retrieve floating IPs: %v", err)
23 }
24
25 allFloatingIPs, err := floatingips.ExtractFloatingIPs(allPages)
26 if err != nil {
27 t.Fatalf("Unable to extract floating IPs: %v", err)
28 }
29
30 for _, floatingIP := range allFloatingIPs {
Joe Topjian66a046c2017-01-19 22:07:26 -070031 tools.PrintResource(t, floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000032 }
33}
34
35func TestFloatingIPsCreate(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060036 client, err := clients.NewComputeV2Client()
Joe Topjian48209e32016-07-25 16:31:06 +000037 if err != nil {
38 t.Fatalf("Unable to create a compute client: %v", err)
39 }
40
Joe Topjian66a046c2017-01-19 22:07:26 -070041 floatingIP, err := CreateFloatingIP(t, client)
Joe Topjian48209e32016-07-25 16:31:06 +000042 if err != nil {
43 t.Fatalf("Unable to create floating IP: %v", err)
44 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060045 defer DeleteFloatingIP(t, client, floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000046
Joe Topjian66a046c2017-01-19 22:07:26 -070047 tools.PrintResource(t, floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000048}
49
50func TestFloatingIPsAssociate(t *testing.T) {
Joe Topjiandee32222015-02-09 23:56:26 +000051 if testing.Short() {
52 t.Skip("Skipping test that requires server creation in short mode.")
53 }
54
Joe Topjian1c15e3f2016-08-08 10:48:38 -060055 client, err := clients.NewComputeV2Client()
Joe Topjian48209e32016-07-25 16:31:06 +000056 if err != nil {
57 t.Fatalf("Unable to create a compute client: %v", err)
58 }
Joe Topjiandee32222015-02-09 23:56:26 +000059
Joe Topjian66a046c2017-01-19 22:07:26 -070060 server, err := CreateServer(t, client)
Joe Topjiandee32222015-02-09 23:56:26 +000061 if err != nil {
62 t.Fatalf("Unable to create server: %v", err)
63 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060064 defer DeleteServer(t, client, server)
Joe Topjiandee32222015-02-09 23:56:26 +000065
Joe Topjian66a046c2017-01-19 22:07:26 -070066 floatingIP, err := CreateFloatingIP(t, client)
Joe Topjiand97fe9b2015-09-17 02:08:38 +000067 if err != nil {
Joe Topjian48209e32016-07-25 16:31:06 +000068 t.Fatalf("Unable to create floating IP: %v", err)
Joe Topjiand97fe9b2015-09-17 02:08:38 +000069 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060070 defer DeleteFloatingIP(t, client, floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000071
Joe Topjian66a046c2017-01-19 22:07:26 -070072 tools.PrintResource(t, floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000073
Joe Topjian1c15e3f2016-08-08 10:48:38 -060074 err = AssociateFloatingIP(t, client, floatingIP, server)
Joe Topjian48209e32016-07-25 16:31:06 +000075 if err != nil {
76 t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, server.ID, err)
77 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060078 defer DisassociateFloatingIP(t, client, floatingIP, server)
Joe Topjian48209e32016-07-25 16:31:06 +000079
80 newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract()
81 if err != nil {
82 t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err)
83 }
84
Joe Topjian1c15e3f2016-08-08 10:48:38 -060085 t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, newFloatingIP.FixedIP)
86
Joe Topjian66a046c2017-01-19 22:07:26 -070087 tools.PrintResource(t, newFloatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +000088}
89
90func TestFloatingIPsFixedIPAssociate(t *testing.T) {
91 if testing.Short() {
92 t.Skip("Skipping test that requires server creation in short mode.")
93 }
94
Joe Topjian1c15e3f2016-08-08 10:48:38 -060095 client, err := clients.NewComputeV2Client()
Joe Topjian48209e32016-07-25 16:31:06 +000096 if err != nil {
97 t.Fatalf("Unable to create a compute client: %v", err)
98 }
99
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600100 choices, err := clients.AcceptanceTestChoicesFromEnv()
Joe Topjian48209e32016-07-25 16:31:06 +0000101 if err != nil {
102 t.Fatal(err)
103 }
104
Joe Topjian66a046c2017-01-19 22:07:26 -0700105 server, err := CreateServer(t, client)
Joe Topjian48209e32016-07-25 16:31:06 +0000106 if err != nil {
107 t.Fatalf("Unable to create server: %v", err)
108 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600109 defer DeleteServer(t, client, server)
Joe Topjian48209e32016-07-25 16:31:06 +0000110
111 newServer, err := servers.Get(client, server.ID).Extract()
112 if err != nil {
113 t.Fatalf("Unable to get server %s: %v", server.ID, err)
114 }
115
Joe Topjian66a046c2017-01-19 22:07:26 -0700116 floatingIP, err := CreateFloatingIP(t, client)
Joe Topjian48209e32016-07-25 16:31:06 +0000117 if err != nil {
118 t.Fatalf("Unable to create floating IP: %v", err)
119 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600120 defer DeleteFloatingIP(t, client, floatingIP)
Joe Topjian48209e32016-07-25 16:31:06 +0000121
Joe Topjian66a046c2017-01-19 22:07:26 -0700122 tools.PrintResource(t, floatingIP)
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000123
124 var fixedIP string
Joe Topjian48209e32016-07-25 16:31:06 +0000125 for _, networkAddresses := range newServer.Addresses[choices.NetworkName].([]interface{}) {
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000126 address := networkAddresses.(map[string]interface{})
127 if address["OS-EXT-IPS:type"] == "fixed" {
128 if address["version"].(float64) == 4 {
129 fixedIP = address["addr"].(string)
130 }
131 }
132 }
133
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600134 err = AssociateFloatingIPWithFixedIP(t, client, floatingIP, newServer, fixedIP)
Joe Topjian48209e32016-07-25 16:31:06 +0000135 if err != nil {
136 t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, newServer.ID, err)
137 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600138 defer DisassociateFloatingIP(t, client, floatingIP, newServer)
Joe Topjian48209e32016-07-25 16:31:06 +0000139
140 newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract()
141 if err != nil {
142 t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err)
143 }
144
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600145 t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, newFloatingIP.FixedIP)
Joe Topjiand97fe9b2015-09-17 02:08:38 +0000146
Joe Topjian66a046c2017-01-19 22:07:26 -0700147 tools.PrintResource(t, newFloatingIP)
Joe Topjiandee32222015-02-09 23:56:26 +0000148}