forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space_quotas.go
118 lines (98 loc) · 3.79 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
package space_quotas
import (
"fmt"
"strings"
"github.com/cloudfoundry/cli/cf/api/resources"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/errors"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/cli/cf/net"
)
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)
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 core_config.Reader
gateway net.Gateway
}
func NewCloudControllerSpaceQuotaRepository(config core_config.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) {
quotas, apiErr := repo.FindByOrg(repo.config.OrganizationFields().Guid)
if apiErr != nil {
return
}
for _, quota := range quotas {
if quota.Name == name {
return quota, nil
}
}
apiErr = errors.NewModelNotFoundError("Space Quota", name)
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)
}