github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv2/organization_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  // OrganizationQuota is the definition of a quota for an organization.
    10  type OrganizationQuota struct {
    11  
    12  	// GUID is the unique OrganizationQuota identifier.
    13  	GUID string
    14  
    15  	// Name is the name of the OrganizationQuota.
    16  	Name string
    17  }
    18  
    19  // UnmarshalJSON helps unmarshal a Cloud Controller organization quota response.
    20  func (application *OrganizationQuota) UnmarshalJSON(data []byte) error {
    21  	var ccOrgQuota struct {
    22  		Metadata internal.Metadata `json:"metadata"`
    23  		Entity   struct {
    24  			Name string `json:"name"`
    25  		} `json:"entity"`
    26  	}
    27  	err := cloudcontroller.DecodeJSON(data, &ccOrgQuota)
    28  	if err != nil {
    29  		return err
    30  	}
    31  
    32  	application.GUID = ccOrgQuota.Metadata.GUID
    33  	application.Name = ccOrgQuota.Entity.Name
    34  
    35  	return nil
    36  }
    37  
    38  // GetOrganizationQuota returns an Organization Quota associated with the
    39  // provided GUID.
    40  func (client *Client) GetOrganizationQuota(guid string) (OrganizationQuota, Warnings, error) {
    41  	request, err := client.newHTTPRequest(requestOptions{
    42  		RequestName: internal.GetOrganizationQuotaDefinitionRequest,
    43  		URIParams:   Params{"organization_quota_guid": guid},
    44  	})
    45  	if err != nil {
    46  		return OrganizationQuota{}, nil, err
    47  	}
    48  
    49  	var orgQuota OrganizationQuota
    50  	response := cloudcontroller.Response{
    51  		DecodeJSONResponseInto: &orgQuota,
    52  	}
    53  
    54  	err = client.connection.Make(request, &response)
    55  	return orgQuota, response.Warnings, err
    56  }
    57  
    58  // GetOrganizationQuotas returns an Organization Quota list associated with the
    59  // provided filters.
    60  func (client *Client) GetOrganizationQuotas(filters ...Filter) ([]OrganizationQuota, Warnings, error) {
    61  	allQueries := ConvertFilterParameters(filters)
    62  	request, err := client.newHTTPRequest(requestOptions{
    63  		RequestName: internal.GetOrganizationQuotaDefinitionsRequest,
    64  		Query:       allQueries,
    65  	})
    66  
    67  	if err != nil {
    68  		return []OrganizationQuota{}, nil, err
    69  	}
    70  
    71  	var fullOrgQuotasList []OrganizationQuota
    72  
    73  	warnings, err := client.paginate(request, OrganizationQuota{}, func(item interface{}) error {
    74  		if org, ok := item.(OrganizationQuota); ok {
    75  			fullOrgQuotasList = append(fullOrgQuotasList, org)
    76  		} else {
    77  			return ccerror.UnknownObjectInListError{
    78  				Expected:   OrganizationQuota{},
    79  				Unexpected: item,
    80  			}
    81  		}
    82  		return nil
    83  	})
    84  
    85  	return fullOrgQuotasList, warnings, err
    86  }