forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spaces.go
89 lines (76 loc) · 3.05 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
package spaces
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 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) (space models.Space, apiErr error)
Rename(spaceGuid, newName string) (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 string, orgGuid string) (space models.Space, apiErr 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(resources.SpaceResource)
apiErr = repo.gateway.CreateResource(path, strings.NewReader(body), resource)
if apiErr != nil {
return
}
space = resource.ToModel()
return
}
func (repo CloudControllerSpaceRepository) Rename(spaceGuid, newName string) (apiErr error) {
path := fmt.Sprintf("%s/v2/spaces/%s", repo.config.ApiEndpoint(), spaceGuid)
body := fmt.Sprintf(`{"name":"%s"}`, newName)
return repo.gateway.UpdateResource(path, strings.NewReader(body))
}
func (repo CloudControllerSpaceRepository) Delete(spaceGuid string) (apiErr error) {
path := fmt.Sprintf("%s/v2/spaces/%s?recursive=true", repo.config.ApiEndpoint(), spaceGuid)
return repo.gateway.DeleteResource(path)
}