github.com/IBM-Cloud/bluemix-go@v0.0.0-20240314082800-4e02a69b84b2/api/cis/cisv1/pools_test.go (about) 1 package cisv1 2 3 import ( 4 "log" 5 "net/http" 6 7 bluemix "github.com/IBM-Cloud/bluemix-go" 8 "github.com/IBM-Cloud/bluemix-go/client" 9 bluemixHttp "github.com/IBM-Cloud/bluemix-go/http" 10 "github.com/IBM-Cloud/bluemix-go/session" 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 "github.com/onsi/gomega/ghttp" 14 ) 15 16 var _ = Describe("Pools", func() { 17 var server *ghttp.Server 18 AfterEach(func() { 19 server.Close() 20 }) 21 Describe("Create", func() { 22 Context("When creation is successful", func() { 23 BeforeEach(func() { 24 server = ghttp.NewServer() 25 server.AppendHandlers( 26 ghttp.CombineHandlers( 27 ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/pools"), 28 ghttp.RespondWith(http.StatusCreated, ` 29 { 30 "result": { 31 "description": "", 32 "created_on": "2018-11-22T10:09:44.288581Z", 33 "modified_on": "2018-11-22T10:09:44.288581Z", 34 "id": "4112ba6c2974ec43886f90736968e838", 35 "enabled": true, 36 "minimum_origins": 1, 37 "monitor": "92859a0f6b4d3e55b953e0e29bb96338", 38 "name": "eu-pool", 39 "notification_email": "", 40 "check_regions": [ 41 "EEU" 42 ], 43 "origins": [ 44 { 45 "name": "eu-origin1", 46 "address": "150.0.0.1", 47 "enabled": true, 48 "weight": 1 49 }, 50 { 51 "name": "eu-origin2", 52 "address": "150.0.0.2", 53 "enabled": true, 54 "weight": 1 55 } 56 ] 57 }, 58 "success": true, 59 "errors": [], 60 "messages": [] 61 } 62 `), 63 ), 64 ) 65 }) 66 67 It("should return zone created", func() { 68 69 origins := []Origin{ 70 {Name: "eu-origin1", Address: "150.0.0.1", Enabled: true, Weight: 1}, 71 {Name: "eu-origin2", Address: "150.0.0.2", Enabled: true, Weight: 1}, 72 } 73 checkRegions := []string{"EEU"} 74 params := PoolBody{ 75 Name: "eu-pool", 76 Description: "", 77 Origins: origins, 78 CheckRegions: checkRegions, 79 Enabled: true, 80 MinOrigins: 1, 81 Monitor: "92859a0f6b4d3e55b953e0e29bb96338", 82 NotEmail: "", 83 } 84 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 85 myPoolPtr, err := newPool(server.URL()).CreatePool(target, params) 86 myPool := *myPoolPtr 87 Expect(err).NotTo(HaveOccurred()) 88 Expect(myPool).ShouldNot(BeNil()) 89 Expect(myPool.Id).Should(Equal("4112ba6c2974ec43886f90736968e838")) 90 Expect(myPool.Name).Should(Equal("eu-pool")) 91 }) 92 }) 93 Context("When creation is unsuccessful", func() { 94 BeforeEach(func() { 95 server = ghttp.NewServer() 96 server.SetAllowUnhandledRequests(true) 97 server.AppendHandlers( 98 ghttp.CombineHandlers( 99 ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/pools"), 100 ghttp.RespondWith(http.StatusInternalServerError, `Failed to create Pool`), 101 ), 102 ) 103 }) 104 105 It("should return error during Pool creation", func() { 106 origins := []Origin{ 107 Origin{Name: "eu-origin1", Address: "150.0.0.1", Enabled: true, Weight: 1}, 108 Origin{Name: "eu-origin2", Address: "150.0.0.2", Enabled: true, Weight: 1}, 109 } 110 checkRegions := []string{"EEU"} 111 params := PoolBody{ 112 Name: "eu-pool", 113 Description: "", 114 Origins: origins, 115 CheckRegions: checkRegions, 116 Enabled: true, 117 MinOrigins: 1, 118 Monitor: "92859a0f6b4d3e55b953e0e29bb96338", 119 NotEmail: "", 120 } 121 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 122 myPoolPtr, err := newPool(server.URL()).CreatePool(target, params) 123 myPool := myPoolPtr 124 Expect(err).To(HaveOccurred()) 125 Expect(myPool).Should(BeNil()) 126 }) 127 }) 128 }) 129 //List 130 Describe("List", func() { 131 Context("When read of Pools is successful", func() { 132 BeforeEach(func() { 133 server = ghttp.NewServer() 134 server.AppendHandlers( 135 ghttp.CombineHandlers( 136 ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/pools"), 137 ghttp.RespondWith(http.StatusOK, ` 138 { 139 "result": [ 140 { 141 "description": "", 142 "created_on": "2018-11-22T10:00:17.220005Z", 143 "modified_on": "2018-11-22T10:00:17.220005Z", 144 "id": "19901040048d70b15014330a6e252ba9", 145 "enabled": true, 146 "minimum_origins": 1, 147 "monitor": "92859a0f6b4d3e55b953e0e29bb96338", 148 "name": "ap-pool", 149 "notification_email": "", 150 "check_regions": [ 151 "SEAS" 152 ], 153 "healthy": false, 154 "origins": [ 155 { 156 "name": "ap-origin1", 157 "address": "100.0.0.1", 158 "enabled": true, 159 "weight": 1, 160 "healthy": false, 161 "failure_reason": "HTTP timeout occurred" 162 }, 163 { 164 "name": "ap-origin2", 165 "address": "100.0.0.2", 166 "enabled": true, 167 "weight": 1, 168 "healthy": false, 169 "failure_reason": "HTTP timeout occurred" 170 } 171 ] 172 }, 173 { 174 "description": "", 175 "created_on": "2018-11-20T07:20:06.057298Z", 176 "modified_on": "2018-11-22T10:00:52.867903Z", 177 "id": "19901040048d70b15014330a6e252ba9", 178 "enabled": true, 179 "minimum_origins": 1, 180 "monitor": "92859a0f6b4d3e55b953e0e29bb96338", 181 "name": "us-pool", 182 "notification_email": "", 183 "check_regions": [ 184 "WNAM" 185 ], 186 "healthy": false, 187 "origins": [ 188 { 189 "name": "us-origin1", 190 "address": "200.0.0.1", 191 "enabled": true, 192 "weight": 1, 193 "healthy": false, 194 "failure_reason": "Unspecified error" 195 }, 196 { 197 "name": "us-origin2", 198 "address": "200.0.0.2", 199 "enabled": true, 200 "weight": 1, 201 "healthy": false, 202 "failure_reason": "Unspecified error" 203 } 204 ] 205 } 206 ], 207 "success": true, 208 "errors": [], 209 "messages": [] 210 } 211 `), 212 ), 213 ) 214 }) 215 216 It("should return Pool list", func() { 217 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 218 myPools, err := newPool(server.URL()).ListPools(target) 219 Expect(myPools).ShouldNot(BeNil()) 220 for _, Pool := range myPools { 221 Expect(err).NotTo(HaveOccurred()) 222 Expect(Pool.Id).Should(Equal("19901040048d70b15014330a6e252ba9")) 223 } 224 }) 225 }) 226 Context("When read of Pools is unsuccessful", func() { 227 BeforeEach(func() { 228 server = ghttp.NewServer() 229 server.SetAllowUnhandledRequests(true) 230 server.AppendHandlers( 231 ghttp.CombineHandlers( 232 ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/pools"), 233 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve Pools`), 234 ), 235 ) 236 }) 237 238 It("should return error when Pool are retrieved", func() { 239 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 240 myPoolPtr, err := newPool(server.URL()).ListPools(target) 241 myPool := myPoolPtr 242 Expect(err).To(HaveOccurred()) 243 Expect(myPool).Should(BeNil()) 244 }) 245 }) 246 }) 247 //Delete 248 Describe("Delete", func() { 249 Context("When delete of Pool is successful", func() { 250 BeforeEach(func() { 251 server = ghttp.NewServer() 252 server.AppendHandlers( 253 ghttp.CombineHandlers( 254 ghttp.VerifyRequest(http.MethodDelete, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/pools/4112ba6c2974ec43886f90736968e838"), 255 ghttp.RespondWith(http.StatusOK, `{ 256 }`), 257 ), 258 ) 259 }) 260 261 It("should delete Pool", func() { 262 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 263 params := "4112ba6c2974ec43886f90736968e838" 264 err := newPool(server.URL()).DeletePool(target, params) 265 Expect(err).NotTo(HaveOccurred()) 266 }) 267 }) 268 Context("When Pool delete has failed", func() { 269 BeforeEach(func() { 270 server = ghttp.NewServer() 271 server.SetAllowUnhandledRequests(true) 272 server.AppendHandlers( 273 ghttp.CombineHandlers( 274 ghttp.VerifyRequest(http.MethodDelete, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/pools/4112ba6c2974ec43886f90736968e838"), 275 ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete service key`), 276 ), 277 ) 278 }) 279 280 It("should return error zone delete", func() { 281 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 282 params := "4112ba6c2974ec43886f90736968e838" 283 err := newPool(server.URL()).DeletePool(target, params) 284 Expect(err).To(HaveOccurred()) 285 }) 286 }) 287 }) 288 //Find 289 Describe("Get", func() { 290 Context("When read of Pool is successful", func() { 291 BeforeEach(func() { 292 server = ghttp.NewServer() 293 server.AppendHandlers( 294 ghttp.CombineHandlers( 295 ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/pools/968826044d875e0841742564d2a4e994"), 296 ghttp.RespondWith(http.StatusOK, ` 297 { 298 "result": { 299 "description": "", 300 "created_on": "2018-11-20T07:20:06.057298Z", 301 "modified_on": "2018-11-22T10:09:10.629388Z", 302 "id": "968826044d875e0841742564d2a4e994", 303 "enabled": true, 304 "minimum_origins": 1, 305 "monitor": "92859a0f6b4d3e55b953e0e29bb96338", 306 "name": "us-pool", 307 "notification_email": "", 308 "check_regions": [ 309 "EEU" 310 ], 311 "healthy": false, 312 "origins": [ 313 { 314 "name": "us-origin1", 315 "address": "200.0.0.1", 316 "enabled": true, 317 "weight": 1, 318 "healthy": false, 319 "failure_reason": "HTTP timeout occurred" 320 }, 321 { 322 "name": "us-origin2", 323 "address": "200.0.0.2", 324 "enabled": true, 325 "weight": 1, 326 "healthy": false, 327 "failure_reason": "HTTP timeout occurred" 328 } 329 ] 330 }, 331 "success": true, 332 "errors": [], 333 "messages": [] 334 } 335 `), 336 ), 337 ) 338 }) 339 340 It("should return Pool", func() { 341 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 342 params := "968826044d875e0841742564d2a4e994" 343 myPoolPtr, err := newPool(server.URL()).GetPool(target, params) 344 myPool := myPoolPtr 345 Expect(err).NotTo(HaveOccurred()) 346 Expect(myPool).ShouldNot(BeNil()) 347 Expect(myPool.Id).Should(Equal("968826044d875e0841742564d2a4e994")) 348 }) 349 }) 350 Context("When Pool get has failed", func() { 351 BeforeEach(func() { 352 server = ghttp.NewServer() 353 server.SetAllowUnhandledRequests(true) 354 server.AppendHandlers( 355 ghttp.CombineHandlers( 356 ghttp.VerifyRequest(http.MethodGet, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/pools/968826044d875e0841742564d2a4e994"), 357 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve Pool`), 358 ), 359 ) 360 }) 361 362 It("should return error when Pool is retrieved", func() { 363 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 364 params := "968826044d875e0841742564d2a4e994" 365 myPoolPtr, err := newPool(server.URL()).GetPool(target, params) 366 myPool := myPoolPtr 367 Expect(err).To(HaveOccurred()) 368 Expect(myPool).Should(BeNil()) 369 }) 370 }) 371 }) 372 //Update 373 Describe("Update", func() { 374 Context("When Update is successful", func() { 375 BeforeEach(func() { 376 server = ghttp.NewServer() 377 server.AppendHandlers( 378 ghttp.CombineHandlers( 379 ghttp.VerifyRequest(http.MethodPost, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/pools"), 380 ghttp.RespondWith(http.StatusCreated, ` 381 { 382 "result": { 383 "description": "", 384 "created_on": "2018-11-22T10:09:44.288581Z", 385 "modified_on": "2018-11-22T10:09:44.288581Z", 386 "id": "4112ba6c2974ec43886f90736968e838", 387 "enabled": true, 388 "minimum_origins": 1, 389 "monitor": "92859a0f6b4d3e55b953e0e29bb96338", 390 "name": "eu-pool", 391 "notification_email": "", 392 "check_regions": [ 393 "EEU" 394 ], 395 "origins": [ 396 { 397 "name": "eu-origin1", 398 "address": "150.0.0.1", 399 "enabled": true, 400 "weight": 1 401 }, 402 { 403 "name": "eu-origin2", 404 "address": "150.0.0.2", 405 "enabled": true, 406 "weight": 1 407 } 408 ] 409 }, 410 "success": true, 411 "errors": [], 412 "messages": [] 413 } 414 `), 415 ), 416 ghttp.CombineHandlers( 417 ghttp.VerifyRequest(http.MethodPut, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/pools/4112ba6c2974ec43886f90736968e838"), 418 ghttp.RespondWith(http.StatusCreated, ` 419 { 420 "result": { 421 "description": "", 422 "created_on": "2018-11-22T10:09:44.288581Z", 423 "modified_on": "2018-11-22T10:09:44.288581Z", 424 "id": "4112ba6c2974ec43886f90736968e838", 425 "enabled": true, 426 "minimum_origins": 1, 427 "monitor": "92859a0f6b4d3e55b953e0e29bb96888", 428 "name": "eu-pool", 429 "notification_email": "", 430 "check_regions": [ 431 "EEU" 432 ], 433 "origins": [ 434 { 435 "name": "eu-origin1", 436 "address": "150.0.0.1", 437 "enabled": true, 438 "weight": 1 439 }, 440 { 441 "name": "eu-origin2", 442 "address": "150.0.0.2", 443 "enabled": true, 444 "weight": 1 445 } 446 ] 447 }, 448 "success": true, 449 "errors": [], 450 "messages": [] 451 } 452 `), 453 ), 454 ) 455 }) 456 457 It("should return zone created", func() { 458 459 origins := []Origin{ 460 {Name: "eu-origin1", Address: "150.0.0.1", Enabled: true, Weight: 1}, 461 {Name: "eu-origin2", Address: "150.0.0.2", Enabled: true, Weight: 1}, 462 } 463 checkRegions := []string{"EEU"} 464 params := PoolBody{ 465 Name: "eu-pool", 466 Description: "", 467 Origins: origins, 468 CheckRegions: checkRegions, 469 Enabled: true, 470 MinOrigins: 1, 471 Monitor: "92859a0f6b4d3e55b953e0e29bb96338", 472 NotEmail: "", 473 } 474 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 475 myPoolPtr, err := newPool(server.URL()).CreatePool(target, params) 476 myPool := *myPoolPtr 477 Expect(err).NotTo(HaveOccurred()) 478 Expect(myPool).ShouldNot(BeNil()) 479 Expect(myPool.Id).Should(Equal("4112ba6c2974ec43886f90736968e838")) 480 Expect(myPool.Name).Should(Equal("eu-pool")) 481 params = PoolBody{ 482 Name: "eu-pool", 483 Description: "", 484 Origins: origins, 485 CheckRegions: checkRegions, 486 Enabled: true, 487 MinOrigins: 1, 488 Monitor: "92859a0f6b4d3e55b953e0e29bb96888", 489 NotEmail: "", 490 } 491 target = "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 492 poolId := "4112ba6c2974ec43886f90736968e838" 493 myPoolPtr, err = newPool(server.URL()).UpdatePool(target, poolId, params) 494 myPool = *myPoolPtr 495 Expect(err).NotTo(HaveOccurred()) 496 Expect(myPool).ShouldNot(BeNil()) 497 Expect(myPool.Id).Should(Equal("4112ba6c2974ec43886f90736968e838")) 498 Expect(myPool.Name).Should(Equal("eu-pool")) 499 Expect(myPool.Monitor).Should(Equal("92859a0f6b4d3e55b953e0e29bb96888")) 500 }) 501 }) 502 Context("When Update is unsuccessful", func() { 503 BeforeEach(func() { 504 server = ghttp.NewServer() 505 server.AppendHandlers( 506 ghttp.CombineHandlers( 507 ghttp.VerifyRequest(http.MethodPut, "/v1/crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a/load_balancers/pools/4112ba6c2974ec43886f90736968e838"), 508 ghttp.RespondWith(http.StatusInternalServerError, `Failed to create Pool`), 509 ), 510 ) 511 }) 512 513 It("should return error during Pool creation", func() { 514 origins := []Origin{ 515 Origin{Name: "eu-origin1", Address: "150.0.0.1", Enabled: true, Weight: 1}, 516 Origin{Name: "eu-origin2", Address: "150.0.0.2", Enabled: true, Weight: 1}, 517 } 518 checkRegions := []string{"EEU"} 519 params := PoolBody{ 520 Name: "eu-pool", 521 Description: "", 522 Origins: origins, 523 CheckRegions: checkRegions, 524 Enabled: true, 525 MinOrigins: 1, 526 Monitor: "92859a0f6b4d3e55b953e0e29bb96338", 527 NotEmail: "", 528 } 529 target := "crn:v1:staging:public:iam::::apikey:ApiKey-62fefdd1-4557-4c7d-8a1c-f6da7ee2ff3a" 530 poolId := "4112ba6c2974ec43886f90736968e838" 531 myPoolPtr, err := newPool(server.URL()).UpdatePool(target, poolId, params) 532 myPool := myPoolPtr 533 Expect(err).To(HaveOccurred()) 534 Expect(myPool).Should(BeNil()) 535 }) 536 }) 537 }) 538 }) 539 540 func newPool(url string) Pools { 541 542 sess, err := session.New() 543 if err != nil { 544 log.Fatal(err) 545 } 546 conf := sess.Config.Copy() 547 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 548 conf.Endpoint = &url 549 550 client := client.Client{ 551 Config: conf, 552 ServiceName: bluemix.CisService, 553 } 554 return newPoolAPI(&client) 555 }