github.com/IBM-Cloud/bluemix-go@v0.0.0-20240423071914-9e96525baef4/api/mccp/mccpv2/organization_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  //OrgQuota ...
    13  type OrgQuota struct {
    14  	GUID                    string
    15  	Name                    string
    16  	NonBasicServicesAllowed bool
    17  	ServicesLimit           int
    18  	RoutesLimit             int
    19  	MemoryLimitInMB         int64
    20  	InstanceMemoryLimitInMB int64
    21  	TrialDBAllowed          bool
    22  	AppInstanceLimit        int
    23  	PrivateDomainsLimit     int
    24  	AppTasksLimit           int
    25  	ServiceKeysLimit        int
    26  	RoutePortsLimit         int
    27  }
    28  
    29  //OrgQuotaFields ...
    30  type OrgQuotaFields struct {
    31  	Metadata OrgQuotaMetadata
    32  	Entity   OrgQuotaEntity
    33  }
    34  
    35  //OrgQuotaMetadata ...
    36  type OrgQuotaMetadata struct {
    37  	GUID string `json:"guid"`
    38  	URL  string `json:"url"`
    39  }
    40  
    41  //ErrCodeOrgQuotaDoesnotExist ...
    42  const ErrCodeOrgQuotaDoesnotExist = "OrgQuotaDoesnotExist"
    43  
    44  //OrgQuotaResource ...
    45  type OrgQuotaResource struct {
    46  	Resource
    47  	Entity OrgQuotaEntity
    48  }
    49  
    50  //OrgQuotaEntity ...
    51  type OrgQuotaEntity struct {
    52  	Name                    string      `json:"name"`
    53  	NonBasicServicesAllowed bool        `json:"non_basic_services_allowed"`
    54  	ServicesLimit           int         `json:"total_services"`
    55  	RoutesLimit             int         `json:"total_routes"`
    56  	MemoryLimitInMB         int64       `json:"memory_limit"`
    57  	InstanceMemoryLimitInMB int64       `json:"instance_memory_limit"`
    58  	TrialDBAllowed          bool        `json:"trial_db_allowed"`
    59  	AppInstanceLimit        json.Number `json:"app_instance_limit"`
    60  	PrivateDomainsLimit     json.Number `json:"total_private_domains"`
    61  	AppTasksLimit           json.Number `json:"app_tasks_limit"`
    62  	ServiceKeysLimit        json.Number `json:"total_service_keys"`
    63  	RoutePortsLimit         int         `json:"total_reserved_route_ports"`
    64  }
    65  
    66  //ToFields ...
    67  func (resource OrgQuotaResource) ToFields() OrgQuota {
    68  	entity := resource.Entity
    69  
    70  	return OrgQuota{
    71  		GUID:                    resource.Metadata.GUID,
    72  		Name:                    entity.Name,
    73  		NonBasicServicesAllowed: entity.NonBasicServicesAllowed,
    74  		ServicesLimit:           entity.ServicesLimit,
    75  		RoutesLimit:             entity.RoutesLimit,
    76  		MemoryLimitInMB:         entity.MemoryLimitInMB,
    77  		InstanceMemoryLimitInMB: entity.InstanceMemoryLimitInMB,
    78  		TrialDBAllowed:          entity.TrialDBAllowed,
    79  		AppInstanceLimit:        NumberToInt(entity.AppInstanceLimit, -1),
    80  		PrivateDomainsLimit:     NumberToInt(entity.PrivateDomainsLimit, -1),
    81  		AppTasksLimit:           NumberToInt(entity.AppTasksLimit, -1),
    82  		ServiceKeysLimit:        NumberToInt(entity.ServiceKeysLimit, -1),
    83  		RoutePortsLimit:         entity.RoutePortsLimit,
    84  	}
    85  }
    86  
    87  //OrgQuotas ...
    88  type OrgQuotas interface {
    89  	FindByName(name string) (*OrgQuota, error)
    90  	Get(orgQuotaGUID string) (*OrgQuotaFields, error)
    91  	List() ([]OrgQuota, error)
    92  }
    93  
    94  type orgQuota struct {
    95  	client *client.Client
    96  }
    97  
    98  func newOrgQuotasAPI(c *client.Client) OrgQuotas {
    99  	return &orgQuota{
   100  		client: c,
   101  	}
   102  }
   103  
   104  func (r *orgQuota) FindByName(name string) (*OrgQuota, error) {
   105  	rawURL := fmt.Sprintf("/v2/quota_definitions")
   106  	req := rest.GetRequest(rawURL).Query("q", "name:"+name)
   107  
   108  	httpReq, err := req.Build()
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  	path := httpReq.URL.String()
   113  
   114  	orgQuotas, err := r.listOrgQuotaWithPath(path)
   115  	if err != nil {
   116  		return nil, err
   117  	}
   118  
   119  	if len(orgQuotas) == 0 {
   120  		return nil, bmxerror.New(ErrCodeAppDoesnotExist,
   121  			fmt.Sprintf("Given quota definition: %q doesn't exist", name))
   122  
   123  	}
   124  	return &orgQuotas[0], nil
   125  }
   126  
   127  func (r *orgQuota) List() ([]OrgQuota, error) {
   128  	rawURL := fmt.Sprintf("/v2/quota_definitions")
   129  	req := rest.GetRequest(rawURL)
   130  
   131  	httpReq, err := req.Build()
   132  	if err != nil {
   133  		return nil, err
   134  	}
   135  	path := httpReq.URL.String()
   136  
   137  	orgQuotas, err := r.listOrgQuotaWithPath(path)
   138  	if err != nil {
   139  		return nil, err
   140  	}
   141  
   142  	return orgQuotas, nil
   143  }
   144  
   145  func (r *orgQuota) listOrgQuotaWithPath(path string) ([]OrgQuota, error) {
   146  	var orgQuota []OrgQuota
   147  	_, err := r.client.GetPaginated(path, NewCCPaginatedResources(OrgQuotaResource{}), func(resource interface{}) bool {
   148  		if orgQuotaResource, ok := resource.(OrgQuotaResource); ok {
   149  			orgQuota = append(orgQuota, orgQuotaResource.ToFields())
   150  			return true
   151  		}
   152  		return false
   153  	})
   154  	return orgQuota, err
   155  }
   156  
   157  func (r *orgQuota) Get(quotaGUID string) (*OrgQuotaFields, error) {
   158  	rawURL := fmt.Sprintf("/v2/quota_definitions/%s", quotaGUID)
   159  	orgQuotaFields := OrgQuotaFields{}
   160  	_, err := r.client.Get(rawURL, &orgQuotaFields)
   161  	if err != nil {
   162  		return nil, err
   163  	}
   164  
   165  	return &orgQuotaFields, err
   166  }