forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaces.go
107 lines (90 loc) · 3.59 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
package spaces
import (
"encoding/json"
"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 SpaceRepository interface {
ListSpaces(func(models.Space) bool) error
FindByName(name string) (space models.Space, apiErr error)
FindByNameInOrg(name, orgGuid string) (space models.Space, apiErr error)
Create(name string, orgGuid string, spaceQuotaGuid string) (space models.Space, apiErr error)
Rename(spaceGuid, newName string) (apiErr error)
SetAllowSSH(spaceGuid string, allow bool) (apiErr error)
Delete(spaceGuid string) (apiErr error)
}
type CloudControllerSpaceRepository struct {
config core_config.Reader
gateway net.Gateway
}
func NewCloudControllerSpaceRepository(config core_config.Reader, gateway net.Gateway) (repo CloudControllerSpaceRepository) {
repo.config = config
repo.gateway = gateway
return
}
func (repo CloudControllerSpaceRepository) ListSpaces(callback func(models.Space) bool) error {
return repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
fmt.Sprintf("/v2/organizations/%s/spaces?inline-relations-depth=1", repo.config.OrganizationFields().Guid),
resources.SpaceResource{},
func(resource interface{}) bool {
return callback(resource.(resources.SpaceResource).ToModel())
})
}
func (repo CloudControllerSpaceRepository) FindByName(name string) (space models.Space, apiErr error) {
return repo.FindByNameInOrg(name, repo.config.OrganizationFields().Guid)
}
func (repo CloudControllerSpaceRepository) FindByNameInOrg(name, orgGuid string) (space models.Space, apiErr error) {
foundSpace := false
apiErr = repo.gateway.ListPaginatedResources(
repo.config.ApiEndpoint(),
fmt.Sprintf("/v2/organizations/%s/spaces?q=%s&inline-relations-depth=1", orgGuid, url.QueryEscape("name:"+strings.ToLower(name))),
resources.SpaceResource{},
func(resource interface{}) bool {
space = resource.(resources.SpaceResource).ToModel()
foundSpace = true
return false
})
if !foundSpace {
apiErr = errors.NewModelNotFoundError("Space", name)
}
return
}
func (repo CloudControllerSpaceRepository) Create(name, orgGuid, spaceQuotaGuid string) (space models.Space, apiErr error) {
path := "/v2/spaces?inline-relations-depth=1"
bodyMap := map[string]string{"name": name, "organization_guid": orgGuid}
if spaceQuotaGuid != "" {
bodyMap["space_quota_definition_guid"] = spaceQuotaGuid
}
body, apiErr := json.Marshal(bodyMap)
if apiErr != nil {
return
}
resource := new(resources.SpaceResource)
apiErr = repo.gateway.CreateResource(repo.config.ApiEndpoint(), path, strings.NewReader(string(body)), resource)
if apiErr != nil {
return
}
space = resource.ToModel()
return
}
func (repo CloudControllerSpaceRepository) Rename(spaceGuid, newName string) (apiErr error) {
path := fmt.Sprintf("/v2/spaces/%s", spaceGuid)
body := fmt.Sprintf(`{"name":"%s"}`, newName)
return repo.gateway.UpdateResource(repo.config.ApiEndpoint(), path, strings.NewReader(body))
}
func (repo CloudControllerSpaceRepository) SetAllowSSH(spaceGuid string, allow bool) (apiErr error) {
path := fmt.Sprintf("/v2/spaces/%s", spaceGuid)
body := fmt.Sprintf(`{"allow_ssh":%t}`, allow)
return repo.gateway.UpdateResource(repo.config.ApiEndpoint(), path, strings.NewReader(body))
}
func (repo CloudControllerSpaceRepository) Delete(spaceGuid string) (apiErr error) {
path := fmt.Sprintf("/v2/spaces/%s?recursive=true", spaceGuid)
return repo.gateway.DeleteResource(repo.config.ApiEndpoint(), path)
}