forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildpacks.go
69 lines (62 loc) · 1.75 KB
/
buildpacks.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
package cfclient
import (
"encoding/json"
"io/ioutil"
"github.com/pkg/errors"
)
type BuildpackResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []BuildpackResource `json:"resources"`
}
type BuildpackResource struct {
Meta Meta `json:"metadata"`
Entity Buildpack `json:"entity"`
}
type Buildpack struct {
Guid string `json:"guid"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
Locked bool `json:"locked"`
Filename string `json:"filename"`
c *Client
}
func (c *Client) ListBuildpacks() ([]Buildpack, error) {
var buildpacks []Buildpack
requestUrl := "/v2/buildpacks"
for {
buildpackResp, err := c.getBuildpackResponse(requestUrl)
if err != nil {
return []Buildpack{}, err
}
for _, buildpack := range buildpackResp.Resources {
buildpack.Entity.Guid = buildpack.Meta.Guid
buildpack.Entity.c = c
buildpacks = append(buildpacks, buildpack.Entity)
}
requestUrl = buildpackResp.NextUrl
if requestUrl == "" {
break
}
}
return buildpacks, nil
}
func (c *Client) getBuildpackResponse(requestUrl string) (BuildpackResponse, error) {
var buildpackResp BuildpackResponse
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return BuildpackResponse{}, errors.Wrap(err, "Error requesting buildpacks")
}
resBody, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return BuildpackResponse{}, errors.Wrap(err, "Error reading buildpack request")
}
err = json.Unmarshal(resBody, &buildpackResp)
if err != nil {
return BuildpackResponse{}, errors.Wrap(err, "Error unmarshalling buildpack")
}
return buildpackResp, nil
}