-
Notifications
You must be signed in to change notification settings - Fork 147
/
data_source_variables.go
190 lines (165 loc) · 5 KB
/
data_source_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
package tfe
import (
"fmt"
"log"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceTFEWorkspaceVariables() *schema.Resource {
varSchema := map[string]*schema.Schema{
"category": {
Type: schema.TypeString,
Computed: true,
},
"hcl": {
Type: schema.TypeBool,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"sensitive": {
Type: schema.TypeBool,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
}
return &schema.Resource{
Read: dataSourceVariableRead,
Schema: map[string]*schema.Schema{
"env": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: varSchema,
},
},
"terraform": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: varSchema,
},
},
"variables": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: varSchema,
},
},
"workspace_id": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"workspace_id", "variable_set_id"},
},
"variable_set_id": {
Type: schema.TypeString,
Optional: true,
ExactlyOneOf: []string{"workspace_id", "variable_set_id"},
},
},
}
}
func dataSourceVariableRead(d *schema.ResourceData, meta interface{}) error {
//Switch to variable set variable logic
_, variableSetIdProvided := d.GetOk("variable_set_id")
if variableSetIdProvided {
return dataSourceVariableSetVariableRead(d, meta)
}
tfeClient := meta.(*tfe.Client)
// Get the name and organization.
workspaceID := d.Get("workspace_id").(string)
log.Printf("[DEBUG] Read configuration of workspace: %s", workspaceID)
totalEnvVariables := make([]interface{}, 0)
totalTerraformVariables := make([]interface{}, 0)
options := &tfe.VariableListOptions{}
for {
variableList, err := tfeClient.Variables.List(ctx, workspaceID, options)
if err != nil {
return fmt.Errorf("Error retrieving variable list: %w", err)
}
terraformVars := make([]interface{}, 0)
envVars := make([]interface{}, 0)
for _, variable := range variableList.Items {
result := make(map[string]interface{})
result["id"] = variable.ID
result["category"] = variable.Category
result["hcl"] = variable.HCL
result["name"] = variable.Key
result["sensitive"] = variable.Sensitive
result["value"] = variable.Value
if variable.Category == "terraform" {
terraformVars = append(terraformVars, result)
} else if variable.Category == "env" {
envVars = append(envVars, result)
}
}
totalEnvVariables = append(totalEnvVariables, envVars...)
totalTerraformVariables = append(totalTerraformVariables, terraformVars...)
// Exit the loop when we've seen all pages.
if variableList.CurrentPage >= variableList.TotalPages {
break
}
// Update the page number to get the next page.
options.PageNumber = variableList.NextPage
}
d.SetId(fmt.Sprintf("variables/%v", workspaceID))
d.Set("variables", append(totalTerraformVariables, totalEnvVariables...))
d.Set("terraform", totalTerraformVariables)
d.Set("env", totalEnvVariables)
return nil
}
func dataSourceVariableSetVariableRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)
// Get the id.
variableSetId := d.Get("variable_set_id").(string)
log.Printf("[DEBUG] Read configuration of variable set: %s", variableSetId)
totalEnvVariables := make([]interface{}, 0)
totalTerraformVariables := make([]interface{}, 0)
options := tfe.VariableSetVariableListOptions{}
for {
variableList, err := tfeClient.VariableSetVariables.List(ctx, variableSetId, &options)
if err != nil {
return fmt.Errorf("Error retrieving variable list: %w", err)
}
terraformVars := make([]interface{}, 0)
envVars := make([]interface{}, 0)
for _, variable := range variableList.Items {
result := make(map[string]interface{})
result["id"] = variable.ID
result["category"] = variable.Category
result["hcl"] = variable.HCL
result["name"] = variable.Key
result["sensitive"] = variable.Sensitive
result["value"] = variable.Value
if variable.Category == "terraform" {
terraformVars = append(terraformVars, result)
} else if variable.Category == "env" {
envVars = append(envVars, result)
}
}
totalEnvVariables = append(totalEnvVariables, envVars...)
totalTerraformVariables = append(totalTerraformVariables, terraformVars...)
// Exit the loop when we've seen all pages.
if variableList.CurrentPage >= variableList.TotalPages {
break
}
// Update the page number to get the next page.
options.PageNumber = variableList.NextPage
}
d.SetId(fmt.Sprintf("variables/%v", variableSetId))
d.Set("variables", append(totalTerraformVariables, totalEnvVariables...))
d.Set("terraform", totalTerraformVariables)
d.Set("env", totalEnvVariables)
return nil
}