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