Skip to content

Commit 28230bb

Browse files
qdegraafmvantellingen
authored andcommitted
Fix flattenwebhook filter bug
1 parent 4276250 commit 28230bb

4 files changed

Lines changed: 40 additions & 25 deletions

File tree

amplience/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func Provider() *schema.Provider {
4646
Description: "ID of the Hub to manage",
4747
Type: schema.TypeString,
4848
Required: true,
49+
DefaultFunc: schema.EnvDefaultFunc("AMPLIENCE_HUB_ID", nil),
4950
ValidateDiagFunc: ValidateDiagWrapper(validation.StringDoesNotContainAny(" ")),
5051
},
5152
},

amplience/resource_content_repository_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ func TestAccContentRepository_CreateAndUpdate(t *testing.T) {
4848
func testAccContentRepositoryConfig(name, label string) string {
4949
return fmt.Sprintf(`
5050
resource "amplience_content_repository" "testrepo" {
51-
hub_id = "foobar"
52-
5351
name = "%[1]s"
5452
label = "%[2]s"
5553
}

amplience/resource_webhook.go

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func resourceWebhookSaveState(data *schema.ResourceData, webhook content.Webhook
237237
data.Set("active", webhook.Active)
238238
data.Set("secret", webhook.Secret)
239239
data.Set("method", webhook.Method)
240-
data.Set("filter", flattenWebhookFilters(webhook.Filters))
240+
data.Set("filter", flattenWebhookFilters(&webhook.Filters))
241241
data.Set("custom_payload", convertCustomPayloadToMap(webhook.CustomPayload))
242242
}
243243

@@ -440,30 +440,47 @@ func resourceWebhookGetCustomPayloadAndValidate(input interface{}) (*content.Web
440440
return &payload, nil
441441
}
442442

443-
func flattenWebhookFilters(filters []content.WebhookFilter) []interface{} {
444-
result := make([]interface{}, len(filters))
445-
446-
for _, filter := range filters {
447-
item := make(map[string]interface{})
448-
arguments := make(map[string]interface{})
449-
450-
switch v := filter.(type) {
451-
case content.WebhookFilterEqual:
452-
item["type"] = "equal"
453-
arguments["json_path"] = v.JSONPath
454-
arguments["values"] = []string{v.Value}
455-
item["arguments"] = arguments
456-
457-
case content.WebhookFilterIn:
458-
item["type"] = "in"
459-
arguments["json_path"] = v.JSONPath
460-
arguments["values"] = v.Values
461-
item["arguments"] = arguments
462-
}
443+
func flattenWebhookFilters(filters *[]content.WebhookFilter) []interface{} {
444+
if filters != nil {
445+
fs := make([]interface{}, len(*filters), len(*filters))
446+
447+
for i, filter := range *filters {
448+
f := make(map[string]interface{})
449+
450+
switch v := filter.(type) {
451+
case content.WebhookFilterEqual:
452+
f["type"] = "equal"
453+
f["arguments"] = flattenWebhookFilterEqualArguments(v.Value, v.JSONPath)
454+
fs[i] = f
463455

456+
case content.WebhookFilterIn:
457+
f["type"] = "in"
458+
f["arguments"] = flattenWebhookFilterInArguments(v.Values, v.JSONPath)
459+
fs[i] = f
460+
}
461+
}
462+
return fs
464463
}
465464

466-
return result
465+
return make([]interface{}, 0)
466+
}
467+
468+
func flattenWebhookFilterEqualArguments(Value string, JSONPath string) interface{} {
469+
args := make([]interface{}, 1, 1)
470+
argMap := make(map[string]interface{})
471+
argMap["json_path"] = JSONPath
472+
argMap["value"] = []string{Value}
473+
args[0] = argMap
474+
return args
475+
}
476+
477+
func flattenWebhookFilterInArguments(Values []string, JSONPath string) interface{} {
478+
args := make([]interface{}, 1, 1)
479+
argMap := make(map[string]interface{})
480+
argMap["json_path"] = JSONPath
481+
argMap["value"] = Values
482+
args[0] = argMap
483+
return args
467484
}
468485

469486
func convertCustomPayloadToMap(payload *content.WebhookCustomPayload) map[string]string {

amplience/resource_webhook_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ func testAccWebhooksConfig(label string) string {
156156
return fmt.Sprintf(`
157157
resource "amplience_webhook" "standard" {
158158
label = "%[1]s"
159-
hub_id = "foobar"
160159
161160
events = [
162161
"dynamic-content.content-item.created",

0 commit comments

Comments
 (0)