forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.go
61 lines (52 loc) · 1.43 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
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"`
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"`
c *Client
}
type ServiceSummary struct {
Guid string `json:"guid"`
Name string `json:"name"`
BoundAppCount int `json:"bound_app_count"`
}
func (c *Client) ListServicesByQuery(query url.Values) ([]Service, error) {
var services []Service
var serviceResp ServicesResponse
r := c.NewRequest("GET", "/v2/services?"+query.Encode())
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)
}
return services, nil
}
func (c *Client) ListServices() ([]Service, error) {
return c.ListServicesByQuery(nil)
}