forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relationship.go
200 lines (169 loc) · 6.24 KB
/
relationship.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package ccv3
import (
"bytes"
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
)
// Relationship represents a one to one relationship.
// An empty GUID will be marshaled as `null`.
type Relationship struct {
GUID string
}
func (r Relationship) MarshalJSON() ([]byte, error) {
if r.GUID == "" {
var emptyCCRelationship struct {
Data interface{} `json:"data"`
}
return json.Marshal(emptyCCRelationship)
}
var ccRelationship struct {
Data struct {
GUID string `json:"guid"`
} `json:"data"`
}
ccRelationship.Data.GUID = r.GUID
return json.Marshal(ccRelationship)
}
func (r *Relationship) UnmarshalJSON(data []byte) error {
var ccRelationship struct {
Data struct {
GUID string `json:"guid"`
} `json:"data"`
}
err := cloudcontroller.DecodeJSON(data, &ccRelationship)
if err != nil {
return err
}
r.GUID = ccRelationship.Data.GUID
return nil
}
// DeleteIsolationSegmentOrganization will delete the relationship between
// the isolation segment and the organization provided.
func (client *Client) DeleteIsolationSegmentOrganization(isolationSegmentGUID string, orgGUID string) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteIsolationSegmentRelationshipOrganizationRequest,
URIParams: internal.Params{"isolation_segment_guid": isolationSegmentGUID, "organization_guid": orgGUID},
})
if err != nil {
return nil, err
}
var response cloudcontroller.Response
err = client.connection.Make(request, &response)
return response.Warnings, err
}
// DeleteServiceInstanceRelationshipsSharedSpace will delete the sharing relationship
// between the service instance and the shared-to space provided.
func (client *Client) DeleteServiceInstanceRelationshipsSharedSpace(serviceInstanceGUID string, spaceGUID string) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.DeleteServiceInstanceRelationshipsSharedSpaceRequest,
URIParams: internal.Params{"service_instance_guid": serviceInstanceGUID, "space_guid": spaceGUID},
})
if err != nil {
return nil, err
}
response := cloudcontroller.Response{}
err = client.connection.Make(request, &response)
return response.Warnings, err
}
// GetOrganizationDefaultIsolationSegment returns the relationship between an
// organization and it's default isolation segment.
func (client *Client) GetOrganizationDefaultIsolationSegment(orgGUID string) (Relationship, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetOrganizationRelationshipDefaultIsolationSegmentRequest,
URIParams: internal.Params{"organization_guid": orgGUID},
})
if err != nil {
return Relationship{}, nil, err
}
var relationship Relationship
response := cloudcontroller.Response{
DecodeJSONResponseInto: &relationship,
}
err = client.connection.Make(request, &response)
return relationship, response.Warnings, err
}
// GetSpaceIsolationSegment returns the relationship between a space and it's
// isolation segment.
func (client *Client) GetSpaceIsolationSegment(spaceGUID string) (Relationship, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSpaceRelationshipIsolationSegmentRequest,
URIParams: internal.Params{"space_guid": spaceGUID},
})
if err != nil {
return Relationship{}, nil, err
}
var relationship Relationship
response := cloudcontroller.Response{
DecodeJSONResponseInto: &relationship,
}
err = client.connection.Make(request, &response)
return relationship, response.Warnings, err
}
// SetApplicationDroplet sets the specified droplet on the given application.
func (client *Client) SetApplicationDroplet(appGUID string, dropletGUID string) (Relationship, Warnings, error) {
relationship := Relationship{GUID: dropletGUID}
bodyBytes, err := json.Marshal(relationship)
if err != nil {
return Relationship{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PatchApplicationCurrentDropletRequest,
URIParams: map[string]string{"app_guid": appGUID},
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return Relationship{}, nil, err
}
var responseRelationship Relationship
response := cloudcontroller.Response{
DecodeJSONResponseInto: &responseRelationship,
}
err = client.connection.Make(request, &response)
return responseRelationship, response.Warnings, err
}
// UpdateOrganizationDefaultIsolationSegmentRelationship sets the default isolation segment
// for an organization on the controller.
// If isoSegGuid is empty it will reset the default isolation segment.
func (client *Client) UpdateOrganizationDefaultIsolationSegmentRelationship(orgGUID string, isoSegGUID string) (Relationship, Warnings, error) {
body, err := json.Marshal(Relationship{GUID: isoSegGUID})
if err != nil {
return Relationship{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PatchOrganizationRelationshipDefaultIsolationSegmentRequest,
Body: bytes.NewReader(body),
URIParams: internal.Params{"organization_guid": orgGUID},
})
if err != nil {
return Relationship{}, nil, err
}
var relationship Relationship
response := cloudcontroller.Response{
DecodeJSONResponseInto: &relationship,
}
err = client.connection.Make(request, &response)
return relationship, response.Warnings, err
}
// UpdateSpaceIsolationSegmentRelationship assigns an isolation segment to a space and
// returns the relationship.
func (client *Client) UpdateSpaceIsolationSegmentRelationship(spaceGUID string, isolationSegmentGUID string) (Relationship, Warnings, error) {
body, err := json.Marshal(Relationship{GUID: isolationSegmentGUID})
if err != nil {
return Relationship{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PatchSpaceRelationshipIsolationSegmentRequest,
URIParams: internal.Params{"space_guid": spaceGUID},
Body: bytes.NewReader(body),
})
if err != nil {
return Relationship{}, nil, err
}
var relationship Relationship
response := cloudcontroller.Response{
DecodeJSONResponseInto: &relationship,
}
err = client.connection.Make(request, &response)
return relationship, response.Warnings, err
}