-
Notifications
You must be signed in to change notification settings - Fork 929
/
organizations.go
115 lines (96 loc) · 3.53 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
package api
import (
"cf/configuration"
"cf/models"
"cf/net"
"fmt"
"net/url"
"strings"
)
type OrganizationEntity struct {
Name string
QuotaDefinition QuotaResource `json:"quota_definition"`
Spaces []SpaceResource
Domains []DomainResource
}
type OrganizationResource struct {
Resource
Entity OrganizationEntity
}
func (resource OrganizationResource) ToFields() (fields models.OrganizationFields) {
fields.Name = resource.Entity.Name
fields.Guid = resource.Metadata.Guid
fields.QuotaDefinition = resource.Entity.QuotaDefinition.ToFields()
return
}
func (resource OrganizationResource) ToModel() (org models.Organization) {
org.OrganizationFields = resource.ToFields()
spaces := []models.SpaceFields{}
for _, s := range resource.Entity.Spaces {
spaces = append(spaces, s.ToFields())
}
org.Spaces = spaces
domains := []models.DomainFields{}
for _, d := range resource.Entity.Domains {
domains = append(domains, d.ToFields())
}
org.Domains = domains
return
}
type OrganizationRepository interface {
ListOrgs(func(models.Organization) bool) (apiResponse net.ApiResponse)
FindByName(name string) (org models.Organization, apiResponse net.ApiResponse)
Create(name string) (apiResponse net.ApiResponse)
Rename(orgGuid string, name string) (apiResponse net.ApiResponse)
Delete(orgGuid string) (apiResponse net.ApiResponse)
}
type CloudControllerOrganizationRepository struct {
config configuration.Reader
gateway net.Gateway
}
func NewCloudControllerOrganizationRepository(config configuration.Reader, gateway net.Gateway) (repo CloudControllerOrganizationRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerOrganizationRepository) ListOrgs(cb func(models.Organization) bool) (apiResponse net.ApiResponse) {
return repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
repo.config.AccessToken(),
"/v2/organizations",
OrganizationResource{},
func(resource interface{}) bool {
return cb(resource.(OrganizationResource).ToModel())
})
}
func (repo CloudControllerOrganizationRepository) FindByName(name string) (org models.Organization, apiResponse net.ApiResponse) {
found := false
apiResponse = repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
repo.config.AccessToken(),
fmt.Sprintf("/v2/organizations?q=%s&inline-relations-depth=1", url.QueryEscape("name:"+strings.ToLower(name))),
OrganizationResource{},
func(resource interface{}) bool {
org = resource.(OrganizationResource).ToModel()
found = true
return false
})
if !found {
apiResponse = net.NewNotFoundApiResponse("Organization %s not found", name)
}
return
}
func (repo CloudControllerOrganizationRepository) Create(name string) (apiResponse net.ApiResponse) {
url := repo.config.ApiEndpoint() + "/v2/organizations"
data := fmt.Sprintf(`{"name":"%s"}`, name)
return repo.gateway.CreateResource(url, repo.config.AccessToken(), strings.NewReader(data))
}
func (repo CloudControllerOrganizationRepository) Rename(orgGuid string, name string) (apiResponse net.ApiResponse) {
url := fmt.Sprintf("%s/v2/organizations/%s", repo.config.ApiEndpoint(), orgGuid)
data := fmt.Sprintf(`{"name":"%s"}`, name)
return repo.gateway.UpdateResource(url, repo.config.AccessToken(), strings.NewReader(data))
}
func (repo CloudControllerOrganizationRepository) Delete(orgGuid string) (apiResponse net.ApiResponse) {
url := fmt.Sprintf("%s/v2/organizations/%s?recursive=true", repo.config.ApiEndpoint(), orgGuid)
return repo.gateway.DeleteResource(url, repo.config.AccessToken())
}