forked from cloudfoundry/cli
/
domain.go
248 lines (201 loc) · 5.73 KB
/
domain.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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"
"code.cloudfoundry.org/cli/types"
)
type Domain struct {
GUID string `json:"guid,omitempty"`
Name string `json:"name"`
Internal types.NullBool `json:"internal,omitempty"`
OrganizationGUID string `json:"orgguid,omitempty"`
}
func (d Domain) MarshalJSON() ([]byte, error) {
type Data struct {
GUID string `json:"guid,omitempty"`
}
type OrgData struct {
Data Data `json:"data,omitempty"`
}
type OrgRelationship struct {
Org OrgData `json:"organization,omitempty"`
}
type ccDomain struct {
GUID string `json:"guid,omitempty"`
Name string `json:"name"`
Internal *bool `json:"internal,omitempty"`
Relationships *OrgRelationship `json:"relationships,omitempty"`
}
ccDom := ccDomain{
Name: d.Name,
}
if d.Internal.IsSet {
ccDom.Internal = &d.Internal.Value
}
if d.GUID != "" {
ccDom.GUID = d.GUID
}
if d.OrganizationGUID != "" {
ccDom.Relationships = &OrgRelationship{OrgData{Data{GUID: d.OrganizationGUID}}}
}
return json.Marshal(ccDom)
}
func (d *Domain) UnmarshalJSON(data []byte) error {
var alias struct {
GUID string `json:"guid,omitempty"`
Name string `json:"name"`
Internal types.NullBool `json:"internal,omitempty"`
Relationships struct {
Organization struct {
Data struct {
GUID string `json:"guid,omitempty"`
} `json:"data,omitempty"`
} `json:"organization,omitempty"`
} `json:"relationships,omitempty"`
}
err := cloudcontroller.DecodeJSON(data, &alias)
if err != nil {
return err
}
d.GUID = alias.GUID
d.Name = alias.Name
d.Internal = alias.Internal
d.OrganizationGUID = alias.Relationships.Organization.Data.GUID
return nil
}
type SharedOrgs struct {
GUIDs []string
}
func (sharedOrgs SharedOrgs) MarshalJSON() ([]byte, error) {
type Org struct {
GUID string `json:"guid,omitempty"`
}
type Data = []Org
type sharedOrgsRelationship struct {
Data Data `json:"data"`
}
var orgs []Org
for _, sharedOrgGUID := range sharedOrgs.GUIDs {
orgs = append(orgs, Org{GUID: sharedOrgGUID})
}
relationship := sharedOrgsRelationship{
Data: orgs,
}
return json.Marshal(relationship)
}
func (sharedOrgs *SharedOrgs) UnmarshalJSON(data []byte) error {
var alias struct {
Data []struct {
GUID string `json:"guid,omitempty"`
} `json:"data,omitempty"`
}
err := cloudcontroller.DecodeJSON(data, &alias)
if err != nil {
return err
}
var guids []string
for _, org := range alias.Data {
guids = append(guids, org.GUID)
}
sharedOrgs.GUIDs = guids
return nil
}
func (client Client) CreateDomain(domain Domain) (Domain, Warnings, error) {
bodyBytes, err := json.Marshal(domain)
if err != nil {
return Domain{}, nil, err
}
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.PostDomainRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return Domain{}, nil, err
}
var ccDomain Domain
response := cloudcontroller.Response{
DecodeJSONResponseInto: &ccDomain,
}
err = client.connection.Make(request, &response)
return ccDomain, response.Warnings, err
}
func (client Client) GetDomains(query ...Query) ([]Domain, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetDomainsRequest,
Query: query,
})
if err != nil {
return nil, nil, err
}
var fullDomainsList []Domain
warnings, err := client.paginate(request, Domain{}, func(item interface{}) error {
if domain, ok := item.(Domain); ok {
fullDomainsList = append(fullDomainsList, domain)
} else {
return ccerror.UnknownObjectInListError{
Expected: Domain{},
Unexpected: item,
}
}
return nil
})
return fullDomainsList, warnings, err
}
func (client Client) GetOrganizationDomains(orgGUID string, query ...Query) ([]Domain, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
URIParams: internal.Params{"organization_guid": orgGUID},
RequestName: internal.GetOrganizationDomainsRequest,
Query: query,
})
if err != nil {
return nil, nil, err
}
var fullDomainsList []Domain
warnings, err := client.paginate(request, Domain{}, func(item interface{}) error {
if domain, ok := item.(Domain); ok {
fullDomainsList = append(fullDomainsList, domain)
} else {
return ccerror.UnknownObjectInListError{
Expected: Domain{},
Unexpected: item,
}
}
return nil
})
return fullDomainsList, warnings, err
}
func (client Client) SharePrivateDomainToOrgs(domainGuid string, sharedOrgs SharedOrgs) (Warnings, error) {
bodyBytes, err := json.Marshal(sharedOrgs)
if err != nil {
return nil, err
}
request, err := client.newHTTPRequest(requestOptions{
URIParams: internal.Params{"domain_guid": domainGuid},
RequestName: internal.SharePrivateDomainRequest,
Body: bytes.NewReader(bodyBytes),
})
if err != nil {
return nil, err
}
var ccSharedOrgs SharedOrgs
response := cloudcontroller.Response{
DecodeJSONResponseInto: &ccSharedOrgs,
}
err = client.connection.Make(request, &response)
return response.Warnings, err
}
func (client Client) UnsharePrivateDomainFromOrg(domainGuid string, orgGUID string) (Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
URIParams: internal.Params{"domain_guid": domainGuid, "org_guid": orgGUID},
RequestName: internal.DeleteSharedOrgFromDomainRequest,
})
if err != nil {
return nil, err
}
var response cloudcontroller.Response
err = client.connection.Make(request, &response)
return response.Warnings, err
}