/
data_source_scalr_variables.go
200 lines (188 loc) · 6.32 KB
/
data_source_scalr_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package scalr
import (
"context"
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scalr/go-scalr"
)
func dataSourceScalrVariables() *schema.Resource {
return &schema.Resource{
Description: "Retrieves the list of variables by the given filters.",
ReadContext: dataSourceScalrVariablesRead,
Schema: map[string]*schema.Schema{
"variables": {
Description: "The list of Scalr variables with all attributes.",
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Description: "ID of the variable.",
Type: schema.TypeString,
Required: true,
},
"category": {
Description: "Indicates if this is a Terraform or shell variable.",
Type: schema.TypeString,
Required: true,
},
"hcl": {
Description: "If the variable is configured as a string of HCL code.",
Type: schema.TypeBool,
Required: true,
},
"key": {
Description: "Key of the variable.",
Type: schema.TypeString,
Required: true,
},
"sensitive": {
Description: "If the variable is configured as sensitive.",
Type: schema.TypeBool,
Required: true,
},
"final": {
Description: "If the variable is configured as final. Indicates whether the variable can be overridden on a lower scope down the Scalr organizational model.",
Type: schema.TypeBool,
Required: true,
},
"value": {
Description: "Variable value if it is not sensitive.",
Type: schema.TypeString,
Optional: true,
},
"description": {
Description: "Variable verbose description.",
Type: schema.TypeString,
Required: true,
},
"workspace_id": {
Description: "The workspace that owns the variable, specified as an ID, in the format `ws-<RANDOM STRING>`.",
Type: schema.TypeString,
Optional: true,
},
"environment_id": {
Description: "The environment that owns the variable, specified as an ID, in the format `env-<RANDOM STRING>`.",
Type: schema.TypeString,
Optional: true,
},
"account_id": {
Description: "The account that owns the variable, specified as an ID, in the format `acc-<RANDOM STRING>`.",
Type: schema.TypeString,
Optional: true,
},
},
},
Set: func(value interface{}) int {
variable := value.(map[string]interface{})
return schema.HashString(variable["id"].(string))
},
},
"keys": {
Description: "A list of keys to be used in the query used in a Scalr variable name filter.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"account_id": {
Description: "ID of the account, in the format `acc-<RANDOM STRING>`.",
Type: schema.TypeString,
Optional: true,
Computed: true,
DefaultFunc: scalrAccountIDDefaultFunc,
},
"category": {
Description: "The category of a Scalr variable.",
Type: schema.TypeString,
Optional: true,
},
"environment_ids": {
Description: "A list of identifiers of the Scalr environments, in the format `env-<RANDOM STRING>`. Used to shrink the variable's scope in case the variable name exists in multiple environments.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"workspace_ids": {
Description: "A list of identifiers of the Scalr workspace, in the format `ws-<RANDOM STRING>`. Used to shrink the variable's scope in case the variable name exists on multiple workspaces.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
}}
}
func dataSourceScalrVariablesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
scalrClient := meta.(*scalr.Client)
filters := scalr.VariableFilter{}
options := scalr.VariableListOptions{Filter: &filters}
filters.Account = scalr.String(d.Get("account_id").(string))
if keysI, ok := d.GetOk("keys"); ok {
keys := make([]string, 0)
for _, keyI := range keysI.(*schema.Set).List() {
keys = append(keys, keyI.(string))
}
if len(keys) > 0 {
filters.Key = scalr.String("in:" + strings.Join(keys, ","))
}
}
if categoryI, ok := d.GetOk("category"); ok {
filters.Category = scalr.String(categoryI.(string))
}
if envIdsI, ok := d.GetOk("environment_ids"); ok {
envIds := make([]string, 0)
for _, envIdI := range envIdsI.(*schema.Set).List() {
envIds = append(envIds, envIdI.(string))
}
if len(envIds) > 0 {
filters.Environment = scalr.String("in:" + strings.Join(envIds, ","))
}
}
if wsIdsI, ok := d.GetOk("workspace_ids"); ok {
wsIds := make([]string, 0)
for _, wsIdI := range wsIdsI.(*schema.Set).List() {
wsIds = append(wsIds, wsIdI.(string))
}
if len(wsIds) > 0 {
filters.Workspace = scalr.String("in:" + strings.Join(wsIds, ","))
}
}
variables := make([]map[string]interface{}, 0)
ids := make([]string, 0)
for {
page, err := scalrClient.Variables.List(ctx, options)
if err != nil {
return diag.Errorf("Error retrieving Scalr variables: %v", err)
}
for _, variable := range page.Items {
variableI := map[string]interface{}{
"id": variable.ID,
"category": string(variable.Category),
"hcl": variable.HCL,
"key": variable.Key,
"sensitive": variable.Sensitive,
"final": variable.Final,
"value": variable.Value,
"description": variable.Description,
}
if variable.Workspace != nil {
variableI["workspace_id"] = variable.Workspace.ID
}
if variable.Environment != nil {
variableI["environment_id"] = variable.Environment.ID
}
if variable.Account != nil {
variableI["account_id"] = variable.Account.ID
}
variables = append(variables, variableI)
ids = append(ids, variable.ID)
}
if page.CurrentPage >= page.TotalPages {
break
}
options.PageNumber = page.NextPage
}
_ = d.Set("variables", variables)
d.SetId(fmt.Sprintf("%d", schema.HashString(strings.Join(ids, ""))))
return nil
}