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