forked from hashicorp/atlas-go
/
variables.go
157 lines (140 loc) · 3.81 KB
/
variables.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package tfe
import (
"bytes"
"fmt"
"reflect"
"github.com/korfuri/jsonapi"
)
func filterGetParams(organization, workspace string) map[string]string {
return map[string]string{
"filter[workspace][name]": workspace,
"filter[organization][name]": organization,
}
}
func (c *Client) ListVariables(organization string, workspace string) ([]*Variable, error) {
ro := &RequestOptions{
Params: filterGetParams(organization, workspace),
}
request, err := c.NewRequest("GET", "/vars", ro)
if err != nil {
return nil, err
}
response, err := CheckResp(c.HTTPClient.Do(request))
if err != nil {
return nil, err
}
objs, err := jsonapi.UnmarshalManyPayload(response.Body, reflect.TypeOf(new(Variable)))
if err != nil {
return nil, err
}
vars := make([]*Variable, len(objs))
for i, o := range objs {
t, ok := o.(*Variable)
if !ok {
return nil, fmt.Errorf("Invalid type during unmarshaling, data was %v", o)
}
vars[i] = t
}
return vars, nil
}
func (c *Client) GetVariableByKey(organization string, workspace string, key string) (*Variable, error) {
vars, err := c.ListVariables(organization, workspace)
if err != nil {
return nil, err
}
for _, v := range vars {
if v.Key == key {
return v, nil
}
}
return nil, ErrNotFound
}
func (c *Client) GetVariableByID(organization string, workspace string, id string) (*Variable, error) {
vars, err := c.ListVariables(organization, workspace)
if err != nil {
return nil, err
}
for _, v := range vars {
if v.ID == id {
return v, nil
}
}
return nil, ErrNotFound
}
// This is a mild hack: because TFE's API expects the workspace and
// organization to be passed as a "filter" object (similar to a "meta"
// or "links", except it's not standard JSONAPI behavior), we use a
// forked version of google/jsonapi that supports these objects. To
// add this object, we implement the method below.
func (v *Variable) JSONAPIFilter() *jsonapi.Filter {
type namedObject struct {
Name string `json:"name"`
}
// if we don't know the workspace nor the org, we're probably
// not creating a variable, most likely we're updating it. No
// need to specify filters here.
if v.workspaceForCreation == "" && v.orgForCreation == "" {
return &jsonapi.Filter{}
}
return &jsonapi.Filter{
"workspace": namedObject{v.workspaceForCreation},
"organization": namedObject{v.orgForCreation},
}
}
func (c *Client) CreateVariable(organization string, workspace string, variable *Variable) (*Variable, error) {
buf := new(bytes.Buffer)
variable.workspaceForCreation = workspace
variable.orgForCreation = organization
if err := jsonapi.MarshalPayload(buf, variable); err != nil {
return nil, err
}
ro := &RequestOptions{
Body: buf,
}
request, err := c.NewRequest("POST", "/vars", ro)
if err != nil {
return nil, err
}
response, err := CheckResp(c.HTTPClient.Do(request))
if err != nil {
return nil, err
}
out_var := new(Variable)
if err := jsonapi.UnmarshalPayload(response.Body, out_var); err != nil {
return nil, err
}
return out_var, nil
}
func (c *Client) UpdateVariable(variable *Variable) (*Variable, error) {
buf := new(bytes.Buffer)
if err := jsonapi.MarshalPayload(buf, variable); err != nil {
return nil, err
}
ro := &RequestOptions{
Body: buf,
}
request, err := c.NewRequest("PATCH", fmt.Sprintf("/vars/%s", variable.ID), ro)
if err != nil {
return nil, err
}
response, err := CheckResp(c.HTTPClient.Do(request))
if err != nil {
return nil, err
}
out_var := new(Variable)
if err := jsonapi.UnmarshalPayload(response.Body, out_var); err != nil {
return nil, err
}
return out_var, nil
}
func (c *Client) DeleteVariable(id string) error {
request, err := c.NewRequest("DELETE", fmt.Sprintf("/vars/%s", id), nil)
if err != nil {
return err
}
_, err = CheckResp(c.HTTPClient.Do(request))
if err != nil {
return err
}
return nil
}