forked from cloudfoundry/cli
/
spaces.go
126 lines (107 loc) · 4.08 KB
/
spaces.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
126
package api
import (
"cf/configuration"
"cf/errors"
"cf/models"
"cf/net"
"fmt"
"net/url"
"strings"
)
type SpaceResource struct {
Metadata Metadata
Entity SpaceEntity
}
func (resource SpaceResource) ToFields() (fields models.SpaceFields) {
fields.Guid = resource.Metadata.Guid
fields.Name = resource.Entity.Name
return
}
func (resource SpaceResource) ToModel() (space models.Space) {
space.SpaceFields = resource.ToFields()
for _, app := range resource.Entity.Applications {
space.Applications = append(space.Applications, app.ToFields())
}
for _, domainResource := range resource.Entity.Domains {
space.Domains = append(space.Domains, domainResource.ToFields())
}
for _, serviceResource := range resource.Entity.ServiceInstances {
space.ServiceInstances = append(space.ServiceInstances, serviceResource.ToFields())
}
space.Organization = resource.Entity.Organization.ToFields()
return
}
type SpaceEntity struct {
Name string
Organization OrganizationResource
Applications []ApplicationResource `json:"apps"`
Domains []DomainResource
ServiceInstances []ServiceInstanceResource `json:"service_instances"`
}
type SpaceRepository interface {
ListSpaces(func(models.Space) bool) errors.Error
FindByName(name string) (space models.Space, apiErr errors.Error)
FindByNameInOrg(name, orgGuid string) (space models.Space, apiErr errors.Error)
Create(name string, orgGuid string) (space models.Space, apiErr errors.Error)
Rename(spaceGuid, newName string) (apiErr errors.Error)
Delete(spaceGuid string) (apiErr errors.Error)
}
type CloudControllerSpaceRepository struct {
config configuration.Reader
gateway net.Gateway
}
func NewCloudControllerSpaceRepository(config configuration.Reader, gateway net.Gateway) (repo CloudControllerSpaceRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerSpaceRepository) ListSpaces(callback func(models.Space) bool) errors.Error {
return repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
repo.config.AccessToken(),
fmt.Sprintf("/v2/organizations/%s/spaces", repo.config.OrganizationFields().Guid),
SpaceResource{},
func(resource interface{}) bool {
return callback(resource.(SpaceResource).ToModel())
})
}
func (repo CloudControllerSpaceRepository) FindByName(name string) (space models.Space, apiErr errors.Error) {
return repo.FindByNameInOrg(name, repo.config.OrganizationFields().Guid)
}
func (repo CloudControllerSpaceRepository) FindByNameInOrg(name, orgGuid string) (space models.Space, apiErr errors.Error) {
foundSpace := false
apiErr = repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
repo.config.AccessToken(),
fmt.Sprintf("/v2/organizations/%s/spaces?q=%s&inline-relations-depth=1", orgGuid, url.QueryEscape("name:"+strings.ToLower(name))),
SpaceResource{},
func(resource interface{}) bool {
space = resource.(SpaceResource).ToModel()
foundSpace = true
return false
})
if !foundSpace {
apiErr = errors.NewModelNotFoundError("Space", name)
}
return
}
func (repo CloudControllerSpaceRepository) Create(name string, orgGuid string) (space models.Space, apiErr errors.Error) {
path := fmt.Sprintf("%s/v2/spaces?inline-relations-depth=1", repo.config.ApiEndpoint())
body := fmt.Sprintf(`{"name":"%s","organization_guid":"%s"}`, name, orgGuid)
resource := new(SpaceResource)
apiErr = repo.gateway.CreateResourceForResponse(path, repo.config.AccessToken(), strings.NewReader(body), resource)
if apiErr != nil {
return
}
space = resource.ToModel()
return
}
func (repo CloudControllerSpaceRepository) Rename(spaceGuid, newName string) (apiErr errors.Error) {
path := fmt.Sprintf("%s/v2/spaces/%s", repo.config.ApiEndpoint(), spaceGuid)
body := fmt.Sprintf(`{"name":"%s"}`, newName)
return repo.gateway.UpdateResource(path, repo.config.AccessToken(), strings.NewReader(body))
}
func (repo CloudControllerSpaceRepository) Delete(spaceGuid string) (apiErr errors.Error) {
path := fmt.Sprintf("%s/v2/spaces/%s?recursive=true", repo.config.ApiEndpoint(), spaceGuid)
return repo.gateway.DeleteResource(path, repo.config.AccessToken())
}