This repository has been archived by the owner on Jul 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_steampipecloud_workspace_connection.go
417 lines (377 loc) · 12.9 KB
/
resource_steampipecloud_workspace_connection.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
package steampipecloud
import (
"context"
"fmt"
"log"
"net/http"
"regexp"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
steampipe "github.com/turbot/steampipe-cloud-sdk-go"
)
func resourceWorkspaceConnection() *schema.Resource {
return &schema.Resource{
CreateContext: resourceWorkspaceConnectionCreate,
ReadContext: resourceWorkspaceConnectionRead,
UpdateContext: resourceWorkspaceConnectionUpdate,
DeleteContext: resourceWorkspaceConnectionDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"connection_handle": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-z][a-z0-9_]{0,37}[a-z0-9]?$`), "Handle must be between 1 and 39 characters, and may only contain alphanumeric characters or single underscores, cannot start with a number or underscore and cannot end with an underscore."),
},
"workspace_handle": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-z0-9]{1,23}$`), "Handle must be between 1 and 23 characters, and may only contain alphanumeric characters."),
},
"organization": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"association_id": {
Type: schema.TypeString,
Computed: true,
},
"connection_id": {
Type: schema.TypeString,
Computed: true,
},
"workspace_id": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
"created_by": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"updated_by": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"identity_id": {
Type: schema.TypeString,
Computed: true,
},
"version_id": {
Type: schema.TypeInt,
Computed: true,
},
"connection_created_at": {
Type: schema.TypeString,
Computed: true,
},
"connection_identity_id": {
Type: schema.TypeString,
Computed: true,
},
"connection_plugin": {
Type: schema.TypeString,
Computed: true,
},
"connection_type": {
Type: schema.TypeString,
Computed: true,
},
"connection_updated_at": {
Type: schema.TypeString,
Computed: true,
},
"connection_version_id": {
Type: schema.TypeInt,
Computed: true,
},
"workspace_created_at": {
Type: schema.TypeString,
Computed: true,
},
"workspace_database_name": {
Type: schema.TypeString,
Computed: true,
},
"workspace_hive": {
Type: schema.TypeString,
Computed: true,
},
"workspace_host": {
Type: schema.TypeString,
Computed: true,
},
"workspace_identity_id": {
Type: schema.TypeString,
Computed: true,
},
"workspace_public_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
"workspace_updated_at": {
Type: schema.TypeString,
Computed: true,
},
"workspace_version_id": {
Type: schema.TypeInt,
Computed: true,
},
"workspace_state": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceWorkspaceConnectionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
workspaceHandle := d.Get("workspace_handle").(string)
connHandle := d.Get("connection_handle").(string)
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
var resp steampipe.WorkspaceConn
var err error
var r *http.Response
// Create request
req := steampipe.CreateWorkspaceConnRequest{ConnectionHandle: connHandle}
client := meta.(*SteampipeClient)
isUser, orgHandle := isUserConnection(d)
if isUser {
var actorHandle string
actorHandle, r, err = getUserHandler(ctx, client)
if err != nil {
return diag.Errorf("resourceWorkspaceConnectionCreate. getUserHandler error %v", decodeResponse(r))
}
resp, r, err = client.APIClient.UserWorkspaceConnectionAssociations.Create(ctx, actorHandle, workspaceHandle).Request(req).Execute()
} else {
resp, r, err = client.APIClient.OrgWorkspaceConnectionAssociations.Create(ctx, orgHandle, workspaceHandle).Request(req).Execute()
}
// Error check
if err != nil {
return diag.Errorf("error creating workspace connection association: %v", decodeResponse(r))
}
// Set property values
d.Set("association_id", resp.Id)
d.Set("connection_id", resp.ConnectionId)
d.Set("workspace_id", resp.WorkspaceId)
d.Set("organization", orgHandle)
d.Set("created_at", resp.CreatedAt)
d.Set("updated_at", resp.UpdatedAt)
d.Set("identity_id", resp.IdentityId)
if resp.CreatedBy != nil {
d.Set("created_by", resp.CreatedBy.Handle)
}
if resp.UpdatedBy != nil {
d.Set("updated_by", resp.UpdatedBy.Handle)
}
d.Set("version_id", resp.VersionId)
d.Set("workspace_handle", workspaceHandle)
d.Set("connection_handle", resp.Connection.Handle)
d.Set("connection_created_at", resp.Connection.CreatedAt)
d.Set("connection_updated_at", resp.Connection.UpdatedAt)
d.Set("connection_identity_id", resp.Connection.IdentityId)
d.Set("connection_plugin", resp.Connection.Plugin)
d.Set("connection_type", resp.Connection.Type)
d.Set("connection_version_id", resp.Connection.VersionId)
// Get the workspace details
workspaceResp, r, err := getWorkspaceDetails(ctx, client, d)
// Error check
if err != nil {
return diag.Errorf("error getting workspace details for connection association: %v", decodeResponse(r))
}
d.Set("workspace_state", workspaceResp.State)
d.Set("workspace_created_at", workspaceResp.CreatedAt)
d.Set("workspace_database_name", workspaceResp.DatabaseName)
d.Set("workspace_hive", workspaceResp.Hive)
d.Set("workspace_host", workspaceResp.Host)
d.Set("workspace_identity_id", workspaceResp.IdentityId)
d.Set("workspace_public_key", workspaceResp.PublicKey)
d.Set("workspace_updated_at", workspaceResp.UpdatedAt)
d.Set("workspace_version_id", workspaceResp.VersionId)
// If workspace connection association is created inside an Organization the id will be of the
// format "OrganizationHandle/WorkspaceHandle/ConnectionHandle" otherwise "WorkspaceHandle/ConnectionHandle"
id := fmt.Sprintf("%s/%s", workspaceHandle, resp.Connection.Handle)
if strings.HasPrefix(resp.IdentityId, "o_") {
d.SetId(fmt.Sprintf("%s/%s", orgHandle, id))
} else {
d.SetId(id)
}
return diags
}
func resourceWorkspaceConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*SteampipeClient)
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
var orgHandle, workspaceHandle, connectionHandle string
var isUser = false
// For backward-compatibility, we see whether the id contains : or /
separator := "/"
if strings.Contains(d.Id(), ":") {
separator = ":"
}
// If workspace connection association is created inside an Organization the id will be of the
// format "OrganizationHandle/WorkspaceHandle/ConnectionHandle" otherwise "WorkspaceHandle/ConnectionHandle"
idParts := strings.Split(d.Id(), separator)
if len(idParts) < 2 && len(idParts) > 3 {
return diag.Errorf("unexpected format of ID (%q), expected <workspace-handle>/<connection-handle>", d.Id())
}
if len(idParts) == 3 {
orgHandle = idParts[0]
workspaceHandle = idParts[1]
connectionHandle = idParts[2]
} else if len(idParts) == 2 {
isUser = true
workspaceHandle = idParts[0]
connectionHandle = idParts[1]
}
var resp steampipe.WorkspaceConn
var err error
var r *http.Response
if isUser {
var actorHandle string
actorHandle, r, err = getUserHandler(ctx, client)
if err != nil {
return diag.Errorf("resourceConnectionRead. getUserHandler error %v", decodeResponse(r))
}
resp, r, err = client.APIClient.UserWorkspaceConnectionAssociations.Get(ctx, actorHandle, workspaceHandle, connectionHandle).Execute()
} else {
resp, r, err = client.APIClient.OrgWorkspaceConnectionAssociations.Get(ctx, orgHandle, workspaceHandle, connectionHandle).Execute()
}
if err != nil {
if r.StatusCode == 404 {
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("Association (%s) not found", resp.Id),
})
d.SetId("")
return diags
}
return diag.Errorf("resourceWorkspaceConnectionRead. Get workspace connection association error: %v", decodeResponse(r))
}
log.Printf("\n[DEBUG] Association received: %s", resp.Id)
if separator == ":" {
d.SetId(strings.ReplaceAll(d.Id(), ":", "/"))
}
d.Set("association_id", resp.Id)
d.Set("workspace_id", resp.WorkspaceId)
d.Set("connection_id", resp.ConnectionId)
d.Set("organization", orgHandle)
d.Set("created_at", resp.CreatedAt)
d.Set("updated_at", resp.UpdatedAt)
d.Set("identity_id", resp.IdentityId)
if resp.CreatedBy != nil {
d.Set("created_by", resp.CreatedBy.Handle)
}
if resp.UpdatedBy != nil {
d.Set("updated_by", resp.UpdatedBy.Handle)
}
d.Set("version_id", resp.VersionId)
d.Set("connection_handle", resp.Connection.Handle)
d.Set("workspace_handle", workspaceHandle)
d.Set("connection_created_at", resp.Connection.CreatedAt)
d.Set("connection_updated_at", resp.Connection.UpdatedAt)
d.Set("connection_identity_id", resp.Connection.IdentityId)
d.Set("connection_plugin", resp.Connection.Plugin)
d.Set("connection_type", resp.Connection.Type)
d.Set("connection_version_id", resp.Connection.VersionId)
// Get the workspace details
workspaceResp, r, err := getWorkspaceDetails(ctx, client, d)
// Error check
if err != nil {
return diag.Errorf("error getting workspace details for connection association: %v", decodeResponse(r))
}
d.Set("workspace_state", workspaceResp.State)
d.Set("workspace_created_at", workspaceResp.CreatedAt)
d.Set("workspace_database_name", workspaceResp.DatabaseName)
d.Set("workspace_hive", workspaceResp.Hive)
d.Set("workspace_host", workspaceResp.Host)
d.Set("workspace_identity_id", workspaceResp.IdentityId)
d.Set("workspace_public_key", workspaceResp.PublicKey)
d.Set("workspace_updated_at", workspaceResp.UpdatedAt)
d.Set("workspace_version_id", workspaceResp.VersionId)
return diags
}
func resourceWorkspaceConnectionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
workspaceHandle := d.State().Attributes["workspace_handle"]
connHandle := d.State().Attributes["connection_handle"]
orgHandle := d.State().Attributes["organization"]
if d.HasChange("workspace_handle") {
_, newWorkspaceHandle := d.GetChange("workspace_handle")
workspaceHandle = newWorkspaceHandle.(string)
}
if d.HasChange("connection_handle") {
_, newConnHandle := d.GetChange("connection_handle")
connHandle = newConnHandle.(string)
}
var id string
if workspaceHandle != "" && connHandle != "" {
id = fmt.Sprintf("%s/%s", workspaceHandle, connHandle)
d.Set("workspace_handle", workspaceHandle)
d.Set("connection_handle", connHandle)
}
if orgHandle != "" {
d.SetId(fmt.Sprintf("%s/%s", orgHandle, id))
} else {
d.SetId(id)
}
return diags
}
func resourceWorkspaceConnectionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*SteampipeClient)
// Warning or errors can be collected in a slice type
var diags diag.Diagnostics
var orgHandle, workspaceHandle, connectionHandle string
var isUser = false
// For backward-compatibility, we see whether the id contains : or /
separator := "/"
if strings.Contains(d.Id(), ":") {
separator = ":"
}
idParts := strings.Split(d.Id(), separator)
if len(idParts) < 2 {
return diag.Errorf("unexpected format of ID (%q), expected <workspace-handle>/<connection-handle>", d.Id())
}
if len(idParts) == 3 {
orgHandle = idParts[0]
workspaceHandle = idParts[1]
connectionHandle = idParts[2]
} else if len(idParts) == 2 {
isUser = true
workspaceHandle = idParts[0]
connectionHandle = idParts[1]
}
log.Printf("\n[DEBUG] Deleting Workspace Connection association: %s", fmt.Sprintf("%s/%s", workspaceHandle, connectionHandle))
var err error
var r *http.Response
if isUser {
var actorHandle string
actorHandle, r, err = getUserHandler(ctx, client)
if err != nil {
return diag.Errorf("resourceConnectionDelete. getUserHandler error: %v", decodeResponse(r))
}
_, r, err = client.APIClient.UserWorkspaceConnectionAssociations.Delete(ctx, actorHandle, workspaceHandle, connectionHandle).Execute()
} else {
_, r, err = client.APIClient.OrgWorkspaceConnectionAssociations.Delete(ctx, orgHandle, workspaceHandle, connectionHandle).Execute()
}
if err != nil {
return diag.Errorf("error deleting workspace connection association: %v", decodeResponse(r))
}
d.SetId("")
return diags
}