github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/subnets/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common" 9 "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets" 10 "github.com/gophercloud/gophercloud/pagination" 11 th "github.com/gophercloud/gophercloud/testhelper" 12 ) 13 14 func TestList(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { 19 th.TestMethod(t, r, "GET") 20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 21 22 w.Header().Add("Content-Type", "application/json") 23 w.WriteHeader(http.StatusOK) 24 25 fmt.Fprintf(w, SubnetListResult) 26 }) 27 28 count := 0 29 30 subnets.List(fake.ServiceClient(), subnets.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 31 count++ 32 actual, err := subnets.ExtractSubnets(page) 33 if err != nil { 34 t.Errorf("Failed to extract subnets: %v", err) 35 return false, nil 36 } 37 38 expected := []subnets.Subnet{ 39 Subnet1, 40 Subnet2, 41 Subnet3, 42 Subnet4, 43 } 44 45 th.CheckDeepEquals(t, expected, actual) 46 47 return true, nil 48 }) 49 50 if count != 1 { 51 t.Errorf("Expected 1 page, got %d", count) 52 } 53 } 54 55 func TestGet(t *testing.T) { 56 th.SetupHTTP() 57 defer th.TeardownHTTP() 58 59 th.Mux.HandleFunc("/v2.0/subnets/54d6f61d-db07-451c-9ab3-b9609b6b6f0b", func(w http.ResponseWriter, r *http.Request) { 60 th.TestMethod(t, r, "GET") 61 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 62 63 w.Header().Add("Content-Type", "application/json") 64 w.WriteHeader(http.StatusOK) 65 66 fmt.Fprintf(w, SubnetGetResult) 67 }) 68 69 s, err := subnets.Get(fake.ServiceClient(), "54d6f61d-db07-451c-9ab3-b9609b6b6f0b").Extract() 70 th.AssertNoErr(t, err) 71 72 th.AssertEquals(t, s.Name, "my_subnet") 73 th.AssertEquals(t, s.EnableDHCP, true) 74 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 75 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869") 76 th.AssertDeepEquals(t, s.DNSNameservers, []string{}) 77 th.AssertDeepEquals(t, s.AllocationPools, []subnets.AllocationPool{ 78 { 79 Start: "192.0.0.2", 80 End: "192.255.255.254", 81 }, 82 }) 83 th.AssertDeepEquals(t, s.HostRoutes, []subnets.HostRoute{}) 84 th.AssertEquals(t, s.IPVersion, 4) 85 th.AssertEquals(t, s.GatewayIP, "192.0.0.1") 86 th.AssertEquals(t, s.CIDR, "192.0.0.0/8") 87 th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0b") 88 th.AssertEquals(t, s.SubnetPoolID, "b80340c7-9960-4f67-a99c-02501656284b") 89 } 90 91 func TestCreate(t *testing.T) { 92 th.SetupHTTP() 93 defer th.TeardownHTTP() 94 95 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { 96 th.TestMethod(t, r, "POST") 97 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 98 th.TestHeader(t, r, "Content-Type", "application/json") 99 th.TestHeader(t, r, "Accept", "application/json") 100 th.TestJSONRequest(t, r, SubnetCreateRequest) 101 102 w.Header().Add("Content-Type", "application/json") 103 w.WriteHeader(http.StatusCreated) 104 105 fmt.Fprintf(w, SubnetCreateResult) 106 }) 107 108 var gatewayIP = "192.168.199.1" 109 opts := subnets.CreateOpts{ 110 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", 111 IPVersion: 4, 112 CIDR: "192.168.199.0/24", 113 GatewayIP: &gatewayIP, 114 AllocationPools: []subnets.AllocationPool{ 115 { 116 Start: "192.168.199.2", 117 End: "192.168.199.254", 118 }, 119 }, 120 DNSNameservers: []string{"foo"}, 121 ServiceTypes: []string{"network:routed"}, 122 HostRoutes: []subnets.HostRoute{ 123 {NextHop: "bar"}, 124 }, 125 SubnetPoolID: "b80340c7-9960-4f67-a99c-02501656284b", 126 } 127 s, err := subnets.Create(fake.ServiceClient(), opts).Extract() 128 th.AssertNoErr(t, err) 129 130 th.AssertEquals(t, s.Name, "") 131 th.AssertEquals(t, s.EnableDHCP, true) 132 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 133 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869") 134 th.AssertDeepEquals(t, s.DNSNameservers, []string{"foo"}) 135 th.AssertDeepEquals(t, s.ServiceTypes, []string{"network:routed"}) 136 th.AssertDeepEquals(t, s.AllocationPools, []subnets.AllocationPool{ 137 { 138 Start: "192.168.199.2", 139 End: "192.168.199.254", 140 }, 141 }) 142 th.AssertDeepEquals(t, s.HostRoutes, []subnets.HostRoute{}) 143 th.AssertEquals(t, s.IPVersion, 4) 144 th.AssertEquals(t, s.GatewayIP, "192.168.199.1") 145 th.AssertEquals(t, s.CIDR, "192.168.199.0/24") 146 th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126") 147 th.AssertEquals(t, s.SubnetPoolID, "b80340c7-9960-4f67-a99c-02501656284b") 148 } 149 150 func TestCreateNoGateway(t *testing.T) { 151 th.SetupHTTP() 152 defer th.TeardownHTTP() 153 154 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { 155 th.TestMethod(t, r, "POST") 156 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 157 th.TestHeader(t, r, "Content-Type", "application/json") 158 th.TestHeader(t, r, "Accept", "application/json") 159 th.TestJSONRequest(t, r, SubnetCreateWithNoGatewayRequest) 160 161 w.Header().Add("Content-Type", "application/json") 162 w.WriteHeader(http.StatusCreated) 163 164 fmt.Fprintf(w, SubnetCreateWithNoGatewayResponse) 165 }) 166 167 var noGateway = "" 168 opts := subnets.CreateOpts{ 169 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a23", 170 IPVersion: 4, 171 CIDR: "192.168.1.0/24", 172 GatewayIP: &noGateway, 173 AllocationPools: []subnets.AllocationPool{ 174 { 175 Start: "192.168.1.2", 176 End: "192.168.1.254", 177 }, 178 }, 179 DNSNameservers: []string{}, 180 } 181 s, err := subnets.Create(fake.ServiceClient(), opts).Extract() 182 th.AssertNoErr(t, err) 183 184 th.AssertEquals(t, s.Name, "") 185 th.AssertEquals(t, s.EnableDHCP, true) 186 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a23") 187 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869") 188 th.AssertDeepEquals(t, s.AllocationPools, []subnets.AllocationPool{ 189 { 190 Start: "192.168.1.2", 191 End: "192.168.1.254", 192 }, 193 }) 194 th.AssertDeepEquals(t, s.HostRoutes, []subnets.HostRoute{}) 195 th.AssertEquals(t, s.IPVersion, 4) 196 th.AssertEquals(t, s.GatewayIP, "") 197 th.AssertEquals(t, s.CIDR, "192.168.1.0/24") 198 th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0c") 199 } 200 201 func TestCreateDefaultGateway(t *testing.T) { 202 th.SetupHTTP() 203 defer th.TeardownHTTP() 204 205 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { 206 th.TestMethod(t, r, "POST") 207 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 208 th.TestHeader(t, r, "Content-Type", "application/json") 209 th.TestHeader(t, r, "Accept", "application/json") 210 th.TestJSONRequest(t, r, SubnetCreateWithDefaultGatewayRequest) 211 212 w.Header().Add("Content-Type", "application/json") 213 w.WriteHeader(http.StatusCreated) 214 215 fmt.Fprintf(w, SubnetCreateWithDefaultGatewayResponse) 216 }) 217 218 opts := subnets.CreateOpts{ 219 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a23", 220 IPVersion: 4, 221 CIDR: "192.168.1.0/24", 222 AllocationPools: []subnets.AllocationPool{ 223 { 224 Start: "192.168.1.2", 225 End: "192.168.1.254", 226 }, 227 }, 228 DNSNameservers: []string{}, 229 } 230 s, err := subnets.Create(fake.ServiceClient(), opts).Extract() 231 th.AssertNoErr(t, err) 232 233 th.AssertEquals(t, s.Name, "") 234 th.AssertEquals(t, s.EnableDHCP, true) 235 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a23") 236 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869") 237 th.AssertDeepEquals(t, s.AllocationPools, []subnets.AllocationPool{ 238 { 239 Start: "192.168.1.2", 240 End: "192.168.1.254", 241 }, 242 }) 243 th.AssertDeepEquals(t, s.HostRoutes, []subnets.HostRoute{}) 244 th.AssertEquals(t, s.IPVersion, 4) 245 th.AssertEquals(t, s.GatewayIP, "192.168.1.1") 246 th.AssertEquals(t, s.CIDR, "192.168.1.0/24") 247 th.AssertEquals(t, s.ID, "54d6f61d-db07-451c-9ab3-b9609b6b6f0c") 248 } 249 250 func TestCreateIPv6RaAddressMode(t *testing.T) { 251 th.SetupHTTP() 252 defer th.TeardownHTTP() 253 254 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { 255 th.TestMethod(t, r, "POST") 256 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 257 th.TestHeader(t, r, "Content-Type", "application/json") 258 th.TestHeader(t, r, "Accept", "application/json") 259 th.TestJSONRequest(t, r, SubnetCreateWithIPv6RaAddressModeRequest) 260 261 w.Header().Add("Content-Type", "application/json") 262 w.WriteHeader(http.StatusCreated) 263 264 fmt.Fprintf(w, SubnetCreateWithIPv6RaAddressModeResponse) 265 }) 266 267 var gatewayIP = "2001:db8:0:a::1" 268 opts := subnets.CreateOpts{ 269 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", 270 IPVersion: 6, 271 CIDR: "2001:db8:0:a:0:0:0:0/64", 272 GatewayIP: &gatewayIP, 273 IPv6AddressMode: "slaac", 274 IPv6RAMode: "slaac", 275 } 276 s, err := subnets.Create(fake.ServiceClient(), opts).Extract() 277 th.AssertNoErr(t, err) 278 279 th.AssertEquals(t, s.Name, "") 280 th.AssertEquals(t, s.EnableDHCP, true) 281 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 282 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869") 283 th.AssertEquals(t, s.IPVersion, 6) 284 th.AssertEquals(t, s.GatewayIP, "2001:db8:0:a::1") 285 th.AssertEquals(t, s.CIDR, "2001:db8:0:a:0:0:0:0/64") 286 th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126") 287 th.AssertEquals(t, s.IPv6AddressMode, "slaac") 288 th.AssertEquals(t, s.IPv6RAMode, "slaac") 289 } 290 291 func TestCreateWithNoCIDR(t *testing.T) { 292 th.SetupHTTP() 293 defer th.TeardownHTTP() 294 295 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { 296 th.TestMethod(t, r, "POST") 297 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 298 th.TestHeader(t, r, "Content-Type", "application/json") 299 th.TestHeader(t, r, "Accept", "application/json") 300 th.TestJSONRequest(t, r, SubnetCreateRequestWithNoCIDR) 301 302 w.Header().Add("Content-Type", "application/json") 303 w.WriteHeader(http.StatusCreated) 304 305 fmt.Fprintf(w, SubnetCreateResult) 306 }) 307 308 opts := subnets.CreateOpts{ 309 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", 310 IPVersion: 4, 311 DNSNameservers: []string{"foo"}, 312 HostRoutes: []subnets.HostRoute{ 313 {NextHop: "bar"}, 314 }, 315 SubnetPoolID: "b80340c7-9960-4f67-a99c-02501656284b", 316 } 317 s, err := subnets.Create(fake.ServiceClient(), opts).Extract() 318 th.AssertNoErr(t, err) 319 320 th.AssertEquals(t, s.Name, "") 321 th.AssertEquals(t, s.EnableDHCP, true) 322 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 323 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869") 324 th.AssertDeepEquals(t, s.DNSNameservers, []string{"foo"}) 325 th.AssertDeepEquals(t, s.AllocationPools, []subnets.AllocationPool{ 326 { 327 Start: "192.168.199.2", 328 End: "192.168.199.254", 329 }, 330 }) 331 th.AssertDeepEquals(t, s.HostRoutes, []subnets.HostRoute{}) 332 th.AssertEquals(t, s.IPVersion, 4) 333 th.AssertEquals(t, s.GatewayIP, "192.168.199.1") 334 th.AssertEquals(t, s.CIDR, "192.168.199.0/24") 335 th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126") 336 th.AssertEquals(t, s.SubnetPoolID, "b80340c7-9960-4f67-a99c-02501656284b") 337 } 338 339 func TestCreateWithPrefixlen(t *testing.T) { 340 th.SetupHTTP() 341 defer th.TeardownHTTP() 342 343 th.Mux.HandleFunc("/v2.0/subnets", func(w http.ResponseWriter, r *http.Request) { 344 th.TestMethod(t, r, "POST") 345 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 346 th.TestHeader(t, r, "Content-Type", "application/json") 347 th.TestHeader(t, r, "Accept", "application/json") 348 th.TestJSONRequest(t, r, SubnetCreateRequestWithPrefixlen) 349 350 w.Header().Add("Content-Type", "application/json") 351 w.WriteHeader(http.StatusCreated) 352 353 fmt.Fprintf(w, SubnetCreateResult) 354 }) 355 356 opts := subnets.CreateOpts{ 357 NetworkID: "d32019d3-bc6e-4319-9c1d-6722fc136a22", 358 IPVersion: 4, 359 DNSNameservers: []string{"foo"}, 360 HostRoutes: []subnets.HostRoute{ 361 {NextHop: "bar"}, 362 }, 363 SubnetPoolID: "b80340c7-9960-4f67-a99c-02501656284b", 364 Prefixlen: 12, 365 } 366 s, err := subnets.Create(fake.ServiceClient(), opts).Extract() 367 th.AssertNoErr(t, err) 368 369 th.AssertEquals(t, s.Name, "") 370 th.AssertEquals(t, s.EnableDHCP, true) 371 th.AssertEquals(t, s.NetworkID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") 372 th.AssertEquals(t, s.TenantID, "4fd44f30292945e481c7b8a0c8908869") 373 th.AssertDeepEquals(t, s.DNSNameservers, []string{"foo"}) 374 th.AssertDeepEquals(t, s.AllocationPools, []subnets.AllocationPool{ 375 { 376 Start: "192.168.199.2", 377 End: "192.168.199.254", 378 }, 379 }) 380 th.AssertDeepEquals(t, s.HostRoutes, []subnets.HostRoute{}) 381 th.AssertEquals(t, s.IPVersion, 4) 382 th.AssertEquals(t, s.GatewayIP, "192.168.199.1") 383 th.AssertEquals(t, s.CIDR, "192.168.199.0/24") 384 th.AssertEquals(t, s.ID, "3b80198d-4f7b-4f77-9ef5-774d54e17126") 385 th.AssertEquals(t, s.SubnetPoolID, "b80340c7-9960-4f67-a99c-02501656284b") 386 } 387 388 func TestRequiredCreateOpts(t *testing.T) { 389 res := subnets.Create(fake.ServiceClient(), subnets.CreateOpts{}) 390 if res.Err == nil { 391 t.Fatalf("Expected error, got none") 392 } 393 394 res = subnets.Create(fake.ServiceClient(), subnets.CreateOpts{NetworkID: "foo"}) 395 if res.Err == nil { 396 t.Fatalf("Expected error, got none") 397 } 398 399 res = subnets.Create(fake.ServiceClient(), subnets.CreateOpts{NetworkID: "foo", CIDR: "bar", IPVersion: 40}) 400 if res.Err == nil { 401 t.Fatalf("Expected error, got none") 402 } 403 } 404 405 func TestUpdate(t *testing.T) { 406 th.SetupHTTP() 407 defer th.TeardownHTTP() 408 409 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { 410 th.TestMethod(t, r, "PUT") 411 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 412 th.TestHeader(t, r, "Content-Type", "application/json") 413 th.TestHeader(t, r, "Accept", "application/json") 414 th.TestJSONRequest(t, r, SubnetUpdateRequest) 415 416 w.Header().Add("Content-Type", "application/json") 417 w.WriteHeader(http.StatusCreated) 418 419 fmt.Fprintf(w, SubnetUpdateResponse) 420 }) 421 422 dnsNameservers := []string{"foo"} 423 name := "my_new_subnet" 424 opts := subnets.UpdateOpts{ 425 Name: &name, 426 DNSNameservers: &dnsNameservers, 427 HostRoutes: &[]subnets.HostRoute{ 428 {NextHop: "bar"}, 429 }, 430 } 431 s, err := subnets.Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract() 432 th.AssertNoErr(t, err) 433 434 th.AssertEquals(t, s.Name, "my_new_subnet") 435 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b") 436 } 437 438 func TestUpdateGateway(t *testing.T) { 439 th.SetupHTTP() 440 defer th.TeardownHTTP() 441 442 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { 443 th.TestMethod(t, r, "PUT") 444 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 445 th.TestHeader(t, r, "Content-Type", "application/json") 446 th.TestHeader(t, r, "Accept", "application/json") 447 th.TestJSONRequest(t, r, SubnetUpdateGatewayRequest) 448 449 w.Header().Add("Content-Type", "application/json") 450 w.WriteHeader(http.StatusCreated) 451 452 fmt.Fprintf(w, SubnetUpdateGatewayResponse) 453 }) 454 455 var gatewayIP = "10.0.0.1" 456 name := "my_new_subnet" 457 opts := subnets.UpdateOpts{ 458 Name: &name, 459 GatewayIP: &gatewayIP, 460 } 461 s, err := subnets.Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract() 462 th.AssertNoErr(t, err) 463 464 th.AssertEquals(t, s.Name, "my_new_subnet") 465 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b") 466 th.AssertEquals(t, s.GatewayIP, "10.0.0.1") 467 } 468 469 func TestUpdateRemoveGateway(t *testing.T) { 470 th.SetupHTTP() 471 defer th.TeardownHTTP() 472 473 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { 474 th.TestMethod(t, r, "PUT") 475 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 476 th.TestHeader(t, r, "Content-Type", "application/json") 477 th.TestHeader(t, r, "Accept", "application/json") 478 th.TestJSONRequest(t, r, SubnetUpdateRemoveGatewayRequest) 479 480 w.Header().Add("Content-Type", "application/json") 481 w.WriteHeader(http.StatusCreated) 482 483 fmt.Fprintf(w, SubnetUpdateRemoveGatewayResponse) 484 }) 485 486 var noGateway = "" 487 name := "my_new_subnet" 488 opts := subnets.UpdateOpts{ 489 Name: &name, 490 GatewayIP: &noGateway, 491 } 492 s, err := subnets.Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract() 493 th.AssertNoErr(t, err) 494 495 th.AssertEquals(t, s.Name, "my_new_subnet") 496 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b") 497 th.AssertEquals(t, s.GatewayIP, "") 498 } 499 500 func TestUpdateHostRoutes(t *testing.T) { 501 th.SetupHTTP() 502 defer th.TeardownHTTP() 503 504 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { 505 th.TestMethod(t, r, "PUT") 506 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 507 th.TestHeader(t, r, "Content-Type", "application/json") 508 th.TestHeader(t, r, "Accept", "application/json") 509 th.TestJSONRequest(t, r, SubnetUpdateHostRoutesRequest) 510 511 w.Header().Add("Content-Type", "application/json") 512 w.WriteHeader(http.StatusCreated) 513 514 fmt.Fprintf(w, SubnetUpdateHostRoutesResponse) 515 }) 516 517 HostRoutes := []subnets.HostRoute{ 518 { 519 DestinationCIDR: "192.168.1.1/24", 520 NextHop: "bar", 521 }, 522 } 523 524 name := "my_new_subnet" 525 opts := subnets.UpdateOpts{ 526 Name: &name, 527 HostRoutes: &HostRoutes, 528 } 529 s, err := subnets.Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract() 530 th.AssertNoErr(t, err) 531 532 th.AssertEquals(t, s.Name, "my_new_subnet") 533 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b") 534 th.AssertDeepEquals(t, s.HostRoutes, HostRoutes) 535 } 536 537 func TestUpdateRemoveHostRoutes(t *testing.T) { 538 th.SetupHTTP() 539 defer th.TeardownHTTP() 540 541 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { 542 th.TestMethod(t, r, "PUT") 543 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 544 th.TestHeader(t, r, "Content-Type", "application/json") 545 th.TestHeader(t, r, "Accept", "application/json") 546 th.TestJSONRequest(t, r, SubnetUpdateRemoveHostRoutesRequest) 547 548 w.Header().Add("Content-Type", "application/json") 549 w.WriteHeader(http.StatusCreated) 550 551 fmt.Fprintf(w, SubnetUpdateRemoveHostRoutesResponse) 552 }) 553 554 noHostRoutes := []subnets.HostRoute{} 555 opts := subnets.UpdateOpts{ 556 HostRoutes: &noHostRoutes, 557 } 558 s, err := subnets.Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract() 559 th.AssertNoErr(t, err) 560 561 th.AssertEquals(t, s.Name, "my_new_subnet") 562 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b") 563 th.AssertDeepEquals(t, s.HostRoutes, noHostRoutes) 564 } 565 566 func TestUpdateAllocationPool(t *testing.T) { 567 th.SetupHTTP() 568 defer th.TeardownHTTP() 569 570 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { 571 th.TestMethod(t, r, "PUT") 572 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 573 th.TestHeader(t, r, "Content-Type", "application/json") 574 th.TestHeader(t, r, "Accept", "application/json") 575 th.TestJSONRequest(t, r, SubnetUpdateAllocationPoolRequest) 576 577 w.Header().Add("Content-Type", "application/json") 578 w.WriteHeader(http.StatusCreated) 579 580 fmt.Fprintf(w, SubnetUpdateAllocationPoolResponse) 581 }) 582 583 name := "my_new_subnet" 584 opts := subnets.UpdateOpts{ 585 Name: &name, 586 AllocationPools: []subnets.AllocationPool{ 587 { 588 Start: "10.1.0.2", 589 End: "10.1.0.254", 590 }, 591 }, 592 } 593 s, err := subnets.Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract() 594 th.AssertNoErr(t, err) 595 596 th.AssertEquals(t, s.Name, "my_new_subnet") 597 th.AssertEquals(t, s.ID, "08eae331-0402-425a-923c-34f7cfe39c1b") 598 th.AssertDeepEquals(t, s.AllocationPools, []subnets.AllocationPool{ 599 { 600 Start: "10.1.0.2", 601 End: "10.1.0.254", 602 }, 603 }) 604 } 605 606 func TestUpdateRevision(t *testing.T) { 607 th.SetupHTTP() 608 defer th.TeardownHTTP() 609 610 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { 611 th.TestMethod(t, r, "PUT") 612 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 613 th.TestHeader(t, r, "Content-Type", "application/json") 614 th.TestHeader(t, r, "Accept", "application/json") 615 th.TestHeaderUnset(t, r, "If-Match") 616 th.TestJSONRequest(t, r, SubnetUpdateRequest) 617 618 w.Header().Add("Content-Type", "application/json") 619 w.WriteHeader(http.StatusCreated) 620 621 fmt.Fprintf(w, SubnetUpdateResponse) 622 }) 623 624 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1c", func(w http.ResponseWriter, r *http.Request) { 625 th.TestMethod(t, r, "PUT") 626 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 627 th.TestHeader(t, r, "Content-Type", "application/json") 628 th.TestHeader(t, r, "Accept", "application/json") 629 th.TestHeader(t, r, "If-Match", "revision_number=42") 630 th.TestJSONRequest(t, r, SubnetUpdateRequest) 631 632 w.Header().Add("Content-Type", "application/json") 633 w.WriteHeader(http.StatusCreated) 634 635 fmt.Fprintf(w, SubnetUpdateResponse) 636 }) 637 638 dnsNameservers := []string{"foo"} 639 name := "my_new_subnet" 640 opts := subnets.UpdateOpts{ 641 Name: &name, 642 DNSNameservers: &dnsNameservers, 643 HostRoutes: &[]subnets.HostRoute{ 644 {NextHop: "bar"}, 645 }, 646 } 647 _, err := subnets.Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b", opts).Extract() 648 th.AssertNoErr(t, err) 649 650 revisionNumber := 42 651 opts.RevisionNumber = &revisionNumber 652 _, err = subnets.Update(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1c", opts).Extract() 653 th.AssertNoErr(t, err) 654 } 655 656 func TestDelete(t *testing.T) { 657 th.SetupHTTP() 658 defer th.TeardownHTTP() 659 660 th.Mux.HandleFunc("/v2.0/subnets/08eae331-0402-425a-923c-34f7cfe39c1b", func(w http.ResponseWriter, r *http.Request) { 661 th.TestMethod(t, r, "DELETE") 662 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 663 w.WriteHeader(http.StatusNoContent) 664 }) 665 666 res := subnets.Delete(fake.ServiceClient(), "08eae331-0402-425a-923c-34f7cfe39c1b") 667 th.AssertNoErr(t, res.Err) 668 }