github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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  		Context("when the update is successful", func() {
    23  			Context("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  					}))
    59  					Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    60  				})
    61  			})
    62  		})
    63  
    64  		Context("when the create returns an error", func() {
    65  			BeforeEach(func() {
    66  				response := `
    67  					{
    68  						"description": "Request invalid due to parse error: Field: name, Error: Missing field name, Field: space_guid, Error: Missing field space_guid",
    69  						"error_code": "CF-MessageParseError",
    70  						"code": 1001
    71  					}`
    72  
    73  				server.AppendHandlers(
    74  					CombineHandlers(
    75  						VerifyRequest(http.MethodPost, "/v2/apps"),
    76  						RespondWith(http.StatusBadRequest, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
    77  					),
    78  				)
    79  			})
    80  
    81  			It("returns the error and warnings", func() {
    82  				_, warnings, err := client.CreateApplication(Application{})
    83  				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"}))
    84  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
    85  			})
    86  		})
    87  	})
    88  
    89  	Describe("ServiceInstance", func() {
    90  		Describe("UserProvided", func() {
    91  			Context("when type is USER_PROVIDED_SERVICE", func() {
    92  				It("returns true", func() {
    93  					service := ServiceInstance{Type: UserProvidedService}
    94  					Expect(service.UserProvided()).To(BeTrue())
    95  				})
    96  			})
    97  
    98  			Context("when type is MANAGED_SERVICE", func() {
    99  				It("returns false", func() {
   100  					service := ServiceInstance{Type: ManagedService}
   101  					Expect(service.UserProvided()).To(BeFalse())
   102  				})
   103  			})
   104  		})
   105  
   106  		Describe("Managed", func() {
   107  			Context("when type is MANAGED_SERVICE", func() {
   108  				It("returns false", func() {
   109  					service := ServiceInstance{Type: ManagedService}
   110  					Expect(service.Managed()).To(BeTrue())
   111  				})
   112  			})
   113  
   114  			Context("when type is USER_PROVIDED_SERVICE", func() {
   115  				It("returns true", func() {
   116  					service := ServiceInstance{Type: UserProvidedService}
   117  					Expect(service.Managed()).To(BeFalse())
   118  				})
   119  			})
   120  		})
   121  	})
   122  
   123  	Describe("GetServiceInstance", func() {
   124  		BeforeEach(func() {
   125  			response := `{
   126  				"metadata": {
   127  					"guid": "some-service-guid"
   128  				},
   129  				"entity": {
   130  					"name": "some-service-name",
   131  					"space_guid": "some-space-guid",
   132  					"service_guid": "some-service-guid",
   133  					"service_plan_guid": "some-service-plan-guid",
   134  					"type": "managed_service_instance",
   135  					"tags": [
   136  						"tag-1",
   137  						"tag-2"
   138  					],
   139  					"dashboard_url": "some-dashboard-url",
   140  					"last_operation": {
   141  						"type": "create",
   142  						"state": "succeeded",
   143  						"description": "service broker-provided description",
   144  						"updated_at": "updated-at-time",
   145  						"created_at": "created-at-time"
   146  					}
   147  				}
   148  			}`
   149  
   150  			server.AppendHandlers(
   151  				CombineHandlers(
   152  					VerifyRequest(http.MethodGet, "/v2/service_instances/some-service-guid"),
   153  					RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   154  				),
   155  			)
   156  		})
   157  
   158  		Context("when service instances exist", func() {
   159  			It("returns the service instance and warnings", func() {
   160  				serviceInstance, warnings, err := client.GetServiceInstance("some-service-guid")
   161  				Expect(err).NotTo(HaveOccurred())
   162  
   163  				Expect(serviceInstance).To(Equal(ServiceInstance{
   164  					GUID:            "some-service-guid",
   165  					Name:            "some-service-name",
   166  					SpaceGUID:       "some-space-guid",
   167  					ServiceGUID:     "some-service-guid",
   168  					ServicePlanGUID: "some-service-plan-guid",
   169  					Type:            ManagedService,
   170  					Tags:            []string{"tag-1", "tag-2"},
   171  					DashboardURL:    "some-dashboard-url",
   172  					LastOperation: LastOperation{
   173  						Type:        "create",
   174  						State:       "succeeded",
   175  						Description: "service broker-provided description",
   176  						UpdatedAt:   "updated-at-time",
   177  						CreatedAt:   "created-at-time",
   178  					},
   179  				}))
   180  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   181  			})
   182  		})
   183  	})
   184  
   185  	Describe("GetServiceInstances", func() {
   186  		BeforeEach(func() {
   187  			response1 := `{
   188  				"next_url": "/v2/service_instances?q=space_guid:some-space-guid&page=2",
   189  				"resources": [
   190  					{
   191  						"metadata": {
   192  							"guid": "some-service-guid-1"
   193  						},
   194  						"entity": {
   195  							"name": "some-service-name-1",
   196  							"space_guid": "some-space-guid",
   197  					"service_guid": "some-service-guid",
   198  							"type": "managed_service_instance"
   199  						}
   200  					},
   201  					{
   202  						"metadata": {
   203  							"guid": "some-service-guid-2"
   204  						},
   205  						"entity": {
   206  							"name": "some-service-name-2",
   207  							"space_guid": "some-space-guid",
   208  							"type": "managed_service_instance"
   209  						}
   210  					}
   211  				]
   212  			}`
   213  
   214  			response2 := `{
   215  				"next_url": null,
   216  				"resources": [
   217  					{
   218  						"metadata": {
   219  							"guid": "some-service-guid-3"
   220  						},
   221  						"entity": {
   222  							"name": "some-service-name-3",
   223  							"space_guid": "some-space-guid",
   224  							"type": "managed_service_instance"
   225  						}
   226  					},
   227  					{
   228  						"metadata": {
   229  							"guid": "some-service-guid-4"
   230  						},
   231  						"entity": {
   232  							"name": "some-service-name-4",
   233  							"space_guid": "some-space-guid",
   234  							"type": "managed_service_instance"
   235  						}
   236  					}
   237  				]
   238  			}`
   239  
   240  			server.AppendHandlers(
   241  				CombineHandlers(
   242  					VerifyRequest(http.MethodGet, "/v2/service_instances", "q=space_guid:some-space-guid"),
   243  					RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   244  				),
   245  			)
   246  
   247  			server.AppendHandlers(
   248  				CombineHandlers(
   249  					VerifyRequest(http.MethodGet, "/v2/service_instances", "q=space_guid:some-space-guid&page=2"),
   250  					RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   251  				),
   252  			)
   253  		})
   254  
   255  		Context("when service instances exist", func() {
   256  			It("returns all the queried service instances", func() {
   257  				serviceInstances, warnings, err := client.GetServiceInstances(Filter{
   258  					Type:     constant.SpaceGUIDFilter,
   259  					Operator: constant.EqualOperator,
   260  					Values:   []string{"some-space-guid"},
   261  				})
   262  				Expect(err).NotTo(HaveOccurred())
   263  
   264  				Expect(serviceInstances).To(ConsistOf([]ServiceInstance{
   265  					{
   266  						Name:        "some-service-name-1",
   267  						GUID:        "some-service-guid-1",
   268  						SpaceGUID:   "some-space-guid",
   269  						ServiceGUID: "some-service-guid",
   270  						Type:        ManagedService,
   271  					},
   272  					{
   273  						Name:      "some-service-name-2",
   274  						GUID:      "some-service-guid-2",
   275  						SpaceGUID: "some-space-guid",
   276  						Type:      ManagedService,
   277  					},
   278  					{
   279  						Name:      "some-service-name-3",
   280  						GUID:      "some-service-guid-3",
   281  						SpaceGUID: "some-space-guid",
   282  						Type:      ManagedService,
   283  					},
   284  					{
   285  						Name:      "some-service-name-4",
   286  						GUID:      "some-service-guid-4",
   287  						SpaceGUID: "some-space-guid",
   288  						Type:      ManagedService,
   289  					},
   290  				}))
   291  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   292  			})
   293  		})
   294  	})
   295  
   296  	Describe("GetSpaceServiceInstances", func() {
   297  		Context("including user provided services", func() {
   298  			BeforeEach(func() {
   299  				response1 := `{
   300  					"next_url": "/v2/spaces/some-space-guid/service_instances?return_user_provided_service_instances=true&q=name:foobar&page=2",
   301  					"resources": [
   302  						{
   303  							"metadata": {
   304  								"guid": "some-service-guid-1"
   305  							},
   306  							"entity": {
   307  								"name": "some-service-name-1",
   308  								"space_guid": "some-space-guid",
   309  					"service_guid": "some-service-guid",
   310  								"type": "managed_service_instance"
   311  							}
   312  						},
   313  						{
   314  							"metadata": {
   315  								"guid": "some-service-guid-2"
   316  							},
   317  							"entity": {
   318  								"name": "some-service-name-2",
   319  								"space_guid": "some-space-guid",
   320  								"type": "user_provided_service_instance"
   321  							}
   322  						}
   323  					]
   324  				}`
   325  
   326  				response2 := `{
   327  					"next_url": null,
   328  					"resources": [
   329  						{
   330  							"metadata": {
   331  								"guid": "some-service-guid-3"
   332  							},
   333  							"entity": {
   334  								"name": "some-service-name-3",
   335  								"space_guid": "some-space-guid",
   336  								"type": "managed_service_instance"
   337  							}
   338  						},
   339  						{
   340  							"metadata": {
   341  								"guid": "some-service-guid-4"
   342  							},
   343  							"entity": {
   344  								"name": "some-service-name-4",
   345  								"space_guid": "some-space-guid",
   346  								"type": "user_provided_service_instance"
   347  							}
   348  						}
   349  					]
   350  				}`
   351  
   352  				server.AppendHandlers(
   353  					CombineHandlers(
   354  						VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/service_instances", "return_user_provided_service_instances=true&q=name:foobar"),
   355  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   356  					),
   357  				)
   358  
   359  				server.AppendHandlers(
   360  					CombineHandlers(
   361  						VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/service_instances", "return_user_provided_service_instances=true&q=name:foobar&page=2"),
   362  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   363  					),
   364  				)
   365  			})
   366  
   367  			Context("when service instances exist", func() {
   368  				It("returns all the queried service instances", func() {
   369  					serviceInstances, warnings, err := client.GetSpaceServiceInstances("some-space-guid", true, Filter{
   370  						Type:     constant.NameFilter,
   371  						Operator: constant.EqualOperator,
   372  						Values:   []string{"foobar"},
   373  					})
   374  					Expect(err).NotTo(HaveOccurred())
   375  
   376  					Expect(serviceInstances).To(ConsistOf([]ServiceInstance{
   377  						{Name: "some-service-name-1", GUID: "some-service-guid-1", SpaceGUID: "some-space-guid", ServiceGUID: "some-service-guid", Type: ManagedService},
   378  						{Name: "some-service-name-2", GUID: "some-service-guid-2", SpaceGUID: "some-space-guid", Type: UserProvidedService},
   379  						{Name: "some-service-name-3", GUID: "some-service-guid-3", SpaceGUID: "some-space-guid", Type: ManagedService},
   380  						{Name: "some-service-name-4", GUID: "some-service-guid-4", SpaceGUID: "some-space-guid", Type: UserProvidedService},
   381  					}))
   382  					Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   383  				})
   384  			})
   385  		})
   386  
   387  		Context("excluding user provided services", func() {
   388  			BeforeEach(func() {
   389  				response := `{
   390  					"next_url": null,
   391  					"resources": [
   392  						{
   393  							"metadata": {
   394  								"guid": "some-service-guid-1"
   395  							},
   396  							"entity": {
   397  								"name": "some-service-name-1",
   398  								"space_guid": "some-space-guid",
   399  								"type": "managed_service_instance"
   400  							}
   401  						},
   402  						{
   403  							"metadata": {
   404  								"guid": "some-service-guid-2"
   405  							},
   406  							"entity": {
   407  								"name": "some-service-name-2",
   408  								"space_guid": "some-space-guid",
   409  								"type": "managed_service_instance"
   410  							}
   411  						}
   412  					]
   413  				}`
   414  
   415  				server.AppendHandlers(
   416  					CombineHandlers(
   417  						VerifyRequest(http.MethodGet, "/v2/spaces/some-space-guid/service_instances", "q=name:foobar"),
   418  						RespondWith(http.StatusOK, response, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   419  					),
   420  				)
   421  			})
   422  
   423  			Context("when service instances exist", func() {
   424  				It("returns all the queried service instances", func() {
   425  					serviceInstances, warnings, err := client.GetSpaceServiceInstances("some-space-guid", false, Filter{
   426  						Type:     constant.NameFilter,
   427  						Operator: constant.EqualOperator,
   428  						Values:   []string{"foobar"},
   429  					})
   430  					Expect(err).NotTo(HaveOccurred())
   431  
   432  					Expect(serviceInstances).To(ConsistOf([]ServiceInstance{
   433  						{Name: "some-service-name-1", GUID: "some-service-guid-1", SpaceGUID: "some-space-guid", Type: ManagedService},
   434  						{Name: "some-service-name-2", GUID: "some-service-guid-2", SpaceGUID: "some-space-guid", Type: ManagedService},
   435  					}))
   436  					Expect(warnings).To(ConsistOf(Warnings{"this is a warning"}))
   437  				})
   438  			})
   439  		})
   440  	})
   441  })