github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv2/space_quota.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     7  )
     8  
     9  // SpaceQuota represents the Cloud Controller configured quota assigned to the
    10  // space.
    11  type SpaceQuota struct {
    12  
    13  	// GUID is the unique space quota identifier.
    14  	GUID string
    15  
    16  	// Name is the name given to the space quota.
    17  	Name string
    18  }
    19  
    20  // UnmarshalJSON helps unmarshal a Cloud Controller Space Quota response.
    21  func (spaceQuota *SpaceQuota) UnmarshalJSON(data []byte) error {
    22  	var ccSpaceQuota struct {
    23  		Metadata internal.Metadata `json:"metadata"`
    24  		Entity   struct {
    25  			Name string `json:"name"`
    26  		} `json:"entity"`
    27  	}
    28  	err := cloudcontroller.DecodeJSON(data, &ccSpaceQuota)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	spaceQuota.GUID = ccSpaceQuota.Metadata.GUID
    34  	spaceQuota.Name = ccSpaceQuota.Entity.Name
    35  	return nil
    36  }
    37  
    38  // GetSpaceQuotaDefinition returns a Space Quota.
    39  func (client *Client) GetSpaceQuotaDefinition(guid string) (SpaceQuota, Warnings, error) {
    40  	request, err := client.newHTTPRequest(requestOptions{
    41  		RequestName: internal.GetSpaceQuotaDefinitionRequest,
    42  		URIParams:   Params{"space_quota_guid": guid},
    43  	})
    44  	if err != nil {
    45  		return SpaceQuota{}, nil, err
    46  	}
    47  
    48  	var spaceQuota SpaceQuota
    49  	response := cloudcontroller.Response{
    50  		DecodeJSONResponseInto: &spaceQuota,
    51  	}
    52  
    53  	err = client.connection.Make(request, &response)
    54  	return spaceQuota, response.Warnings, err
    55  }
    56  
    57  // GetSpaceQuotas returns all the space quotas for the org
    58  func (client *Client) GetSpaceQuotas(orgGUID string) ([]SpaceQuota, Warnings, error) {
    59  	request, err := client.newHTTPRequest(requestOptions{
    60  		RequestName: internal.GetOrganizationSpaceQuotasRequest,
    61  		URIParams:   Params{"organization_guid": orgGUID},
    62  	})
    63  
    64  	if err != nil {
    65  		return nil, nil, err
    66  	}
    67  
    68  	var spaceQuotas []SpaceQuota
    69  	warnings, err := client.paginate(request, SpaceQuota{}, func(item interface{}) error {
    70  		if spaceQuota, ok := item.(SpaceQuota); ok {
    71  			spaceQuotas = append(spaceQuotas, spaceQuota)
    72  		} else {
    73  			return ccerror.UnknownObjectInListError{
    74  				Expected:   SpaceQuota{},
    75  				Unexpected: item,
    76  			}
    77  		}
    78  		return nil
    79  	})
    80  
    81  	return spaceQuotas, warnings, err
    82  }
    83  
    84  // SetSpaceQuota should set the quota for the space and returns the warnings
    85  func (client *Client) SetSpaceQuota(spaceGUID string, quotaGUID string) (Warnings, error) {
    86  	request, err := client.newHTTPRequest(requestOptions{
    87  		RequestName: internal.PutSpaceQuotaRequest,
    88  		URIParams:   Params{"space_quota_guid": quotaGUID, "space_guid": spaceGUID},
    89  	})
    90  
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	response := cloudcontroller.Response{}
    96  	err = client.connection.Make(request, &response)
    97  
    98  	return response.Warnings, err
    99  }