github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/api/cloudcontroller/ccv2/service_instance_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("Service Instance", func() { 15 var client *Client 16 17 BeforeEach(func() { 18 client = NewTestClient() 19 }) 20 21 Describe("Bind", func() { 22 When("the update is successful", func() { 23 When("setting the minimum", func() { // are we **only** encoding the things we want 24 BeforeEach(func() { 25 response := ` 26 { 27 "metadata": { 28 "guid": "some-app-guid" 29 }, 30 "entity": { 31 "name": "some-app-name", 32 "space_guid": "some-space-guid" 33 } 34 }` 35 requestBody := map[string]string{ 36 "name": "some-app-name", 37 "space_guid": "some-space-guid", 38 } 39 server.AppendHandlers( 40 CombineHandlers( 41 VerifyRequest(http.MethodPost, "/v2/apps"), 42 VerifyJSONRepresenting(requestBody), 43 RespondWith(http.StatusCreated, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 44 ), 45 ) 46 }) 47 48 It("returns the created object and warnings", func() { 49 app, warnings, err := client.CreateApplication(Application{ 50 Name: "some-app-name", 51 SpaceGUID: "some-space-guid", 52 }) 53 Expect(err).NotTo(HaveOccurred()) 54 55 Expect(app).To(Equal(Application{ 56 GUID: "some-app-guid", 57 Name: "some-app-name", 58 SpaceGUID: "some-space-guid", 59 })) 60 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 61 }) 62 }) 63 }) 64 65 When("the create returns an error", func() { 66 BeforeEach(func() { 67 response := ` 68 { 69 "description": "Request invalid due to parse error: Field: name, Error: Missing field name, Field: space_guid, Error: Missing field space_guid", 70 "error_code": "CF-MessageParseError", 71 "code": 1001 72 }` 73 74 server.AppendHandlers( 75 CombineHandlers( 76 VerifyRequest(http.MethodPost, "/v2/apps"), 77 RespondWith(http.StatusBadRequest, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 78 ), 79 ) 80 }) 81 82 It("returns the error and warnings", func() { 83 _, warnings, err := client.CreateApplication(Application{}) 84 Expect(err).To(MatchError(ccerror.BadRequestError{Message: "Request invalid due to parse error: Field: name, Error: Missing field name, Field: space_guid, Error: Missing field space_guid"})) 85 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 86 }) 87 }) 88 }) 89 90 Describe("ServiceInstance", func() { 91 Describe("Managed", func() { 92 When("type is MANAGED_SERVICE", func() { 93 It("returns false", func() { 94 service := ServiceInstance{Type: constant.ServiceInstanceTypeManagedService} 95 Expect(service.Managed()).To(BeTrue()) 96 }) 97 }) 98 99 When("type is USER_PROVIDED_SERVICE", func() { 100 It("returns true", func() { 101 service := ServiceInstance{Type: constant.ServiceInstanceTypeUserProvidedService} 102 Expect(service.Managed()).To(BeFalse()) 103 }) 104 }) 105 }) 106 107 Describe("UserProvided", func() { 108 When("type is USER_PROVIDED_SERVICE", func() { 109 It("returns true", func() { 110 service := ServiceInstance{Type: constant.ServiceInstanceTypeUserProvidedService} 111 Expect(service.UserProvided()).To(BeTrue()) 112 }) 113 }) 114 115 When("type is MANAGED_SERVICE", func() { 116 It("returns false", func() { 117 service := ServiceInstance{Type: constant.ServiceInstanceTypeManagedService} 118 Expect(service.UserProvided()).To(BeFalse()) 119 }) 120 }) 121 }) 122 }) 123 124 Describe("GetServiceInstance", func() { 125 BeforeEach(func() { 126 response := `{ 127 "metadata": { 128 "guid": "some-service-guid" 129 }, 130 "entity": { 131 "name": "some-service-name", 132 "space_guid": "some-space-guid", 133 "service_guid": "some-service-guid", 134 "service_plan_guid": "some-service-plan-guid", 135 "type": "managed_service_instance", 136 "tags": [ 137 "tag-1", 138 "tag-2" 139 ], 140 "dashboard_url": "some-dashboard-url", 141 "route_service_url": "some-route-service-url", 142 "last_operation": { 143 "type": "create", 144 "state": "succeeded", 145 "description": "service broker-provided description", 146 "updated_at": "updated-at-time", 147 "created_at": "created-at-time" 148 } 149 } 150 }` 151 152 server.AppendHandlers( 153 CombineHandlers( 154 VerifyRequest(http.MethodGet, "/v2/service_instances/some-service-guid"), 155 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 156 ), 157 ) 158 }) 159 160 When("service instances exist", func() { 161 It("returns the service instance and warnings", func() { 162 serviceInstance, warnings, err := client.GetServiceInstance("some-service-guid") 163 Expect(err).NotTo(HaveOccurred()) 164 165 Expect(serviceInstance).To(Equal(ServiceInstance{ 166 GUID: "some-service-guid", 167 Name: "some-service-name", 168 SpaceGUID: "some-space-guid", 169 ServiceGUID: "some-service-guid", 170 ServicePlanGUID: "some-service-plan-guid", 171 Type: constant.ServiceInstanceTypeManagedService, 172 Tags: []string{"tag-1", "tag-2"}, 173 DashboardURL: "some-dashboard-url", 174 RouteServiceURL: "some-route-service-url", 175 LastOperation: LastOperation{ 176 Type: "create", 177 State: "succeeded", 178 Description: "service broker-provided description", 179 UpdatedAt: "updated-at-time", 180 CreatedAt: "created-at-time", 181 }, 182 })) 183 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 184 }) 185 }) 186 }) 187 188 Describe("GetServiceInstances", func() { 189 BeforeEach(func() { 190 response1 := `{ 191 "next_url": "/v2/service_instances?q=space_guid:some-space-guid&page=2", 192 "resources": [ 193 { 194 "metadata": { 195 "guid": "some-service-guid-1" 196 }, 197 "entity": { 198 "name": "some-service-name-1", 199 "space_guid": "some-space-guid", 200 "service_guid": "some-service-guid", 201 "type": "managed_service_instance" 202 } 203 }, 204 { 205 "metadata": { 206 "guid": "some-service-guid-2" 207 }, 208 "entity": { 209 "name": "some-service-name-2", 210 "space_guid": "some-space-guid", 211 "type": "managed_service_instance" 212 } 213 } 214 ] 215 }` 216 217 response2 := `{ 218 "next_url": null, 219 "resources": [ 220 { 221 "metadata": { 222 "guid": "some-service-guid-3" 223 }, 224 "entity": { 225 "name": "some-service-name-3", 226 "space_guid": "some-space-guid", 227 "type": "managed_service_instance" 228 } 229 }, 230 { 231 "metadata": { 232 "guid": "some-service-guid-4" 233 }, 234 "entity": { 235 "name": "some-service-name-4", 236 "space_guid": "some-space-guid", 237 "type": "managed_service_instance" 238 } 239 } 240 ] 241 }` 242 243 server.AppendHandlers( 244 CombineHandlers( 245 VerifyRequest(http.MethodGet, "/v2/service_instances", "q=space_guid:some-space-guid"), 246 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 247 ), 248 ) 249 250 server.AppendHandlers( 251 CombineHandlers( 252 VerifyRequest(http.MethodGet, "/v2/service_instances", "q=space_guid:some-space-guid&page=2"), 253 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 254 ), 255 ) 256 }) 257 258 When("service instances exist", func() { 259 It("returns all the queried service instances", func() { 260 serviceInstances, warnings, err := client.GetServiceInstances(Filter{ 261 Type: constant.SpaceGUIDFilter, 262 Operator: constant.EqualOperator, 263 Values: []string{"some-space-guid"}, 264 }) 265 Expect(err).NotTo(HaveOccurred()) 266 267 Expect(serviceInstances).To(ConsistOf([]ServiceInstance{ 268 { 269 Name: "some-service-name-1", 270 GUID: "some-service-guid-1", 271 SpaceGUID: "some-space-guid", 272 ServiceGUID: "some-service-guid", 273 Type: constant.ServiceInstanceTypeManagedService, 274 }, 275 { 276 Name: "some-service-name-2", 277 GUID: "some-service-guid-2", 278 SpaceGUID: "some-space-guid", 279 Type: constant.ServiceInstanceTypeManagedService, 280 }, 281 { 282 Name: "some-service-name-3", 283 GUID: "some-service-guid-3", 284 SpaceGUID: "some-space-guid", 285 Type: constant.ServiceInstanceTypeManagedService, 286 }, 287 { 288 Name: "some-service-name-4", 289 GUID: "some-service-guid-4", 290 SpaceGUID: "some-space-guid", 291 Type: constant.ServiceInstanceTypeManagedService, 292 }, 293 })) 294 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 295 }) 296 }) 297 }) 298 299 Describe("GetSpaceServiceInstances", func() { 300 Context("including user provided services", func() { 301 BeforeEach(func() { 302 response1 := `{ 303 "next_url": "/v2/spaces/some-space-guid/service_instances?return_user_provided_service_instances=true&q=name:foobar&page=2", 304 "resources": [ 305 { 306 "metadata": { 307 "guid": "some-service-guid-1" 308 }, 309 "entity": { 310 "name": "some-service-name-1", 311 "space_guid": "some-space-guid", 312 "service_guid": "some-service-guid", 313 "type": "managed_service_instance" 314 } 315 }, 316 { 317 "metadata": { 318 "guid": "some-service-guid-2" 319 }, 320 "entity": { 321 "name": "some-service-name-2", 322 "space_guid": "some-space-guid", 323 "type": "user_provided_service_instance" 324 } 325 } 326 ] 327 }` 328 329 response2 := `{ 330 "next_url": null, 331 "resources": [ 332 { 333 "metadata": { 334 "guid": "some-service-guid-3" 335 }, 336 "entity": { 337 "name": "some-service-name-3", 338 "space_guid": "some-space-guid", 339 "type": "managed_service_instance" 340 } 341 }, 342 { 343 "metadata": { 344 "guid": "some-service-guid-4" 345 }, 346 "entity": { 347 "name": "some-service-name-4", 348 "space_guid": "some-space-guid", 349 "type": "user_provided_service_instance" 350 } 351 } 352 ] 353 }` 354 355 server.AppendHandlers( 356 CombineHandlers( 357 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/service_instances", "return_user_provided_service_instances=true&q=name:foobar"), 358 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 359 ), 360 ) 361 362 server.AppendHandlers( 363 CombineHandlers( 364 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/service_instances", "return_user_provided_service_instances=true&q=name:foobar&page=2"), 365 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 366 ), 367 ) 368 }) 369 370 When("service instances exist", func() { 371 It("returns all the queried service instances", func() { 372 serviceInstances, warnings, err := client.GetSpaceServiceInstances("some-space-guid", true, Filter{ 373 Type: constant.NameFilter, 374 Operator: constant.EqualOperator, 375 Values: []string{"foobar"}, 376 }) 377 Expect(err).NotTo(HaveOccurred()) 378 379 Expect(serviceInstances).To(ConsistOf([]ServiceInstance{ 380 {Name: "some-service-name-1", GUID: "some-service-guid-1", SpaceGUID: "some-space-guid", ServiceGUID: "some-service-guid", Type: constant.ServiceInstanceTypeManagedService}, 381 {Name: "some-service-name-2", GUID: "some-service-guid-2", SpaceGUID: "some-space-guid", Type: constant.ServiceInstanceTypeUserProvidedService}, 382 {Name: "some-service-name-3", GUID: "some-service-guid-3", SpaceGUID: "some-space-guid", Type: constant.ServiceInstanceTypeManagedService}, 383 {Name: "some-service-name-4", GUID: "some-service-guid-4", SpaceGUID: "some-space-guid", Type: constant.ServiceInstanceTypeUserProvidedService}, 384 })) 385 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 386 }) 387 }) 388 }) 389 390 Context("excluding user provided services", func() { 391 BeforeEach(func() { 392 response := `{ 393 "next_url": null, 394 "resources": [ 395 { 396 "metadata": { 397 "guid": "some-service-guid-1" 398 }, 399 "entity": { 400 "name": "some-service-name-1", 401 "space_guid": "some-space-guid", 402 "type": "managed_service_instance" 403 } 404 }, 405 { 406 "metadata": { 407 "guid": "some-service-guid-2" 408 }, 409 "entity": { 410 "name": "some-service-name-2", 411 "space_guid": "some-space-guid", 412 "type": "managed_service_instance" 413 } 414 } 415 ] 416 }` 417 418 server.AppendHandlers( 419 CombineHandlers( 420 VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/service_instances", "q=name:foobar"), 421 RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 422 ), 423 ) 424 }) 425 426 When("service instances exist", func() { 427 It("returns all the queried service instances", func() { 428 serviceInstances, warnings, err := client.GetSpaceServiceInstances("some-space-guid", false, Filter{ 429 Type: constant.NameFilter, 430 Operator: constant.EqualOperator, 431 Values: []string{"foobar"}, 432 }) 433 Expect(err).NotTo(HaveOccurred()) 434 435 Expect(serviceInstances).To(ConsistOf([]ServiceInstance{ 436 {Name: "some-service-name-1", GUID: "some-service-guid-1", SpaceGUID: "some-space-guid", Type: constant.ServiceInstanceTypeManagedService}, 437 {Name: "some-service-name-2", GUID: "some-service-guid-2", SpaceGUID: "some-space-guid", Type: constant.ServiceInstanceTypeManagedService}, 438 })) 439 Expect(warnings).To(ConsistOf(Warnings{"this is a warning"})) 440 }) 441 }) 442 }) 443 }) 444 445 Describe("GetUserProvidedServiceInstances", func() { 446 var ( 447 serviceInstances []ServiceInstance 448 warnings Warnings 449 executeErr error 450 ) 451 452 JustBeforeEach(func() { 453 serviceInstances, warnings, executeErr = client.GetUserProvidedServiceInstances(Filter{ 454 Type: constant.SpaceGUIDFilter, 455 Operator: constant.EqualOperator, 456 Values: []string{"some-space-guid"}, 457 }) 458 }) 459 460 When("getting user provided service instances errors", func() { 461 BeforeEach(func() { 462 response := `{ 463 "code": 1, 464 "description": "some error description", 465 "error_code": "CF-SomeError" 466 }` 467 server.AppendHandlers( 468 CombineHandlers( 469 VerifyRequest(http.MethodGet, "/v2/user_provided_service_instances", "q=space_guid:some-space-guid"), 470 RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}), 471 ), 472 ) 473 }) 474 475 It("returns the error and all warnings", func() { 476 Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{ 477 V2ErrorResponse: ccerror.V2ErrorResponse{ 478 Code: 1, 479 Description: "some error description", 480 ErrorCode: "CF-SomeError", 481 }, 482 ResponseCode: http.StatusTeapot, 483 })) 484 485 Expect(warnings).To(ConsistOf("warning-1", "warning-2")) 486 }) 487 }) 488 489 When("getting user provided service instances succeeds", func() { 490 BeforeEach(func() { 491 response1 := `{ 492 "next_url": "/v2/user_provided_service_instances?q=space_guid:some-space-guid&page=2", 493 "resources": [ 494 { 495 "metadata": { 496 "guid": "some-service-guid-1" 497 }, 498 "entity": { 499 "name": "some-service-name-1", 500 "route_service_url": "some-route-service-url", 501 "space_guid": "some-space-guid", 502 "type": "user_provided_service_instance" 503 } 504 }, 505 { 506 "metadata": { 507 "guid": "some-service-guid-2" 508 }, 509 "entity": { 510 "name": "some-service-name-2", 511 "route_service_url": "some-route-service-url", 512 "space_guid": "some-space-guid", 513 "type": "user_provided_service_instance" 514 } 515 } 516 ] 517 }` 518 519 response2 := `{ 520 "next_url": null, 521 "resources": [ 522 { 523 "metadata": { 524 "guid": "some-service-guid-3" 525 }, 526 "entity": { 527 "name": "some-service-name-3", 528 "route_service_url": "some-route-service-url", 529 "space_guid": "some-space-guid", 530 "type": "user_provided_service_instance" 531 } 532 }, 533 { 534 "metadata": { 535 "guid": "some-service-guid-4" 536 }, 537 "entity": { 538 "name": "some-service-name-4", 539 "route_service_url": "some-route-service-url", 540 "space_guid": "some-space-guid", 541 "type": "user_provided_service_instance" 542 } 543 } 544 ] 545 }` 546 547 server.AppendHandlers( 548 CombineHandlers( 549 VerifyRequest(http.MethodGet, "/v2/user_provided_service_instances", "q=space_guid:some-space-guid"), 550 RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}), 551 ), 552 ) 553 554 server.AppendHandlers( 555 CombineHandlers( 556 VerifyRequest(http.MethodGet, "/v2/user_provided_service_instances", "q=space_guid:some-space-guid&page=2"), 557 RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}), 558 ), 559 ) 560 }) 561 562 It("returns all the queried service instances", func() { 563 Expect(executeErr).NotTo(HaveOccurred()) 564 565 Expect(serviceInstances).To(ConsistOf([]ServiceInstance{ 566 { 567 Name: "some-service-name-1", 568 GUID: "some-service-guid-1", 569 SpaceGUID: "some-space-guid", 570 RouteServiceURL: "some-route-service-url", 571 Type: constant.ServiceInstanceTypeUserProvidedService, 572 }, 573 { 574 Name: "some-service-name-2", 575 GUID: "some-service-guid-2", 576 SpaceGUID: "some-space-guid", 577 RouteServiceURL: "some-route-service-url", 578 Type: constant.ServiceInstanceTypeUserProvidedService, 579 }, 580 { 581 Name: "some-service-name-3", 582 GUID: "some-service-guid-3", 583 SpaceGUID: "some-space-guid", 584 RouteServiceURL: "some-route-service-url", 585 Type: constant.ServiceInstanceTypeUserProvidedService, 586 }, 587 { 588 Name: "some-service-name-4", 589 GUID: "some-service-guid-4", 590 SpaceGUID: "some-space-guid", 591 RouteServiceURL: "some-route-service-url", 592 Type: constant.ServiceInstanceTypeUserProvidedService, 593 }, 594 })) 595 596 Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"})) 597 }) 598 }) 599 }) 600 })