/
data_source_scalr_workspace.go
329 lines (286 loc) · 10.1 KB
/
data_source_scalr_workspace.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package scalr
import (
"context"
"errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scalr/go-scalr"
)
func dataSourceScalrWorkspace() *schema.Resource {
return &schema.Resource{
Description: "Retrieves the details of a single workspace.",
ReadContext: dataSourceScalrWorkspaceRead,
Schema: map[string]*schema.Schema{
"id": {
Description: "ID of the workspace.",
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotWhiteSpace,
AtLeastOneOf: []string{"name"},
},
"name": {
Description: "Name of the workspace.",
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringIsNotWhiteSpace,
},
"environment_id": {
Description: "ID of the environment, in the format `env-<RANDOM STRING>`.",
Type: schema.TypeString,
Required: true,
},
"vcs_provider_id": {
Description: "The identifier of a VCS provider in the format `vcs-<RANDOM STRING>`.",
Type: schema.TypeString,
Computed: true,
},
"module_version_id": {
Description: "The identifier of a module version in the format `modver-<RANDOM STRING>`.",
Type: schema.TypeString,
Computed: true,
},
"agent_pool_id": {
Description: "The identifier of an agent pool in the format `apool-<RANDOM STRING>`.",
Type: schema.TypeString,
Computed: true,
},
"auto_apply": {
Description: "Boolean indicates if `terraform apply` will be automatically run when `terraform plan` ends without error.",
Type: schema.TypeBool,
Computed: true,
},
"force_latest_run": {
Description: "Boolean indicates if latest new run will be automatically raised in priority.",
Type: schema.TypeBool,
Computed: true,
},
"deletion_protection_enabled": {
Description: "Boolean, indicates if the workspace has the protection from an accidental state lost. If enabled and the workspace has resource, the deletion will not be allowed.",
Type: schema.TypeBool,
Computed: true,
},
"operations": {
Description: "Boolean indicates if the workspace is being used for remote execution.",
Type: schema.TypeBool,
Computed: true,
},
"execution_mode": {
Description: "Execution mode of the workspace.",
Type: schema.TypeString,
Computed: true,
},
"terraform_version": {
Description: "The version of Terraform used for this workspace.",
Type: schema.TypeString,
Computed: true,
},
"iac_platform": {
Description: "The IaC platform used for this workspace.",
Type: schema.TypeString,
Computed: true,
},
"working_directory": {
Description: "A relative path that Terraform will execute within.",
Type: schema.TypeString,
Computed: true,
},
"has_resources": {
Description: "The presence of active terraform resources in the current state version.",
Type: schema.TypeBool,
Computed: true,
},
"auto_queue_runs": {
Description: "Indicates if runs have to be queued automatically when a new configuration version is uploaded." +
"\n\n Supported values are `skip_first`, `always`, `never`:" +
"\n\n * `skip_first` - after the very first configuration version is uploaded into the workspace the run will not be triggered. But the following configurations will do. This is the default behavior." +
"\n * `always` - runs will be triggered automatically on every upload of the configuration version." +
"\n * `never` - configuration versions are uploaded into the workspace, but runs will not be triggered.",
Type: schema.TypeString,
Computed: true,
},
"hooks": {
Description: "List of custom hooks in a workspace.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"pre_init": {
Description: "Script or action configured to call before init phase.",
Type: schema.TypeString,
Computed: true,
},
"pre_plan": {
Description: "Script or action configured to call before plan phase.",
Type: schema.TypeString,
Computed: true,
},
"post_plan": {
Description: "Script or action configured to call after plan phase.",
Type: schema.TypeString,
Computed: true,
},
"pre_apply": {
Description: "Script or action configured to call before apply phase.",
Type: schema.TypeString,
Computed: true,
},
"post_apply": {
Description: "Script or action configured to call after apply phase.",
Type: schema.TypeString,
Computed: true,
},
},
},
},
"vcs_repo": {
Description: "If a workspace is linked to a VCS repository this block shows the details, otherwise `{}`",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"identifier": {
Description: "The reference to the VCS repository in the format `:org/:repo`, this refers to the organization and repository in your VCS provider.",
Type: schema.TypeString,
Computed: true,
},
"path": {
Description: "Path within the repo, if any.",
Type: schema.TypeString,
Computed: true,
},
"dry_runs_enabled": {
Description: "Boolean indicates the VCS-driven dry runs should run when the pull request to the configuration versions branch is created.",
Type: schema.TypeBool,
Computed: true,
},
"ingress_submodules": {
Description: "Designates whether to clone git submodules of the VCS repository.",
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"tag_ids": {
Description: "List of tag IDs associated with the workspace.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"created_by": {
Description: "Details of the user that created the workspace.",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"username": {
Description: "Username of creator.",
Type: schema.TypeString,
Computed: true,
},
"email": {
Description: "Email address of creator.",
Type: schema.TypeString,
Computed: true,
},
"full_name": {
Description: "Full name of creator.",
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceScalrWorkspaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
scalrClient := meta.(*scalr.Client)
workspaceID := d.Get("id").(string)
name := d.Get("name").(string)
environmentID := d.Get("environment_id").(string)
options := scalr.WorkspaceListOptions{
Include: "created-by",
Filter: &scalr.WorkspaceFilter{Environment: scalr.String(environmentID)},
}
if workspaceID != "" {
options.Filter.Id = scalr.String(workspaceID)
}
if name != "" {
options.Filter.Name = scalr.String(name)
}
log.Printf("[DEBUG] Read configuration of workspace with ID '%s', name '%s', and environment_id '%s'", workspaceID, name, environmentID)
workspaces, err := scalrClient.Workspaces.List(ctx, options)
if err != nil {
return diag.Errorf("error retrieving workspace: %v", err)
}
if len(workspaces.Items) > 1 {
return diag.FromErr(errors.New("Your query returned more than one result. Please try a more specific search criteria."))
}
if len(workspaces.Items) == 0 {
return diag.Errorf("Could not find workspace with ID '%s', name '%s' and environment_id '%s'", workspaceID, name, environmentID)
}
workspace := workspaces.Items[0]
// Update the config.
_ = d.Set("name", workspace.Name)
_ = d.Set("auto_apply", workspace.AutoApply)
_ = d.Set("force_latest_run", workspace.ForceLatestRun)
_ = d.Set("deletion_protection_enabled", workspace.DeletionProtectionEnabled)
_ = d.Set("operations", workspace.Operations)
_ = d.Set("execution_mode", workspace.ExecutionMode)
_ = d.Set("terraform_version", workspace.TerraformVersion)
_ = d.Set("iac_platform", workspace.IaCPlatform)
_ = d.Set("working_directory", workspace.WorkingDirectory)
_ = d.Set("has_resources", workspace.HasResources)
_ = d.Set("auto_queue_runs", workspace.AutoQueueRuns)
if workspace.ModuleVersion != nil {
_ = d.Set("module_version_id", workspace.ModuleVersion.ID)
}
if workspace.VcsProvider != nil {
_ = d.Set("vcs_provider_id", workspace.VcsProvider.ID)
}
var createdBy []interface{}
if workspace.CreatedBy != nil {
createdBy = append(createdBy, map[string]interface{}{
"username": workspace.CreatedBy.Username,
"email": workspace.CreatedBy.Email,
"full_name": workspace.CreatedBy.FullName,
})
}
_ = d.Set("created_by", createdBy)
var vcsRepo []interface{}
if workspace.VCSRepo != nil {
vcsConfig := map[string]interface{}{
"identifier": workspace.VCSRepo.Identifier,
"path": workspace.VCSRepo.Path,
"dry_runs_enabled": workspace.VCSRepo.DryRunsEnabled,
"ingress_submodules": workspace.VCSRepo.IngressSubmodules,
}
vcsRepo = append(vcsRepo, vcsConfig)
}
_ = d.Set("vcs_repo", vcsRepo)
var hooks []interface{}
if workspace.Hooks != nil {
hooks = append(hooks, map[string]interface{}{
"pre_init": workspace.Hooks.PreInit,
"pre_plan": workspace.Hooks.PrePlan,
"post_plan": workspace.Hooks.PostPlan,
"pre_apply": workspace.Hooks.PreApply,
"post_apply": workspace.Hooks.PostApply,
})
}
_ = d.Set("hooks", hooks)
var tags []string
if len(workspace.Tags) != 0 {
for _, tag := range workspace.Tags {
tags = append(tags, tag.ID)
}
}
_ = d.Set("tag_ids", tags)
d.SetId(workspace.ID)
return nil
}