github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/service_instance_user_provided_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/ginkgo/extensions/table"
    12  	. "github.com/onsi/gomega"
    13  	. "github.com/onsi/gomega/ghttp"
    14  )
    15  
    16  var _ = Describe("User-Provided Service Instance", func() {
    17  	var client *Client
    18  
    19  	BeforeEach(func() {
    20  		client = NewTestClient()
    21  	})
    22  
    23  	Describe("GetUserProvidedServiceInstances", func() {
    24  		var (
    25  			serviceInstances []ServiceInstance
    26  			warnings         Warnings
    27  			executeErr       error
    28  		)
    29  
    30  		JustBeforeEach(func() {
    31  			serviceInstances, warnings, executeErr = client.GetUserProvidedServiceInstances(Filter{
    32  				Type:     constant.SpaceGUIDFilter,
    33  				Operator: constant.EqualOperator,
    34  				Values:   []string{"some-space-guid"},
    35  			})
    36  		})
    37  
    38  		When("getting user provided service instances errors", func() {
    39  			BeforeEach(func() {
    40  				response := `{
    41  					"code": 1,
    42  					"description": "some error description",
    43  					"error_code": "CF-SomeError"
    44  				}`
    45  				server.AppendHandlers(
    46  					CombineHandlers(
    47  						VerifyRequest(http.MethodGet, "/v2/user_provided_service_instances", "q=space_guid:some-space-guid"),
    48  						RespondWith(http.StatusTeapot, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
    49  					),
    50  				)
    51  			})
    52  
    53  			It("returns the error and all warnings", func() {
    54  				Expect(executeErr).To(MatchError(ccerror.V2UnexpectedResponseError{
    55  					V2ErrorResponse: ccerror.V2ErrorResponse{
    56  						Code:        1,
    57  						Description: "some error description",
    58  						ErrorCode:   "CF-SomeError",
    59  					},
    60  					ResponseCode: http.StatusTeapot,
    61  				}))
    62  
    63  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
    64  			})
    65  		})
    66  
    67  		When("getting user provided service instances succeeds", func() {
    68  			BeforeEach(func() {
    69  				response1 := `{
    70  				"next_url": "/v2/user_provided_service_instances?q=space_guid:some-space-guid&page=2",
    71  				"resources": [
    72  					{
    73  						"metadata": {
    74  							"guid": "some-service-guid-1"
    75  						},
    76  						"entity": {
    77  							"name": "some-service-name-1",
    78  							"route_service_url": "some-route-service-url",
    79  							"space_guid": "some-space-guid",
    80  							"type": "user_provided_service_instance"
    81  						}
    82  					},
    83  					{
    84  						"metadata": {
    85  							"guid": "some-service-guid-2"
    86  						},
    87  						"entity": {
    88  							"name": "some-service-name-2",
    89  							"route_service_url": "some-route-service-url",
    90  							"space_guid": "some-space-guid",
    91  							"type": "user_provided_service_instance"
    92  						}
    93  					}
    94  				]
    95  			}`
    96  
    97  				response2 := `{
    98  				"next_url": null,
    99  				"resources": [
   100  					{
   101  						"metadata": {
   102  							"guid": "some-service-guid-3"
   103  						},
   104  						"entity": {
   105  							"name": "some-service-name-3",
   106  							"route_service_url": "some-route-service-url",
   107  							"space_guid": "some-space-guid",
   108  							"type": "user_provided_service_instance"
   109  						}
   110  					},
   111  					{
   112  						"metadata": {
   113  							"guid": "some-service-guid-4"
   114  						},
   115  						"entity": {
   116  							"name": "some-service-name-4",
   117  							"route_service_url": "some-route-service-url",
   118  							"space_guid": "some-space-guid",
   119  							"type": "user_provided_service_instance"
   120  						}
   121  					}
   122  				]
   123  			}`
   124  
   125  				server.AppendHandlers(
   126  					CombineHandlers(
   127  						VerifyRequest(http.MethodGet, "/v2/user_provided_service_instances", "q=space_guid:some-space-guid"),
   128  						RespondWith(http.StatusOK, response1, http.Header{"X-Cf-Warnings": {"this is a warning"}}),
   129  					),
   130  				)
   131  
   132  				server.AppendHandlers(
   133  					CombineHandlers(
   134  						VerifyRequest(http.MethodGet, "/v2/user_provided_service_instances", "q=space_guid:some-space-guid&page=2"),
   135  						RespondWith(http.StatusOK, response2, http.Header{"X-Cf-Warnings": {"this is another warning"}}),
   136  					),
   137  				)
   138  			})
   139  
   140  			It("returns all the queried service instances", func() {
   141  				Expect(executeErr).NotTo(HaveOccurred())
   142  
   143  				Expect(serviceInstances).To(ConsistOf([]ServiceInstance{
   144  					{
   145  						Name:            "some-service-name-1",
   146  						GUID:            "some-service-guid-1",
   147  						SpaceGUID:       "some-space-guid",
   148  						RouteServiceURL: "some-route-service-url",
   149  						Type:            constant.UserProvidedService,
   150  					},
   151  					{
   152  						Name:            "some-service-name-2",
   153  						GUID:            "some-service-guid-2",
   154  						SpaceGUID:       "some-space-guid",
   155  						RouteServiceURL: "some-route-service-url",
   156  						Type:            constant.UserProvidedService,
   157  					},
   158  					{
   159  						Name:            "some-service-name-3",
   160  						GUID:            "some-service-guid-3",
   161  						SpaceGUID:       "some-space-guid",
   162  						RouteServiceURL: "some-route-service-url",
   163  						Type:            constant.UserProvidedService,
   164  					},
   165  					{
   166  						Name:            "some-service-name-4",
   167  						GUID:            "some-service-guid-4",
   168  						SpaceGUID:       "some-space-guid",
   169  						RouteServiceURL: "some-route-service-url",
   170  						Type:            constant.UserProvidedService,
   171  					},
   172  				}))
   173  
   174  				Expect(warnings).To(ConsistOf(Warnings{"this is a warning", "this is another warning"}))
   175  			})
   176  		})
   177  	})
   178  
   179  	Describe("UpdateUserProvidedServiceInstance", func() {
   180  		const (
   181  			spaceGUID   = "fake-space-guid"
   182  			serviceGUID = "fake-service-instance-guid"
   183  		)
   184  
   185  		DescribeTable("updating properties",
   186  			func(body string, instance UserProvidedServiceInstance) {
   187  				server.AppendHandlers(
   188  					CombineHandlers(
   189  						VerifyRequest(http.MethodPut, fmt.Sprintf("/v2/user_provided_service_instances/%s", serviceGUID)),
   190  						VerifyJSON(body),
   191  						RespondWith(http.StatusOK, "", http.Header{"X-Cf-Warnings": {"warning-1,warning-2"}}),
   192  					),
   193  				)
   194  
   195  				previousRequests := len(server.ReceivedRequests())
   196  				warnings, executeErr := client.UpdateUserProvidedServiceInstance(serviceGUID, instance)
   197  
   198  				Expect(server.ReceivedRequests()).To(HaveLen(previousRequests + 1))
   199  				Expect(executeErr).NotTo(HaveOccurred())
   200  				Expect(warnings).To(ConsistOf(Warnings{"warning-1", "warning-2"}))
   201  			},
   202  			Entry(
   203  				"setting log URL",
   204  				`{"syslog_drain_url": "fake-syslog-drain-url"}`,
   205  				UserProvidedServiceInstance{}.WithSyslogDrainURL("fake-syslog-drain-url"),
   206  			),
   207  			Entry(
   208  				"removing log URL",
   209  				`{"syslog_drain_url": ""}`,
   210  				UserProvidedServiceInstance{}.WithSyslogDrainURL(""),
   211  			),
   212  			Entry(
   213  				"setting routes URL",
   214  				`{"route_service_url": "fake-route-url"}`,
   215  				UserProvidedServiceInstance{}.WithRouteServiceURL("fake-route-url"),
   216  			),
   217  			Entry(
   218  				"removing routes URL",
   219  				`{"route_service_url": ""}`,
   220  				UserProvidedServiceInstance{}.WithRouteServiceURL(""),
   221  			),
   222  			Entry(
   223  				"setting tags",
   224  				`{"tags": ["tag1", "tag2"]}`,
   225  				UserProvidedServiceInstance{}.WithTags([]string{"tag1", "tag2"}),
   226  			),
   227  			Entry(
   228  				"removing tags",
   229  				`{"tags": []}`,
   230  				UserProvidedServiceInstance{}.WithTags(nil),
   231  			),
   232  			Entry(
   233  				"setting credentials",
   234  				`{"credentials": {"username": "super-secret-password"}}`,
   235  				UserProvidedServiceInstance{}.WithCredentials(map[string]interface{}{"username": "super-secret-password"}),
   236  			),
   237  			Entry(
   238  				"removing credentials",
   239  				`{"credentials": {}}`,
   240  				UserProvidedServiceInstance{}.WithCredentials(nil),
   241  			),
   242  			Entry(
   243  				"setting everything",
   244  				`{
   245  					"syslog_drain_url":  "fake-syslog-drain-url",
   246  					"route_service_url": "fake-route-url",
   247  					"tags":              ["tag1", "tag2"],
   248  					"credentials":       {"username": "super-secret-password"}
   249  				}`,
   250  				UserProvidedServiceInstance{}.
   251  					WithSyslogDrainURL("fake-syslog-drain-url").
   252  					WithRouteServiceURL("fake-route-url").
   253  					WithTags([]string{"tag1", "tag2"}).
   254  					WithCredentials(map[string]interface{}{"username": "super-secret-password"}),
   255  			),
   256  		)
   257  
   258  		When("the endpoint returns an error", func() {
   259  			BeforeEach(func() {
   260  				response := `{
   261  												"code": 10003,
   262  												"description": "You are not authorized to perform the requested action"
   263  			  							}`
   264  
   265  				server.AppendHandlers(
   266  					CombineHandlers(
   267  						VerifyRequest(http.MethodPut, fmt.Sprintf("/v2/user_provided_service_instances/%s", serviceGUID)),
   268  						RespondWith(http.StatusForbidden, response, http.Header{"X-Cf-Warnings": {"warning-1, warning-2"}}),
   269  					))
   270  			})
   271  
   272  			It("returns all warnings and propagates the error", func() {
   273  				priorRequests := len(server.ReceivedRequests())
   274  				warnings, err := client.UpdateUserProvidedServiceInstance(serviceGUID, UserProvidedServiceInstance{})
   275  
   276  				Expect(server.ReceivedRequests()).To(HaveLen(priorRequests + 1))
   277  				Expect(err).To(MatchError(ccerror.ForbiddenError{
   278  					Message: "You are not authorized to perform the requested action",
   279  				}))
   280  				Expect(warnings).To(ConsistOf("warning-1", "warning-2"))
   281  			})
   282  		})
   283  	})
   284  })