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 automations run updated event trigger support #167

Merged
Show file tree
Hide file tree
Changes from all 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
38 changes: 38 additions & 0 deletions examples/resources/port_action/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,41 @@ resource "port_action" "restart_microservice" {
url = "https://app.getport.io"
}
}

resource "port_action" "notifiy_on_mocrosiervice_creation" {
title = "Notify On Microservice Creation"
icon = "Terraform"
identifier = "examples-automation-notify-on-microservice-creation"
automation_trigger = {
entity_created_event = {
blueprint_identifier = port_blueprint.microservice.identifier
}
}
webhook_method = {
type = "WEBHOOK"
url = "https://example.com"
}
publish = true
}

resource "port_action" "notifiy_on_microservice_restart_failed" {
title = "Notify On Microservice Restart Failed"
icon = "Terraform"
identifier = "examples-automation-notify-on-microservice-restart-failed"
automation_trigger = {
run_updated_event = {
action_identifier = port_action.restart_microservice.identifier
}
jq_condition = {
combinator = "and"
expressions = [
".diff.after.status == \"FAILURE\""
],
}
}
webhook_method = {
type = "WEBHOOK"
url = "https://example.com"
}
publish = true
}
1 change: 1 addition & 0 deletions internal/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ type (
Type string `json:"type"`
BlueprintIdentifier *string `json:"blueprintIdentifier,omitempty"`
PropertyIdentifier *string `json:"propertyIdentifier,omitempty"`
ActionIdentifier *string `json:"actionIdentifier,omitempty"`
}

TriggerCondition struct {
Expand Down
1 change: 1 addition & 0 deletions internal/consts/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ const (
EntityDeleted = "ENTITY_DELETED"
TimerPropertyExpired = "TIMER_PROPERTY_EXPIRED"
AnyEntityChange = "ANY_ENTITY_CHANGE"
RunUpdated = "RUN_UPDATED"
JqCondition = "JQ"
)
7 changes: 7 additions & 0 deletions port/action/actionStateToPortBody.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ func triggerToBody(ctx context.Context, data *ActionModel) (*cli.Trigger, error)
}
}

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

return automationTrigger, nil
}

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

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

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

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

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

state.AutomationTrigger = automationTrigger
}

Expand Down
45 changes: 45 additions & 0 deletions port/action/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,51 @@ func TestAccPortAutomationTimerPropertyExpired(t *testing.T) {
})
}

func TestAccPortAutomationRunUpdated(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_updated_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_updated_event.action_identifier", "self_serve_action"),
),
},
},
})
}

func TestAccPortWebhookApproval(t *testing.T) {
identifier := utils.GenID()
actionIdentifier := utils.GenID()
Expand Down
11 changes: 11 additions & 0 deletions port/action/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ 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_updated_event"),
),
},
},
Expand Down Expand Up @@ -208,6 +209,16 @@ func ActionSchema() map[string]schema.Attribute {
},
},
},
"run_updated_event": schema.SingleNestedAttribute{
MarkdownDescription: "Run updated event trigger",
Optional: true,
Attributes: map[string]schema.Attribute{
"action_identifier": schema.StringAttribute{
MarkdownDescription: "The action identifier of the updated run",
Required: true,
},
},
},
"jq_condition": schema.SingleNestedAttribute{
MarkdownDescription: "JQ condition for automation trigger",
Optional: true,
Expand Down
Loading