forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
route_mappings.go
159 lines (143 loc) · 4.41 KB
/
route_mappings.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
package cfclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type RouteMappingRequest struct {
AppGUID string `json:"app_guid"`
RouteGUID string `json:"route_guid"`
AppPort int `json:"app_port"`
}
type RouteMappingResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []RouteMappingResource `json:"resources"`
}
type RouteMapping struct {
Guid string `json:"guid"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
AppPort int `json:"app_port"`
AppGUID string `json:"app_guid"`
RouteGUID string `json:"route_guid"`
AppUrl string `json:"app_url"`
RouteUrl string `json:"route_url"`
c *Client
}
type RouteMappingResource struct {
Meta Meta `json:"metadata"`
Entity RouteMapping `json:"entity"`
}
func (c *Client) MappingAppAndRoute(req RouteMappingRequest) (*RouteMapping, error) {
buf := bytes.NewBuffer(nil)
err := json.NewEncoder(buf).Encode(req)
if err != nil {
return nil, err
}
r := c.NewRequestWithBody("POST", "/v2/route_mappings", buf)
resp, err := c.DoRequest(r)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("CF API returned with status code %d", resp.StatusCode)
}
return c.handleMappingResp(resp)
}
func (c *Client) ListRouteMappings() ([]*RouteMapping, error) {
return c.ListRouteMappingsByQuery(nil)
}
func (c *Client) ListRouteMappingsByQuery(query url.Values) ([]*RouteMapping, error) {
var routeMappings []*RouteMapping
var routeMappingsResp RouteMappingResponse
pages := 0
requestUrl := "/v2/route_mappings?" + query.Encode()
for {
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting route mappings")
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading route mappings request:")
}
err = json.Unmarshal(resBody, &routeMappingsResp)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshalling route mappings")
}
for _, routeMapping := range routeMappingsResp.Resources {
routeMappings = append(routeMappings, c.mergeRouteMappingResource(routeMapping))
}
requestUrl = routeMappingsResp.NextUrl
if requestUrl == "" {
break
}
pages++
totalPages := routeMappingsResp.Pages
if totalPages > 0 && pages >= totalPages {
break
}
}
return routeMappings, nil
}
func (c *Client) GetRouteMappingByGuid(guid string) (*RouteMapping, error) {
var routeMapping RouteMappingResource
requestUrl := fmt.Sprintf("/v2/route_mappings/%s", guid)
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting route mapping")
}
defer resp.Body.Close()
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading route mapping response body")
}
err = json.Unmarshal(resBody, &routeMapping)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshalling route mapping")
}
routeMapping.Entity.Guid = routeMapping.Meta.Guid
routeMapping.Entity.CreatedAt = routeMapping.Meta.CreatedAt
routeMapping.Entity.UpdatedAt = routeMapping.Meta.UpdatedAt
routeMapping.Entity.c = c
return &routeMapping.Entity, nil
}
func (c *Client) DeleteRouteMapping(guid string) error {
requestUrl := fmt.Sprintf("/v2/route_mappings/%s?", guid)
resp, err := c.DoRequest(c.NewRequest("DELETE", requestUrl))
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error deleting route mapping %s, response code %d", guid, resp.StatusCode)
}
return nil
}
func (c *Client) handleMappingResp(resp *http.Response) (*RouteMapping, error) {
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
}
var mappingResource RouteMappingResource
err = json.Unmarshal(body, &mappingResource)
if err != nil {
return nil, err
}
return c.mergeRouteMappingResource(mappingResource), nil
}
func (c *Client) mergeRouteMappingResource(mapping RouteMappingResource) *RouteMapping {
mapping.Entity.Guid = mapping.Meta.Guid
mapping.Entity.CreatedAt = mapping.Meta.CreatedAt
mapping.Entity.UpdatedAt = mapping.Meta.UpdatedAt
mapping.Entity.c = c
return &mapping.Entity
}