This repository has been archived by the owner on Jan 21, 2022. It is now read-only.
/
stack.go
80 lines (69 loc) · 2 KB
/
stack.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
package ccv2
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
)
// Stack represents a Cloud Controller Stack.
type Stack struct {
GUID string
Name string
Description string
}
// UnmarshalJSON helps unmarshal a Cloud Controller Stack response.
func (stack *Stack) UnmarshalJSON(data []byte) error {
var ccStack struct {
Metadata internal.Metadata `json:"metadata"`
Entity struct {
Name string `json:"name"`
Description string `json:"description"`
} `json:"entity"`
}
if err := json.Unmarshal(data, &ccStack); err != nil {
return err
}
stack.GUID = ccStack.Metadata.GUID
stack.Name = ccStack.Entity.Name
stack.Description = ccStack.Entity.Description
return nil
}
// GetStack returns the requested stack.
func (client *Client) GetStack(guid string) (Stack, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetStackRequest,
URIParams: Params{"stack_guid": guid},
})
if err != nil {
return Stack{}, nil, err
}
var stack Stack
response := cloudcontroller.Response{
Result: &stack,
}
err = client.connection.Make(request, &response)
return stack, response.Warnings, err
}
// GetStacks returns a list of Stacks based off of the provided queries.
func (client *Client) GetStacks(queries ...Query) ([]Stack, Warnings, error) {
request, err := client.newHTTPRequest(requestOptions{
RequestName: internal.GetStacksRequest,
Query: FormatQueryParameters(queries),
})
if err != nil {
return nil, nil, err
}
var fullStacksList []Stack
warnings, err := client.paginate(request, Stack{}, func(item interface{}) error {
if space, ok := item.(Stack); ok {
fullStacksList = append(fullStacksList, space)
} else {
return ccerror.UnknownObjectInListError{
Expected: Stack{},
Unexpected: item,
}
}
return nil
})
return fullStacksList, warnings, err
}