github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv2/space_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  // GetSpaceQuotaDefinition returns a Space Quota.
    12  func (client *Client) GetSpaceQuotaDefinition(guid string) (Quota, Warnings, error) {
    13  	request, err := client.newHTTPRequest(requestOptions{
    14  		RequestName: internal.GetSpaceQuotaDefinitionRequest,
    15  		URIParams:   Params{"space_quota_guid": guid},
    16  	})
    17  	if err != nil {
    18  		return Quota{}, nil, err
    19  	}
    20  
    21  	var spaceQuota Quota
    22  	response := cloudcontroller.Response{
    23  		DecodeJSONResponseInto: &spaceQuota,
    24  	}
    25  
    26  	err = client.connection.Make(request, &response)
    27  	return spaceQuota, response.Warnings, err
    28  }
    29  
    30  // GetSpaceQuotas returns all the space quotas for the org
    31  func (client *Client) GetSpaceQuotas(spaceGUID string) ([]Quota, Warnings, error) {
    32  	request, err := client.newHTTPRequest(requestOptions{
    33  		RequestName: internal.GetOrganizationSpaceQuotasRequest,
    34  		URIParams:   Params{"space_guid": spaceGUID},
    35  	})
    36  
    37  	if err != nil {
    38  		return nil, nil, err
    39  	}
    40  
    41  	var spaceQuotas []Quota
    42  	warnings, err := client.paginate(request, Quota{}, func(item interface{}) error {
    43  		if spaceQuota, ok := item.(Quota); ok {
    44  			spaceQuotas = append(spaceQuotas, spaceQuota)
    45  		} else {
    46  			return ccerror.UnknownObjectInListError{
    47  				Expected:   Quota{},
    48  				Unexpected: item,
    49  			}
    50  		}
    51  		return nil
    52  	})
    53  
    54  	return spaceQuotas, warnings, err
    55  }
    56  
    57  // SetSpaceQuota should set the quota for the space and returns the warnings
    58  func (client *Client) SetSpaceQuota(spaceGUID string, quotaGUID string) (Warnings, error) {
    59  	request, err := client.newHTTPRequest(requestOptions{
    60  		RequestName: internal.PutSpaceQuotaRequest,
    61  		URIParams:   Params{"space_quota_guid": quotaGUID, "space_guid": spaceGUID},
    62  	})
    63  
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	response := cloudcontroller.Response{}
    69  	err = client.connection.Make(request, &response)
    70  
    71  	return response.Warnings, err
    72  }
    73  
    74  // DeleteSpaceQuota delete a space quota
    75  func (client *Client) DeleteSpaceQuota(quotaGuid, spaceGuid string) (Warnings, error) {
    76  	request, err := client.newHTTPRequest(requestOptions{
    77  		RequestName: internal.DeleteSpaceQuotaRequest,
    78  		URIParams: Params{
    79  			"space_quota_guid": quotaGuid,
    80  			"space_guid":       spaceGuid,
    81  		},
    82  	})
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	response := cloudcontroller.Response{}
    88  
    89  	err = client.connection.Make(request, &response)
    90  	return response.Warnings, err
    91  }
    92  
    93  // GetQuotas returns back a list of quotas based off of the
    94  // provided filters.
    95  func (client *Client) GetSpaceQuotaDefinitions(filters ...Filter) ([]Quota, Warnings, error) {
    96  	request, err := client.newHTTPRequest(requestOptions{
    97  		RequestName: internal.GetSpaceQuotaDefinitionsRequest,
    98  		Query:       ConvertFilterParameters(filters),
    99  	})
   100  	if err != nil {
   101  		return nil, nil, err
   102  	}
   103  
   104  	var fullObjList []Quota
   105  	warnings, err := client.paginate(request, Quota{}, func(item interface{}) error {
   106  		if app, ok := item.(Quota); ok {
   107  			fullObjList = append(fullObjList, app)
   108  		} else {
   109  			return ccerror.UnknownObjectInListError{
   110  				Expected:   Quota{},
   111  				Unexpected: item,
   112  			}
   113  		}
   114  		return nil
   115  	})
   116  
   117  	return fullObjList, warnings, err
   118  }
   119  
   120  // CreateQuota creates a cloud controller quota in with the given settings.
   121  func (client *Client) CreateSpaceQuotaDefinition(quota Quota) (Quota, Warnings, error) {
   122  	body, err := json.Marshal(quota)
   123  	if err != nil {
   124  		return Quota{}, nil, err
   125  	}
   126  
   127  	request, err := client.newHTTPRequest(requestOptions{
   128  		RequestName: internal.PostSpaceQuotaDefinitionsRequest,
   129  		Body:        bytes.NewReader(body),
   130  	})
   131  	if err != nil {
   132  		return Quota{}, nil, err
   133  	}
   134  
   135  	var updatedObj Quota
   136  	response := cloudcontroller.Response{
   137  		DecodeJSONResponseInto: &updatedObj,
   138  	}
   139  
   140  	err = client.connection.Make(request, &response)
   141  	return updatedObj, response.Warnings, err
   142  }
   143  
   144  // DeleteQuota delete a quota
   145  func (client *Client) DeleteSpaceQuotaDefinition(guid string) (Warnings, error) {
   146  	request, err := client.newHTTPRequest(requestOptions{
   147  		RequestName: internal.DeleteSpaceQuotaDefinitionRequest,
   148  		URIParams: Params{
   149  			"space_quota_guid": guid,
   150  		},
   151  	})
   152  	if err != nil {
   153  		return nil, err
   154  	}
   155  
   156  	response := cloudcontroller.Response{}
   157  
   158  	err = client.connection.Make(request, &response)
   159  	return response.Warnings, err
   160  }
   161  
   162  // UpdateQuota updates the quota with the given GUID.
   163  func (client *Client) UpdateSpaceQuotaDefinition(quota Quota) (Quota, Warnings, error) {
   164  	body, err := json.Marshal(quota)
   165  	if err != nil {
   166  		return Quota{}, nil, err
   167  	}
   168  
   169  	request, err := client.newHTTPRequest(requestOptions{
   170  		RequestName: internal.PutSpaceQuotaDefinitionRequest,
   171  		URIParams:   Params{"space_quota_guid": quota.GUID},
   172  		Body:        bytes.NewReader(body),
   173  	})
   174  	if err != nil {
   175  		return Quota{}, nil, err
   176  	}
   177  
   178  	var updatedObj Quota
   179  	response := cloudcontroller.Response{
   180  		DecodeJSONResponseInto: &updatedObj,
   181  	}
   182  
   183  	err = client.connection.Make(request, &response)
   184  	return updatedObj, response.Warnings, err
   185  }