github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/space_quota.go (about) 1 package mccpv2 2 3 import ( 4 "encoding/json" 5 "fmt" 6 7 "github.com/IBM-Cloud/bluemix-go/bmxerror" 8 "github.com/IBM-Cloud/bluemix-go/client" 9 "github.com/IBM-Cloud/bluemix-go/rest" 10 ) 11 12 //SpaceQuotaCreateRequest ... 13 type SpaceQuotaCreateRequest struct { 14 Name string `json:"name"` 15 OrgGUID string `json:"organization_guid"` 16 MemoryLimitInMB int64 `json:"memory_limit,omitempty"` 17 InstanceMemoryLimitInMB int64 `json:"instance_memory_limit,omitempty"` 18 RoutesLimit int `json:"total_routes,omitempty"` 19 ServicesLimit int `json:"total_services,omitempty"` 20 NonBasicServicesAllowed bool `json:"non_basic_services_allowed"` 21 } 22 23 //SpaceQuotaUpdateRequest ... 24 type SpaceQuotaUpdateRequest struct { 25 Name string `json:"name"` 26 OrgGUID string `json:"organization_guid,omitempty"` 27 MemoryLimitInMB int64 `json:"memory_limit,omitempty"` 28 InstanceMemoryLimitInMB int64 `json:"instance_memory_limit,omitempty"` 29 RoutesLimit int `json:"total_routes,omitempty"` 30 ServicesLimit int `json:"total_services,omitempty"` 31 NonBasicServicesAllowed bool `json:"non_basic_services_allowed"` 32 } 33 34 type SpaceQuota struct { 35 GUID string 36 Name string 37 NonBasicServicesAllowed bool 38 ServicesLimit int 39 RoutesLimit int 40 MemoryLimitInMB int64 41 InstanceMemoryLimitInMB int64 42 TrialDBAllowed bool 43 AppInstanceLimit int 44 PrivateDomainsLimit int 45 AppTaskLimit int 46 } 47 48 //SpaceQuotaFields ... 49 type SpaceQuotaFields struct { 50 Metadata SpaceQuotaMetadata 51 Entity SpaceQuotaEntity 52 } 53 54 //SpaceQuotaMetadata ... 55 type SpaceQuotaMetadata struct { 56 GUID string `json:"guid"` 57 URL string `json:"url"` 58 } 59 60 //ErrCodeSpaceQuotaDoesnotExist ... 61 const ErrCodeSpaceQuotaDoesnotExist = "SpaceQuotaDoesnotExist" 62 63 type SpaceQuotaResource struct { 64 Resource 65 Entity SpaceQuotaEntity 66 } 67 68 type SpaceQuotaEntity struct { 69 Name string `json:"name"` 70 NonBasicServicesAllowed bool `json:"non_basic_services_allowed"` 71 ServicesLimit int `json:"total_services"` 72 RoutesLimit int `json:"total_routes"` 73 MemoryLimitInMB int64 `json:"memory_limit"` 74 InstanceMemoryLimitInMB int64 `json:"instance_memory_limit"` 75 TrialDBAllowed bool `json:"trial_db_allowed"` 76 AppInstanceLimit json.Number `json:"app_instance_limit"` 77 PrivateDomainsLimit json.Number `json:"total_private_domains"` 78 AppTaskLimit json.Number `json:"app_task_limit"` 79 } 80 81 func (resource SpaceQuotaResource) ToFields() SpaceQuota { 82 entity := resource.Entity 83 84 return SpaceQuota{ 85 GUID: resource.Metadata.GUID, 86 Name: entity.Name, 87 NonBasicServicesAllowed: entity.NonBasicServicesAllowed, 88 ServicesLimit: entity.ServicesLimit, 89 RoutesLimit: entity.RoutesLimit, 90 MemoryLimitInMB: entity.MemoryLimitInMB, 91 InstanceMemoryLimitInMB: entity.InstanceMemoryLimitInMB, 92 TrialDBAllowed: entity.TrialDBAllowed, 93 AppInstanceLimit: NumberToInt(entity.AppInstanceLimit, -1), 94 PrivateDomainsLimit: NumberToInt(entity.PrivateDomainsLimit, -1), 95 AppTaskLimit: NumberToInt(entity.AppTaskLimit, -1), 96 } 97 } 98 99 //SpaceQuotas ... 100 type SpaceQuotas interface { 101 FindByName(name, orgGUID string) (*SpaceQuota, error) 102 Create(createRequest SpaceQuotaCreateRequest) (*SpaceQuotaFields, error) 103 Update(updateRequest SpaceQuotaUpdateRequest, spaceQuotaGUID string) (*SpaceQuotaFields, error) 104 Delete(spaceQuotaGUID string, opts ...bool) error 105 Get(spaceQuotaGUID string) (*SpaceQuotaFields, error) 106 } 107 108 type spaceQuota struct { 109 client *client.Client 110 } 111 112 func newSpaceQuotasAPI(c *client.Client) SpaceQuotas { 113 return &spaceQuota{ 114 client: c, 115 } 116 } 117 118 func (r *spaceQuota) FindByName(name, orgGUID string) (*SpaceQuota, error) { 119 rawURL := fmt.Sprintf("/v2/organizations/%s/space_quota_definitions", orgGUID) 120 req := rest.GetRequest(rawURL) 121 122 httpReq, err := req.Build() 123 if err != nil { 124 return nil, err 125 } 126 path := httpReq.URL.String() 127 128 spaceQuotas, err := r.listSpaceQuotaWithPath(path) 129 if err != nil { 130 return nil, err 131 } 132 133 if len(spaceQuotas) == 0 { 134 return nil, bmxerror.New(ErrCodeSpaceQuotaDoesnotExist, 135 fmt.Sprintf("Given space quota %q doesn't exist for the organization %q", name, orgGUID)) 136 } 137 138 for _, q := range spaceQuotas { 139 if q.Name == name { 140 return &q, nil 141 } 142 143 } 144 return nil, bmxerror.New(ErrCodeSpaceQuotaDoesnotExist, 145 fmt.Sprintf("Given space quota %q doesn't exist for the organization %q", name, orgGUID)) 146 } 147 148 func (r *spaceQuota) listSpaceQuotaWithPath(path string) ([]SpaceQuota, error) { 149 var spaceQuota []SpaceQuota 150 _, err := r.client.GetPaginated(path, NewCCPaginatedResources(SpaceQuotaResource{}), func(resource interface{}) bool { 151 if spaceQuotaResource, ok := resource.(SpaceQuotaResource); ok { 152 spaceQuota = append(spaceQuota, spaceQuotaResource.ToFields()) 153 return true 154 } 155 return false 156 }) 157 return spaceQuota, err 158 } 159 160 func (r *spaceQuota) Create(createRequest SpaceQuotaCreateRequest) (*SpaceQuotaFields, error) { 161 rawURL := "/v2/space_quota_definitions" 162 spaceQuotaFields := SpaceQuotaFields{} 163 _, err := r.client.Post(rawURL, createRequest, &spaceQuotaFields) 164 if err != nil { 165 return nil, err 166 } 167 return &spaceQuotaFields, nil 168 } 169 170 func (r *spaceQuota) Get(spaceQuotaGUID string) (*SpaceQuotaFields, error) { 171 rawURL := fmt.Sprintf("/v2/space_quota_definitions/%s", spaceQuotaGUID) 172 spaceQuotaFields := SpaceQuotaFields{} 173 _, err := r.client.Get(rawURL, &spaceQuotaFields) 174 if err != nil { 175 return nil, err 176 } 177 178 return &spaceQuotaFields, err 179 } 180 181 func (r *spaceQuota) Update(updateRequest SpaceQuotaUpdateRequest, spaceQuotaGUID string) (*SpaceQuotaFields, error) { 182 rawURL := fmt.Sprintf("/v2/space_quota_definitions/%s", spaceQuotaGUID) 183 spaceQuotaFields := SpaceQuotaFields{} 184 _, err := r.client.Put(rawURL, updateRequest, &spaceQuotaFields) 185 if err != nil { 186 return nil, err 187 } 188 return &spaceQuotaFields, nil 189 } 190 191 // opts is list of boolean parametes 192 // opts[0] - async - Will run the delete request in a background job. Recommended: 'true'. Default to 'true'. 193 194 func (r *spaceQuota) Delete(spaceQuotaGUID string, opts ...bool) error { 195 async := true 196 if len(opts) > 0 { 197 async = opts[0] 198 } 199 rawURL := fmt.Sprintf("/v2/space_quota_definitions/%s?async=%t", spaceQuotaGUID, async) 200 _, err := r.client.Delete(rawURL) 201 return err 202 } 203 204 func NumberToInt(number json.Number, defaultValue int) int { 205 if number != "" { 206 i, err := number.Int64() 207 if err == nil { 208 return int(i) 209 } 210 } 211 return defaultValue 212 }