github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv2/quota.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     7  	"code.cloudfoundry.org/cli/types"
     8  	"encoding/json"
     9  )
    10  
    11  // Quota is the generic definition of a quota.
    12  type Quota struct {
    13  	// GUID is the unique OrganizationQuota identifier.
    14  	GUID string
    15  
    16  	// Name is the name of the OrganizationQuota.
    17  	Name string
    18  
    19  	//  If an organization can have services that are not free
    20  	NonBasicServicesAllowed bool
    21  
    22  	// How many services an organization can have. (-1 represents an unlimited amount)
    23  	TotalServices int
    24  
    25  	// How many service keys an organization can have. (-1 represents an unlimited amount)
    26  	TotalServiceKeys types.NullInt
    27  
    28  	// How many routes an organization can have. (-1 represents an unlimited amount)
    29  	TotalRoutes int
    30  
    31  	// How many routes an organization can have that use a reserved port.
    32  	// These routes count toward total_routes. (-1 represents an unlimited amount)
    33  	TotalReservedRoutePorts types.NullInt
    34  
    35  	// How many private domains an organization can have. (-1 represents an unlimited amount)
    36  	TotalPrivateDomains types.NullInt
    37  
    38  	// How much memory in megabyte an organization can have.
    39  	MemoryLimit types.NullByteSizeInMb
    40  
    41  	// The maximum amount of memory in megabyte an application instance can have. (-1 represents an unlimited amount)
    42  	InstanceMemoryLimit types.NullByteSizeInMb
    43  
    44  	// How many app instances an organization can create. (-1 represents an unlimited amount)
    45  	AppInstanceLimit types.NullInt
    46  
    47  	// The number of tasks that can be run per app. (-1 represents an unlimited amount)
    48  	AppTaskLimit types.NullInt
    49  
    50  	// The owning organization of the space quota
    51  	OrganizationGUID string
    52  }
    53  
    54  func (q *Quota) UnmarshalJSON(data []byte) error {
    55  	var ccQ struct {
    56  		Metadata internal.Metadata `json:"metadata"`
    57  		Entity   struct {
    58  			Name                    string `json:"name"`
    59  			NonBasicServicesAllowed bool   `json:"non_basic_services_allowed"`
    60  			TotalServices           int    `json:"total_services"`
    61  			TotalServiceKeys        *int   `json:"total_service_keys,omitempty"`
    62  			TotalRoutes             int    `json:"total_routes"`
    63  			TotalReservedRoutePorts *int   `json:"total_reserved_route_ports,omitempty"`
    64  			TotalPrivateDomains     *int   `json:"total_private_domains,omitempty"`
    65  			MemoryLimit             int64  `json:"memory_limit"`
    66  			InstanceMemoryLimit     int64  `json:"instance_memory_limit"`
    67  			AppInstanceLimit        *int   `json:"app_instance_limit"`
    68  			AppTaskLimit            *int   `json:"app_task_limit"`
    69  			OrganizationGUID        string `json:"organization_guid,omitempty"`
    70  		} `json:"entity"`
    71  	}
    72  	err := cloudcontroller.DecodeJSON(data, &ccQ)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	q.GUID = ccQ.Metadata.GUID
    78  	q.Name = ccQ.Entity.Name
    79  	q.NonBasicServicesAllowed = ccQ.Entity.NonBasicServicesAllowed
    80  	q.TotalServices = ccQ.Entity.TotalServices
    81  	q.TotalServiceKeys.ParseIntValue(ccQ.Entity.TotalServiceKeys)
    82  	q.TotalRoutes = ccQ.Entity.TotalRoutes
    83  	q.TotalReservedRoutePorts.ParseIntValue(ccQ.Entity.TotalReservedRoutePorts)
    84  	q.TotalPrivateDomains.ParseIntValue(ccQ.Entity.TotalPrivateDomains)
    85  	if ccQ.Entity.MemoryLimit > 0 {
    86  		toUint := uint64(ccQ.Entity.MemoryLimit)
    87  		q.MemoryLimit.ParseUint64Value(&toUint)
    88  	}
    89  	if ccQ.Entity.InstanceMemoryLimit > 0 {
    90  		toUint := uint64(ccQ.Entity.InstanceMemoryLimit)
    91  		q.InstanceMemoryLimit.ParseUint64Value(&toUint)
    92  	}
    93  	q.AppInstanceLimit.ParseIntValue(ccQ.Entity.AppInstanceLimit)
    94  	q.AppTaskLimit.ParseIntValue(ccQ.Entity.AppTaskLimit)
    95  	q.OrganizationGUID = ccQ.Entity.OrganizationGUID
    96  
    97  	return nil
    98  }
    99  
   100  func (q Quota) MarshalJSON() ([]byte, error) {
   101  	ccQ := struct {
   102  		Name                    string `json:"name,omitempty"`
   103  		NonBasicServicesAllowed bool   `json:"non_basic_services_allowed"`
   104  		TotalServices           int    `json:"total_services,omitempty"`
   105  		TotalServiceKeys        *int   `json:"total_service_keys,omitempty"`
   106  		TotalRoutes             int    `json:"total_routes,omitempty"`
   107  		TotalReservedRoutePorts *int   `json:"total_reserved_route_ports,omitempty"`
   108  		TotalPrivateDomains     int    `json:"total_private_domains,omitempty"`
   109  		MemoryLimit             int64  `json:"memory_limit,omitempty"`
   110  		InstanceMemoryLimit     int64  `json:"instance_memory_limit,omitempty"`
   111  		AppInstanceLimit        *int   `json:"app_instance_limit,omitempty"`
   112  		AppTaskLimit            *int   `json:"app_task_limit,omitempty"`
   113  		OrganizationGUID        string `json:"organization_guid,omitempty"`
   114  	}{
   115  		Name:                    q.Name,
   116  		NonBasicServicesAllowed: q.NonBasicServicesAllowed,
   117  		TotalServices:           q.TotalServices,
   118  		TotalRoutes:             q.TotalRoutes,
   119  		OrganizationGUID:        q.OrganizationGUID,
   120  	}
   121  
   122  	if !q.MemoryLimit.IsSet {
   123  		ccQ.MemoryLimit = -1
   124  	} else {
   125  		ccQ.MemoryLimit = int64(q.MemoryLimit.Value)
   126  	}
   127  
   128  	if !q.InstanceMemoryLimit.IsSet {
   129  		ccQ.InstanceMemoryLimit = -1
   130  	} else {
   131  		ccQ.InstanceMemoryLimit = int64(q.InstanceMemoryLimit.Value)
   132  	}
   133  
   134  	if q.TotalServiceKeys.IsSet {
   135  		ccQ.TotalServiceKeys = &q.TotalServiceKeys.Value
   136  	}
   137  
   138  	if q.TotalReservedRoutePorts.IsSet {
   139  		ccQ.TotalReservedRoutePorts = &q.TotalReservedRoutePorts.Value
   140  	}
   141  
   142  	if !q.TotalPrivateDomains.IsSet {
   143  		ccQ.TotalPrivateDomains = -1
   144  	} else {
   145  		ccQ.TotalPrivateDomains = q.TotalPrivateDomains.Value
   146  	}
   147  
   148  	if q.AppInstanceLimit.IsSet {
   149  		ccQ.AppInstanceLimit = &q.AppInstanceLimit.Value
   150  	}
   151  
   152  	if q.AppTaskLimit.IsSet {
   153  		ccQ.AppTaskLimit = &q.AppTaskLimit.Value
   154  	}
   155  
   156  	return json.Marshal(ccQ)
   157  }
   158  
   159  // CreateQuota returns an Quota associated with the
   160  // provided quota and for type either SpaceQuota or OrgQuota.
   161  func (client *Client) CreateQuota(quotaType constant.QuotaType, quota Quota) (Quota, Warnings, error) {
   162  	if quotaType == constant.SpaceQuota {
   163  		return client.CreateSpaceQuotaDefinition(quota)
   164  	}
   165  	return client.CreateOrganizationQuota(quota)
   166  }
   167  
   168  // GetQuota returns an Quota associated with the
   169  // provided GUID and for type either SpaceQuota or OrgQuota.
   170  func (client *Client) GetQuota(quotaType constant.QuotaType, guid string) (Quota, Warnings, error) {
   171  	if quotaType == constant.SpaceQuota {
   172  		return client.GetSpaceQuotaDefinition(guid)
   173  	}
   174  	return client.GetOrganizationQuota(guid)
   175  }
   176  
   177  // GetQuotas returns an Quota list associated with the
   178  // provided filters and for type either SpaceQuota or OrgQuota.
   179  func (client *Client) GetQuotas(quotaType constant.QuotaType, filters ...Filter) ([]Quota, Warnings, error) {
   180  	if quotaType == constant.SpaceQuota {
   181  		return client.GetSpaceQuotaDefinitions(filters...)
   182  	}
   183  	return client.GetOrganizationQuotas(filters...)
   184  }
   185  
   186  // UpdateQuota updates the quota with the given GUID and for type either SpaceQuota or OrgQuota.
   187  func (client *Client) UpdateQuota(quotaType constant.QuotaType, quota Quota) (Quota, Warnings, error) {
   188  	if quotaType == constant.SpaceQuota {
   189  		return client.UpdateSpaceQuotaDefinition(quota)
   190  	}
   191  	return client.UpdateOrganizationQuota(quota)
   192  }
   193  
   194  // DeleteQuota delete a quota and for type either SpaceQuota or OrgQuota
   195  func (client *Client) DeleteQuota(quotaType constant.QuotaType, guid string) (Warnings, error) {
   196  	if quotaType == constant.SpaceQuota {
   197  		return client.DeleteSpaceQuotaDefinition(guid)
   198  	}
   199  	return client.DeleteOrganizationQuota(guid)
   200  }