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 | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 9 | "github.com/gophercloud/gophercloud/acceptance/tools" |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 10 | "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/floatingips" |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 11 | "github.com/gophercloud/gophercloud/openstack/compute/v2/servers" |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 12 | ) |
| 13 | |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 14 | func TestFloatingIPsList(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 15 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 16 | 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 Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 31 | tools.PrintResource(t, floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | func TestFloatingIPsCreate(t *testing.T) { |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 36 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 37 | if err != nil { |
| 38 | t.Fatalf("Unable to create a compute client: %v", err) |
| 39 | } |
| 40 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 41 | floatingIP, err := CreateFloatingIP(t, client) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 42 | if err != nil { |
| 43 | t.Fatalf("Unable to create floating IP: %v", err) |
| 44 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 45 | defer DeleteFloatingIP(t, client, floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 46 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 47 | tools.PrintResource(t, floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | func TestFloatingIPsAssociate(t *testing.T) { |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 51 | if testing.Short() { |
| 52 | t.Skip("Skipping test that requires server creation in short mode.") |
| 53 | } |
| 54 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 55 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 56 | if err != nil { |
| 57 | t.Fatalf("Unable to create a compute client: %v", err) |
| 58 | } |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 59 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 60 | server, err := CreateServer(t, client) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 61 | if err != nil { |
| 62 | t.Fatalf("Unable to create server: %v", err) |
| 63 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 64 | defer DeleteServer(t, client, server) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 65 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 66 | floatingIP, err := CreateFloatingIP(t, client) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 67 | if err != nil { |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 68 | t.Fatalf("Unable to create floating IP: %v", err) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 69 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 70 | defer DeleteFloatingIP(t, client, floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 71 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 72 | tools.PrintResource(t, floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 73 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 74 | err = AssociateFloatingIP(t, client, floatingIP, server) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 75 | if err != nil { |
| 76 | t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, server.ID, err) |
| 77 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 78 | defer DisassociateFloatingIP(t, client, floatingIP, server) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 79 | |
| 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 Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 85 | t.Logf("Floating IP %s is associated with Fixed IP %s", floatingIP.IP, newFloatingIP.FixedIP) |
| 86 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 87 | tools.PrintResource(t, newFloatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | func TestFloatingIPsFixedIPAssociate(t *testing.T) { |
| 91 | if testing.Short() { |
| 92 | t.Skip("Skipping test that requires server creation in short mode.") |
| 93 | } |
| 94 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 95 | client, err := clients.NewComputeV2Client() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 96 | if err != nil { |
| 97 | t.Fatalf("Unable to create a compute client: %v", err) |
| 98 | } |
| 99 | |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 100 | choices, err := clients.AcceptanceTestChoicesFromEnv() |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 101 | if err != nil { |
| 102 | t.Fatal(err) |
| 103 | } |
| 104 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 105 | server, err := CreateServer(t, client) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 106 | if err != nil { |
| 107 | t.Fatalf("Unable to create server: %v", err) |
| 108 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 109 | defer DeleteServer(t, client, server) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 110 | |
| 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 Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 116 | floatingIP, err := CreateFloatingIP(t, client) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 117 | if err != nil { |
| 118 | t.Fatalf("Unable to create floating IP: %v", err) |
| 119 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 120 | defer DeleteFloatingIP(t, client, floatingIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 121 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 122 | tools.PrintResource(t, floatingIP) |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 123 | |
| 124 | var fixedIP string |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 125 | for _, networkAddresses := range newServer.Addresses[choices.NetworkName].([]interface{}) { |
Joe Topjian | d97fe9b | 2015-09-17 02:08:38 +0000 | [diff] [blame] | 126 | 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 Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 134 | err = AssociateFloatingIPWithFixedIP(t, client, floatingIP, newServer, fixedIP) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 135 | if err != nil { |
| 136 | t.Fatalf("Unable to associate floating IP %s with server %s: %v", floatingIP.IP, newServer.ID, err) |
| 137 | } |
Joe Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 138 | defer DisassociateFloatingIP(t, client, floatingIP, newServer) |
Joe Topjian | 48209e3 | 2016-07-25 16:31:06 +0000 | [diff] [blame] | 139 | |
| 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 Topjian | 1c15e3f | 2016-08-08 10:48:38 -0600 | [diff] [blame] | 145 | 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] | 146 | |
Joe Topjian | 66a046c | 2017-01-19 22:07:26 -0700 | [diff] [blame] | 147 | tools.PrintResource(t, newFloatingIP) |
Joe Topjian | dee3222 | 2015-02-09 23:56:26 +0000 | [diff] [blame] | 148 | } |