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 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 8 | "github.com/gophercloud/gophercloud/acceptance/clients" |
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) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 14 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 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 { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 30 | PrintFloatingIP(t, &floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 31 | } |
| 32 | } |
| 33 | |
| 34 | func TestFloatingIPsCreate(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 35 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 36 | if err != nil { |
| 37 | t.Fatalf("Unable to create a compute client: %v", err) |
| 38 | } |
| 39 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 40 | choices, err := clients.AcceptanceTestChoicesFromEnv() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 45 | floatingIP, err := CreateFloatingIP(t, client, choices) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 46 | if err != nil { |
| 47 | t.Fatalf("Unable to create floating IP: %v", err) |
| 48 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 49 | defer DeleteFloatingIP(t, client, floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 50 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 51 | PrintFloatingIP(t, floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 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 | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 59 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 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 | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 64 | choices, err := clients.AcceptanceTestChoicesFromEnv() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 65 | if err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 68 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [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 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 73 | defer DeleteServer(t, client, server) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 74 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 75 | floatingIP, err := CreateFloatingIP(t, client, choices) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 76 | if err != nil { |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 77 | t.Fatalf("Unable to create floating IP: %v", err) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 78 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 79 | defer DeleteFloatingIP(t, client, floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 80 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 81 | PrintFloatingIP(t, floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 82 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 83 | err = AssociateFloatingIP(t, client, floatingIP, server) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 84 | if err != nil { |
| 85 | t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, server.ID, err) |
| 86 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 87 | defer DisassociateFloatingIP(t, client, floatingIP, server) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 88 | |
| 89 | newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract() |
| 90 | if err != nil { |
| 91 | t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err) |
| 92 | } |
| 93 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 94 | t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, newFloatingIP.FixedIP) |
| 95 | |
| 96 | PrintFloatingIP(t, newFloatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | func TestFloatingIPsFixedIPAssociate(t *testing.T) { |
| 100 | if testing.Short() { |
| 101 | t.Skip("Skipping test that requires server creation in short mode.") |
| 102 | } |
| 103 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 104 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 105 | if err != nil { |
| 106 | t.Fatalf("Unable to create a compute client: %v", err) |
| 107 | } |
| 108 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 109 | choices, err := clients.AcceptanceTestChoicesFromEnv() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 114 | server, err := CreateServer(t, client, choices) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 115 | if err != nil { |
| 116 | t.Fatalf("Unable to create server: %v", err) |
| 117 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 118 | defer DeleteServer(t, client, server) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 119 | |
| 120 | newServer, err := servers.Get(client, server.ID).Extract() |
| 121 | if err != nil { |
| 122 | t.Fatalf("Unable to get server %s: %v", server.ID, err) |
| 123 | } |
| 124 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 125 | floatingIP, err := CreateFloatingIP(t, client, choices) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 126 | if err != nil { |
| 127 | t.Fatalf("Unable to create floating IP: %v", err) |
| 128 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 129 | defer DeleteFloatingIP(t, client, floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 130 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 131 | PrintFloatingIP(t, floatingIP) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 132 | |
| 133 | var fixedIP string |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 134 | for _, networkAddresses := range newServer.Addresses[choices.NetworkName].([]interface{}) { |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 135 | address := networkAddresses.(map[string]interface{}) |
| 136 | if address["OS-EXT-IPS:type"] == "fixed" { |
| 137 | if address["version"].(float64) == 4 { |
| 138 | fixedIP = address["addr"].(string) |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 143 | err = AssociateFloatingIPWithFixedIP(t, client, floatingIP, newServer, fixedIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 144 | if err != nil { |
| 145 | t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, newServer.ID, err) |
| 146 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 147 | defer DisassociateFloatingIP(t, client, floatingIP, newServer) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 148 | |
| 149 | newFloatingIP, err := floatingips.Get(client, floatingIP.ID).Extract() |
| 150 | if err != nil { |
| 151 | t.Fatalf("Unable to get floating IP %s: %v", floatingIP.ID, err) |
| 152 | } |
| 153 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 154 | t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, newFloatingIP.FixedIP) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 155 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 156 | PrintFloatingIP(t, newFloatingIP) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 157 | } |