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 7 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
3 changes: 3 additions & 0 deletions internal/consts/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +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"
)
21 changes: 21 additions & 0 deletions port/action/actionStateToPortBody.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,27 @@ func triggerToBody(ctx context.Context, data *ActionModel) (*cli.Trigger, error)
}
}

if data.AutomationTrigger.RunCreatedEvent != nil {
automationTrigger.Event = &cli.TriggerEvent{
Type: consts.RunUpdated,
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.RunUpdated,
ActionIdentifier: data.AutomationTrigger.AnyRunChangeEvent.ActionIdentifier.ValueStringPointer(),
}
}

return automationTrigger, nil
}

Expand Down
15 changes: 15 additions & 0 deletions port/action/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +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"`
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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 ActionEvent struct {
ActionIdentifier types.String `tfsdk:"action_identifier"`
}
type RunCreatedEvent struct {
ActionEvent
}
type RunUpdatedEvent struct {
ActionEvent
}
type AnyRunChangeEvent struct {
ActionEvent
}

Copy link
Contributor

Choose a reason for hiding this comment

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

maybe the same can be assigned to Entity<action>EventModel ?


type JqConditionModel struct {
Expressions []types.String `tfsdk:"expressions"`
Combinator types.String `tfsdk:"combinator"`
Expand All @@ -310,6 +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_chnage_event"`
JqCondition *JqConditionModel `tfsdk:"jq_condition"`
}

Expand Down
18 changes: 18 additions & 0 deletions port/action/refreshActionState.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +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
135 changes: 135 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,141 @@ 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"),
),
},
},
})
}

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 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"),
),
},
},
})
}

func TestAccPortWebhookApproval(t *testing.T) {
identifier := utils.GenID()
actionIdentifier := utils.GenID()
Expand Down
33 changes: 33 additions & 0 deletions port/action/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +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 @@ -208,6 +211,36 @@ 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,
Attributes: map[string]schema.Attribute{
"action_identifier": schema.StringAttribute{
MarkdownDescription: "The action identifier of the updated run",
Required: true,
},
},
},
"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