-
Notifications
You must be signed in to change notification settings - Fork 0
/
space.go
116 lines (99 loc) · 2.99 KB
/
space.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
package ccv3
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
// Space represents a Cloud Controller V3 Space.
type Space struct {
// GUID is a unique space identifier.
GUID string `json:"guid,omitempty"`
// Name is the name of the space.
Name string `json:"name"`
// Relationships list the relationships to the space.
Relationships Relationships `json:"relationships,omitempty"`
// Metadata is used for custom tagging of API resources
Metadata *Metadata `json:"metadata,omitempty"`
}
func (client *Client) CreateSpace(space Space) (Space, Warnings, error) {
spaceBytes, err := json.Marshal(space)
if err != nil {
return Space{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostSpaceRequest,
Body: bytes.NewReader(spaceBytes),
})
if err != nil {
return Space{}, nil, err
}
var responseSpace Space
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responseSpace,
}
err = client.connection.Make(request, &response)
return responseSpace, response.Warnings, err
}
func (client *Client) DeleteSpace(spaceGUID string) (JobURL, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteSpaceRequest,
URIParams: map[string]string{"space_guid": spaceGUID},
})
if err != nil {
return "", nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return JobURL(response.ResourceLocationURL), response.Warnings, err
}
// GetSpaces lists spaces with optional filters.
func (client *Client) GetSpaces(query ...Query) ([]Space, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSpacesRequest,
Query: query,
})
if err != nil {
return nil, nil, err
}
var fullSpacesList []Space
warnings, err := client.paginate(request, Space{}, func(item interface{}) error {
if space, ok := item.(Space); ok {
fullSpacesList = append(fullSpacesList, space)
} else {
return ccerror.UnknownObjectInListError{
Expected: Space{},
Unexpected: item,
}
}
return nil
})
return fullSpacesList, warnings, err
}
func (client *Client) UpdateSpace(space Space) (Space, Warnings, error) {
spaceGUID := space.GUID
space.GUID = ""
space.Relationships = nil
spaceBytes, err := json.Marshal(space)
if err != nil {
return Space{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PatchSpaceRequest,
Body: bytes.NewReader(spaceBytes),
URIParams: map[string]string{"space_guid": spaceGUID},
})
if err != nil {
return Space{}, nil, err
}
var responseSpace Space
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responseSpace,
}
err = client.connection.Make(request, &response)
if err != nil {
return Space{}, nil, err
}
return responseSpace, response.Warnings, err
}