-
Notifications
You must be signed in to change notification settings - Fork 0
/
stacks.go
107 lines (94 loc) · 2.79 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package api
import (
"context"
"encoding/json"
"fmt"
"gopkg.in/nullstone-io/go-api-client.v0/response"
"gopkg.in/nullstone-io/go-api-client.v0/types"
"net/http"
"net/url"
"strconv"
)
type Stacks struct {
Client *Client
}
func (s Stacks) basePath() string {
return fmt.Sprintf("orgs/%s/stacks", s.Client.Config.OrgName)
}
func (s Stacks) stackPath(stackId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d", s.Client.Config.OrgName, stackId)
}
// List - GET /orgs/:orgName/stacks
func (s Stacks) List(ctx context.Context) ([]*types.Stack, error) {
res, err := s.Client.Do(ctx, http.MethodGet, s.basePath(), nil, nil, nil)
if err != nil {
return nil, err
}
var stacks []*types.Stack
if err := response.ReadJson(res, &stacks); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return stacks, nil
}
// Get - GET /orgs/:orgName/stacks/:id
func (s Stacks) Get(ctx context.Context, stackId int64, includeArchived bool) (*types.Stack, error) {
q := url.Values{
"include_archived": []string{strconv.FormatBool(includeArchived)},
}
res, err := s.Client.Do(ctx, http.MethodGet, s.stackPath(stackId), q, nil, nil)
if err != nil {
return nil, err
}
var stack types.Stack
if err := response.ReadJson(res, &stack); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &stack, nil
}
// Create - POST /orgs/:orgName/stacks
func (s Stacks) Create(ctx context.Context, stack *types.Stack) (*types.Stack, error) {
rawPayload, _ := json.Marshal(stack)
res, err := s.Client.Do(ctx, http.MethodPost, s.basePath(), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
var updatedStack types.Stack
if err := response.ReadJson(res, &updatedStack); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &updatedStack, nil
}
// Update - PUT/PATCH /orgs/:orgName/stacks/:id
func (s Stacks) Update(ctx context.Context, stackId int64, stack *types.Stack) (*types.Stack, error) {
rawPayload, _ := json.Marshal(stack)
res, err := s.Client.Do(ctx, http.MethodPut, s.stackPath(stackId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
var updatedStack types.Stack
if err := response.ReadJson(res, &updatedStack); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &updatedStack, nil
}
// Destroy - DELETE /orgs/:orgName/stacks/:id
func (s Stacks) Destroy(ctx context.Context, stackId int64) (bool, error) {
res, err := s.Client.Do(ctx, http.MethodDelete, s.stackPath(stackId), nil, nil, nil)
if err != nil {
return false, err
}
if err := response.Verify(res); response.IsNotFoundError(err) {
return false, nil
} else if err != nil {
return false, err
}
return true, nil
}