github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/space_qutoa_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 bluemixHttp "github.com/IBM-Cloud/bluemix-go/http" 10 "github.com/IBM-Cloud/bluemix-go/session" 11 12 . "github.com/onsi/ginkgo" 13 . "github.com/onsi/gomega" 14 "github.com/onsi/gomega/ghttp" 15 ) 16 17 var _ = Describe("SpaceQuotas", func() { 18 var server *ghttp.Server 19 AfterEach(func() { 20 server.Close() 21 }) 22 23 Describe("Create", func() { 24 Context("When creation is successful", func() { 25 BeforeEach(func() { 26 server = ghttp.NewServer() 27 server.AppendHandlers( 28 ghttp.CombineHandlers( 29 ghttp.VerifyRequest(http.MethodPost, "/v2/space_quota_definitions"), 30 ghttp.VerifyBody([]byte(`{"name":"test-quota","organization_guid":"3c1b6f9d-ffe5-43b5-ab91-7be2331dc546","memory_limit":1024,"instance_memory_limit":1024,"total_routes":50,"total_services":150,"non_basic_services_allowed":false}`)), 31 ghttp.RespondWith(http.StatusCreated, `{ 32 33 "metadata": { 34 "guid": "be829072-3137-418c-9607-c84e7d77e22a", 35 "url": "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a", 36 "created_at": "2017-05-04T04:31:19Z", 37 "updated_at": null 38 }, 39 "entity": { 40 "name": "test-quota", 41 "organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 42 "non_basic_services_allowed": false, 43 "total_services": 150, 44 "total_routes": 50, 45 "memory_limit": 1024, 46 "instance_memory_limit": 1024, 47 "app_instance_limit": -1, 48 "app_task_limit": 5, 49 "total_service_keys": -1, 50 "organization_url": "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 51 "spaces_url": "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a/spaces" 52 } 53 54 }`), 55 ), 56 ) 57 }) 58 59 It("should return SpaceQuota created", func() { 60 payload := SpaceQuotaCreateRequest{ 61 Name: "test-quota", 62 OrgGUID: "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 63 MemoryLimitInMB: 1024, 64 InstanceMemoryLimitInMB: 1024, 65 RoutesLimit: 50, 66 ServicesLimit: 150, 67 NonBasicServicesAllowed: false, 68 } 69 myspacequota, err := newSpaceQuotas(server.URL()).Create(payload) 70 Expect(err).NotTo(HaveOccurred()) 71 Expect(myspacequota).ShouldNot(BeNil()) 72 Expect(myspacequota.Metadata.GUID).Should(Equal("be829072-3137-418c-9607-c84e7d77e22a")) 73 Expect(myspacequota.Entity.Name).Should(Equal("test-quota")) 74 Expect(myspacequota.Entity.NonBasicServicesAllowed).To(BeFalse()) 75 }) 76 }) 77 Context("When creation is failed", func() { 78 BeforeEach(func() { 79 server = ghttp.NewServer() 80 server.SetAllowUnhandledRequests(true) 81 server.AppendHandlers( 82 ghttp.CombineHandlers( 83 ghttp.VerifyRequest(http.MethodPost, "/v2/space_quota_definitions"), 84 ghttp.VerifyBody([]byte(`{"name":"test-quota","organization_guid":"3c1b6f9d-ffe5-43b5-ab91-7be2331dc546","memory_limit":1024,"instance_memory_limit":1024,"total_routes":50,"total_services":150,"non_basic_services_allowed":false}`)), 85 ghttp.RespondWith(http.StatusInternalServerError, `Failed to create space quota`), 86 ), 87 ) 88 }) 89 90 It("should return error during spacequota creation", func() { 91 payload := SpaceQuotaCreateRequest{ 92 Name: "test-quota", 93 OrgGUID: "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 94 MemoryLimitInMB: 1024, 95 InstanceMemoryLimitInMB: 1024, 96 RoutesLimit: 50, 97 ServicesLimit: 150, 98 NonBasicServicesAllowed: false, 99 } 100 myspacequota, err := newSpaceQuotas(server.URL()).Create(payload) 101 Expect(err).To(HaveOccurred()) 102 Expect(myspacequota).Should(BeNil()) 103 }) 104 }) 105 106 }) 107 108 Describe("Get", func() { 109 Context("When read of space quota is successful", func() { 110 BeforeEach(func() { 111 server = ghttp.NewServer() 112 server.AppendHandlers( 113 ghttp.CombineHandlers( 114 ghttp.VerifyRequest(http.MethodGet, "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a"), 115 ghttp.RespondWith(http.StatusOK, `{ 116 "metadata": { 117 "guid": "be829072-3137-418c-9607-c84e7d77e22a", 118 "url": "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a", 119 "created_at": "2017-05-04T04:31:19Z", 120 "updated_at": null 121 }, 122 "entity": { 123 "name": "test-quota", 124 "organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 125 "non_basic_services_allowed": false, 126 "total_services": 150, 127 "total_routes": 50, 128 "memory_limit": 1024, 129 "instance_memory_limit": 1024, 130 "app_instance_limit": -1, 131 "app_task_limit": 5, 132 "total_service_keys": -1, 133 "organization_url": "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 134 "spaces_url": "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a/spaces" 135 } 136 }`), 137 ), 138 ) 139 }) 140 141 It("should return space quota", func() { 142 myspacequota, err := newSpaceQuotas(server.URL()).Get("be829072-3137-418c-9607-c84e7d77e22a") 143 Expect(err).NotTo(HaveOccurred()) 144 Expect(myspacequota).ShouldNot(BeNil()) 145 Expect(myspacequota.Metadata.GUID).Should(Equal("be829072-3137-418c-9607-c84e7d77e22a")) 146 Expect(myspacequota.Entity.Name).Should(Equal("test-quota")) 147 Expect(myspacequota.Entity.NonBasicServicesAllowed).To(BeFalse()) 148 }) 149 }) 150 Context("When space quota retrieve is failed", func() { 151 BeforeEach(func() { 152 server = ghttp.NewServer() 153 server.SetAllowUnhandledRequests(true) 154 server.AppendHandlers( 155 ghttp.CombineHandlers( 156 ghttp.VerifyRequest(http.MethodGet, "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a"), 157 ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve space quota`), 158 ), 159 ) 160 }) 161 162 It("should return error when space quota is retrieved", func() { 163 myspacequota, err := newSpaceQuotas(server.URL()).Get("be829072-3137-418c-9607-c84e7d77e22a") 164 Expect(err).To(HaveOccurred()) 165 Expect(myspacequota).Should(BeNil()) 166 }) 167 }) 168 }) 169 Describe("Update", func() { 170 Context("When update of space quota is successful", func() { 171 BeforeEach(func() { 172 server = ghttp.NewServer() 173 server.AppendHandlers( 174 ghttp.CombineHandlers( 175 ghttp.VerifyRequest(http.MethodPut, "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a"), 176 ghttp.VerifyBody([]byte(`{"name":"testspacequotaupdate","organization_guid":"3c1b6f9d-ffe5-43b5-ab91-7be2331dc546","memory_limit":1024,"instance_memory_limit":1024,"total_routes":50,"total_services":150,"non_basic_services_allowed":false}`)), 177 ghttp.RespondWith(http.StatusCreated, `{ 178 "metadata": { 179 "guid": "be829072-3137-418c-9607-c84e7d77e22a", 180 "url": "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a", 181 "created_at": "2017-05-04T04:31:19Z", 182 "updated_at": null 183 }, 184 "entity": { 185 "name": "testspacequotaupdate", 186 "organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 187 "non_basic_services_allowed": false, 188 "total_services": 150, 189 "total_routes": 50, 190 "memory_limit": 1024, 191 "instance_memory_limit": 1024, 192 "app_instance_limit": -1, 193 "app_task_limit": 5, 194 "total_service_keys": -1, 195 "organization_url": "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 196 "spaces_url": "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a/spaces" 197 } 198 }`), 199 ), 200 ) 201 }) 202 203 It("should return spacequota update", func() { 204 payload := SpaceQuotaUpdateRequest{ 205 Name: "testspacequotaupdate", 206 OrgGUID: "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 207 MemoryLimitInMB: 1024, 208 InstanceMemoryLimitInMB: 1024, 209 RoutesLimit: 50, 210 ServicesLimit: 150, 211 NonBasicServicesAllowed: false, 212 } 213 myspacequota, err := newSpaceQuotas(server.URL()).Update(payload, "be829072-3137-418c-9607-c84e7d77e22a") 214 Expect(err).NotTo(HaveOccurred()) 215 Expect(myspacequota).ShouldNot(BeNil()) 216 Expect(myspacequota.Metadata.GUID).Should(Equal("be829072-3137-418c-9607-c84e7d77e22a")) 217 Expect(myspacequota.Entity.Name).Should(Equal("testspacequotaupdate")) 218 Expect(myspacequota.Entity.NonBasicServicesAllowed).To(BeFalse()) 219 }) 220 }) 221 Context("When spacequota update is failed", func() { 222 BeforeEach(func() { 223 server = ghttp.NewServer() 224 server.SetAllowUnhandledRequests(true) 225 server.AppendHandlers( 226 ghttp.CombineHandlers( 227 ghttp.VerifyRequest(http.MethodPut, "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a"), 228 ghttp.RespondWith(http.StatusInternalServerError, `Failed to update space quota`), 229 ), 230 ) 231 }) 232 233 It("should return error spacequota updated", func() { 234 payload := SpaceQuotaUpdateRequest{ 235 Name: "test-quota", 236 OrgGUID: "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 237 MemoryLimitInMB: 1024, 238 InstanceMemoryLimitInMB: 1024, 239 RoutesLimit: 50, 240 ServicesLimit: 150, 241 NonBasicServicesAllowed: false, 242 } 243 myspacequota, err := newSpaceQuotas(server.URL()).Update(payload, "be829072-3137-418c-9607-c84e7d77e22a") 244 Expect(err).To(HaveOccurred()) 245 Expect(myspacequota).Should(BeNil()) 246 }) 247 }) 248 }) 249 250 Describe("Delete", func() { 251 Context("When delete of space quota is successful", func() { 252 BeforeEach(func() { 253 server = ghttp.NewServer() 254 server.AppendHandlers( 255 ghttp.CombineHandlers( 256 ghttp.VerifyRequest(http.MethodDelete, "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a"), 257 ghttp.RespondWith(http.StatusOK, `{ 258 }`), 259 ), 260 ) 261 }) 262 263 It("should delete Space quota", func() { 264 err := newSpaceQuotas(server.URL()).Delete("be829072-3137-418c-9607-c84e7d77e22a") 265 Expect(err).NotTo(HaveOccurred()) 266 }) 267 }) 268 Context("When space quota delete is failed", func() { 269 BeforeEach(func() { 270 server = ghttp.NewServer() 271 server.SetAllowUnhandledRequests(true) 272 server.AppendHandlers( 273 ghttp.CombineHandlers( 274 ghttp.VerifyRequest(http.MethodDelete, "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a"), 275 ghttp.RespondWith(http.StatusInternalServerError, `Failed to delete space quota`), 276 ), 277 ) 278 }) 279 280 It("should return error space quota delete", func() { 281 err := newSpaceQuotas(server.URL()).Delete("be829072-3137-418c-9607-c84e7d77e22a") 282 Expect(err).To(HaveOccurred()) 283 }) 284 }) 285 }) 286 287 }) 288 289 var _ = Describe("Space Quota by Name", func() { 290 var server *ghttp.Server 291 AfterEach(func() { 292 server.Close() 293 }) 294 Describe("FindByNameInOrg()", func() { 295 Context("Server return space quota by name", func() { 296 BeforeEach(func() { 297 server = ghttp.NewServer() 298 server.AppendHandlers( 299 ghttp.CombineHandlers( 300 ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546/space_quota_definitions"), 301 ghttp.RespondWith(http.StatusOK, `{ 302 "total_results": 2, 303 "total_pages": 1, 304 "prev_url": null, 305 "next_url": null, 306 "resources": [ 307 { 308 "metadata": { 309 "guid": "1d11ba0f-aa87-417c-b390-2e4d035279d6", 310 "url": "/v2/space_quota_definitions/1d11ba0f-aa87-417c-b390-2e4d035279d6", 311 "created_at": "2017-05-04T06:27:51Z", 312 "updated_at": null 313 314 }, 315 "entity": { 316 "name": "new", 317 "organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 318 "non_basic_services_allowed": false, 319 "total_services": 0, 320 "total_routes": 0, 321 "memory_limit": 0, 322 "instance_memory_limit": -1, 323 "app_instance_limit": -1, 324 "app_task_limit": 5, 325 "total_service_keys": -1, 326 "organization_url": "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 327 "spaces_url": "/v2/space_quota_definitions/1d11ba0f-aa87-417c-b390-2e4d035279d6/spaces" 328 } 329 330 }, 331 { 332 "metadata": { 333 "guid": "be829072-3137-418c-9607-c84e7d77e22a", 334 "url": "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a", 335 "created_at": "2017-05-04T06:45:46Z", 336 "updated_at": "2017-05-04T06:45:46Z" 337 338 }, 339 "entity": { 340 "name": "testspacequotaupdate", 341 "organization_guid": "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 342 "non_basic_services_allowed": false, 343 "total_services": 150, 344 "total_routes": 50, 345 "memory_limit": 1024, 346 "instance_memory_limit": 1024, 347 "app_instance_limit": -1, 348 "app_task_limit": 5, 349 "total_service_keys": -1, 350 "organization_url": "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546", 351 "spaces_url": "/v2/space_quota_definitions/be829072-3137-418c-9607-c84e7d77e22a/spaces" 352 353 } 354 } 355 ] 356 357 }`), 358 ), 359 ) 360 }) 361 362 It("should return space quota by name", func() { 363 myspacequota, err := newSpaceQuotas(server.URL()).FindByName("testspacequotaupdate", "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546") 364 Expect(err).NotTo(HaveOccurred()) 365 Expect(myspacequota).ShouldNot(BeNil()) 366 Expect(myspacequota.GUID).Should(Equal("be829072-3137-418c-9607-c84e7d77e22a")) 367 Expect(myspacequota.Name).Should(Equal("testspacequotaupdate")) 368 Expect(myspacequota.NonBasicServicesAllowed).To(BeFalse()) 369 }) 370 371 }) 372 373 Context("Server return no spacequota by name", func() { 374 BeforeEach(func() { 375 server = ghttp.NewServer() 376 server.AppendHandlers( 377 ghttp.CombineHandlers( 378 ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546/space_quota_definitions"), 379 ghttp.RespondWith(http.StatusOK, `{ 380 "total_results": 0, 381 "resources": [ 382 ] 383 384 }`), 385 ), 386 ) 387 }) 388 389 It("should return no space quota", func() { 390 myspacequota, err := newSpaceQuotas(server.URL()).FindByName("testspacequotaupdate", "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546") 391 Expect(err).To(HaveOccurred()) 392 Expect(myspacequota).To(BeNil()) 393 }) 394 395 }) 396 Context("Server return error", func() { 397 BeforeEach(func() { 398 server = ghttp.NewServer() 399 server.SetAllowUnhandledRequests(true) 400 server.AppendHandlers( 401 ghttp.CombineHandlers( 402 ghttp.VerifyRequest(http.MethodGet, "/v2/organizations/3c1b6f9d-ffe5-43b5-ab91-7be2331dc546/space_quota_definitions"), 403 ghttp.RespondWith(http.StatusInternalServerError, `{ 404 405 }`), 406 ), 407 ) 408 }) 409 410 It("should return no spaces", func() { 411 myspacequota, err := newSpaceQuotas(server.URL()).FindByName("testspacequotaupdate", "3c1b6f9d-ffe5-43b5-ab91-7be2331dc546") 412 Expect(err).To(HaveOccurred()) 413 Expect(myspacequota).To(BeNil()) 414 }) 415 416 }) 417 418 }) 419 }) 420 421 func newSpaceQuotas(url string) SpaceQuotas { 422 sess, err := session.New() 423 if err != nil { 424 log.Fatal(err) 425 } 426 conf := sess.Config.Copy() 427 conf.HTTPClient = bluemixHttp.NewHTTPClient(conf) 428 conf.Endpoint = &url 429 430 client := client.Client{ 431 Config: conf, 432 ServiceName: bluemix.MccpService, 433 } 434 435 return newSpaceQuotasAPI(&client) 436 }