github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/organization_quota_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("OrgQuotas", func() {
    18  	var server *ghttp.Server
    19  	AfterEach(func() {
    20  		server.Close()
    21  	})
    22  
    23  	Describe("Get", func() {
    24  		Context("When read of organization quota is successful", func() {
    25  			BeforeEach(func() {
    26  				server = ghttp.NewServer()
    27  				server.AppendHandlers(
    28  					ghttp.CombineHandlers(
    29  						ghttp.VerifyRequest(http.MethodGet, "/v2/quota_definitions/23d1cc15-c2c4-4641-b6e5-08e5530b8ea8"),
    30  						ghttp.RespondWith(http.StatusOK, `{
    31  							"metadata": {
    32  							  "guid": "23d1cc15-c2c4-4641-b6e5-08e5530b8ea8",
    33  							  "url": "/v2/quota_definitions/23d1cc15-c2c4-4641-b6e5-08e5530b8ea8",
    34  							  "created_at": "2016-04-16T01:23:49Z",
    35  							  "updated_at": null
    36  							},
    37  							"entity": {
    38  							  "name": "gold_quota",
    39  							  "non_basic_services_allowed": true,
    40  							  "total_services": -1,
    41  							  "total_routes": 4,
    42  							  "total_private_domains": -1,
    43  							  "memory_limit": 5120,
    44  							  "trial_db_allowed": false,
    45  							  "instance_memory_limit": 10240,
    46  							  "app_instance_limit": 10,
    47  							  "app_task_limit": 5,
    48  							  "total_service_keys": -1,
    49  							  "total_reserved_route_ports": 3
    50  							}
    51  						  }`),
    52  					),
    53  				)
    54  			})
    55  
    56  			It("should return org quota", func() {
    57  				myorgquota, err := newOrgQuotas(server.URL()).Get("23d1cc15-c2c4-4641-b6e5-08e5530b8ea8")
    58  				Expect(err).NotTo(HaveOccurred())
    59  				Expect(myorgquota).ShouldNot(BeNil())
    60  				Expect(myorgquota.Metadata.GUID).Should(Equal("23d1cc15-c2c4-4641-b6e5-08e5530b8ea8"))
    61  				Expect(myorgquota.Entity.Name).Should(Equal("gold_quota"))
    62  				Expect(myorgquota.Entity.NonBasicServicesAllowed).To(BeTrue())
    63  			})
    64  		})
    65  		Context("When org quota retrieve is failed", func() {
    66  			BeforeEach(func() {
    67  				server = ghttp.NewServer()
    68  				server.SetAllowUnhandledRequests(true)
    69  				server.AppendHandlers(
    70  					ghttp.CombineHandlers(
    71  						ghttp.VerifyRequest(http.MethodGet, "/v2/quota_definitions/be829072-3137-418c-9607-c84e7d77e22a"),
    72  						ghttp.RespondWith(http.StatusInternalServerError, `Failed to retrieve org quota`),
    73  					),
    74  				)
    75  			})
    76  
    77  			It("should return error when org quota is retrieved", func() {
    78  				myorgquota, err := newOrgQuotas(server.URL()).Get("be829072-3137-418c-9607-c84e7d77e22a")
    79  				Expect(err).To(HaveOccurred())
    80  				Expect(myorgquota).Should(BeNil())
    81  			})
    82  		})
    83  	})
    84  
    85  })
    86  
    87  var _ = Describe("Org Quota by Name", func() {
    88  	var server *ghttp.Server
    89  	AfterEach(func() {
    90  		server.Close()
    91  	})
    92  	Describe("FindByName()", func() {
    93  		Context("Server return org quota by name", func() {
    94  			BeforeEach(func() {
    95  				server = ghttp.NewServer()
    96  				server.AppendHandlers(
    97  					ghttp.CombineHandlers(
    98  						ghttp.VerifyRequest(http.MethodGet, "/v2/quota_definitions", "q=name:testorgquotaupdate"),
    99  						ghttp.RespondWith(http.StatusOK, `{
   100  							"total_results": 1,
   101  							"total_pages": 1,
   102  							"prev_url": null,
   103  							"next_url": null,
   104  							"resources": [
   105  							  {
   106  								"metadata": {
   107  								  "guid": "be829072-3137-418c-9607-c84e7d77e22a",
   108  								  "url": "/v2/quota_definitions/be829072-3137-418c-9607-c84e7d77e22a",
   109  								  "created_at": "2014-05-30T19:07:52Z",
   110  								  "updated_at": "2017-11-10T03:32:50Z"
   111  								},
   112  								"entity": {
   113  								  "name": "testorgquotaupdate",
   114  								  "non_basic_services_allowed": false,
   115  								  "total_services": 10,
   116  								  "total_routes": 500,
   117  								  "total_private_domains": -1,
   118  								  "memory_limit": 2048,
   119  								  "trial_db_allowed": false,
   120  								  "instance_memory_limit": 2048,
   121  								  "app_instance_limit": -1,
   122  								  "app_task_limit": -1,
   123  								  "total_service_keys": -1,
   124  								  "total_reserved_route_ports": 0
   125  								}
   126  							  }
   127  							]
   128  						  }`),
   129  					),
   130  				)
   131  			})
   132  
   133  			It("should return org quota by name", func() {
   134  				myorgquota, err := newOrgQuotas(server.URL()).FindByName("testorgquotaupdate")
   135  				Expect(err).NotTo(HaveOccurred())
   136  				Expect(myorgquota).ShouldNot(BeNil())
   137  				Expect(myorgquota.GUID).Should(Equal("be829072-3137-418c-9607-c84e7d77e22a"))
   138  				Expect(myorgquota.Name).Should(Equal("testorgquotaupdate"))
   139  				Expect(myorgquota.NonBasicServicesAllowed).To(BeFalse())
   140  			})
   141  
   142  		})
   143  
   144  		Context("Server return no org quota by name", func() {
   145  			BeforeEach(func() {
   146  				server = ghttp.NewServer()
   147  				server.AppendHandlers(
   148  					ghttp.CombineHandlers(
   149  						ghttp.VerifyRequest(http.MethodGet, "/v2/quota_definitions", "q=name:testorgquotaupdate"),
   150  						ghttp.RespondWith(http.StatusOK, `{
   151  															
   152  						}`),
   153  					),
   154  				)
   155  			})
   156  
   157  			It("should return no org quota", func() {
   158  				myorgquota, err := newOrgQuotas(server.URL()).FindByName("testorgquotaupdate")
   159  				Expect(err).To(HaveOccurred())
   160  				Expect(myorgquota).To(BeNil())
   161  			})
   162  
   163  		})
   164  		Context("Server return error", func() {
   165  			BeforeEach(func() {
   166  				server = ghttp.NewServer()
   167  				server.SetAllowUnhandledRequests(true)
   168  				server.AppendHandlers(
   169  					ghttp.CombineHandlers(
   170  						ghttp.VerifyRequest(http.MethodGet, "/v2/quota_definitions", "q=name:testorgquotaupdate"),
   171  						ghttp.RespondWith(http.StatusInternalServerError, `{
   172  															
   173  						}`),
   174  					),
   175  				)
   176  			})
   177  
   178  			It("should return no orgs", func() {
   179  				myorgquota, err := newOrgQuotas(server.URL()).FindByName("testorgquotaupdate")
   180  				Expect(err).To(HaveOccurred())
   181  				Expect(myorgquota).To(BeNil())
   182  			})
   183  
   184  		})
   185  
   186  	})
   187  })
   188  
   189  func newOrgQuotas(url string) OrgQuotas {
   190  	sess, err := session.New()
   191  	if err != nil {
   192  		log.Fatal(err)
   193  	}
   194  	conf := sess.Config.Copy()
   195  	conf.HTTPClient = bluemixHttp.NewHTTPClient(conf)
   196  	conf.Endpoint = &url
   197  
   198  	client := client.Client{
   199  		Config:      conf,
   200  		ServiceName: bluemix.MccpService,
   201  	}
   202  
   203  	return newOrgQuotasAPI(&client)
   204  }