Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 1 | // +build acceptance compute servers |
| 2 | |
| 3 | package v2 |
| 4 | |
| 5 | import ( |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 6 | "testing" |
| 7 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud" |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 11 | ) |
| 12 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 13 | func TestFloatingIPsList(t *testing.T) { |
| 14 | client, err := newClient() |
| 15 | if err != nil { |
| 16 | t.Fatalf("Unable to create a compute client: %v", err) |
| 17 | } |
| 18 | |
| 19 | allPages, err := floatingips.List(client).AllPages() |
| 20 | if err != nil { |
| 21 | t.Fatalf("Unable to retrieve floating IPs: %v", err) |
| 22 | } |
| 23 | |
| 24 | allFloatingIPs, err := floatingips.ExtractFloatingIPs(allPages) |
| 25 | if err != nil { |
| 26 | t.Fatalf("Unable to extract floating IPs: %v", err) |
| 27 | } |
| 28 | |
| 29 | for _, floatingIP := range allFloatingIPs { |
| 30 | printFloatingIP(t, &floatingIP) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestFloatingIPsCreate(t *testing.T) { |
| 35 | client, err := newClient() |
| 36 | if err != nil { |
| 37 | t.Fatalf("Unable to create a compute client: %v", err) |
| 38 | } |
| 39 | |
| 40 | choices, err := ComputeChoicesFromEnv() |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | |
| 45 | floatingIP, err := createFloatingIP(t, client, choices) |
| 46 | if err != nil { |
| 47 | t.Fatalf("Unable to create floating IP: %v", err) |
| 48 | } |
| 49 | defer deleteFloatingIP(t, client, floatingIP) |
| 50 | |
| 51 | printFloatingIP(t, floatingIP) |
| 52 | } |
| 53 | |
| 54 | func TestFloatingIPsAssociate(t *testing.T) { |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 55 | if testing.Short() { |
| 56 | t.Skip("Skipping test that requires server creation in short mode.") |
| 57 | } |
| 58 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 59 | client, err := newClient() |
| 60 | if err != nil { |
| 61 | t.Fatalf("Unable to create a compute client: %v", err) |
| 62 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 63 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 64 | choices, err := ComputeChoicesFromEnv() |
| 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 68 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 69 | server, err := createServer(t, client, choices) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 70 | if err != nil { |
| 71 | t.Fatalf("Unable to create server: %v", err) |
| 72 | } |
| 73 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 74 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 75 | t.Fatalf("Unable to wait for server: %v", err) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 76 | } |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 77 | defer deleteServer(t, client, server) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 78 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 79 | floatingIP, err := createFloatingIP(t, client, choices) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 80 | if err != nil { |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 81 | t.Fatalf("Unable to create floating IP: %v", err) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 82 | } |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 83 | defer deleteFloatingIP(t, client, floatingIP) |
| 84 | |
| 85 | printFloatingIP(t, floatingIP) |
| 86 | |
| 87 | associateOpts := floatingips.AssociateOpts{ |
| 88 | FloatingIP: floatingIP.IP, |
| 89 | } |
| 90 | |
| 91 | t.Logf("Attempting to associate floating IP %s to instance %s", floatingIP.IP, server.ID) |
| 92 | err = floatingips.AssociateInstance(client, server.ID, associateOpts).ExtractErr() |
| 93 | if err != nil { |
| 94 | t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, server.ID, err) |
| 95 | } |
| 96 | defer disassociateFloatingIP(t, client, floatingIP, server) |
| 97 | t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, floatingIP.FixedIP) |
| 98 | |
| 99 | newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract() |
| 100 | if err != nil { |
| 101 | t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err) |
| 102 | } |
| 103 | |
| 104 | printFloatingIP(t, newFloatingIP) |
| 105 | } |
| 106 | |
| 107 | func TestFloatingIPsFixedIPAssociate(t *testing.T) { |
| 108 | if testing.Short() { |
| 109 | t.Skip("Skipping test that requires server creation in short mode.") |
| 110 | } |
| 111 | |
| 112 | client, err := newClient() |
| 113 | if err != nil { |
| 114 | t.Fatalf("Unable to create a compute client: %v", err) |
| 115 | } |
| 116 | |
| 117 | choices, err := ComputeChoicesFromEnv() |
| 118 | if err != nil { |
| 119 | t.Fatal(err) |
| 120 | } |
| 121 | |
| 122 | server, err := createServer(t, client, choices) |
| 123 | if err != nil { |
| 124 | t.Fatalf("Unable to create server: %v", err) |
| 125 | } |
| 126 | |
| 127 | if err = waitForStatus(client, server, "ACTIVE"); err != nil { |
| 128 | t.Fatalf("Unable to wait for server: %v", err) |
| 129 | } |
| 130 | defer deleteServer(t, client, server) |
| 131 | |
| 132 | newServer, err := servers.Get(client, server.ID).Extract() |
| 133 | if err != nil { |
| 134 | t.Fatalf("Unable to get server %s: %v", server.ID, err) |
| 135 | } |
| 136 | |
| 137 | floatingIP, err := createFloatingIP(t, client, choices) |
| 138 | if err != nil { |
| 139 | t.Fatalf("Unable to create floating IP: %v", err) |
| 140 | } |
| 141 | defer deleteFloatingIP(t, client, floatingIP) |
| 142 | |
| 143 | printFloatingIP(t, floatingIP) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 144 | |
| 145 | var fixedIP string |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 146 | for _, networkAddresses := range newServer.Addresses[choices.NetworkName].([]interface{}) { |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 147 | address := networkAddresses.(map[string]interface{}) |
| 148 | if address["OS-EXT-IPS:type"] == "fixed" { |
| 149 | if address["version"].(float64) == 4 { |
| 150 | fixedIP = address["addr"].(string) |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 155 | associateOpts := floatingips.AssociateOpts{ |
| 156 | FloatingIP: floatingIP.IP, |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 157 | FixedIP: fixedIP, |
| 158 | } |
| 159 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 160 | t.Logf("Attempting to associate floating IP %s to instance %s", floatingIP.IP, newServer.ID) |
| 161 | err = floatingips.AssociateInstance(client, newServer.ID, associateOpts).ExtractErr() |
| 162 | if err != nil { |
| 163 | t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, newServer.ID, err) |
| 164 | } |
| 165 | defer disassociateFloatingIP(t, client, floatingIP, newServer) |
| 166 | t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, floatingIP.FixedIP) |
| 167 | |
| 168 | newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract() |
| 169 | if err != nil { |
| 170 | t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err) |
| 171 | } |
| 172 | |
| 173 | printFloatingIP(t, newFloatingIP) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 176 | func createFloatingIP(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*floatingips.FloatingIP, error) { |
| 177 | createOpts := floatingips.CreateOpts{ |
| 178 | Pool: choices.FloatingIPPoolName, |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 179 | } |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 180 | floatingIP, err := floatingips.Create(client, createOpts).Extract() |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 181 | if err != nil { |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 182 | return floatingIP, err |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 185 | t.Logf("Created floating IP: %s", floatingIP.ID) |
| 186 | return floatingIP, nil |
| 187 | } |
| 188 | |
| 189 | func deleteFloatingIP(t *testing.T, client *gophercloud.ServiceClient, floatingIP *floatingips.FloatingIP) { |
| 190 | err := floatingips.Delete(client, floatingIP.ID).ExtractErr() |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 191 | if err != nil { |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 192 | t.Fatalf("Unable to delete floating IP %s: %v", floatingIP.ID, err) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 195 | t.Logf("Deleted floating IP: %s", floatingIP.ID) |
| 196 | } |
| 197 | |
| 198 | func disassociateFloatingIP(t *testing.T, client *gophercloud.ServiceClient, floatingIP *floatingips.FloatingIP, server *servers.Server) { |
| 199 | disassociateOpts := floatingips.DisassociateOpts{ |
| 200 | FloatingIP: floatingIP.IP, |
| 201 | } |
| 202 | |
| 203 | err := floatingips.DisassociateInstance(client, server.ID, disassociateOpts).ExtractErr() |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 204 | if err != nil { |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 205 | t.Fatalf("Unable to disassociate floating IP %s from server %s: %v", floatingIP.IP, server.ID, err) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 208 | t.Logf("Disassociated floating IP %s from server %s", floatingIP.IP, server.ID) |
| 209 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 210 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 211 | func printFloatingIP(t *testing.T, floatingIP *floatingips.FloatingIP) { |
| 212 | t.Logf("ID: %s", floatingIP.ID) |
| 213 | t.Logf("Fixed IP: %s", floatingIP.FixedIP) |
| 214 | t.Logf("Instance ID: %s", floatingIP.InstanceID) |
| 215 | t.Logf("IP: %s", floatingIP.IP) |
| 216 | t.Logf("Pool: %s", floatingIP.Pool) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 217 | } |