forked from cloudfoundry/cli
/
organizations.go
89 lines (77 loc) · 2.85 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
package organizations
import (
"fmt"
"net/url"
"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 OrganizationRepository interface {
ListOrgs() (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)
}
type CloudControllerOrganizationRepository struct {
config core_config.Reader
gateway net.Gateway
}
func NewCloudControllerOrganizationRepository(config core_config.Reader, gateway net.Gateway) (repo CloudControllerOrganizationRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerOrganizationRepository) ListOrgs() ([]models.Organization, error) {
orgs := []models.Organization{}
err := repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
"/v2/organizations",
resources.OrganizationResource{},
func(resource interface{}) bool {
orgResource, ok := resource.(resources.OrganizationResource)
if ok {
orgs = append(orgs, orgResource.ToModel())
return true
} else {
return false
}
})
return orgs, err
}
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)
}