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

     1  package quotas
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"code.cloudfoundry.org/cli/cf/api/resources"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/errors"
    11  	"code.cloudfoundry.org/cli/cf/models"
    12  	"code.cloudfoundry.org/cli/cf/net"
    13  )
    14  
    15  //go:generate counterfeiter . QuotaRepository
    16  
    17  type QuotaRepository interface {
    18  	FindAll() (quotas []models.QuotaFields, apiErr error)
    19  	FindByName(name string) (quota models.QuotaFields, apiErr error)
    20  
    21  	AssignQuotaToOrg(orgGUID, quotaGUID string) error
    22  
    23  	// CRUD ahoy
    24  	Create(quota models.QuotaFields) error
    25  	Update(quota models.QuotaFields) error
    26  	Delete(quotaGUID string) error
    27  }
    28  
    29  type CloudControllerQuotaRepository struct {
    30  	config  coreconfig.Reader
    31  	gateway net.Gateway
    32  }
    33  
    34  func NewCloudControllerQuotaRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerQuotaRepository) {
    35  	repo.config = config
    36  	repo.gateway = gateway
    37  	return
    38  }
    39  
    40  func (repo CloudControllerQuotaRepository) findAllWithPath(path string) ([]models.QuotaFields, error) {
    41  	var quotas []models.QuotaFields
    42  	apiErr := repo.gateway.ListPaginatedResources(
    43  		repo.config.APIEndpoint(),
    44  		path,
    45  		resources.QuotaResource{},
    46  		func(resource interface{}) bool {
    47  			if qr, ok := resource.(resources.QuotaResource); ok {
    48  				quotas = append(quotas, qr.ToFields())
    49  			}
    50  			return true
    51  		})
    52  	return quotas, apiErr
    53  }
    54  
    55  func (repo CloudControllerQuotaRepository) FindAll() (quotas []models.QuotaFields, apiErr error) {
    56  	return repo.findAllWithPath("/v2/quota_definitions")
    57  }
    58  
    59  func (repo CloudControllerQuotaRepository) FindByName(name string) (quota models.QuotaFields, apiErr error) {
    60  	path := fmt.Sprintf("/v2/quota_definitions?q=%s", url.QueryEscape("name:"+name))
    61  	quotas, apiErr := repo.findAllWithPath(path)
    62  	if apiErr != nil {
    63  		return
    64  	}
    65  
    66  	if len(quotas) == 0 {
    67  		apiErr = errors.NewModelNotFoundError("Quota", name)
    68  		return
    69  	}
    70  
    71  	quota = quotas[0]
    72  	return
    73  }
    74  
    75  func (repo CloudControllerQuotaRepository) Create(quota models.QuotaFields) error {
    76  	return repo.gateway.CreateResourceFromStruct(repo.config.APIEndpoint(), "/v2/quota_definitions", quota)
    77  }
    78  
    79  func (repo CloudControllerQuotaRepository) Update(quota models.QuotaFields) error {
    80  	path := fmt.Sprintf("/v2/quota_definitions/%s", quota.GUID)
    81  	return repo.gateway.UpdateResourceFromStruct(repo.config.APIEndpoint(), path, quota)
    82  }
    83  
    84  func (repo CloudControllerQuotaRepository) AssignQuotaToOrg(orgGUID, quotaGUID string) (apiErr error) {
    85  	path := fmt.Sprintf("/v2/organizations/%s", orgGUID)
    86  	data := fmt.Sprintf(`{"quota_definition_guid":"%s"}`, quotaGUID)
    87  	return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, strings.NewReader(data))
    88  }
    89  
    90  func (repo CloudControllerQuotaRepository) Delete(quotaGUID string) (apiErr error) {
    91  	path := fmt.Sprintf("/v2/quota_definitions/%s", quotaGUID)
    92  	return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
    93  }