forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain.go
132 lines (113 loc) · 3.47 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
package ccv2
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// Domain represents a Cloud Controller Domain.
type Domain struct {
GUID string
Name string
}
// UnmarshalJSON helps unmarshal a Cloud Controller Domain response.
func (domain *Domain) UnmarshalJSON(data []byte) error {
var ccDomain struct {
Metadata internal.Metadata `json:"metadata"`
Entity struct {
Name string `json:"name"`
} `json:"entity"`
}
if err := json.Unmarshal(data, &ccDomain); err != nil {
return err
}
domain.GUID = ccDomain.Metadata.GUID
domain.Name = ccDomain.Entity.Name
return nil
}
// GetSharedDomain returns the Shared Domain associated with the provided
// Domain GUID.
func (client *Client) GetSharedDomain(domainGUID string) (Domain, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSharedDomainRequest,
URIParams: map[string]string{"shared_domain_guid": domainGUID},
})
if err != nil {
return Domain{}, nil, err
}
var domain Domain
response := cloudcontroller.Response{
Result: &domain,
}
err = client.connection.Make(request, &response)
if err != nil {
return Domain{}, response.Warnings, err
}
return domain, response.Warnings, nil
}
// GetPrivateDomain returns the Private Domain associated with the provided
// Domain GUID.
func (client *Client) GetPrivateDomain(domainGUID string) (Domain, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetPrivateDomainRequest,
URIParams: map[string]string{"private_domain_guid": domainGUID},
})
if err != nil {
return Domain{}, nil, err
}
var domain Domain
response := cloudcontroller.Response{
Result: &domain,
}
err = client.connection.Make(request, &response)
if err != nil {
return Domain{}, response.Warnings, err
}
return domain, response.Warnings, nil
}
// GetSharedDomains returns the global shared domains.
func (client *Client) GetSharedDomains() ([]Domain, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSharedDomainsRequest,
})
if err != nil {
return []Domain{}, nil, err
}
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
}
// GetOrganizationPrivateDomains returns the private domains associated with an organization.
func (client *Client) GetOrganizationPrivateDomains(orgGUID string, queries []Query) ([]Domain, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetOrganizationPrivateDomainsRequest,
Query: FormatQueryParameters(queries),
URIParams: map[string]string{"organization_guid": orgGUID},
})
if err != nil {
return []Domain{}, nil, err
}
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
}