github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv2/domain_test.go (about) 1 package ccv2_test 2 3 import ( 4 "fmt" 5 "net/http" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 8 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 10 . "github.com/onsi/ginkgo" 11 . "github.com/onsi/gomega" 12 . "github.com/onsi/gomega/ghttp" 13 ) 14 15 var _ = Describe("Domain", func() { 16 var client *Client 17 18 BeforeEach(func() { 19 client = NewTestClient() 20 }) 21 22 Describe("CreateSharedDomain", func() { 23 var ( 24 domain string 25 routerGroupGUID string 26 isInternal bool 27 ) 28 29 When("no errors are encountered", func() { 30 BeforeEach(func() { 31 response := `{ 32 "metadata": { 33 "guid": "43436c2d-2b4f-45c2-9f50-e530e1cedba6", 34 "url": "/v2/shared_domains/43436c2d-2b4f-45c2-9f50-e530e1cedba6", 35 "created_at": "2016-06-08T16:41:37Z", 36 "updated_at": "2016-06-08T16:41:26Z" 37 }, 38 "entity": { 39 "name": "example.com", 40 "internal": false, 41 "router_group_guid": "some-guid", 42 "router_group_type": "tcp" 43 } 44 } 45 ` 46 domain = "some-domain-name.com" 47 routerGroupGUID = "some-guid" 48 isInternal = false 49 body := fmt.Sprintf(`{"name":"%s","router_group_guid":"%s","internal":%t}`, domain, routerGroupGUID, isInternal) 50 server.AppendHandlers( 51 CombineHandlers( 52 VerifyRequest(http.MethodPost, "/v2/shared_domains"), 53 VerifyBody([]byte(body)), 54 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"warning-1,warning-2"}}), 55 )) 56 }) 57 58 It("should call the API and return all warnings", func() { 59 warnings, err := client.CreateSharedDomain(domain, routerGroupGUID, isInternal) 60 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 61 Expect(err).ToNot(HaveOccurred()) 62 }) 63 }) 64 65 When("the API returns an unauthorized error", func() { 66 BeforeEach(func() { 67 response := `{ 68 "description": "You are not authorized to perform the requested action", 69 "error_code": "CF-NotAuthorized", 70 "code": 10003 71 }` 72 domain = "some-domain-name.com" 73 isInternal = false 74 body := fmt.Sprintf(`{"name":"%s","internal":%t}`, domain, isInternal) 75 server.AppendHandlers( 76 CombineHandlers( 77 VerifyRequest(http.MethodPost, "/v2/shared_domains"), 78 VerifyBody([]byte(body)), 79 RespondWith(http.StatusForbidden, response, http.Header{"X-Cf-Warnings": {"this is your final warning"}}), 80 )) 81 }) 82 83 It("should return the error and all warnings", func() { 84 warnings, err := client.CreateSharedDomain(domain, "", isInternal) 85 Expect(warnings).To(ConsistOf("this is your final warning")) 86 Expect(err).To(MatchError(ccerror.ForbiddenError{Message: "You are not authorized to perform the requested action"})) 87 }) 88 }) 89 90 When("internal flag is set to true", func() { 91 BeforeEach(func() { 92 response := `{ 93 "metadata": { 94 "guid": "43436c2d-2b4f-45c2-9f50-e530e1cedba6", 95 "url": "/v2/shared_domains/43436c2d-2b4f-45c2-9f50-e530e1cedba6", 96 "created_at": "2016-06-08T16:41:37Z", 97 "updated_at": "2016-06-08T16:41:26Z" 98 }, 99 "entity": { 100 "name": "example.com", 101 "internal": true 102 } 103 } 104 ` 105 domain = "some-domain-name.com" 106 isInternal = true 107 body := fmt.Sprintf(`{"name":"%s","internal":%t}`, domain, isInternal) 108 server.AppendHandlers( 109 CombineHandlers( 110 VerifyRequest(http.MethodPost, "/v2/shared_domains"), 111 VerifyBody([]byte(body)), 112 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 113 )) 114 }) 115 116 It("should call the API and return all warnings", func() { 117 warnings, err := client.CreateSharedDomain(domain, "", isInternal) 118 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 119 Expect(err).ToNot(HaveOccurred()) 120 }) 121 }) 122 123 }) 124 125 Describe("GetSharedDomain", func() { 126 When("the shared domain exists", func() { 127 BeforeEach(func() { 128 response := `{ 129 "metadata": { 130 "guid": "shared-domain-guid", 131 "updated_at": null 132 }, 133 "entity": { 134 "name": "shared-domain-1.com", 135 "router_group_guid": "some-router-group-guid", 136 "router_group_type": "http" 137 } 138 }` 139 server.AppendHandlers( 140 CombineHandlers( 141 VerifyRequest(http.MethodGet, "/v2/shared_domains/shared-domain-guid"), 142 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 143 ), 144 ) 145 }) 146 147 It("returns the shared domain and all warnings", func() { 148 domain, warnings, err := client.GetSharedDomain("shared-domain-guid") 149 Expect(err).NotTo(HaveOccurred()) 150 Expect(domain).To(Equal(Domain{ 151 Name: "shared-domain-1.com", 152 GUID: "shared-domain-guid", 153 RouterGroupGUID: "some-router-group-guid", 154 RouterGroupType: constant.HTTPRouterGroup, 155 Type: constant.SharedDomain, 156 })) 157 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 158 }) 159 }) 160 161 When("the shared domain does not exist", func() { 162 BeforeEach(func() { 163 response := `{ 164 "code": 130002, 165 "description": "The domain could not be found: shared-domain-guid", 166 "error_code": "CF-DomainNotFound" 167 }` 168 server.AppendHandlers( 169 CombineHandlers( 170 VerifyRequest(http.MethodGet, "/v2/shared_domains/shared-domain-guid"), 171 RespondWith(http.StatusNotFound, response), 172 ), 173 ) 174 }) 175 176 It("returns an error", func() { 177 domain, _, err := client.GetSharedDomain("shared-domain-guid") 178 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 179 Message: "The domain could not be found: shared-domain-guid", 180 })) 181 Expect(domain).To(Equal(Domain{})) 182 }) 183 }) 184 }) 185 186 Describe("GetPrivateDomain", func() { 187 When("the private domain exists", func() { 188 BeforeEach(func() { 189 response := `{ 190 "metadata": { 191 "guid": "private-domain-guid", 192 "updated_at": null 193 }, 194 "entity": { 195 "name": "private-domain-1.com" 196 } 197 }` 198 server.AppendHandlers( 199 CombineHandlers( 200 VerifyRequest(http.MethodGet, "/v2/private_domains/private-domain-guid"), 201 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 202 ), 203 ) 204 }) 205 206 It("returns the private domain and all warnings", func() { 207 domain, warnings, err := client.GetPrivateDomain("private-domain-guid") 208 Expect(err).NotTo(HaveOccurred()) 209 Expect(domain).To(Equal(Domain{ 210 Name: "private-domain-1.com", 211 GUID: "private-domain-guid", 212 Type: constant.PrivateDomain, 213 })) 214 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 215 }) 216 }) 217 218 When("the private domain does not exist", func() { 219 BeforeEach(func() { 220 response := `{ 221 "code": 130002, 222 "description": "The domain could not be found: private-domain-guid", 223 "error_code": "CF-DomainNotFound" 224 }` 225 server.AppendHandlers( 226 CombineHandlers( 227 VerifyRequest(http.MethodGet, "/v2/private_domains/private-domain-guid"), 228 RespondWith(http.StatusNotFound, response), 229 ), 230 ) 231 }) 232 233 It("returns an error", func() { 234 domain, _, err := client.GetPrivateDomain("private-domain-guid") 235 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 236 Message: "The domain could not be found: private-domain-guid", 237 })) 238 Expect(domain).To(Equal(Domain{})) 239 }) 240 }) 241 }) 242 243 Describe("GetPrivateDomains", func() { 244 When("the cloud controller does not return an error", func() { 245 BeforeEach(func() { 246 response1 := `{ 247 "next_url": "/v2/private_domains?q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4&page=2", 248 "resources": [ 249 { 250 "metadata": { 251 "guid": "domain-guid-1" 252 }, 253 "entity": { 254 "name": "domain-name-1" 255 } 256 }, 257 { 258 "metadata": { 259 "guid": "domain-guid-2" 260 }, 261 "entity": { 262 "name": "domain-name-2" 263 } 264 } 265 ] 266 }` 267 response2 := `{ 268 "next_url": null, 269 "resources": [ 270 { 271 "metadata": { 272 "guid": "domain-guid-3" 273 }, 274 "entity": { 275 "name": "domain-name-3" 276 } 277 }, 278 { 279 "metadata": { 280 "guid": "domain-guid-4" 281 }, 282 "entity": { 283 "name": "domain-name-4" 284 } 285 } 286 ] 287 }` 288 server.AppendHandlers( 289 CombineHandlers( 290 VerifyRequest(http.MethodGet, "/v2/private_domains", "q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4"), 291 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 292 ), 293 ) 294 server.AppendHandlers( 295 CombineHandlers( 296 VerifyRequest(http.MethodGet, "/v2/private_domains", "q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4&page=2"), 297 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 298 ), 299 ) 300 }) 301 302 It("returns the private domains and warnings", func() { 303 domains, warnings, err := client.GetPrivateDomains(Filter{ 304 Type: constant.NameFilter, 305 Operator: constant.InOperator, 306 Values: []string{"domain-name-1", "domain-name-2", "domain-name-3", "domain-name-4"}, 307 }) 308 Expect(err).NotTo(HaveOccurred()) 309 Expect(domains).To(Equal([]Domain{ 310 { 311 GUID: "domain-guid-1", 312 Name: "domain-name-1", 313 Type: constant.PrivateDomain, 314 }, 315 { 316 GUID: "domain-guid-2", 317 Name: "domain-name-2", 318 Type: constant.PrivateDomain, 319 }, 320 { 321 GUID: "domain-guid-3", 322 Name: "domain-name-3", 323 Type: constant.PrivateDomain, 324 }, 325 { 326 GUID: "domain-guid-4", 327 Name: "domain-name-4", 328 Type: constant.PrivateDomain, 329 }, 330 })) 331 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 332 }) 333 }) 334 335 When("the cloud controller returns an error", func() { 336 BeforeEach(func() { 337 response := `{ 338 "code": 1, 339 "description": "some error description", 340 "error_code": "CF-SomeError" 341 }` 342 server.AppendHandlers( 343 CombineHandlers( 344 VerifyRequest(http.MethodGet, "/v2/private_domains"), 345 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 346 ), 347 ) 348 }) 349 350 It("returns the warnings and error", func() { 351 domains, warnings, err := client.GetPrivateDomains() 352 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 353 V2ErrorResponse: ccerror.V2ErrorResponse{ 354 Code: 1, 355 Description: "some error description", 356 ErrorCode: "CF-SomeError", 357 }, 358 ResponseCode: http.StatusTeapot, 359 })) 360 Expect(domains).To(Equal([]Domain{})) 361 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 362 }) 363 }) 364 }) 365 366 Describe("GetSharedDomains", func() { 367 When("the cloud controller does not return an error", func() { 368 BeforeEach(func() { 369 response1 := `{ 370 "next_url": "/v2/shared_domains?q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4&page=2", 371 "resources": [ 372 { 373 "metadata": { 374 "guid": "domain-guid-1" 375 }, 376 "entity": { 377 "name": "domain-name-1", 378 "router_group_guid": "some-router-group-guid-1", 379 "router_group_type": "http" 380 } 381 }, 382 { 383 "metadata": { 384 "guid": "domain-guid-2" 385 }, 386 "entity": { 387 "name": "domain-name-2", 388 "router_group_guid": "some-router-group-guid-2", 389 "router_group_type": "http" 390 } 391 } 392 ] 393 }` 394 response2 := `{ 395 "next_url": null, 396 "resources": [ 397 { 398 "metadata": { 399 "guid": "domain-guid-3" 400 }, 401 "entity": { 402 "name": "domain-name-3", 403 "router_group_guid": "some-router-group-guid-3", 404 "router_group_type": "http" 405 } 406 }, 407 { 408 "metadata": { 409 "guid": "domain-guid-4" 410 }, 411 "entity": { 412 "name": "domain-name-4", 413 "router_group_guid": "some-router-group-guid-4", 414 "router_group_type": "http" 415 } 416 } 417 ] 418 }` 419 server.AppendHandlers( 420 CombineHandlers( 421 VerifyRequest(http.MethodGet, "/v2/shared_domains", "q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4"), 422 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 423 ), 424 ) 425 server.AppendHandlers( 426 CombineHandlers( 427 VerifyRequest(http.MethodGet, "/v2/shared_domains", "q=name%20IN%20domain-name-1,domain-name-2,domain-name-3,domain-name-4&page=2"), 428 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 429 ), 430 ) 431 }) 432 433 It("returns the shared domain and warnings", func() { 434 domains, warnings, err := client.GetSharedDomains(Filter{ 435 Type: constant.NameFilter, 436 Operator: constant.InOperator, 437 Values: []string{"domain-name-1", "domain-name-2", "domain-name-3", "domain-name-4"}, 438 }) 439 Expect(err).NotTo(HaveOccurred()) 440 Expect(domains).To(Equal([]Domain{ 441 { 442 GUID: "domain-guid-1", 443 Name: "domain-name-1", 444 RouterGroupGUID: "some-router-group-guid-1", 445 RouterGroupType: constant.HTTPRouterGroup, 446 Type: constant.SharedDomain, 447 }, 448 { 449 GUID: "domain-guid-2", 450 Name: "domain-name-2", 451 RouterGroupGUID: "some-router-group-guid-2", 452 RouterGroupType: constant.HTTPRouterGroup, 453 Type: constant.SharedDomain, 454 }, 455 { 456 GUID: "domain-guid-3", 457 Name: "domain-name-3", 458 RouterGroupGUID: "some-router-group-guid-3", 459 RouterGroupType: constant.HTTPRouterGroup, 460 Type: constant.SharedDomain, 461 }, 462 { 463 GUID: "domain-guid-4", 464 Name: "domain-name-4", 465 RouterGroupGUID: "some-router-group-guid-4", 466 RouterGroupType: constant.HTTPRouterGroup, 467 Type: constant.SharedDomain, 468 }, 469 })) 470 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 471 }) 472 }) 473 474 When("the cloud controller returns an error", func() { 475 BeforeEach(func() { 476 response := `{ 477 "code": 1, 478 "description": "some error description", 479 "error_code": "CF-SomeError" 480 }` 481 server.AppendHandlers( 482 CombineHandlers( 483 VerifyRequest(http.MethodGet, "/v2/shared_domains"), 484 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 485 ), 486 ) 487 }) 488 489 It("returns the warnings and error", func() { 490 domains, warnings, err := client.GetSharedDomains() 491 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 492 V2ErrorResponse: ccerror.V2ErrorResponse{ 493 Code: 1, 494 Description: "some error description", 495 ErrorCode: "CF-SomeError", 496 }, 497 ResponseCode: http.StatusTeapot, 498 })) 499 Expect(domains).To(Equal([]Domain{})) 500 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 501 }) 502 }) 503 }) 504 505 Describe("GetOrganizationPrivateDomains", func() { 506 When("the cloud controller does not return an error", func() { 507 BeforeEach(func() { 508 response1 := `{ 509 "next_url": "/v2/organizations/some-org-guid/private_domains?page=2", 510 "resources": [ 511 { 512 "metadata": { 513 "guid": "private-domain-guid-1" 514 }, 515 "entity": { 516 "name": "private-domain-name-1" 517 } 518 }, 519 { 520 "metadata": { 521 "guid": "private-domain-guid-2" 522 }, 523 "entity": { 524 "name": "private-domain-name-2" 525 } 526 } 527 ] 528 }` 529 response2 := `{ 530 "next_url": null, 531 "resources": [ 532 { 533 "metadata": { 534 "guid": "private-domain-guid-3" 535 }, 536 "entity": { 537 "name": "private-domain-name-3" 538 } 539 }, 540 { 541 "metadata": { 542 "guid": "private-domain-guid-4" 543 }, 544 "entity": { 545 "name": "private-domain-name-4" 546 } 547 } 548 ] 549 }` 550 server.AppendHandlers( 551 CombineHandlers( 552 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains"), 553 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 554 ), 555 ) 556 server.AppendHandlers( 557 CombineHandlers( 558 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains", "page=2"), 559 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 560 ), 561 ) 562 }) 563 564 It("returns the domains and warnings", func() { 565 domains, warnings, err := client.GetOrganizationPrivateDomains("some-org-guid") 566 Expect(err).NotTo(HaveOccurred()) 567 Expect(domains).To(Equal([]Domain{ 568 { 569 Name: "private-domain-name-1", 570 GUID: "private-domain-guid-1", 571 Type: constant.PrivateDomain, 572 }, 573 { 574 Name: "private-domain-name-2", 575 GUID: "private-domain-guid-2", 576 Type: constant.PrivateDomain, 577 }, 578 { 579 Name: "private-domain-name-3", 580 GUID: "private-domain-guid-3", 581 Type: constant.PrivateDomain, 582 }, 583 { 584 Name: "private-domain-name-4", 585 GUID: "private-domain-guid-4", 586 Type: constant.PrivateDomain, 587 }, 588 })) 589 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 590 }) 591 }) 592 593 When("the client includes includes query parameters for name", func() { 594 It("it includes the query parameters in the request", func() { 595 server.AppendHandlers( 596 CombineHandlers( 597 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains", "q=name:private-domain-name"), 598 RespondWith(http.StatusOK, ""), 599 ), 600 ) 601 602 client.GetOrganizationPrivateDomains("some-org-guid", Filter{ 603 Type: constant.NameFilter, 604 Operator: constant.EqualOperator, 605 Values: []string{"private-domain-name"}, 606 }) 607 }) 608 }) 609 610 When("the cloud controller returns an error", func() { 611 BeforeEach(func() { 612 response := `{ 613 "description": "The organization could not be found: glah", 614 "error_code": "CF-OrganizationNotFound", 615 "code": 30003 616 }` 617 server.AppendHandlers( 618 CombineHandlers( 619 VerifyRequest(http.MethodGet, "/v2/organizations/some-org-guid/private_domains"), 620 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 621 ), 622 ) 623 }) 624 625 It("returns the warnings and error", func() { 626 domains, warnings, err := client.GetOrganizationPrivateDomains("some-org-guid") 627 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 628 Message: "The organization could not be found: glah", 629 })) 630 Expect(domains).To(Equal([]Domain{})) 631 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 632 }) 633 }) 634 }) 635 })