forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
130 lines (117 loc) · 3.49 KB
/
routes.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
package cfclient
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"github.com/pkg/errors"
)
type RoutesResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []RoutesResource `json:"resources"`
}
type RoutesResource struct {
Meta Meta `json:"metadata"`
Entity Route `json:"entity"`
}
type RouteRequest struct {
DomainGuid string `json:"domain_guid"`
SpaceGuid string `json:"space_guid"`
}
type Route struct {
Guid string `json:"guid"`
Host string `json:"host"`
Path string `json:"path"`
DomainGuid string `json:"domain_guid"`
SpaceGuid string `json:"space_guid"`
ServiceInstanceGuid string `json:"service_instance_guid"`
Port int `json:"port"`
c *Client
}
func (c *Client) CreateTcpRoute(routeRequest RouteRequest) (Route, error) {
routesResource, err := c.createRoute("/v2/routes?generate_port=true", routeRequest)
if nil != err {
return Route{}, err
}
return routesResource.Entity, nil
}
func (c *Client) ListRoutesByQuery(query url.Values) ([]Route, error) {
return c.fetchRoutes("/v2/routes?" + query.Encode())
}
func (c *Client) fetchRoutes(requestUrl string) ([]Route, error) {
var routes []Route
for {
routesResp, err := c.getRoutesResponse(requestUrl)
if err != nil {
return []Route{}, err
}
for _, route := range routesResp.Resources {
route.Entity.Guid = route.Meta.Guid
route.Entity.c = c
routes = append(routes, route.Entity)
}
requestUrl = routesResp.NextUrl
if requestUrl == "" {
break
}
}
return routes, nil
}
func (c *Client) ListRoutes() ([]Route, error) {
return c.ListRoutesByQuery(nil)
}
func (c *Client) getRoutesResponse(requestUrl string) (RoutesResponse, error) {
var routesResp RoutesResponse
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return RoutesResponse{}, errors.Wrap(err, "Error requesting routes")
}
resBody, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return RoutesResponse{}, errors.Wrap(err, "Error reading routes body")
}
err = json.Unmarshal(resBody, &routesResp)
if err != nil {
return RoutesResponse{}, errors.Wrap(err, "Error unmarshalling routes")
}
return routesResp, nil
}
func (c *Client) createRoute(requestUrl string, routeRequest RouteRequest) (RoutesResource, error) {
var routeResp RoutesResource
buf := bytes.NewBuffer(nil)
err := json.NewEncoder(buf).Encode(routeRequest)
if err != nil {
return RoutesResource{}, errors.Wrap(err, "Error creating route - failed to serialize request body")
}
r := c.NewRequestWithBody("POST", requestUrl, buf)
resp, err := c.DoRequest(r)
if err != nil {
return RoutesResource{}, errors.Wrap(err, "Error creating route")
}
resBody, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return RoutesResource{}, errors.Wrap(err, "Error creating route")
}
err = json.Unmarshal(resBody, &routeResp)
if err != nil {
return RoutesResource{}, errors.Wrap(err, "Error unmarshalling routes")
}
return routeResp, nil
}
func (c *Client) DeleteRoute(guid string) error {
resp, err := c.DoRequest(c.NewRequest("DELETE", fmt.Sprintf("/v2/routes/%s", guid)))
if err != nil {
return err
}
if resp.StatusCode != http.StatusNoContent {
return errors.Wrapf(err, "Error deleting route %s, response code: %d", guid, resp.StatusCode)
}
return nil
}