forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomains.go
194 lines (175 loc) · 5.17 KB
/
domains.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
package cfclient
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type DomainsResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []DomainResource `json:"resources"`
}
type SharedDomainsResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []SharedDomainResource `json:"resources"`
}
type DomainResource struct {
Meta Meta `json:"metadata"`
Entity Domain `json:"entity"`
}
type SharedDomainResource struct {
Meta Meta `json:"metadata"`
Entity SharedDomain `json:"entity"`
}
type Domain struct {
Guid string `json:"guid"`
Name string `json:"name"`
OwningOrganizationGuid string `json:"owning_organization_guid"`
OwningOrganizationUrl string `json:"owning_organization_url"`
SharedOrganizationsUrl string `json:"shared_organizations_url"`
c *Client
}
type SharedDomain struct {
Guid string `json:"guid"`
Name string `json:"name"`
RouterGroupGuid string `json:"router_group_guid"`
RouterGroupType string `json:"router_group_type"`
c *Client
}
func (c *Client) ListDomainsByQuery(query url.Values) ([]Domain, error) {
var domains []Domain
requestUrl := "/v2/private_domains?" + query.Encode()
for {
var domainResp DomainsResponse
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting domains")
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading domains request")
}
err = json.Unmarshal(resBody, &domainResp)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshaling domains")
}
for _, domain := range domainResp.Resources {
domain.Entity.Guid = domain.Meta.Guid
domain.Entity.c = c
domains = append(domains, domain.Entity)
}
requestUrl = domainResp.NextUrl
if requestUrl == "" {
break
}
}
return domains, nil
}
func (c *Client) ListDomains() ([]Domain, error) {
return c.ListDomainsByQuery(nil)
}
func (c *Client) ListSharedDomainsByQuery(query url.Values) ([]SharedDomain, error) {
var domains []SharedDomain
requestUrl := "/v2/shared_domains?" + query.Encode()
for {
var domainResp SharedDomainsResponse
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting shared domains")
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading shared domains request")
}
err = json.Unmarshal(resBody, &domainResp)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshaling shared domains")
}
for _, domain := range domainResp.Resources {
domain.Entity.Guid = domain.Meta.Guid
domain.Entity.c = c
domains = append(domains, domain.Entity)
}
requestUrl = domainResp.NextUrl
if requestUrl == "" {
break
}
}
return domains, nil
}
func (c *Client) ListSharedDomains() ([]SharedDomain, error) {
return c.ListSharedDomainsByQuery(nil)
}
func (c *Client) GetDomainByName(name string) (Domain, error) {
q := url.Values{}
q.Set("q", "name:"+name)
domains, err := c.ListDomainsByQuery(q)
if err != nil {
return Domain{}, errors.Wrapf(err, "Error during domain lookup %s", name)
}
if len(domains) == 0 {
return Domain{}, fmt.Errorf("Unable to find domain %s", name)
}
return domains[0], nil
}
func (c *Client) GetSharedDomainByName(name string) (SharedDomain, error) {
q := url.Values{}
q.Set("q", "name:"+name)
domains, err := c.ListSharedDomainsByQuery(q)
if err != nil {
return SharedDomain{}, errors.Wrapf(err, "Error during shared domain lookup %s", name)
}
if len(domains) == 0 {
return SharedDomain{}, fmt.Errorf("Unable to find shared domain %s", name)
}
return domains[0], nil
}
func (c *Client) CreateDomain(name, orgGuid string) (*Domain, error) {
req := c.NewRequest("POST", "/v2/private_domains")
req.obj = map[string]interface{}{
"name": name,
"owning_organization_guid": orgGuid,
}
resp, err := c.DoRequest(req)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusCreated {
return nil, errors.Wrapf(err, "Error creating domain %s, response code: %d", name, resp.StatusCode)
}
return respBodyToDomain(resp.Body, c)
}
func (c *Client) DeleteDomain(guid string) error {
resp, err := c.DoRequest(c.NewRequest("DELETE", fmt.Sprintf("/v2/private_domains/%s", guid)))
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error deleting domain %s, response code: %d", guid, resp.StatusCode)
}
return nil
}
func respBodyToDomain(body io.ReadCloser, c *Client) (*Domain, error) {
bodyRaw, err := ioutil.ReadAll(body)
if err != nil {
return nil, err
}
domainRes := DomainResource{}
err = json.Unmarshal(bodyRaw, &domainRes)
if err != nil {
return nil, err
}
domain := domainRes.Entity
domain.Guid = domainRes.Meta.Guid
domain.c = c
return &domain, nil
}