forked from cloudfoundry/go-cfclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacks.go
76 lines (68 loc) · 1.88 KB
/
stacks.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
package cfclient
import (
"encoding/json"
"io/ioutil"
"net/url"
"github.com/pkg/errors"
)
type StacksResponse struct {
Count int `json:"total_results"`
Pages int `json:"total_pages"`
NextUrl string `json:"next_url"`
Resources []StacksResource `json:"resources"`
}
type StacksResource struct {
Meta Meta `json:"metadata"`
Entity Stack `json:"entity"`
}
type Stack struct {
Guid string `json:"guid"`
Name string `json:"name"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Description string `json:"description"`
c *Client
}
func (c *Client) ListStacksByQuery(query url.Values) ([]Stack, error) {
var stacks []Stack
requestUrl := "/v2/stacks?" + query.Encode()
for {
stacksResp, err := c.getStacksResponse(requestUrl)
if err != nil {
return []Stack{}, err
}
for _, stack := range stacksResp.Resources {
stack.Entity.Guid = stack.Meta.Guid
stack.Entity.CreatedAt = stack.Meta.CreatedAt
stack.Entity.UpdatedAt = stack.Meta.UpdatedAt
stack.Entity.c = c
stacks = append(stacks, stack.Entity)
}
requestUrl = stacksResp.NextUrl
if requestUrl == "" {
break
}
}
return stacks, nil
}
func (c *Client) ListStacks() ([]Stack, error) {
return c.ListStacksByQuery(nil)
}
func (c *Client) getStacksResponse(requestUrl string) (StacksResponse, error) {
var stacksResp StacksResponse
r := c.NewRequest("GET", requestUrl)
resp, err := c.DoRequest(r)
if err != nil {
return StacksResponse{}, errors.Wrap(err, "Error requesting stacks")
}
resBody, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return StacksResponse{}, errors.Wrap(err, "Error reading stacks body")
}
err = json.Unmarshal(resBody, &stacksResp)
if err != nil {
return StacksResponse{}, errors.Wrap(err, "Error unmarshalling stacks")
}
return stacksResp, nil
}