github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/organizations_test.go (about) 1 package mccpv2 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 "github.com/IBM-Cloud/bluemix-go/helpers" 10 bluemixHttp "github.com/IBM-Cloud/bluemix-go/http" 11 "github.com/IBM-Cloud/bluemix-go/session" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 "github.com/onsi/gomega/ghttp" 16 ) 17 18 var _ = Describe("Organizations", func() { 19 var server *ghttp.Server 20 AfterEach(func() { 21 server.Close() 22 }) 23 24 Describe("Create", func() { 25 Context("When creation is successful", func() { 26 BeforeEach(func() { 27 server = ghttp.NewServer() 28 server.AppendHandlers( 29 ghttp.CombineHandlers( 30 ghttp.VerifyRequest(http.MethodPost, "/v2/organizations"), 31 ghttp.VerifyBody([]byte(`{"name":"test-org"}`)), 32 ghttp.RespondWith(http.StatusCreated, `{ 33 "metadata": { 34 "guid": "a87f1cde-2070-4fb1-b23e-09109d9eaa93", 35 "url": "/v2/organizations/a87f1cde-2070-4fb1-b23e-09109d9eaa93", 36 "created_at": "2016-04-16T01:23:42Z", 37 "updated_at": null 38 }, 39 "entity": { 40 "name": "test-org", 41 "billing_enabled": false, 42 "quota_definition_guid": "813ca4a3-e83f-463e-a034-3a7ed7ba6280", 43 "status": "active", 44 "quota_definition_url": "/v2/quota_definitions/813ca4a3-e83f-463e-a034-3a7ed7ba6280", 45 "spaces_url": "/v2/organizations/a87f1cde-2070-4fb1-b23e-09109d9eaa93/spaces", 46 "domains_url": "/v2/organizations/a87f1cde-2070-4fb1-b23e-09109d9eaa93/domains", 47 "private_domains_url": "/v2/organizations/a87f1cde-2070-4fb1-b23e-09109d9eaa93/private_domains", 48 "users_url": "/v2/organizations/a87f1cde-2070-4fb1-b23e-09109d9eaa93/users", 49 "managers_url": "/v2/organizations/a87f1cde-2070-4fb1-b23e-09109d9eaa93/managers", 50 "billing_managers_url": "/v2/organizations/a87f1cde-2070-4fb1-b23e-09109d9eaa93/billing_managers", 51 "auditors_url": "/v2/organizations/a87f1cde-2070-4fb1-b23e-09109d9eaa93/auditors", 52 "app_events_url": "/v2/organizations/a87f1cde-2070-4fb1-b23e-09109d9eaa93/app_events", 53 "space_quota_definitions_url": "/v2/organizations/a87f1cde-2070-4fb1-b23e-09109d9eaa93/space_quota_definitions" 54 } 55 }`), 56 ), 57 ) 58 }) 59 60 It("Should create Organization", func() { 61 payload := OrgCreateRequest{ 62 Name: "test-org", 63 } 64 _, err := newOrganizations(server.URL()).Create(payload) 65 Expect(err).NotTo(HaveOccurred()) 66 }) 67 }) 68 69 Context("When creation is failed", func() { 70 BeforeEach(func() { 71 server = ghttp.NewServer() 72 server.SetAllowUnhandledRequests(true) 73 server.AppendHandlers( 74 ghttp.CombineHandlers( 75 ghttp.VerifyRequest(http.MethodPost, "/v2/organizations"), 76 ghttp.VerifyBody([]byte(`{"name":"test-org"}`)), 77 ghttp.RespondWith(http.StatusInternalServerError, `Failed to create`), 78 ), 79 ) 80 }) 81 82 It("Should return error when created", func() { 83 payload := OrgCreateRequest{ 84 Name: "test-org", 85 } 86 _, err := newOrganizations(server.URL()).Create(payload) 87 Expect(err).To(HaveOccurred()) 88 }) 89 }) 90 }) 91 92 Describe("FindByName", func() { 93 Context("When there is match", func() { 94 BeforeEach(func() { 95 server = ghttp.NewServer() 96 server.AppendHandlers( 97 ghttp.CombineHandlers( 98 ghttp.VerifyRequest(http.MethodGet, "/v2/organizations", "q=name:foo®ion=region"), 99 ghttp.RespondWith(http.StatusOK, `{ 100 "total_results": 1, 101 "total_pages": 1, 102 "prev_url": null, 103 "next_url": null, 104 "resources": [ 105 { 106 "metadata": { 107 "guid": "a695a906-e225-428a-9238-b1d01b96017f", 108 "url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f", 109 "created_at": "2016-04-16T01:23:41Z", 110 "updated_at": null 111 }, 112 "entity": { 113 "name": "foo", 114 "billing_enabled": false, 115 "quota_definition_guid": "255876b7-4f1c-48d9-ad7c-2d6cd590781e", 116 "status": "active", 117 "quota_definition_url": "/v2/quota_definitions/255876b7-4f1c-48d9-ad7c-2d6cd590781e", 118 "spaces_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/spaces", 119 "domains_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/domains", 120 "private_domains_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/private_domains", 121 "users_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/users", 122 "managers_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/managers", 123 "billing_managers_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/billing_managers", 124 "auditors_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/auditors", 125 "app_events_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/app_events", 126 "space_quota_definitions_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/space_quota_definitions" 127 } 128 } 129 ] 130 }`), 131 ), 132 ) 133 }) 134 135 It("Should return the match", func() { 136 org, err := newOrganizations(server.URL()).FindByName("foo", "region") 137 Expect(err).NotTo(HaveOccurred()) 138 Expect(org).NotTo(BeNil()) 139 Expect(org.GUID).To(Equal("a695a906-e225-428a-9238-b1d01b96017f")) 140 Expect(org.Name).To(Equal("foo")) 141 Expect(org.BillingEnabled).To(BeFalse()) 142 }) 143 }) 144 145 Context("When Organizations FindByName is failed", func() { 146 BeforeEach(func() { 147 server = ghttp.NewServer() 148 server.SetAllowUnhandledRequests(true) 149 server.AppendHandlers( 150 ghttp.CombineHandlers( 151 ghttp.VerifyRequest(http.MethodGet, "/v2/organizations", "region=region&q=name:foo"), 152 ghttp.RespondWith(http.StatusInternalServerError, `Failed to find by name`), 153 ), 154 ) 155 }) 156 157 It("Should return error when Organization is found by name", func() { 158 org, err := newOrganizations(server.URL()).FindByName("foo", "region") 159 Expect(err).To(HaveOccurred()) 160 Expect(org).Should(BeNil()) 161 }) 162 }) 163 }) 164 165 Describe("Get", func() { 166 Context("When fetched by GUID", func() { 167 BeforeEach(func() { 168 server = ghttp.NewServer() 169 server.AppendHandlers( 170 ghttp.CombineHandlers( 171 ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c"), 172 ghttp.RespondWith(http.StatusOK, `{ 173 "metadata": { 174 "guid": "007c547f-9d6e-4d75-bb03-d9584e7bc62c", 175 "url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c", 176 "created_at": "2016-04-16T01:23:42Z", 177 "updated_at": "2016-04-16T01:23:42Z" 178 }, 179 "entity": { 180 "name": "org-name", 181 "billing_enabled": false, 182 "quota_definition_guid": "838224b1-c841-4b96-90f2-f181bb5fffa5", 183 "status": "active", 184 "quota_definition_url": "/v2/quota_definitions/838224b1-c841-4b96-90f2-f181bb5fffa5", 185 "spaces_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/spaces", 186 "domains_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/domains", 187 "private_domains_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/private_domains", 188 "users_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/users", 189 "managers_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/managers", 190 "billing_managers_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/billing_managers", 191 "auditors_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/auditors", 192 "app_events_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/app_events", 193 "space_quota_definitions_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/space_quota_definitions" 194 } 195 }`), 196 ), 197 ) 198 }) 199 200 It("Should return the match", func() { 201 org, err := newOrganizations(server.URL()).Get("007c547f-9d6e-4d75-bb03-d9584e7bc62c") 202 Expect(err).NotTo(HaveOccurred()) 203 Expect(org).NotTo(BeNil()) 204 Expect(org.Metadata.GUID).To(Equal("007c547f-9d6e-4d75-bb03-d9584e7bc62c")) 205 Expect(org.Entity.Name).To(Equal("org-name")) 206 Expect(org.Entity.BillingEnabled).To(BeFalse()) 207 }) 208 }) 209 }) 210 211 Describe("List", func() { 212 Context("When there is one Organization", func() { 213 BeforeEach(func() { 214 server = ghttp.NewServer() 215 server.AppendHandlers( 216 ghttp.CombineHandlers( 217 ghttp.VerifyRequest(http.MethodGet, "/v2/organizations", "region=region"), 218 ghttp.RespondWith(http.StatusOK, `{ 219 "total_results": 1, 220 "total_pages": 1, 221 "prev_url": null, 222 "next_url": null, 223 "resources": [ 224 { 225 "metadata": { 226 "guid": "a695a906-e225-428a-9238-b1d01b96017f", 227 "url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f", 228 "created_at": "2016-04-16T01:23:41Z", 229 "updated_at": null 230 }, 231 "entity": { 232 "name": "test-org-name", 233 "billing_enabled": false, 234 "quota_definition_guid": "255876b7-4f1c-48d9-ad7c-2d6cd590781e", 235 "status": "active", 236 "quota_definition_url": "/v2/quota_definitions/255876b7-4f1c-48d9-ad7c-2d6cd590781e", 237 "spaces_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/spaces", 238 "domains_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/domains", 239 "private_domains_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/private_domains", 240 "users_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/users", 241 "managers_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/managers", 242 "billing_managers_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/billing_managers", 243 "auditors_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/auditors", 244 "app_events_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/app_events", 245 "space_quota_definitions_url": "/v2/organizations/a695a906-e225-428a-9238-b1d01b96017f/space_quota_definitions" 246 } 247 } 248 ] 249 }`), 250 ), 251 ) 252 }) 253 254 It("Should return all Organizations", func() { 255 orgs, err := newOrganizations(server.URL()).List("region") 256 Expect(err).ShouldNot(HaveOccurred()) 257 Expect(orgs).Should(HaveLen(1)) 258 org := orgs[0] 259 Expect(org.GUID).To(Equal("a695a906-e225-428a-9238-b1d01b96017f")) 260 Expect(org.Name).To(Equal("test-org-name")) 261 Expect(org.BillingEnabled).To(BeFalse()) 262 }) 263 }) 264 265 Context("When Organizations List is failed", func() { 266 BeforeEach(func() { 267 server = ghttp.NewServer() 268 server.SetAllowUnhandledRequests(true) 269 server.AppendHandlers( 270 ghttp.CombineHandlers( 271 ghttp.VerifyRequest(http.MethodGet, "/v2/organizations"), 272 ghttp.RespondWith(http.StatusInternalServerError, `Failed to get Organizations`), 273 ), 274 ) 275 }) 276 277 It("Should return error when Organization List is failed", func() { 278 orgs, err := newOrganizations(server.URL()).List("region") 279 Expect(err).To(HaveOccurred()) 280 Expect(orgs).Should(BeNil()) 281 }) 282 }) 283 }) 284 285 Describe("Delete", func() { 286 Context("When Organization deletion is successful", func() { 287 BeforeEach(func() { 288 server = ghttp.NewServer() 289 server.AppendHandlers( 290 ghttp.CombineHandlers( 291 ghttp.VerifyRequest(http.MethodDelete, "/v2/organizations/827ec655-c2ed-4577-b226-82271fe471d7"), 292 ghttp.RespondWith(http.StatusNoContent, ` 293 {}`), 294 ), 295 ) 296 }) 297 298 It("Should delete Organization", func() { 299 err := newOrganizations(server.URL()).Delete("827ec655-c2ed-4577-b226-82271fe471d7", true) 300 Expect(err).ShouldNot(HaveOccurred()) 301 }) 302 }) 303 304 Context("When Organization Delete is failed", func() { 305 BeforeEach(func() { 306 server = ghttp.NewServer() 307 server.SetAllowUnhandledRequests(true) 308 server.AppendHandlers( 309 ghttp.CombineHandlers( 310 ghttp.VerifyRequest(http.MethodDelete, "/v2/organizations/827ec655-c2ed-4577-b226-82271fe471d7"), 311 ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete`), 312 ), 313 ) 314 }) 315 316 It("Should return error when Organization is deleted", func() { 317 err := newOrganizations(server.URL()).Delete("827ec655-c2ed-4577-b226-82271fe471d7", true) 318 Expect(err).To(HaveOccurred()) 319 }) 320 }) 321 }) 322 323 Describe("DeleteByRegion", func() { 324 Context("When Organization deletion by region is successful", func() { 325 BeforeEach(func() { 326 server = ghttp.NewServer() 327 server.AppendHandlers( 328 ghttp.CombineHandlers( 329 ghttp.VerifyRequest(http.MethodDelete, "/v2/organizations/827ec655-c2ed-4577-b226-82271fe471d7"), 330 ghttp.RespondWith(http.StatusNoContent, ` 331 {}`), 332 ), 333 ) 334 }) 335 336 It("Should delete Organization", func() { 337 err := newOrganizations(server.URL()).DeleteByRegion("827ec655-c2ed-4577-b226-82271fe471d7", "us-south", true) 338 Expect(err).ShouldNot(HaveOccurred()) 339 }) 340 }) 341 342 Context("When Organization Delete is failed", func() { 343 BeforeEach(func() { 344 server = ghttp.NewServer() 345 server.SetAllowUnhandledRequests(true) 346 server.AppendHandlers( 347 ghttp.CombineHandlers( 348 ghttp.VerifyRequest(http.MethodDelete, "/v2/organizations/827ec655-c2ed-4577-b226-82271fe471d7"), 349 ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete`), 350 ), 351 ) 352 }) 353 354 It("Should return error when Organization is deleted", func() { 355 err := newOrganizations(server.URL()).DeleteByRegion("827ec655-c2ed-4577-b226-82271fe471d7", "us-south", true) 356 Expect(err).To(HaveOccurred()) 357 }) 358 }) 359 }) 360 361 Describe("Update", func() { 362 Context("When update is succeesful", func() { 363 BeforeEach(func() { 364 server = ghttp.NewServer() 365 server.AppendHandlers( 366 ghttp.CombineHandlers( 367 ghttp.VerifyRequest(http.MethodPut, "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c"), 368 ghttp.VerifyBody([]byte(`{"name":"new-org-name"}`)), 369 ghttp.RespondWith(http.StatusCreated, `{ 370 "metadata": { 371 "guid": "007c547f-9d6e-4d75-bb03-d9584e7bc62c", 372 "url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c", 373 "created_at": "2016-04-16T01:23:42Z", 374 "updated_at": "2016-04-16T01:23:42Z" 375 }, 376 "entity": { 377 "name": "new-org-name", 378 "billing_enabled": false, 379 "quota_definition_guid": "838224b1-c841-4b96-90f2-f181bb5fffa5", 380 "status": "active", 381 "quota_definition_url": "/v2/quota_definitions/838224b1-c841-4b96-90f2-f181bb5fffa5", 382 "spaces_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/spaces", 383 "domains_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/domains", 384 "private_domains_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/private_domains", 385 "users_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/users", 386 "managers_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/managers", 387 "billing_managers_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/billing_managers", 388 "auditors_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/auditors", 389 "app_events_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/app_events", 390 "space_quota_definitions_url": "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c/space_quota_definitions" 391 } 392 }`), 393 ), 394 ) 395 }) 396 397 It("Should update Organization", func() { 398 payload := OrgUpdateRequest{ 399 Name: helpers.String("new-org-name"), 400 } 401 _, err := newOrganizations(server.URL()).Update("007c547f-9d6e-4d75-bb03-d9584e7bc62c", payload) 402 Expect(err).ShouldNot(HaveOccurred()) 403 }) 404 }) 405 406 Context("When Organization Update is failed", func() { 407 BeforeEach(func() { 408 server = ghttp.NewServer() 409 server.SetAllowUnhandledRequests(true) 410 server.AppendHandlers( 411 ghttp.CombineHandlers( 412 ghttp.VerifyRequest(http.MethodPut, "/v2/organizations/007c547f-9d6e-4d75-bb03-d9584e7bc62c"), 413 ghttp.VerifyBody([]byte(`{"name":"new-org-name"}`)), 414 ghttp.RespondWith(http.StatusInternalServerError, `Failed to update`), 415 ), 416 ) 417 }) 418 419 It("Should return error when updated", func() { 420 payload := OrgUpdateRequest{ 421 Name: helpers.String("new-org-name"), 422 } 423 _, err := newOrganizations(server.URL()).Update("007c547f-9d6e-4d75-bb03-d9584e7bc62c", payload) 424 Expect(err).To(HaveOccurred()) 425 }) 426 }) 427 }) 428 429 Describe("Get Region Info", func() { 430 Context("When fetched by GUID", func() { 431 BeforeEach(func() { 432 server = ghttp.NewServer() 433 server.AppendHandlers( 434 ghttp.CombineHandlers( 435 ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/d67452c1-1dc4-4131-8558-220a83fe3e00/regions"), 436 ghttp.RespondWith(http.StatusOK, `[ 437 { 438 "id": "ibm:yp:us-south", 439 "domain": "ng.bluemix.net", 440 "name": "us-south", 441 "region": "us-south", 442 "display_name": "Dallas", 443 "customer": { 444 "name": "ibm", 445 "display_name": "IBM" 446 }, 447 "deployment": { 448 "name": "yp", 449 "display_name": "Production" 450 }, 451 "geo": { 452 "name": "us-south", 453 "display_name": "Dallas" 454 }, 455 "public_regions_by_proximity": [ 456 "ibm:yp:us-south", 457 "ibm:yp:us-east", 458 "ibm:yp:eu-gb", 459 "ibm:yp:eu-de", 460 "ibm:yp:au-syd" 461 ], 462 "console_url": "https://console.bluemix.net", 463 "cf_api": "https://api.ng.bluemix.net", 464 "mccp_api": "https://mccp.us-south.cf.cloud.ibm.com", 465 "type": "public", 466 "home": false, 467 "aliases": [], 468 "settings": { 469 "devops": { 470 "enabled": false 471 } 472 }, 473 "org_name": "test-org-name", 474 "org_guid": "8dbcd4bb-f161-45f7-b134-0a880199762d" 475 }, 476 { 477 "id": "ibm:yp:us-east", 478 "domain": "us-east.bluemix.net", 479 "name": "us-east", 480 "region": "us-east", 481 "display_name": "Washington DC", 482 "customer": { 483 "name": "ibm", 484 "display_name": "IBM" 485 }, 486 "deployment": { 487 "name": "yp", 488 "display_name": "Production" 489 }, 490 "geo": { 491 "name": "us-east", 492 "display_name": "Washington DC" 493 }, 494 "public_regions_by_proximity": [ 495 "ibm:yp:us-east", 496 "ibm:yp:us-south", 497 "ibm:yp:eu-gb", 498 "ibm:yp:eu-de", 499 "ibm:yp:au-syd" 500 ], 501 "console_url": "https://console.bluemix.net", 502 "cf_api": "https://api.us-east.bluemix.net", 503 "mccp_api": "https://mccp.us-east.cf.cloud.ibm.com", 504 "type": "public", 505 "home": true, 506 "aliases": [], 507 "settings": { 508 "devops": { 509 "enabled": false 510 } 511 }, 512 "org_name": "test-org-name", 513 "org_guid": "d67452c1-1dc4-4131-8558-220a83fe3e00" 514 } 515 ]`), 516 ), 517 ) 518 }) 519 520 It("Should return Organization with same name but different regions", func() { 521 regionalOrgs, err := newOrganizations(server.URL()).GetRegionInformation("d67452c1-1dc4-4131-8558-220a83fe3e00") 522 Expect(err).NotTo(HaveOccurred()) 523 Expect(regionalOrgs).NotTo(BeEmpty()) 524 Expect(regionalOrgs).To(HaveLen(2)) 525 Expect(regionalOrgs[0].OrgName).To(Equal(regionalOrgs[1].OrgName)) 526 Expect(regionalOrgs[0].Region).To(Equal("us-south")) 527 Expect(regionalOrgs[0].Home).To(BeFalse()) 528 Expect(regionalOrgs[1].Region).To(Equal("us-east")) 529 Expect(regionalOrgs[1].Home).To(BeTrue()) 530 }) 531 }) 532 }) 533 534 }) 535 536 func newOrganizations(url string) Organizations { 537 538 sess, err := session.New() 539 if err != nil { 540 log.Fatal(err) 541 } 542 conf := sess.Config.Copy() 543 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 544 conf.Endpoint = &url 545 546 mccpClient := client.Client{ 547 Config: conf, 548 ServiceName: bluemix.MccpService, 549 } 550 551 return newOrganizationAPI(&mccpClient) 552 }