-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironments.go
101 lines (88 loc) · 2.92 KB
/
environments.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
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 Environments struct {
Client *Client
}
func (s Environments) basePath(stackId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/envs", s.Client.Config.OrgName, stackId)
}
func (s Environments) envPath(stackId, envId int64) string {
return fmt.Sprintf("orgs/%s/stacks/%d/envs/%d", s.Client.Config.OrgName, stackId, envId)
}
// List - GET /orgs/:orgName/stacks/:stackId/envs
func (s Environments) List(ctx context.Context, stackId int64) ([]*types.Environment, error) {
res, err := s.Client.Do(ctx, http.MethodGet, s.basePath(stackId), nil, nil, nil)
if err != nil {
return nil, err
}
var envs []*types.Environment
if err := response.ReadJson(res, &envs); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return envs, nil
}
// Get - GET /orgs/:orgName/stacks/:stack_id/envs/:id
func (s Environments) Get(ctx context.Context, stackId, envId int64, includeArchived bool) (*types.Environment, error) {
q := url.Values{
"include_archived": []string{strconv.FormatBool(includeArchived)},
}
res, err := s.Client.Do(ctx, http.MethodGet, s.envPath(stackId, envId), q, nil, nil)
if err != nil {
return nil, err
}
var env types.Environment
if err := response.ReadJson(res, &env); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &env, nil
}
// Create - POST /orgs/:orgName/stacks/:stack_id/envs
func (s Environments) Create(ctx context.Context, stackId int64, env *types.Environment) (*types.Environment, error) {
rawPayload, _ := json.Marshal(env)
res, err := s.Client.Do(ctx, http.MethodPost, s.basePath(stackId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.Environment](res)
}
// Update - PUT/PATCH /orgs/:orgName/stacks/:stack_id/envs/:id
func (s Environments) Update(ctx context.Context, stackId, envId int64, env *types.Environment) (*types.Environment, error) {
rawPayload, _ := json.Marshal(env)
res, err := s.Client.Do(ctx, http.MethodPut, s.envPath(stackId, envId), nil, nil, json.RawMessage(rawPayload))
if err != nil {
return nil, err
}
var updatedEnv types.Environment
if err := response.ReadJson(res, &updatedEnv); response.IsNotFoundError(err) {
return nil, nil
} else if err != nil {
return nil, err
}
return &updatedEnv, nil
}
// Destroy - DELETE /orgs/:orgName/stacks/:stack_id/envs/:id
func (s Environments) Destroy(ctx context.Context, stackId, envId int64) (bool, error) {
res, err := s.Client.Do(ctx, http.MethodDelete, s.envPath(stackId, envId), 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
}