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