github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/service_instances_test.go (about) 1 package mccpv2 2 3 import ( 4 "log" 5 "net/http" 6 7 bluemix "github.com/IBM-Cloud/bluemix-go" 8 "github.com/IBM-Cloud/bluemix-go/client" 9 "github.com/IBM-Cloud/bluemix-go/helpers" 10 bluemixHttp "github.com/IBM-Cloud/bluemix-go/http" 11 "github.com/IBM-Cloud/bluemix-go/session" 12 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 "github.com/onsi/gomega/ghttp" 16 ) 17 18 var _ = Describe("ServiceInstances", func() { 19 var server *ghttp.Server 20 AfterEach(func() { 21 server.Close() 22 }) 23 24 Describe("Create", func() { 25 Context("When creation is successful", func() { 26 BeforeEach(func() { 27 server = ghttp.NewServer() 28 server.AppendHandlers( 29 ghttp.CombineHandlers( 30 ghttp.VerifyRequest(http.MethodPost, "/v2/service_instances", "accepts_incomplete=true"), 31 ghttp.VerifyBody([]byte(`{"name":"my-service-instance","space_guid":"ba013e75-1da1-4eaa-b30d-0f258211e4c1","service_plan_guid":"817b7e86-551c-416a-bfbc-c96feb4e4a64","parameters":{"the_service_broker":"wants this object"},"tags":["accounting","mongodb"]}`)), 32 ghttp.RespondWith(http.StatusAccepted, `{ 33 "metadata": { 34 "guid": "519b0d69-19e4-4009-a363-461eb117mccp32", 35 "url": "/v2/service_instances/519b0d69-19e4-4009-a363-461eb117mccp32", 36 "created_at": "2016-04-16T01:23:58Z", 37 "updated_at": null 38 }, 39 "entity": { 40 "name": "my-service-instance", 41 "credentials": { 42 }, 43 "service_plan_guid": "817b7e86-551c-416a-bfbc-c96feb4e4a64", 44 "space_guid": "ba013e75-1da1-4eaa-b30d-0f258211e4c1", 45 "gateway_data": null, 46 "dashboard_url": null, 47 "type": "managed_service_instance", 48 "last_operation": { 49 "type": "create", 50 "state": "in progress", 51 "description": "", 52 "updated_at": null, 53 "created_at": "2016-04-16T01:23:58Z" 54 }, 55 "tags": [ 56 "accounting", 57 "mongodb" 58 ], 59 "space_url": "/v2/spaces/ba013e75-1da1-4eaa-b30d-0f258211e4c1", 60 "service_plan_url": "/v2/service_plans/817b7e86-551c-416a-bfbc-c96feb4e4a64", 61 "service_bindings_url": "/v2/service_instances/519b0d69-19e4-4009-a363-461eb117mccp32/service_bindings", 62 "service_keys_url": "/v2/service_instances/519b0d69-19e4-4009-a363-461eb117mccp32/service_keys", 63 "routes_url": "/v2/service_instances/519b0d69-19e4-4009-a363-461eb117mccp32/routes" 64 } 65 }`), 66 ), 67 ) 68 }) 69 70 It("Should create ServiceInstances", func() { 71 param := map[string]interface{}{"the_service_broker": "wants this object"} 72 tags := []string{"accounting", "mongodb"} 73 si, err := newServiceInstances(server.URL()).Create(ServiceInstanceCreateRequest{ 74 Name: "my-service-instance", 75 PlanGUID: "817b7e86-551c-416a-bfbc-c96feb4e4a64", 76 SpaceGUID: "ba013e75-1da1-4eaa-b30d-0f258211e4c1", 77 Params: param, 78 Tags: tags, 79 }) 80 81 Expect(err).NotTo(HaveOccurred()) 82 Expect(si).NotTo(BeNil()) 83 Expect(si.Metadata.GUID).To(Equal("519b0d69-19e4-4009-a363-461eb117mccp32")) 84 Expect(si.Entity.Name).To(Equal("my-service-instance")) 85 Expect(si.Entity.ServicePlanGUID).To(Equal("817b7e86-551c-416a-bfbc-c96feb4e4a64")) 86 Expect(si.Entity.SpaceGUID).To(Equal("ba013e75-1da1-4eaa-b30d-0f258211e4c1")) 87 }) 88 }) 89 90 Context("When creation is failed", func() { 91 BeforeEach(func() { 92 server = ghttp.NewServer() 93 server.SetAllowUnhandledRequests(true) 94 server.AppendHandlers( 95 ghttp.CombineHandlers( 96 ghttp.VerifyRequest(http.MethodPost, "/v2/service_instances"), 97 ghttp.VerifyBody([]byte(`{"name":"my-service-instance","space_guid":"ba013e75-1da1-4eaa-b30d-0f258211e4c1","service_plan_guid":"817b7e86-551c-416a-bfbc-c96feb4e4a64"}`)), 98 ghttp.RespondWith(http.StatusInternalServerError, `Failed to create`), 99 ), 100 ) 101 }) 102 103 It("Should return error when created", func() { 104 si, err := newServiceInstances(server.URL()).Create(ServiceInstanceCreateRequest{ 105 Name: "my-service-instance", 106 PlanGUID: "817b7e86-551c-416a-bfbc-c96feb4e4a64", 107 SpaceGUID: "ba013e75-1da1-4eaa-b30d-0f258211e4c1", 108 }) 109 Expect(err).To(HaveOccurred()) 110 Expect(si).Should(BeNil()) 111 }) 112 }) 113 }) 114 115 Describe("FindByName", func() { 116 Context("When there is match", func() { 117 BeforeEach(func() { 118 server = ghttp.NewServer() 119 server.AppendHandlers( 120 ghttp.CombineHandlers( 121 ghttp.VerifyRequest(http.MethodGet, "/v2/service_instances", "return_user_provided_service_instances=true&q=name:foo"), 122 ghttp.RespondWith(http.StatusOK, `{ 123 "total_results": 1, 124 "total_pages": 1, 125 "prev_url": null, 126 "next_url": null, 127 "resources": [ 128 { 129 "metadata": { 130 "guid": "76f3500e-60b2-4d8c-b478-f6797f2beedb", 131 "url": "/v2/service_instances/76f3500e-60b2-4d8c-b478-f6797f2beedb", 132 "created_at": "2016-04-16T01:23:58Z", 133 "updated_at": null 134 }, 135 "entity": { 136 "name": "foo", 137 "credentials": { 138 "creds-key-39": "creds-val-39" 139 }, 140 "service_plan_guid": "623943a0-96aa-4d96-bc34-1852c74020ac", 141 "space_guid": "259dffac-mccp36-433f-9738-58a3b1af5668", 142 "gateway_data": null, 143 "dashboard_url": null, 144 "type": "managed_service_instance", 145 "last_operation": { 146 "type": "create", 147 "state": "succeeded", 148 "description": "service broker-provided description", 149 "updated_at": "2016-04-16T01:23:58Z", 150 "created_at": "2016-04-16T01:23:58Z" 151 }, 152 "tags": [ 153 "accounting", 154 "mongodb" 155 ], 156 "space_url": "/v2/spaces/259dffac-mccp36-433f-9738-58a3b1af5668", 157 "service_plan_url": "/v2/service_plans/623943a0-96aa-4d96-bc34-1852c74020ac", 158 "service_bindings_url": "/v2/service_instances/76f3500e-60b2-4d8c-b478-f6797f2beedb/service_bindings", 159 "service_keys_url": "/v2/service_instances/76f3500e-60b2-4d8c-b478-f6797f2beedb/service_keys", 160 "routes_url": "/v2/service_instances/76f3500e-60b2-4d8c-b478-f6797f2beedb/routes" 161 } 162 } 163 ] 164 }`), 165 ), 166 ) 167 }) 168 169 It("Should return the match", func() { 170 si, err := newServiceInstances(server.URL()).FindByName("foo") 171 Expect(err).NotTo(HaveOccurred()) 172 Expect(si).NotTo(BeNil()) 173 Expect(si.GUID).To(Equal("76f3500e-60b2-4d8c-b478-f6797f2beedb")) 174 Expect(si.Name).To(Equal("foo")) 175 Expect(si.ServicePlanGUID).To(Equal("623943a0-96aa-4d96-bc34-1852c74020ac")) 176 Expect(si.SpaceGUID).To(Equal("259dffac-mccp36-433f-9738-58a3b1af5668")) 177 }) 178 }) 179 180 Context("When ServiceInstance FindByName is failed", func() { 181 BeforeEach(func() { 182 server = ghttp.NewServer() 183 server.SetAllowUnhandledRequests(true) 184 server.AppendHandlers( 185 ghttp.CombineHandlers( 186 ghttp.VerifyRequest(http.MethodGet, "/v2/service_instances", "return_user_provided_service_instances=true&q=name:foo"), 187 ghttp.RespondWith(http.StatusInternalServerError, `Failed to find by name`), 188 ), 189 ) 190 }) 191 192 It("Should return error when ServiceInstance is found by name", func() { 193 si, err := newServiceInstances(server.URL()).FindByName("foo") 194 Expect(err).To(HaveOccurred()) 195 Expect(si).Should(BeNil()) 196 }) 197 }) 198 }) 199 200 Describe("Get", func() { 201 Context("When there is one ServiceInstance", func() { 202 BeforeEach(func() { 203 server = ghttp.NewServer() 204 server.AppendHandlers( 205 ghttp.CombineHandlers( 206 ghttp.VerifyRequest(http.MethodGet, "/v2/service_instances/fbfd3591-48a8-482c-af62-f2d85d75229c"), 207 ghttp.RespondWith(http.StatusOK, `{ 208 "metadata": { 209 "guid": "fbfd3591-48a8-482c-af62-f2d85d75229c", 210 "url": "/v2/service_instances/fbfd3591-48a8-482c-af62-f2d85d75229c", 211 "created_at": "2016-04-16T01:23:58Z", 212 "updated_at": null 213 }, 214 "entity": { 215 "name": "name-1014", 216 "credentials": { 217 "creds-key-42": "creds-val-42" 218 }, 219 "service_plan_guid": "44f5e1a0-1a08-4099-96ac-f747cc9604f2", 220 "space_guid": "abdf35c0-5c2c-481b-8954-14602b7ce7c2", 221 "gateway_data": null, 222 "dashboard_url": null, 223 "type": "managed_service_instance", 224 "last_operation": { 225 "type": "create", 226 "state": "succeeded", 227 "description": "service broker-provided description", 228 "updated_at": "2016-04-16T01:23:58Z", 229 "created_at": "2016-04-16T01:23:58Z" 230 }, 231 "tags": [ 232 "accounting", 233 "mongodb" 234 ], 235 "space_url": "/v2/spaces/abdf35c0-5c2c-481b-8954-14602b7ce7c2", 236 "service_plan_url": "/v2/service_plans/44f5e1a0-1a08-4099-96ac-f747cc9604f2", 237 "service_bindings_url": "/v2/service_instances/fbfd3591-48a8-482c-af62-f2d85d75229c/service_bindings", 238 "service_keys_url": "/v2/service_instances/fbfd3591-48a8-482c-af62-f2d85d75229c/service_keys", 239 "routes_url": "/v2/service_instances/fbfd3591-48a8-482c-af62-f2d85d75229c/routes" 240 } 241 }`), 242 ), 243 ) 244 }) 245 246 It("Should return the ServiceInstance", func() { 247 si, err := newServiceInstances(server.URL()).Get("fbfd3591-48a8-482c-af62-f2d85d75229c") 248 Expect(err).NotTo(HaveOccurred()) 249 Expect(si).NotTo(BeNil()) 250 Expect(si.Metadata.GUID).To(Equal("fbfd3591-48a8-482c-af62-f2d85d75229c")) 251 Expect(si.Entity.Name).To(Equal("name-1014")) 252 Expect(si.Entity.ServicePlanGUID).To(Equal("44f5e1a0-1a08-4099-96ac-f747cc9604f2")) 253 Expect(si.Entity.SpaceGUID).To(Equal("abdf35c0-5c2c-481b-8954-14602b7ce7c2")) 254 }) 255 }) 256 257 Context("When ServiceInstance Get is failed", func() { 258 BeforeEach(func() { 259 server = ghttp.NewServer() 260 server.SetAllowUnhandledRequests(true) 261 server.AppendHandlers( 262 ghttp.CombineHandlers( 263 ghttp.VerifyRequest(http.MethodGet, "/v2/service_instances/fbfd3591-48a8-482c-af62-f2d85d75229c"), 264 ghttp.RespondWith(http.StatusInternalServerError, `Failed to get`), 265 ), 266 ) 267 }) 268 269 It("Should return error when ServiceInstance is retrieved", func() { 270 si, err := newServiceInstances(server.URL()).Get("fbfd3591-48a8-482c-af62-f2d85d75229c") 271 Expect(err).To(HaveOccurred()) 272 Expect(si).Should(BeNil()) 273 }) 274 }) 275 }) 276 277 Describe("Delete", func() { 278 Context("When deletion is successful", func() { 279 BeforeEach(func() { 280 server = ghttp.NewServer() 281 server.AppendHandlers( 282 ghttp.CombineHandlers( 283 ghttp.VerifyRequest(http.MethodDelete, "/v2/service_instances/8dc26f28-8b60-43b3-bed4-b9b5b0190d05"), 284 ghttp.RespondWith(http.StatusAccepted, `{ 285 "metadata": { 286 "guid": "8dc26f28-8b60-43b3-bed4-b9b5b0190d05", 287 "url": "/v2/service_instances/8dc26f28-8b60-43b3-bed4-b9b5b0190d05", 288 "created_at": "2016-04-16T01:23:58Z", 289 "updated_at": null 290 }, 291 "entity": { 292 "name": "name-1001", 293 "credentials": { 294 "creds-key-40": "creds-val-40" 295 }, 296 "service_plan_guid": "03eb1037-5b45-4841-9bd3-4badcbc438e8", 297 "space_guid": "5db9fe9d-28ef-4704-a906-74f00d3dd0d9", 298 "gateway_data": null, 299 "dashboard_url": null, 300 "type": "managed_service_instance", 301 "last_operation": { 302 "type": "delete", 303 "state": "in progress", 304 "description": "", 305 "updated_at": "2016-04-16T01:23:58Z", 306 "created_at": "2016-04-16T01:23:58Z" 307 }, 308 "tags": [ 309 "accounting", 310 "mongodb" 311 ], 312 "space_url": "/v2/spaces/5db9fe9d-28ef-4704-a906-74f00d3dd0d9", 313 "service_plan_url": "/v2/service_plans/03eb1037-5b45-4841-9bd3-4badcbc438e8", 314 "service_bindings_url": "/v2/service_instances/8dc26f28-8b60-43b3-bed4-b9b5b0190d05/service_bindings", 315 "service_keys_url": "/v2/service_instances/8dc26f28-8b60-43b3-bed4-b9b5b0190d05/service_keys", 316 "routes_url": "/v2/service_instances/8dc26f28-8b60-43b3-bed4-b9b5b0190d05/routes" 317 } 318 }`), 319 ), 320 ) 321 }) 322 323 It("Should delete ServiceInstance", func() { 324 err := newServiceInstances(server.URL()).Delete("8dc26f28-8b60-43b3-bed4-b9b5b0190d05") 325 Expect(err).ShouldNot(HaveOccurred()) 326 }) 327 }) 328 329 Context("When ServiceInstance Delete is failed", func() { 330 BeforeEach(func() { 331 server = ghttp.NewServer() 332 server.SetAllowUnhandledRequests(true) 333 server.AppendHandlers( 334 ghttp.CombineHandlers( 335 ghttp.VerifyRequest(http.MethodDelete, "/v2/service_instances/8dc26f28-8b60-43b3-bed4-b9b5b0190d05"), 336 ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete`), 337 ), 338 ) 339 }) 340 341 It("Should return error when deleted", func() { 342 err := newServiceInstances(server.URL()).Delete("8dc26f28-8b60-43b3-bed4-b9b5b0190d05") 343 Expect(err).To(HaveOccurred()) 344 }) 345 }) 346 }) 347 348 Describe("Update", func() { 349 Context("When update is succeesful", func() { 350 BeforeEach(func() { 351 server = ghttp.NewServer() 352 server.AppendHandlers( 353 ghttp.CombineHandlers( 354 ghttp.VerifyRequest(http.MethodPut, "/v2/service_instances/e764af7b-1603-4ba3-b4bf-0b0da98f7ec2", "accepts_incomplete=true"), 355 ghttp.VerifyBody([]byte(`{"name":"new-name","parameters":{"the_service_broker":"new service broker"}}`)), 356 ghttp.RespondWith(http.StatusAccepted, `{ 357 "metadata": { 358 "guid": "e764af7b-1603-4ba3-b4bf-0b0da98f7ec2", 359 "url": "/v2/service_instances/e764af7b-1603-4ba3-b4bf-0b0da98f7ec2", 360 "created_at": "2016-04-16T01:23:58Z", 361 "updated_at": null 362 }, 363 "entity": { 364 "name": "new-name", 365 "credentials": { 366 "creds-key-41": "creds-val-41" 367 }, 368 "service_plan_guid": "7dadd367-5603-4211-8986-d4d99dfab31c", 369 "space_guid": "0de51925-b333-4c84-9abc-115c917e68d5", 370 "gateway_data": null, 371 "dashboard_url": null, 372 "type": "managed_service_instance", 373 "last_operation": { 374 "type": "update", 375 "state": "in progress", 376 "description": "", 377 "updated_at": "2016-04-16T01:23:58Z", 378 "created_at": "2016-04-16T01:23:58Z" 379 }, 380 "tags": [ 381 382 ], 383 "space_url": "/v2/spaces/0de51925-b333-4c84-9abc-115c917e68d5", 384 "service_plan_url": "/v2/service_plans/7dadd367-5603-4211-8986-d4d99dfab31c", 385 "service_bindings_url": "/v2/service_instances/e764af7b-1603-4ba3-b4bf-0b0da98f7ec2/service_bindings", 386 "service_keys_url": "/v2/service_instances/e764af7b-1603-4ba3-b4bf-0b0da98f7ec2/service_keys", 387 "routes_url": "/v2/service_instances/e764af7b-1603-4ba3-b4bf-0b0da98f7ec2/routes" 388 } 389 }`), 390 ), 391 ) 392 }) 393 394 It("Should update ServiceInstance", func() { 395 param := map[string]interface{}{"the_service_broker": "new service broker"} 396 si, err := newServiceInstances(server.URL()).Update("e764af7b-1603-4ba3-b4bf-0b0da98f7ec2", ServiceInstanceUpdateRequest{ 397 Name: helpers.String("new-name"), 398 Params: param, 399 }) 400 401 Expect(err).NotTo(HaveOccurred()) 402 Expect(si).NotTo(BeNil()) 403 Expect(si.Metadata.GUID).To(Equal("e764af7b-1603-4ba3-b4bf-0b0da98f7ec2")) 404 Expect(si.Entity.Name).To(Equal("new-name")) 405 Expect(si.Entity.ServicePlanGUID).To(Equal("7dadd367-5603-4211-8986-d4d99dfab31c")) 406 Expect(si.Entity.SpaceGUID).To(Equal("0de51925-b333-4c84-9abc-115c917e68d5")) 407 }) 408 }) 409 410 Context("When ServiceInstance Update is failed", func() { 411 BeforeEach(func() { 412 server = ghttp.NewServer() 413 server.SetAllowUnhandledRequests(true) 414 server.AppendHandlers( 415 ghttp.CombineHandlers( 416 ghttp.VerifyRequest(http.MethodPut, "/v2/service_instances/e764af7b-1603-4ba3-b4bf-0b0da98f7ec2", "accepts_incomplete=true"), 417 ghttp.VerifyBody([]byte(`{"name":"new-name"}`)), 418 ghttp.RespondWith(http.StatusInternalServerError, `Failed to update`), 419 ), 420 ) 421 }) 422 423 It("Should return error when updated", func() { 424 si, err := newServiceInstances(server.URL()).Update( 425 "e764af7b-1603-4ba3-b4bf-0b0da98f7ec2", ServiceInstanceUpdateRequest{ 426 Name: helpers.String("new-name"), 427 }) 428 429 Expect(err).To(HaveOccurred()) 430 Expect(si).Should(BeNil()) 431 }) 432 }) 433 }) 434 }) 435 436 func newServiceInstances(url string) ServiceInstances { 437 438 sess, err := session.New() 439 if err != nil { 440 log.Fatal(err) 441 } 442 conf := sess.Config.Copy() 443 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 444 conf.Endpoint = &url 445 446 mccpClient := client.Client{ 447 Config: conf, 448 ServiceName: bluemix.MccpService, 449 } 450 451 return newServiceInstanceAPI(&mccpClient) 452 }