-
Notifications
You must be signed in to change notification settings - Fork 928
/
space.go
136 lines (118 loc) · 4.12 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
package ccv2
import (
"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
}
//go:generate go run $GOPATH/src/code.cloudfoundry.org/cli/util/codegen/generate.go Space codetemplates/delete_async_by_guid.go.template delete_space.go
//go:generate go run $GOPATH/src/code.cloudfoundry.org/cli/util/codegen/generate.go Space codetemplates/delete_async_by_guid_test.go.template delete_space_test.go
// 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
}
// 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
}
// 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
}