-
Notifications
You must be signed in to change notification settings - Fork 928
/
domain.go
156 lines (134 loc) · 4.52 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
package ccv2
import (
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// Domain represents a Cloud Controller Domain.
type Domain struct {
// GUID is the unique domain identifier.
GUID string
// Name is the name given to the domain.
Name string
// RouterGroupGUID is the unique identier of the router group this domain is
// assigned to.
RouterGroupGUID string
// RouterGroupType is the type of router group this domain is assigned to. It
// can be of type `tcp` or `http`.
RouterGroupType constant.RouterGroupType
// DomainType is the access type of the domain. It can be either a domain
// private to a single org or it can be a domain shared to all orgs.
Type constant.DomainType
}
// 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"`
RouterGroupGUID string `json:"router_group_guid"`
RouterGroupType string `json:"router_group_type"`
} `json:"entity"`
}
err := cloudcontroller.DecodeJSON(data, &ccDomain)
if err != nil {
return err
}
domain.GUID = ccDomain.Metadata.GUID
domain.Name = ccDomain.Entity.Name
domain.RouterGroupGUID = ccDomain.Entity.RouterGroupGUID
domain.RouterGroupType = constant.RouterGroupType(ccDomain.Entity.RouterGroupType)
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
}
domain.Type = constant.SharedDomain
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
}
domain.Type = constant.PrivateDomain
return domain, response.Warnings, nil
}
// GetSharedDomains returns the global shared domains.
func (client *Client) GetSharedDomains(filters ...Filter) ([]Domain, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetSharedDomainsRequest,
Query: ConvertFilterParameters(filters),
})
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 {
domain.Type = constant.SharedDomain
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, filters ...Filter) ([]Domain, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetOrganizationPrivateDomainsRequest,
Query: ConvertFilterParameters(filters),
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 {
domain.Type = constant.PrivateDomain
fullDomainsList = append(fullDomainsList, domain)
} else {
return ccerror.UnknownObjectInListError{
Expected: Domain{},
Unexpected: item,
}
}
return nil
})
return fullDomainsList, warnings, err
}