github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/spacequotas/space_quotas.go (about)

     1  package spacequotas
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api/resources"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/models"
    11  	"code.cloudfoundry.org/cli/cf/net"
    12  )
    13  
    14  //go:generate counterfeiter . SpaceQuotaRepository
    15  
    16  type SpaceQuotaRepository interface {
    17  	FindByName(name string) (quota models.SpaceQuota, apiErr error)
    18  	FindByOrg(guid string) (quota []models.SpaceQuota, apiErr error)
    19  	FindByGUID(guid string) (quota models.SpaceQuota, apiErr error)
    20  	FindByNameAndOrgGUID(spaceQuotaName string, orgGUID string) (quota models.SpaceQuota, apiErr error)
    21  
    22  	AssociateSpaceWithQuota(spaceGUID string, quotaGUID string) error
    23  	UnassignQuotaFromSpace(spaceGUID string, quotaGUID string) error
    24  
    25  	// CRUD ahoy
    26  	Create(quota models.SpaceQuota) error
    27  	Update(quota models.SpaceQuota) error
    28  	Delete(quotaGUID string) error
    29  }
    30  
    31  type CloudControllerSpaceQuotaRepository struct {
    32  	config  coreconfig.Reader
    33  	gateway net.Gateway
    34  }
    35  
    36  func NewCloudControllerSpaceQuotaRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerSpaceQuotaRepository) {
    37  	repo.config = config
    38  	repo.gateway = gateway
    39  	return
    40  }
    41  
    42  func (repo CloudControllerSpaceQuotaRepository) findAllWithPath(path string) ([]models.SpaceQuota, error) {
    43  	var quotas []models.SpaceQuota
    44  	apiErr := repo.gateway.ListPaginatedResources(
    45  		repo.config.APIEndpoint(),
    46  		path,
    47  		resources.SpaceQuotaResource{},
    48  		func(resource interface{}) bool {
    49  			if qr, ok := resource.(resources.SpaceQuotaResource); ok {
    50  				quotas = append(quotas, qr.ToModel())
    51  			}
    52  			return true
    53  		})
    54  	return quotas, apiErr
    55  }
    56  
    57  func (repo CloudControllerSpaceQuotaRepository) FindByName(name string) (quota models.SpaceQuota, apiErr error) {
    58  	return repo.FindByNameAndOrgGUID(name, repo.config.OrganizationFields().GUID)
    59  }
    60  
    61  func (repo CloudControllerSpaceQuotaRepository) FindByNameAndOrgGUID(spaceQuotaName string, orgGUID string) (models.SpaceQuota, error) {
    62  	quotas, apiErr := repo.FindByOrg(orgGUID)
    63  	if apiErr != nil {
    64  		return models.SpaceQuota{}, apiErr
    65  	}
    66  
    67  	for _, quota := range quotas {
    68  		if quota.Name == spaceQuotaName {
    69  			return quota, nil
    70  		}
    71  	}
    72  
    73  	apiErr = errors.NewModelNotFoundError("Space Quota", spaceQuotaName)
    74  	return models.SpaceQuota{}, apiErr
    75  }
    76  
    77  func (repo CloudControllerSpaceQuotaRepository) FindByOrg(guid string) ([]models.SpaceQuota, error) {
    78  	path := fmt.Sprintf("/v2/organizations/%s/space_quota_definitions", guid)
    79  	quotas, apiErr := repo.findAllWithPath(path)
    80  	if apiErr != nil {
    81  		return nil, apiErr
    82  	}
    83  	return quotas, nil
    84  }
    85  
    86  func (repo CloudControllerSpaceQuotaRepository) FindByGUID(guid string) (quota models.SpaceQuota, apiErr error) {
    87  	quotas, apiErr := repo.FindByOrg(repo.config.OrganizationFields().GUID)
    88  	if apiErr != nil {
    89  		return
    90  	}
    91  
    92  	for _, quota := range quotas {
    93  		if quota.GUID == guid {
    94  			return quota, nil
    95  		}
    96  	}
    97  
    98  	apiErr = errors.NewModelNotFoundError("Space Quota", guid)
    99  	return models.SpaceQuota{}, apiErr
   100  }
   101  
   102  func (repo CloudControllerSpaceQuotaRepository) Create(quota models.SpaceQuota) error {
   103  	path := "/v2/space_quota_definitions"
   104  	return repo.gateway.CreateResourceFromStruct(repo.config.APIEndpoint(), path, quota)
   105  }
   106  
   107  func (repo CloudControllerSpaceQuotaRepository) Update(quota models.SpaceQuota) error {
   108  	path := fmt.Sprintf("/v2/space_quota_definitions/%s", quota.GUID)
   109  	return repo.gateway.UpdateResourceFromStruct(repo.config.APIEndpoint(), path, quota)
   110  }
   111  
   112  func (repo CloudControllerSpaceQuotaRepository) AssociateSpaceWithQuota(spaceGUID string, quotaGUID string) error {
   113  	path := fmt.Sprintf("/v2/space_quota_definitions/%s/spaces/%s", quotaGUID, spaceGUID)
   114  	return repo.gateway.UpdateResource(repo.config.APIEndpoint(), path, strings.NewReader(""))
   115  }
   116  
   117  func (repo CloudControllerSpaceQuotaRepository) UnassignQuotaFromSpace(spaceGUID string, quotaGUID string) error {
   118  	path := fmt.Sprintf("/v2/space_quota_definitions/%s/spaces/%s", quotaGUID, spaceGUID)
   119  	return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
   120  }
   121  
   122  func (repo CloudControllerSpaceQuotaRepository) Delete(quotaGUID string) (apiErr error) {
   123  	path := fmt.Sprintf("/v2/space_quota_definitions/%s", quotaGUID)
   124  	return repo.gateway.DeleteResource(repo.config.APIEndpoint(), path)
   125  }