Skip to content

Commit e7d626d

Browse files
committed
Source insert function handling with terraform
1 parent 0c243af commit e7d626d

File tree

2 files changed

+223
-3
lines changed

2 files changed

+223
-3
lines changed

internal/provider/function_resource.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (r *functionResource) Create(ctx context.Context, req resource.CreateReques
163163
state.Fill(function)
164164

165165
// Destination functions append workspace name to display name causing inconsistency
166-
if state.ResourceType.ValueString() == "DESTINATION" || state.ResourceType.ValueString() == "INSERT_DESTINATION" {
166+
if state.ResourceType.ValueString() == "DESTINATION" || state.ResourceType.ValueString() == "INSERT_DESTINATION" || state.ResourceType.ValueString() == "INSERT_SOURCE"{
167167
state.DisplayName = plan.DisplayName
168168
}
169169

@@ -210,7 +210,7 @@ func (r *functionResource) Read(ctx context.Context, req resource.ReadRequest, r
210210
state.Fill(function)
211211

212212
// Destination functions append workspace name to display name causing inconsistency
213-
if state.ResourceType.ValueString() == "DESTINATION" || state.ResourceType.ValueString() == "INSERT_DESTINATION" {
213+
if state.ResourceType.ValueString() == "DESTINATION" || state.ResourceType.ValueString() == "INSERT_DESTINATION" || state.ResourceType.ValueString() == "INSERT_SOURCE"{
214214
state.DisplayName = previousState.DisplayName
215215
}
216216

@@ -266,7 +266,7 @@ func (r *functionResource) Update(ctx context.Context, req resource.UpdateReques
266266
state.Fill(function)
267267

268268
// Destination functions append workspace name to display name causing inconsistency
269-
if state.ResourceType.ValueString() == "DESTINATION" || state.ResourceType.ValueString() == "INSERT_DESTINATION" {
269+
if state.ResourceType.ValueString() == "DESTINATION" || state.ResourceType.ValueString() == "INSERT_DESTINATION" || state.ResourceType.ValueString() == "INSERT_SOURCE"{
270270
state.DisplayName = plan.DisplayName
271271
}
272272

internal/provider/function_resource_test.go

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,223 @@ func TestAccFunctionResource(t *testing.T) {
269269
},
270270
})
271271
}
272+
273+
274+
func TestAccFunctionResource_InsertSource(t *testing.T) {
275+
t.Parallel()
276+
277+
// keeps track of whether we’ve already updated the Function
278+
updated := 0
279+
280+
// ────────────────────────── Fake Segment API ──────────────────────────
281+
fakeServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
282+
w.Header().Set("content-type", "application/json")
283+
284+
switch {
285+
// ── CREATE ─────────────────────────────────────────────────────────
286+
case req.URL.Path == "/functions" && req.Method == http.MethodPost:
287+
_, _ = w.Write([]byte(`
288+
{
289+
"data": {
290+
"function": {
291+
"id": "my-function-id",
292+
"workspaceId": "my-workspace-id",
293+
"displayName": "My test function (my-workspace)", // Segment appends workspace
294+
"description": "My function description",
295+
"logoUrl": "https://segment.com/cool-logo.png",
296+
"code": "// My test code!",
297+
"createdAt": "2023-10-11T18:52:07.087Z",
298+
"createdBy": "my-user-id",
299+
"previewWebhookUrl": "",
300+
"settings": [{
301+
"name": "mySettingName",
302+
"label": "My setting label",
303+
"description": "My setting description",
304+
"type": "STRING",
305+
"required": false,
306+
"sensitive": false
307+
}],
308+
"buildpack": "boreal",
309+
"catalogId": "my-catalog-id",
310+
"batchMaxCount": 0,
311+
"resourceType": "INSERT_SOURCE"
312+
}
313+
}
314+
}`))
315+
// ── UPDATE ─────────────────────────────────────────────────────────
316+
case req.URL.Path == "/functions/my-function-id" && req.Method == http.MethodPatch:
317+
updated++
318+
_, _ = w.Write([]byte(`
319+
{
320+
"data": {
321+
"function": {
322+
"id": "my-function-id",
323+
"workspaceId": "my-workspace-id",
324+
"displayName": "My new test function (my-workspace)",
325+
"description": "My new function description",
326+
"logoUrl": "https://segment.com/cool-other-logo.png",
327+
"code": "// My new test code!",
328+
"createdAt": "2023-10-11T18:52:07.087Z",
329+
"createdBy": "my-user-id",
330+
"previewWebhookUrl": "",
331+
"settings": [{
332+
"name": "myNewSettingName",
333+
"label": "My new setting label",
334+
"description": "My new setting description",
335+
"type": "STRING",
336+
"required": true,
337+
"sensitive": true
338+
}],
339+
"buildpack": "boreal",
340+
"catalogId": "my-catalog-id",
341+
"batchMaxCount": 0,
342+
"resourceType": "INSERT_SOURCE"
343+
}
344+
}
345+
}`))
346+
// ── READ (initial + after update) ─────────────────────────────────
347+
case req.URL.Path == "/functions/my-function-id" && req.Method == http.MethodGet:
348+
if updated == 0 {
349+
_, _ = w.Write([]byte(`
350+
{
351+
"data": {
352+
"function": {
353+
"id": "my-function-id",
354+
"workspaceId": "my-workspace-id",
355+
"displayName": "My test function (my-workspace)",
356+
"description": "My function description",
357+
"logoUrl": "https://segment.com/cool-logo.png",
358+
"code": "// My test code!",
359+
"createdAt": "2023-10-11T18:52:07.087Z",
360+
"createdBy": "my-user-id",
361+
"previewWebhookUrl": "",
362+
"settings": [{
363+
"name": "mySettingName",
364+
"label": "My setting label",
365+
"description": "My setting description",
366+
"type": "STRING",
367+
"required": false,
368+
"sensitive": false
369+
}],
370+
"buildpack": "boreal",
371+
"catalogId": "my-catalog-id",
372+
"batchMaxCount": 0,
373+
"resourceType": "INSERT_SOURCE"
374+
}
375+
}
376+
}`))
377+
} else {
378+
_, _ = w.Write([]byte(`
379+
{
380+
"data": {
381+
"function": {
382+
"id": "my-function-id",
383+
"workspaceId": "my-workspace-id",
384+
"displayName": "My new test function (my-workspace)",
385+
"description": "My new function description",
386+
"logoUrl": "https://segment.com/cool-other-logo.png",
387+
"code": "// My new test code!",
388+
"createdAt": "2023-10-11T18:52:07.087Z",
389+
"createdBy": "my-user-id",
390+
"previewWebhookUrl": "",
391+
"settings": [{
392+
"name": "myNewSettingName",
393+
"label": "My new setting label",
394+
"description": "My new setting description",
395+
"type": "STRING",
396+
"required": true,
397+
"sensitive": true
398+
}],
399+
"buildpack": "boreal",
400+
"catalogId": "my-catalog-id",
401+
"batchMaxCount": 0,
402+
"resourceType": "INSERT_SOURCE"
403+
}
404+
}
405+
}`))
406+
}
407+
}
408+
}))
409+
defer fakeServer.Close()
410+
411+
// common provider block
412+
providerConfig := `
413+
provider "segment" {
414+
url = "` + fakeServer.URL + `"
415+
token = "abc123"
416+
}
417+
`
418+
419+
resource.Test(t, resource.TestCase{
420+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
421+
Steps: []resource.TestStep{
422+
// ── CREATE + READ ──────────────────────────────────────────────
423+
{
424+
Config: providerConfig + `
425+
resource "segment_function" "test" {
426+
code = "// My test code!"
427+
display_name = "My test function"
428+
logo_url = "https://segment.com/cool-logo.png"
429+
resource_type = "INSERT_SOURCE"
430+
description = "My function description"
431+
settings = [{
432+
name = "mySettingName"
433+
label = "My setting label"
434+
type = "STRING"
435+
description = "My setting description"
436+
required = false
437+
sensitive = false
438+
}]
439+
}
440+
`,
441+
Check: resource.ComposeAggregateTestCheckFunc(
442+
resource.TestCheckResourceAttr("segment_function.test", "id", "my-function-id"),
443+
resource.TestCheckResourceAttr("segment_function.test", "resource_type", "INSERT_SOURCE"),
444+
resource.TestCheckResourceAttr("segment_function.test", "display_name", "My test function"),
445+
resource.TestCheckResourceAttr("segment_function.test", "logo_url", "https://segment.com/cool-logo.png"),
446+
resource.TestCheckResourceAttr("segment_function.test", "description", "My function description"),
447+
resource.TestCheckResourceAttr("segment_function.test", "settings.#", "1"),
448+
resource.TestCheckResourceAttr("segment_function.test", "settings.0.name", "mySettingName"),
449+
resource.TestCheckResourceAttr("segment_function.test", "settings.0.required", "false"),
450+
resource.TestCheckResourceAttr("segment_function.test", "settings.0.sensitive", "false"),
451+
),
452+
},
453+
// ── IMPORT ────────────────────────────────────────────────────
454+
{
455+
ResourceName: "segment_function.test",
456+
ImportState: true,
457+
ImportStateVerify: true,
458+
},
459+
// ── UPDATE + READ ────────────────────────────────────────────
460+
{
461+
Config: providerConfig + `
462+
resource "segment_function" "test" {
463+
code = "// My new test code!"
464+
display_name = "My new test function"
465+
logo_url = "https://segment.com/cool-other-logo.png"
466+
resource_type = "INSERT_SOURCE"
467+
description = "My new function description"
468+
settings = [{
469+
name = "myNewSettingName"
470+
label = "My new setting label"
471+
type = "STRING"
472+
description = "My new setting description"
473+
required = true
474+
sensitive = true
475+
}]
476+
}
477+
`,
478+
Check: resource.ComposeAggregateTestCheckFunc(
479+
resource.TestCheckResourceAttr("segment_function.test", "resource_type", "INSERT_SOURCE"),
480+
resource.TestCheckResourceAttr("segment_function.test", "display_name", "My new test function"),
481+
resource.TestCheckResourceAttr("segment_function.test", "logo_url", "https://segment.com/cool-other-logo.png"),
482+
resource.TestCheckResourceAttr("segment_function.test", "description", "My new function description"),
483+
resource.TestCheckResourceAttr("segment_function.test", "settings.0.name", "myNewSettingName"),
484+
resource.TestCheckResourceAttr("segment_function.test", "settings.0.required", "true"),
485+
resource.TestCheckResourceAttr("segment_function.test", "settings.0.sensitive", "true"),
486+
),
487+
},
488+
// destroy is implicit
489+
},
490+
})
491+
}

0 commit comments

Comments
 (0)