github.com/sleungcy-sap/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/security_group_test.go (about) 1 package ccv2_test 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 7 . "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 . "github.com/onsi/gomega/ghttp" 12 ) 13 14 var _ = Describe("Security Groups", func() { 15 var client *Client 16 17 BeforeEach(func() { 18 client = NewTestClient() 19 }) 20 21 Describe("DeleteSecurityGroupSpace", func() { 22 var ( 23 warnings Warnings 24 err error 25 ) 26 27 JustBeforeEach(func() { 28 warnings, err = client.DeleteSecurityGroupSpace("security-group-guid", "space-guid") 29 }) 30 31 When("the client call is successful", func() { 32 BeforeEach(func() { 33 server.AppendHandlers( 34 CombineHandlers( 35 VerifyRequest(http.MethodDelete, "/v2/security_groups/security-group-guid/spaces/space-guid"), 36 RespondWith(http.StatusOK, nil, http.Header{"X-Cf-Warnings": {"warning-1"}}), 37 )) 38 }) 39 40 It("returns all warnings", func() { 41 Expect(err).ToNot(HaveOccurred()) 42 Expect(warnings).To(ConsistOf(Warnings{"warning-1"})) 43 }) 44 }) 45 46 When("the client call is unsuccessful", func() { 47 BeforeEach(func() { 48 response := `{ 49 "code": 10001, 50 "description": "Some Error", 51 "error_code": "CF-SomeError" 52 }` 53 server.AppendHandlers( 54 CombineHandlers( 55 VerifyRequest(http.MethodDelete, "/v2/security_groups/security-group-guid/spaces/space-guid"), 56 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1"}}), 57 )) 58 }) 59 60 It("returns the error and all warnings", func() { 61 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 62 ResponseCode: http.StatusTeapot, 63 V2ErrorResponse: ccerror.V2ErrorResponse{ 64 Code: 10001, 65 Description: "Some Error", 66 ErrorCode: "CF-SomeError", 67 }, 68 })) 69 Expect(warnings).To(ConsistOf("warning-1")) 70 }) 71 }) 72 }) 73 74 Describe("DeleteSecurityGroupStagingSpace", func() { 75 var ( 76 warnings Warnings 77 err error 78 ) 79 80 JustBeforeEach(func() { 81 warnings, err = client.DeleteSecurityGroupStagingSpace("security-group-guid", "space-guid") 82 }) 83 84 When("the client call is successful", func() { 85 BeforeEach(func() { 86 server.AppendHandlers( 87 CombineHandlers( 88 VerifyRequest(http.MethodDelete, "/v2/security_groups/security-group-guid/staging_spaces/space-guid"), 89 RespondWith(http.StatusOK, nil, http.Header{"X-Cf-Warnings": {"warning-1"}}), 90 )) 91 }) 92 93 It("returns all warnings", func() { 94 Expect(err).ToNot(HaveOccurred()) 95 Expect(warnings).To(ConsistOf(Warnings{"warning-1"})) 96 }) 97 }) 98 99 When("the client call is unsuccessful", func() { 100 BeforeEach(func() { 101 response := `{ 102 "code": 10001, 103 "description": "Some Error", 104 "error_code": "CF-SomeError" 105 }` 106 server.AppendHandlers( 107 CombineHandlers( 108 VerifyRequest(http.MethodDelete, "/v2/security_groups/security-group-guid/staging_spaces/space-guid"), 109 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1"}}), 110 )) 111 }) 112 113 It("returns the error and all warnings", func() { 114 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 115 ResponseCode: http.StatusTeapot, 116 V2ErrorResponse: ccerror.V2ErrorResponse{ 117 Code: 10001, 118 Description: "Some Error", 119 ErrorCode: "CF-SomeError", 120 }, 121 })) 122 Expect(warnings).To(ConsistOf("warning-1")) 123 }) 124 }) 125 }) 126 127 Describe("GetSecurityGroups", func() { 128 When("no errors are encountered", func() { 129 When("results are paginated", func() { 130 BeforeEach(func() { 131 response1 := `{ 132 "next_url": "/v2/security_groups?q=some-query:some-value&page=2", 133 "resources": [ 134 { 135 "metadata": { 136 "guid": "security-group-guid-1", 137 "url": "/v2/security_groups/security-group-guid-1" 138 }, 139 "entity": { 140 "name": "security-group-1", 141 "rules": [ 142 ], 143 "running_default": false, 144 "staging_default": true, 145 "spaces_url": "/v2/security_groups/security-group-guid-1/spaces" 146 } 147 } 148 ] 149 }` 150 response2 := `{ 151 "next_url": null, 152 "resources": [ 153 { 154 "metadata": { 155 "guid": "security-group-guid-2", 156 "url": "/v2/security_groups/security-group-guid-2" 157 }, 158 "entity": { 159 "name": "security-group-2", 160 "rules": [ 161 ], 162 "running_default": true, 163 "staging_default": false, 164 "spaces_url": "/v2/security_groups/security-group-guid-2/spaces" 165 } 166 } 167 ] 168 }` 169 server.AppendHandlers( 170 CombineHandlers( 171 VerifyRequest(http.MethodGet, "/v2/security_groups", "q=some-query:some-value"), 172 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"warning-1"}}), 173 )) 174 server.AppendHandlers( 175 CombineHandlers( 176 VerifyRequest(http.MethodGet, "/v2/security_groups", "q=some-query:some-value&page=2"), 177 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"warning-2"}}), 178 )) 179 }) 180 181 It("returns paginated results and all warnings", func() { 182 securityGroups, warnings, err := client.GetSecurityGroups(Filter{ 183 Type: "some-query", 184 Operator: constant.EqualOperator, 185 Values: []string{"some-value"}, 186 }) 187 188 Expect(err).NotTo(HaveOccurred()) 189 Expect(securityGroups).To(Equal([]SecurityGroup{ 190 { 191 GUID: "security-group-guid-1", 192 Name: "security-group-1", 193 Rules: []SecurityGroupRule{}, 194 RunningDefault: false, 195 StagingDefault: true, 196 }, 197 { 198 GUID: "security-group-guid-2", 199 Name: "security-group-2", 200 Rules: []SecurityGroupRule{}, 201 RunningDefault: true, 202 StagingDefault: false, 203 }, 204 })) 205 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 206 }) 207 }) 208 }) 209 210 When("an error is encountered", func() { 211 BeforeEach(func() { 212 response := `{ 213 "code": 10001, 214 "description": "Some Error", 215 "error_code": "CF-SomeError" 216 }` 217 server.AppendHandlers( 218 CombineHandlers( 219 VerifyRequest(http.MethodGet, "/v2/security_groups"), 220 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 221 )) 222 }) 223 224 It("returns an error and all warnings", func() { 225 _, warnings, err := client.GetSecurityGroups() 226 227 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 228 ResponseCode: http.StatusTeapot, 229 V2ErrorResponse: ccerror.V2ErrorResponse{ 230 Code: 10001, 231 Description: "Some Error", 232 ErrorCode: "CF-SomeError", 233 }, 234 })) 235 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 236 }) 237 }) 238 }) 239 240 Describe("GetSpaceSecurityGroups", func() { 241 When("the space exists", func() { 242 BeforeEach(func() { 243 response1 := `{ 244 "next_url": "/v2/spaces/some-space-guid/security_groups?q=some-query:some-value&page=2", 245 "resources": [ 246 { 247 "metadata": { 248 "guid": "running-security-group-guid-1", 249 "updated_at": null 250 }, 251 "entity": { 252 "name": "running-security-group-name-1", 253 "rules": [ 254 { 255 "protocol": "udp", 256 "ports": "8080", 257 "description": "description-1", 258 "destination": "198.41.191.47/1" 259 }, 260 { 261 "protocol": "tcp", 262 "ports": "80,443", 263 "description": "description-2", 264 "destination": "254.41.191.47-254.44.255.255" 265 } 266 ] 267 } 268 }, 269 { 270 "metadata": { 271 "guid": "running-security-group-guid-2", 272 "updated_at": null 273 }, 274 "entity": { 275 "name": "running-security-group-name-2", 276 "rules": [ 277 { 278 "protocol": "udp", 279 "ports": "8080", 280 "description": "description-3", 281 "destination": "198.41.191.47/24" 282 }, 283 { 284 "protocol": "tcp", 285 "ports": "80,443", 286 "description": "description-4", 287 "destination": "254.41.191.4-254.44.255.4" 288 } 289 ] 290 } 291 } 292 ] 293 }` 294 response2 := `{ 295 "next_url": null, 296 "resources": [ 297 { 298 "metadata": { 299 "guid": "running-security-group-guid-3", 300 "updated_at": null 301 }, 302 "entity": { 303 "name": "running-security-group-name-3", 304 "rules": [ 305 { 306 "protocol": "udp", 307 "ports": "32767", 308 "description": "description-5", 309 "destination": "127.0.0.1/32" 310 }, 311 { 312 "protocol": "tcp", 313 "ports": "8008,4443", 314 "description": "description-6", 315 "destination": "254.41.191.0-254.44.255.1" 316 } 317 ] 318 } 319 } 320 ] 321 }` 322 server.AppendHandlers( 323 CombineHandlers( 324 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/security_groups", "q=some-query:some-value"), 325 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 326 ), 327 ) 328 server.AppendHandlers( 329 CombineHandlers( 330 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/security_groups", "q=some-query:some-value&page=2"), 331 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 332 ), 333 ) 334 }) 335 336 It("returns the running security groups and all warnings", func() { 337 securityGroups, warnings, err := client.GetSpaceSecurityGroups("some-space-guid", Filter{ 338 Type: "some-query", 339 Operator: constant.EqualOperator, 340 Values: []string{"some-value"}, 341 }) 342 Expect(err).NotTo(HaveOccurred()) 343 Expect(warnings).To(ConsistOf("this is a warning", "this is another warning")) 344 Expect(securityGroups).To(ConsistOf( 345 SecurityGroup{ 346 Name: "running-security-group-name-1", 347 GUID: "running-security-group-guid-1", 348 Rules: []SecurityGroupRule{ 349 { 350 Protocol: "udp", 351 Ports: "8080", 352 Description: "description-1", 353 Destination: "198.41.191.47/1", 354 }, 355 { 356 Protocol: "tcp", 357 Ports: "80,443", 358 Description: "description-2", 359 Destination: "254.41.191.47-254.44.255.255", 360 }, 361 }, 362 }, 363 SecurityGroup{ 364 Name: "running-security-group-name-2", 365 GUID: "running-security-group-guid-2", 366 Rules: []SecurityGroupRule{ 367 { 368 Protocol: "udp", 369 Ports: "8080", 370 Description: "description-3", 371 Destination: "198.41.191.47/24", 372 }, 373 { 374 Protocol: "tcp", 375 Ports: "80,443", 376 Description: "description-4", 377 Destination: "254.41.191.4-254.44.255.4", 378 }, 379 }, 380 }, 381 SecurityGroup{ 382 Name: "running-security-group-name-3", 383 GUID: "running-security-group-guid-3", 384 Rules: []SecurityGroupRule{ 385 { 386 Protocol: "udp", 387 Ports: "32767", 388 Description: "description-5", 389 Destination: "127.0.0.1/32", 390 }, 391 { 392 Protocol: "tcp", 393 Ports: "8008,4443", 394 Description: "description-6", 395 Destination: "254.41.191.0-254.44.255.1", 396 }, 397 }, 398 }, 399 )) 400 }) 401 }) 402 403 When("the client returns an error", func() { 404 BeforeEach(func() { 405 response := `{ 406 "code": 40004, 407 "description": "The space could not be found: some-space-guid", 408 "error_code": "CF-SpaceNotFound" 409 }` 410 server.AppendHandlers( 411 CombineHandlers( 412 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/security_groups"), 413 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 414 ), 415 ) 416 }) 417 418 It("returns the error and warnings", func() { 419 securityGroups, warnings, err := client.GetSpaceSecurityGroups("some-space-guid") 420 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 421 Message: "The space could not be found: some-space-guid", 422 })) 423 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 424 Expect(securityGroups).To(BeEmpty()) 425 }) 426 }) 427 }) 428 429 Describe("GetSpaceStagingSecurityGroups", func() { 430 When("the space exists", func() { 431 BeforeEach(func() { 432 response1 := `{ 433 "next_url": "/v2/spaces/some-space-guid/staging_security_groups?q=some-query:some-value&page=2", 434 "resources": [ 435 { 436 "metadata": { 437 "guid": "staging-security-group-guid-1", 438 "updated_at": null 439 }, 440 "entity": { 441 "name": "staging-security-group-name-1", 442 "rules": [ 443 { 444 "protocol": "udp", 445 "ports": "8080", 446 "description": "description-1", 447 "destination": "198.41.191.47/1" 448 }, 449 { 450 "protocol": "tcp", 451 "ports": "80,443", 452 "description": "description-2", 453 "destination": "254.41.191.47-254.44.255.255" 454 } 455 ] 456 } 457 }, 458 { 459 "metadata": { 460 "guid": "staging-security-group-guid-2", 461 "updated_at": null 462 }, 463 "entity": { 464 "name": "staging-security-group-name-2", 465 "rules": [ 466 { 467 "protocol": "udp", 468 "ports": "8080", 469 "description": "description-3", 470 "destination": "198.41.191.47/24" 471 }, 472 { 473 "protocol": "tcp", 474 "ports": "80,443", 475 "description": "description-4", 476 "destination": "254.41.191.4-254.44.255.4" 477 } 478 ] 479 } 480 } 481 ] 482 }` 483 response2 := `{ 484 "next_url": null, 485 "resources": [ 486 { 487 "metadata": { 488 "guid": "staging-security-group-guid-3", 489 "updated_at": null 490 }, 491 "entity": { 492 "name": "staging-security-group-name-3", 493 "rules": [ 494 { 495 "protocol": "udp", 496 "ports": "32767", 497 "description": "description-5", 498 "destination": "127.0.0.1/32" 499 }, 500 { 501 "protocol": "tcp", 502 "ports": "8008,4443", 503 "description": "description-6", 504 "destination": "254.41.191.0-254.44.255.1" 505 } 506 ] 507 } 508 } 509 ] 510 }` 511 server.AppendHandlers( 512 CombineHandlers( 513 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/staging_security_groups", "q=some-query:some-value"), 514 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 515 ), 516 ) 517 server.AppendHandlers( 518 CombineHandlers( 519 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/staging_security_groups", "q=some-query:some-value&page=2"), 520 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 521 ), 522 ) 523 }) 524 525 It("returns the staging security groups and all warnings", func() { 526 securityGroups, warnings, err := client.GetSpaceStagingSecurityGroups("some-space-guid", Filter{ 527 Type: "some-query", 528 Operator: constant.EqualOperator, 529 Values: []string{"some-value"}, 530 }) 531 Expect(err).NotTo(HaveOccurred()) 532 Expect(warnings).To(ConsistOf("this is a warning", "this is another warning")) 533 Expect(securityGroups).To(ConsistOf( 534 SecurityGroup{ 535 Name: "staging-security-group-name-1", 536 GUID: "staging-security-group-guid-1", 537 Rules: []SecurityGroupRule{ 538 { 539 Protocol: "udp", 540 Ports: "8080", 541 Description: "description-1", 542 Destination: "198.41.191.47/1", 543 }, 544 { 545 Protocol: "tcp", 546 Ports: "80,443", 547 Description: "description-2", 548 Destination: "254.41.191.47-254.44.255.255", 549 }, 550 }, 551 }, 552 SecurityGroup{ 553 Name: "staging-security-group-name-2", 554 GUID: "staging-security-group-guid-2", 555 Rules: []SecurityGroupRule{ 556 { 557 Protocol: "udp", 558 Ports: "8080", 559 Description: "description-3", 560 Destination: "198.41.191.47/24", 561 }, 562 { 563 Protocol: "tcp", 564 Ports: "80,443", 565 Description: "description-4", 566 Destination: "254.41.191.4-254.44.255.4", 567 }, 568 }, 569 }, 570 SecurityGroup{ 571 Name: "staging-security-group-name-3", 572 GUID: "staging-security-group-guid-3", 573 Rules: []SecurityGroupRule{ 574 { 575 Protocol: "udp", 576 Ports: "32767", 577 Description: "description-5", 578 Destination: "127.0.0.1/32", 579 }, 580 { 581 Protocol: "tcp", 582 Ports: "8008,4443", 583 Description: "description-6", 584 Destination: "254.41.191.0-254.44.255.1", 585 }, 586 }, 587 }, 588 )) 589 }) 590 }) 591 592 When("the client returns an error", func() { 593 BeforeEach(func() { 594 response := `{ 595 "code": 40004, 596 "description": "The space could not be found: some-space-guid", 597 "error_code": "CF-SpaceNotFound" 598 }` 599 server.AppendHandlers( 600 CombineHandlers( 601 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/staging_security_groups"), 602 RespondWith(http.StatusNotFound, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 603 ), 604 ) 605 }) 606 607 It("returns the error and warnings", func() { 608 securityGroups, warnings, err := client.GetSpaceStagingSecurityGroups("some-space-guid") 609 Expect(err).To(MatchError(ccerror.ResourceNotFoundError{ 610 Message: "The space could not be found: some-space-guid", 611 })) 612 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 613 Expect(securityGroups).To(BeEmpty()) 614 }) 615 }) 616 }) 617 618 Describe("UpdateSecurityGroupSpace", func() { 619 When("no errors are encountered", func() { 620 BeforeEach(func() { 621 response := `{}` 622 server.AppendHandlers( 623 CombineHandlers( 624 VerifyRequest(http.MethodPut, "/v2/security_groups/security-group-guid/spaces/space-guid"), 625 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}), 626 )) 627 }) 628 629 It("returns all warnings", func() { 630 warnings, err := client.UpdateSecurityGroupSpace("security-group-guid", "space-guid") 631 632 Expect(err).NotTo(HaveOccurred()) 633 Expect(warnings).To(ConsistOf("warning-1")) 634 }) 635 }) 636 637 When("an error is encountered", func() { 638 BeforeEach(func() { 639 response := `{ 640 "code": 10001, 641 "description": "Some Error", 642 "error_code": "CF-SomeError" 643 }` 644 server.AppendHandlers( 645 CombineHandlers( 646 VerifyRequest(http.MethodPut, "/v2/security_groups/security-group-guid/spaces/space-guid"), 647 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 648 )) 649 }) 650 651 It("returns an error and all warnings", func() { 652 warnings, err := client.UpdateSecurityGroupSpace("security-group-guid", "space-guid") 653 654 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 655 ResponseCode: http.StatusTeapot, 656 V2ErrorResponse: ccerror.V2ErrorResponse{ 657 Code: 10001, 658 Description: "Some Error", 659 ErrorCode: "CF-SomeError", 660 }, 661 })) 662 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 663 }) 664 }) 665 }) 666 667 Describe("UpdateSecurityGroupStagingSpace", func() { 668 When("no errors are encountered", func() { 669 BeforeEach(func() { 670 response := `{}` 671 server.AppendHandlers( 672 CombineHandlers( 673 VerifyRequest(http.MethodPut, "/v2/security_groups/security-group-guid/staging_spaces/space-guid"), 674 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"warning-1"}}), 675 )) 676 }) 677 678 It("returns all warnings", func() { 679 warnings, err := client.UpdateSecurityGroupStagingSpace("security-group-guid", "space-guid") 680 681 Expect(err).NotTo(HaveOccurred()) 682 Expect(warnings).To(ConsistOf("warning-1")) 683 }) 684 }) 685 686 When("an error is encountered", func() { 687 BeforeEach(func() { 688 response := `{ 689 "code": 10001, 690 "description": "Some Error", 691 "error_code": "CF-SomeError" 692 }` 693 server.AppendHandlers( 694 CombineHandlers( 695 VerifyRequest(http.MethodPut, "/v2/security_groups/security-group-guid/staging_spaces/space-guid"), 696 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 697 )) 698 }) 699 700 It("returns an error and all warnings", func() { 701 warnings, err := client.UpdateSecurityGroupStagingSpace("security-group-guid", "space-guid") 702 703 Expect(err).To(MatchError(ccerror.V2UnexpectedResponseError{ 704 ResponseCode: http.StatusTeapot, 705 V2ErrorResponse: ccerror.V2ErrorResponse{ 706 Code: 10001, 707 Description: "Some Error", 708 ErrorCode: "CF-SomeError", 709 }, 710 })) 711 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 712 }) 713 }) 714 }) 715 })