forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
organizations.go
116 lines (100 loc) · 4.11 KB
/
organizations.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
package organizations
import (
"fmt"
"net/url"
"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 . OrganizationRepository
type OrganizationRepository interface {
ListOrgs(limit int) ([]models.Organization, error)
GetManyOrgsByGUID(orgGUIDs []string) (orgs []models.Organization, apiErr error)
FindByName(name string) (org models.Organization, apiErr error)
Create(org models.Organization) (apiErr error)
Rename(orgGUID string, name string) (apiErr error)
Delete(orgGUID string) (apiErr error)
SharePrivateDomain(orgGUID string, domainGUID string) (apiErr error)
UnsharePrivateDomain(orgGUID string, domainGUID string) (apiErr error)
}
type CloudControllerOrganizationRepository struct {
config coreconfig.Reader
gateway net.Gateway
}
func NewCloudControllerOrganizationRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerOrganizationRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerOrganizationRepository) ListOrgs(limit int) ([]models.Organization, error) {
orgs := []models.Organization{}
err := repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
"/v2/organizations?order-by=name",
resources.OrganizationResource{},
func(resource interface{}) bool {
if orgResource, ok := resource.(resources.OrganizationResource); ok {
orgs = append(orgs, orgResource.ToModel())
return limit == 0 || len(orgs) < limit
}
return false
},
)
return orgs, err
}
func (repo CloudControllerOrganizationRepository) GetManyOrgsByGUID(orgGUIDs []string) (orgs []models.Organization, err error) {
for _, orgGUID := range orgGUIDs {
url := fmt.Sprintf("%s/v2/organizations/%s", repo.config.APIEndpoint(), orgGUID)
orgResource := resources.OrganizationResource{}
err = repo.gateway.GetResource(url, &orgResource)
if err != nil {
return nil, err
}
orgs = append(orgs, orgResource.ToModel())
}
return
}
func (repo CloudControllerOrganizationRepository) FindByName(name string) (org models.Organization, apiErr error) {
found := false
apiErr = repo.gateway.ListPaginatedResources(
repo.config.APIEndpoint(),
fmt.Sprintf("/v2/organizations?q=%s&inline-relations-depth=1", url.QueryEscape("name:"+strings.ToLower(name))),
resources.OrganizationResource{},
func(resource interface{}) bool {
org = resource.(resources.OrganizationResource).ToModel()
found = true
return false
})
if apiErr == nil && !found {
apiErr = errors.NewModelNotFoundError("Organization", name)
}
return
}
func (repo CloudControllerOrganizationRepository) Create(org models.Organization) (apiErr error) {
data := fmt.Sprintf(`{"name":"%s"`, org.Name)
if org.QuotaDefinition.GUID != "" {
data = data + fmt.Sprintf(`, "quota_definition_guid":"%s"`, org.QuotaDefinition.GUID)
}
data = data + "}"
return repo.gateway.CreateResource(repo.config.APIEndpoint(), "/v2/organizations", strings.NewReader(data))
}
func (repo CloudControllerOrganizationRepository) Rename(orgGUID string, name string) (apiErr error) {
url := fmt.Sprintf("/v2/organizations/%s", orgGUID)
data := fmt.Sprintf(`{"name":"%s"}`, name)
return repo.gateway.UpdateResource(repo.config.APIEndpoint(), url, strings.NewReader(data))
}
func (repo CloudControllerOrganizationRepository) Delete(orgGUID string) (apiErr error) {
url := fmt.Sprintf("/v2/organizations/%s?recursive=true", orgGUID)
return repo.gateway.DeleteResource(repo.config.APIEndpoint(), url)
}
func (repo CloudControllerOrganizationRepository) SharePrivateDomain(orgGUID string, domainGUID string) error {
url := fmt.Sprintf("/v2/organizations/%s/private_domains/%s", orgGUID, domainGUID)
return repo.gateway.UpdateResource(repo.config.APIEndpoint(), url, nil)
}
func (repo CloudControllerOrganizationRepository) UnsharePrivateDomain(orgGUID string, domainGUID string) error {
url := fmt.Sprintf("/v2/organizations/%s/private_domains/%s", orgGUID, domainGUID)
return repo.gateway.DeleteResource(repo.config.APIEndpoint(), url)
}