diff --git a/docs/resources/automation.md b/docs/resources/automation.md index c8e5d44d..4f4af998 100644 --- a/docs/resources/automation.md +++ b/docs/resources/automation.md @@ -304,9 +304,9 @@ Required: Optional: -- `after` (List of String) The event(s) which must first been seen to fire this trigger. If empty, then fire this trigger immediately -- `expect` (List of String) The event(s) this trigger is expecting to see. If empty, this trigger will match any event -- `for_each` (List of String) Evaluate the trigger separately for each distinct value of these labels on the resource +- `after` (Set of String) The event(s) which must first been seen to fire this trigger. If empty, then fire this trigger immediately +- `expect` (Set of String) The event(s) this trigger is expecting to see. If empty, this trigger will match any event +- `for_each` (Set of String) Evaluate the trigger separately for each distinct value of these labels on the resource - `match` (String) (JSON) Resource specification labels which this trigger will match. Use `jsonencode()`. - `match_related` (String) (JSON) Resource specification labels for related resources which this trigger will match. Use `jsonencode()`. - `threshold` (Number) The number of events required for this trigger to fire (Reactive) or expected (Proactive) @@ -349,9 +349,9 @@ Required: Optional: -- `after` (List of String) The event(s) which must first been seen to fire this trigger. If empty, then fire this trigger immediately -- `expect` (List of String) The event(s) this trigger is expecting to see. If empty, this trigger will match any event -- `for_each` (List of String) Evaluate the trigger separately for each distinct value of these labels on the resource +- `after` (Set of String) The event(s) which must first been seen to fire this trigger. If empty, then fire this trigger immediately +- `expect` (Set of String) The event(s) this trigger is expecting to see. If empty, this trigger will match any event +- `for_each` (Set of String) Evaluate the trigger separately for each distinct value of these labels on the resource - `match` (String) (JSON) Resource specification labels which this trigger will match. Use `jsonencode()`. - `match_related` (String) (JSON) Resource specification labels for related resources which this trigger will match. Use `jsonencode()`. - `threshold` (Number) The number of events required for this trigger to fire (Reactive) or expected (Proactive) @@ -411,9 +411,9 @@ Required: Optional: -- `after` (List of String) The event(s) which must first been seen to fire this trigger. If empty, then fire this trigger immediately -- `expect` (List of String) The event(s) this trigger is expecting to see. If empty, this trigger will match any event -- `for_each` (List of String) Evaluate the trigger separately for each distinct value of these labels on the resource +- `after` (Set of String) The event(s) which must first been seen to fire this trigger. If empty, then fire this trigger immediately +- `expect` (Set of String) The event(s) this trigger is expecting to see. If empty, this trigger will match any event +- `for_each` (Set of String) Evaluate the trigger separately for each distinct value of these labels on the resource - `match` (String) (JSON) Resource specification labels which this trigger will match. Use `jsonencode()`. - `match_related` (String) (JSON) Resource specification labels for related resources which this trigger will match. Use `jsonencode()`. - `threshold` (Number) The number of events required for this trigger to fire (Reactive) or expected (Proactive) diff --git a/internal/provider/resources/automation_model.go b/internal/provider/resources/automation_model.go index 6a20db55..b19be232 100644 --- a/internal/provider/resources/automation_model.go +++ b/internal/provider/resources/automation_model.go @@ -41,9 +41,9 @@ type EventTriggerModel struct { Posture types.String `tfsdk:"posture"` Match jsontypes.Normalized `tfsdk:"match"` MatchRelated jsontypes.Normalized `tfsdk:"match_related"` - After types.List `tfsdk:"after"` - Expect types.List `tfsdk:"expect"` - ForEach types.List `tfsdk:"for_each"` + After types.Set `tfsdk:"after"` + Expect types.Set `tfsdk:"expect"` + ForEach types.Set `tfsdk:"for_each"` Threshold types.Int64 `tfsdk:"threshold"` Within types.Float64 `tfsdk:"within"` } diff --git a/internal/provider/resources/automation_resource.go b/internal/provider/resources/automation_resource.go index 934cf045..e84371b9 100644 --- a/internal/provider/resources/automation_resource.go +++ b/internal/provider/resources/automation_resource.go @@ -391,11 +391,11 @@ func mapTriggerAPIToTerraform(ctx context.Context, apiTrigger *api.Trigger, tfTr tfTriggerModel.Event.MatchRelated = jsontypes.NewNormalizedValue(string(matchRelatedByteSlice)) // Parse and set After, Expect, and ForEach (lists) - after, diagnostics := types.ListValueFrom(ctx, types.StringType, apiTrigger.After) + after, diagnostics := types.SetValueFrom(ctx, types.StringType, apiTrigger.After) diags.Append(diagnostics...) - expect, diagnostics := types.ListValueFrom(ctx, types.StringType, apiTrigger.Expect) + expect, diagnostics := types.SetValueFrom(ctx, types.StringType, apiTrigger.Expect) diags.Append(diagnostics...) - forEach, diagnostics := types.ListValueFrom(ctx, types.StringType, apiTrigger.ForEach) + forEach, diagnostics := types.SetValueFrom(ctx, types.StringType, apiTrigger.ForEach) diags.Append(diagnostics...) if diags.HasError() { diff --git a/internal/provider/resources/automation_schema.go b/internal/provider/resources/automation_schema.go index 1f2af465..6c59cdaa 100644 --- a/internal/provider/resources/automation_schema.go +++ b/internal/provider/resources/automation_schema.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/listdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/setdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -143,26 +144,26 @@ func ResourceTriggerSchemaAttributes() map[string]schema.Attribute { CustomType: jsontypes.NormalizedType{}, Default: stringdefault.StaticString("{}"), }, - "after": schema.ListAttribute{ + "after": schema.SetAttribute{ Optional: true, Computed: true, Description: "The event(s) which must first been seen to fire this trigger. If empty, then fire this trigger immediately", ElementType: types.StringType, - Default: listdefault.StaticValue(basetypes.NewListValueMust(types.StringType, []attr.Value{})), + Default: setdefault.StaticValue(basetypes.NewSetValueMust(types.StringType, []attr.Value{})), }, - "expect": schema.ListAttribute{ + "expect": schema.SetAttribute{ Optional: true, Computed: true, Description: "The event(s) this trigger is expecting to see. If empty, this trigger will match any event", ElementType: types.StringType, - Default: listdefault.StaticValue(basetypes.NewListValueMust(types.StringType, []attr.Value{})), + Default: setdefault.StaticValue(basetypes.NewSetValueMust(types.StringType, []attr.Value{})), }, - "for_each": schema.ListAttribute{ + "for_each": schema.SetAttribute{ Optional: true, Computed: true, Description: "Evaluate the trigger separately for each distinct value of these labels on the resource", ElementType: types.StringType, - Default: listdefault.StaticValue(basetypes.NewListValueMust(types.StringType, []attr.Value{})), + Default: setdefault.StaticValue(basetypes.NewSetValueMust(types.StringType, []attr.Value{})), }, "threshold": schema.Int64Attribute{ Optional: true, diff --git a/internal/provider/resources/automation_test.go b/internal/provider/resources/automation_test.go index 747812f3..7366ce7b 100644 --- a/internal/provider/resources/automation_test.go +++ b/internal/provider/resources/automation_test.go @@ -40,9 +40,19 @@ resource "prefect_automation" "{{ .AutomationResourceName }}" { "prefect.resource.id" : ["prefect.flow.ce6ec0c9-4b51-483b-a776-43c085b6c4f8"] "prefect.resource.role" : "flow" }) - after = ["prefect.flow-run.completed"] - expect = ["prefect.flow-run.failed"] - for_each = ["prefect.resource.id"] + after = [ + "prefect.flow-run.Completed", + "prefect.flow-run.Succeeded", + ] + expect = [ + "prefect.flow-run.Failed", + "prefect.flow-run.Cancelled", + "prefect.flow-run.Crashed", + ] + for_each = [ + "prefect.resource.id", + "prefect.resource.role", + ] threshold = 1 within = 60 } @@ -141,6 +151,19 @@ resource "prefect_automation" "{{ .AutomationResourceName }}" { "prefect.resource.role" = "flow" }) posture = "Reactive" + after = [ + "prefect.flow-run.Completed", + "prefect.flow-run.Succeeded", + ] + expect = [ + "prefect.flow-run.Failed", + "prefect.flow-run.Cancelled", + "prefect.flow-run.Crashed", + ] + for_each = [ + "prefect.resource.id", + "prefect.resource.role", + ] threshold = 1 within = 0 } @@ -288,12 +311,16 @@ func TestAccResource_automation(t *testing.T) { resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.posture", "Reactive"), testutils.TestCheckJSONAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.match", `{"prefect.resource.id":"prefect.flow-run.*"}`), testutils.TestCheckJSONAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.match_related", `{"prefect.resource.id":["prefect.flow.ce6ec0c9-4b51-483b-a776-43c085b6c4f8"],"prefect.resource.role":"flow"}`), - resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.after.#", "1"), - resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.after.0", "prefect.flow-run.completed"), - resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.expect.#", "1"), - resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.expect.0", "prefect.flow-run.failed"), - resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.for_each.#", "1"), + resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.after.#", "2"), + resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.after.0", "prefect.flow-run.Completed"), + resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.after.1", "prefect.flow-run.Succeeded"), + resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.expect.#", "3"), + resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.expect.0", "prefect.flow-run.Cancelled"), + resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.expect.1", "prefect.flow-run.Crashed"), + resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.expect.2", "prefect.flow-run.Failed"), + resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.for_each.#", "2"), resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.for_each.0", "prefect.resource.id"), + resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.for_each.1", "prefect.resource.role"), resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.threshold", "1"), resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "trigger.event.within", "60"), resource.TestCheckResourceAttr(eventTriggerAutomationResourceNameAndPath, "actions.#", "1"), @@ -357,13 +384,18 @@ func TestAccResource_automation(t *testing.T) { resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.require", "any"), resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.within", "302"), resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.#", "2"), - resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.expect.#", "1"), - resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.expect.0", "prefect.flow-run.Failed"), + resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.expect.#", "3"), + resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.expect.0", "prefect.flow-run.Cancelled"), + resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.expect.1", "prefect.flow-run.Crashed"), + resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.expect.2", "prefect.flow-run.Failed"), testutils.TestCheckJSONAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.match", `{"prefect.resource.id":"prefect.flow-run.*"}`), testutils.TestCheckJSONAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.match_related", `{"prefect.resource.id":"prefect.flow-run.*","prefect.resource.role":"flow"}`), resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.posture", "Reactive"), resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.threshold", "1"), resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.within", "0"), + resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.after.#", "2"), + resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.after.0", "prefect.flow-run.Completed"), + resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.0.event.after.1", "prefect.flow-run.Succeeded"), resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.1.event.expect.#", "1"), resource.TestCheckResourceAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.1.event.expect.0", "prefect.flow-run.Completed"), testutils.TestCheckJSONAttr(compoundTriggerAutomationResourceNameAndPath, "trigger.compound.triggers.1.event.match", `{"prefect.resource.id":"prefect.flow-run.*"}`),