forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.go
96 lines (85 loc) · 2.41 KB
/
services.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
package cfclient
import (
"encoding/json"
"io/ioutil"
"net/url"
"github.com/pkg/errors"
)
type ServicesResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []ServicesResource `json:"resources"`
}
type ServicesResource struct {
Meta Meta `json:"metadata"`
Entity Service `json:"entity"`
}
type Service struct {
Guid string `json:"guid"`
Label string `json:"label"`
Description string `json:"description"`
Active bool `json:"active"`
Bindable bool `json:"bindable"`
ServiceBrokerGuid string `json:"service_broker_guid"`
PlanUpdateable bool `json:"plan_updateable"`
Tags []string `json:"tags"`
c *Client
}
type ServiceSummary struct {
Guid string `json:"guid"`
Name string `json:"name"`
BoundAppCount int `json:"bound_app_count"`
}
func (c *Client) GetServiceByGuid(guid string) (Service, error) {
var serviceRes ServicesResource
r := c.NewRequest("GET", "/v2/services/"+guid)
resp, err := c.DoRequest(r)
if err != nil {
return Service{}, err
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return Service{}, err
}
err = json.Unmarshal(body, &serviceRes)
if err != nil {
return Service{}, err
}
serviceRes.Entity.Guid = serviceRes.Meta.Guid
return serviceRes.Entity, nil
}
func (c *Client) ListServicesByQuery(query url.Values) ([]Service, error) {
var services []Service
requestUrl := "/v2/services?" + query.Encode()
for {
var serviceResp ServicesResponse
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return nil, errors.Wrap(err, "Error requesting services")
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Wrap(err, "Error reading services request:")
}
err = json.Unmarshal(resBody, &serviceResp)
if err != nil {
return nil, errors.Wrap(err, "Error unmarshaling services")
}
for _, service := range serviceResp.Resources {
service.Entity.Guid = service.Meta.Guid
service.Entity.c = c
services = append(services, service.Entity)
}
requestUrl = serviceResp.NextUrl
if requestUrl == "" {
break
}
}
return services, nil
}
func (c *Client) ListServices() ([]Service, error) {
return c.ListServicesByQuery(nil)
}