Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add automation run created and any run change event trigger support #177

Merged
merged 12 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/consts/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const (
EntityDeleted = "ENTITY_DELETED"
TimerPropertyExpired = "TIMER_PROPERTY_EXPIRED"
AnyEntityChange = "ANY_ENTITY_CHANGE"
RunCreated = "RUN_CREATED"
RunUpdated = "RUN_UPDATED"
AnyRunChange = "ANY_RUN_CHANGE"
JqCondition = "JQ"
)
14 changes: 14 additions & 0 deletions port/action/actionStateToPortBody.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,27 @@ func triggerToBody(ctx context.Context, data *ActionModel) (*cli.Trigger, error)
}
}

if data.AutomationTrigger.RunCreatedEvent != nil {
automationTrigger.Event = &cli.TriggerEvent{
Type: consts.RunCreated,
ActionIdentifier: data.AutomationTrigger.RunCreatedEvent.ActionIdentifier.ValueStringPointer(),
}
}

if data.AutomationTrigger.RunUpdatedEvent != nil {
automationTrigger.Event = &cli.TriggerEvent{
Type: consts.RunUpdated,
ActionIdentifier: data.AutomationTrigger.RunUpdatedEvent.ActionIdentifier.ValueStringPointer(),
}
}

if data.AutomationTrigger.AnyRunChangeEvent != nil {
automationTrigger.Event = &cli.TriggerEvent{
Type: consts.AnyRunChange,
ActionIdentifier: data.AutomationTrigger.AnyRunChangeEvent.ActionIdentifier.ValueStringPointer(),
}
}

return automationTrigger, nil
}

Expand Down
10 changes: 10 additions & 0 deletions port/action/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,18 @@ type TimerPropertyExpiredEventModel struct {
PropertyIdentifier types.String `tfsdk:"property_identifier"`
}

type RunCreatedEvent struct {
ActionIdentifier types.String `tfsdk:"action_identifier"`
}

type RunUpdatedEvent struct {
ActionIdentifier types.String `tfsdk:"action_identifier"`
}

type AnyRunChangeEvent struct {
ActionIdentifier types.String `tfsdk:"action_identifier"`
}

type JqConditionModel struct {
Expressions []types.String `tfsdk:"expressions"`
Combinator types.String `tfsdk:"combinator"`
Expand All @@ -314,7 +322,9 @@ type AutomationTriggerModel struct {
EntityDeletedEvent *EntityDeletedEventModel `tfsdk:"entity_deleted_event"`
AnyEntityChangeEvent *AnyEntityChangeEventModel `tfsdk:"any_entity_change_event"`
TimerPropertyExpiredEvent *TimerPropertyExpiredEventModel `tfsdk:"timer_property_expired_event"`
RunCreatedEvent *RunCreatedEvent `tfsdk:"run_created_event"`
RunUpdatedEvent *RunUpdatedEvent `tfsdk:"run_updated_event"`
AnyRunChangeEvent *AnyRunChangeEvent `tfsdk:"any_run_change_event"`
JqCondition *JqConditionModel `tfsdk:"jq_condition"`
}

Expand Down
12 changes: 12 additions & 0 deletions port/action/refreshActionState.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,24 @@ func writeTriggerToResource(ctx context.Context, a *cli.Action, state *ActionMod
}
}

if a.Trigger.Event.Type == consts.RunCreated {
automationTrigger.RunCreatedEvent = &RunCreatedEvent{
ActionIdentifier: types.StringValue(*a.Trigger.Event.ActionIdentifier),
}
}

if a.Trigger.Event.Type == consts.RunUpdated {
automationTrigger.RunUpdatedEvent = &RunUpdatedEvent{
ActionIdentifier: types.StringValue(*a.Trigger.Event.ActionIdentifier),
}
}

if a.Trigger.Event.Type == consts.AnyRunChange {
automationTrigger.AnyRunChangeEvent = &AnyRunChangeEvent{
ActionIdentifier: types.StringValue(*a.Trigger.Event.ActionIdentifier),
}
}

state.AutomationTrigger = automationTrigger
}

Expand Down
108 changes: 108 additions & 0 deletions port/action/resource_test.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

plase add one import state test as well

Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,57 @@ func TestAccPortAutomationTimerPropertyExpired(t *testing.T) {
})
}

func TestAccPortAutomationRunCreated(t *testing.T) {
identifier := utils.GenID()
actionIdentifier := utils.GenID()
var testAccActionConfigCreate = testAccCreateBlueprintConfig(identifier) + fmt.Sprintf(`
resource "port_action" "self_serve_action" {
title = "self serve action"
identifier = "self_serve_action"
icon = "Terraform"
self_service_trigger = {
operation = "CREATE"
user_properties = {
}
}
kafka_method = {}
}

resource "port_action" "create_microservice" {
title = "TF Provider Test"
identifier = "%s"
icon = "Terraform"
automation_trigger = {
run_created_event = {
action_identifier = "self_serve_action"
}
}
kafka_method = {}
depends_on = [port_action.self_serve_action]
}`, actionIdentifier)
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: acctest.ProviderConfig + testAccActionConfigCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("port_action.create_microservice", "title", "TF Provider Test"),
resource.TestCheckResourceAttr("port_action.create_microservice", "identifier", actionIdentifier),
resource.TestCheckResourceAttr("port_action.create_microservice", "icon", "Terraform"),
resource.TestCheckResourceAttr("port_action.create_microservice", "automation_trigger.run_created_event.action_identifier", "self_serve_action"),
),
},
{
ResourceName: "port_action.create_microservice",
ImportState: true,
ImportStateVerify: true,
ImportStateId: actionIdentifier,
},
},
})
}

func TestAccPortAutomationRunUpdated(t *testing.T) {
identifier := utils.GenID()
actionIdentifier := utils.GenID()
Expand Down Expand Up @@ -1608,6 +1659,63 @@ func TestAccPortAutomationRunUpdated(t *testing.T) {
resource.TestCheckResourceAttr("port_action.create_microservice", "automation_trigger.run_updated_event.action_identifier", "self_serve_action"),
),
},
{
ResourceName: "port_action.create_microservice",
ImportState: true,
ImportStateVerify: true,
ImportStateId: actionIdentifier,
},
},
})
}

func TestAccPortAutomationAnyRunChange(t *testing.T) {
identifier := utils.GenID()
actionIdentifier := utils.GenID()
var testAccActionConfigCreate = testAccCreateBlueprintConfig(identifier) + fmt.Sprintf(`
resource "port_action" "self_serve_action" {
title = "self serve action"
identifier = "self_serve_action"
icon = "Terraform"
self_service_trigger = {
operation = "CREATE"
user_properties = {
}
}
kafka_method = {}
}

resource "port_action" "create_microservice" {
title = "TF Provider Test"
identifier = "%s"
icon = "Terraform"
automation_trigger = {
any_run_change_event = {
action_identifier = "self_serve_action"
}
}
kafka_method = {}
depends_on = [port_action.self_serve_action]
}`, actionIdentifier)
resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: acctest.ProviderConfig + testAccActionConfigCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("port_action.create_microservice", "title", "TF Provider Test"),
resource.TestCheckResourceAttr("port_action.create_microservice", "identifier", actionIdentifier),
resource.TestCheckResourceAttr("port_action.create_microservice", "icon", "Terraform"),
resource.TestCheckResourceAttr("port_action.create_microservice", "automation_trigger.any_run_change_event.action_identifier", "self_serve_action"),
),
},
{
ResourceName: "port_action.create_microservice",
ImportState: true,
ImportStateVerify: true,
ImportStateId: actionIdentifier,
},
},
})
}
Expand Down
22 changes: 22 additions & 0 deletions port/action/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func ActionSchema() map[string]schema.Attribute {
path.MatchRelative().AtParent().AtName("entity_deleted_event"),
path.MatchRelative().AtParent().AtName("any_entity_change_event"),
path.MatchRelative().AtParent().AtName("timer_property_expired_event"),
path.MatchRelative().AtParent().AtName("run_created_event"),
path.MatchRelative().AtParent().AtName("run_updated_event"),
path.MatchRelative().AtParent().AtName("any_run_change_event"),
),
},
},
Expand Down Expand Up @@ -209,6 +211,16 @@ func ActionSchema() map[string]schema.Attribute {
},
},
},
"run_created_event": schema.SingleNestedAttribute{
MarkdownDescription: "Run created event trigger",
Optional: true,
Attributes: map[string]schema.Attribute{
"action_identifier": schema.StringAttribute{
MarkdownDescription: "The action identifier of the created run",
Required: true,
},
},
},
"run_updated_event": schema.SingleNestedAttribute{
MarkdownDescription: "Run updated event trigger",
Optional: true,
Expand All @@ -219,6 +231,16 @@ func ActionSchema() map[string]schema.Attribute {
},
},
},
"any_run_change_event": schema.SingleNestedAttribute{
MarkdownDescription: "Any run change event trigger",
Optional: true,
Attributes: map[string]schema.Attribute{
"action_identifier": schema.StringAttribute{
MarkdownDescription: "The action identifier of the changed run",
Required: true,
},
},
},
"jq_condition": schema.SingleNestedAttribute{
MarkdownDescription: "JQ condition for automation trigger",
Optional: true,
Expand Down
Loading