forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.go
More file actions
49 lines (43 loc) · 1.14 KB
/
Copy pathservices.go
File metadata and controls
49 lines (43 loc) · 1.14 KB
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
package cfclient
import (
"encoding/json"
"fmt"
"io/ioutil"
)
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
}
func (c *Client) ListServices() ([]Service, error) {
var services []Service
var serviceResp servicesResponse
r := c.NewRequest("GET", "/v2/services")
resp, err := c.DoRequest(r)
if err != nil {
return nil, fmt.Errorf("Error requesting services %v", err)
}
resBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Error reading services request: %v", err)
}
err = json.Unmarshal(resBody, &serviceResp)
if err != nil {
return nil, fmt.Errorf("Error unmarshaling services %v", err)
}
for _, service := range serviceResp.Resources {
service.Entity.Guid = service.Meta.Guid
service.Entity.c = c
services = append(services, service.Entity)
}
return services, nil
}