forked from cloudfoundry/cli
/
space.go
160 lines (139 loc) · 4.46 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package ccv2
import (
"net/url"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// Space represents a Cloud Controller Space.
type Space struct {
// GUID is the unique space identifier.
GUID string
// OrganizationGUID is the unique identifier of the organization this space
// belongs to.
OrganizationGUID string
// Name is the name given to the space.
Name string
// AllowSSH specifies whether SSH is enabled for this space.
AllowSSH bool
// SpaceQuotaDefinitionGUID is the unique identifier of the space quota
// defined for this space.
SpaceQuotaDefinitionGUID string
}
// UnmarshalJSON helps unmarshal a Cloud Controller Space response.
func (space *Space) UnmarshalJSON(data []byte) error {
var ccSpace struct {
Metadata internal.Metadata `json:"metadata"`
Entity struct {
Name string `json:"name"`
AllowSSH bool `json:"allow_ssh"`
SpaceQuotaDefinitionGUID string `json:"space_quota_definition_guid"`
OrganizationGUID string `json:"organization_guid"`
} `json:"entity"`
}
err := cloudcontroller.DecodeJSON(data, &ccSpace)
if err != nil {
return err
}
space.GUID = ccSpace.Metadata.GUID
space.Name = ccSpace.Entity.Name
space.AllowSSH = ccSpace.Entity.AllowSSH
space.SpaceQuotaDefinitionGUID = ccSpace.Entity.SpaceQuotaDefinitionGUID
space.OrganizationGUID = ccSpace.Entity.OrganizationGUID
return nil
}
// DeleteSpace deletes the Space associated with the provided
// GUID. It will return the Cloud Controller job that is assigned to the
// Space deletion.
func (client *Client) DeleteSpace(guid string) (Job, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteSpaceRequest,
URIParams: Params{"space_guid": guid},
Query: url.Values{
"recursive": {"true"},
"async": {"true"},
},
})
if err != nil {
return Job{}, nil, err
}
var job Job
response := cloudcontroller.Response{
Result: &job,
}
err = client.connection.Make(request, &response)
return job, response.Warnings, err
}
// GetSecurityGroupSpaces returns a list of Spaces based on the provided
// SecurityGroup GUID.
func (client *Client) GetSecurityGroupSpaces(securityGroupGUID string) ([]Space, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSecurityGroupSpacesRequest,
URIParams: map[string]string{"security_group_guid": securityGroupGUID},
})
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
}
// GetSecurityGroupStagingSpaces returns a list of Spaces based on the provided
// SecurityGroup GUID.
func (client *Client) GetSecurityGroupStagingSpaces(securityGroupGUID string) ([]Space, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSecurityGroupStagingSpacesRequest,
URIParams: map[string]string{"security_group_guid": securityGroupGUID},
})
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
}
// GetSpaces returns a list of Spaces based off of the provided filters.
func (client *Client) GetSpaces(filters ...Filter) ([]Space, Warnings, error) {
params := ConvertFilterParameters(filters)
params.Add("order-by", "name")
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSpacesRequest,
Query: params,
})
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
}