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

     1  package ccv2
     2  
     3  import (
     4  	"bytes"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     8  	"encoding/json"
     9  )
    10  
    11  // GetOrganizationQuota returns an Organization Quota associated with the
    12  // provided GUID.
    13  func (client *Client) GetOrganizationQuota(guid string) (Quota, Warnings, error) {
    14  	request, err := client.newHTTPRequest(requestOptions{
    15  		RequestName: internal.GetOrganizationQuotaDefinitionRequest,
    16  		URIParams:   Params{"organization_quota_guid": guid},
    17  	})
    18  	if err != nil {
    19  		return Quota{}, nil, err
    20  	}
    21  
    22  	var orgQuota Quota
    23  	response := cloudcontroller.Response{
    24  		DecodeJSONResponseInto: &orgQuota,
    25  	}
    26  
    27  	err = client.connection.Make(request, &response)
    28  	return orgQuota, response.Warnings, err
    29  }
    30  
    31  // GetOrganizationQuotas returns an Organization Quota list associated with the
    32  // provided filters.
    33  func (client *Client) GetOrganizationQuotas(filters ...Filter) ([]Quota, Warnings, error) {
    34  	allQueries := ConvertFilterParameters(filters)
    35  	request, err := client.newHTTPRequest(requestOptions{
    36  		RequestName: internal.GetOrganizationQuotaDefinitionsRequest,
    37  		Query:       allQueries,
    38  	})
    39  
    40  	if err != nil {
    41  		return []Quota{}, nil, err
    42  	}
    43  
    44  	var fullOrgQuotasList []Quota
    45  
    46  	warnings, err := client.paginate(request, Quota{}, func(item interface{}) error {
    47  		if org, ok := item.(Quota); ok {
    48  			fullOrgQuotasList = append(fullOrgQuotasList, org)
    49  		} else {
    50  			return ccerror.UnknownObjectInListError{
    51  				Expected:   Quota{},
    52  				Unexpected: item,
    53  			}
    54  		}
    55  		return nil
    56  	})
    57  
    58  	return fullOrgQuotasList, warnings, err
    59  }
    60  
    61  // UpdateQuota updates the quota with the given GUID.
    62  func (client *Client) UpdateOrganizationQuota(quota Quota) (Quota, Warnings, error) {
    63  	body, err := json.Marshal(quota)
    64  	if err != nil {
    65  		return Quota{}, nil, err
    66  	}
    67  
    68  	request, err := client.newHTTPRequest(requestOptions{
    69  		RequestName: internal.PutOrganizationQuotaDefinitionRequest,
    70  		URIParams:   Params{"organization_quota_guid": quota.GUID},
    71  		Body:        bytes.NewReader(body),
    72  	})
    73  	if err != nil {
    74  		return Quota{}, nil, err
    75  	}
    76  
    77  	var updatedObj Quota
    78  	response := cloudcontroller.Response{
    79  		DecodeJSONResponseInto: &updatedObj,
    80  	}
    81  
    82  	err = client.connection.Make(request, &response)
    83  	return updatedObj, response.Warnings, err
    84  }
    85  
    86  // DeleteQuota delete a quota
    87  func (client *Client) DeleteOrganizationQuota(guid string) (Warnings, error) {
    88  	request, err := client.newHTTPRequest(requestOptions{
    89  		RequestName: internal.DeleteOrganizationQuotaDefinitionRequest,
    90  		URIParams: Params{
    91  			"organization_quota_guid": guid,
    92  		},
    93  	})
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  
    98  	response := cloudcontroller.Response{}
    99  
   100  	err = client.connection.Make(request, &response)
   101  	return response.Warnings, err
   102  }
   103  
   104  // CreateQuota creates a cloud controller quota in with the given settings.
   105  func (client *Client) CreateOrganizationQuota(quota Quota) (Quota, Warnings, error) {
   106  	body, err := json.Marshal(quota)
   107  	if err != nil {
   108  		return Quota{}, nil, err
   109  	}
   110  
   111  	request, err := client.newHTTPRequest(requestOptions{
   112  		RequestName: internal.PostOrganizationQuotaDefinitionsRequest,
   113  		Body:        bytes.NewReader(body),
   114  	})
   115  	if err != nil {
   116  		return Quota{}, nil, err
   117  	}
   118  
   119  	var updatedObj Quota
   120  	response := cloudcontroller.Response{
   121  		DecodeJSONResponseInto: &updatedObj,
   122  	}
   123  
   124  	err = client.connection.Make(request, &response)
   125  	return updatedObj, response.Warnings, err
   126  }