Skip to content

Commit

Permalink
fix(automations): use SetAttribute for expect, after, and for_each (#339
Browse files Browse the repository at this point in the history
)

* fix(automations): use SetAttribute for expect, after, and for_each

* Generate Terraform Docs

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
parkedwards and github-actions[bot] authored Dec 18, 2024
1 parent ac60ec8 commit 9a47589
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 31 deletions.
18 changes: 9 additions & 9 deletions docs/resources/automation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/resources/automation_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
6 changes: 3 additions & 3 deletions internal/provider/resources/automation_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
13 changes: 7 additions & 6 deletions internal/provider/resources/automation_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
52 changes: 42 additions & 10 deletions internal/provider/resources/automation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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.*"}`),
Expand Down

0 comments on commit 9a47589

Please sign in to comment.