Skip to content

Commit

Permalink
Add automations and upsert entity invocation method (#138)
Browse files Browse the repository at this point in the history
* Add automation trigger support for actions

* Add upsert entity invocation method for actions

---------

Co-authored-by: Paz Hershberg <[email protected]>
  • Loading branch information
talsabagport and pazhersh authored Jun 10, 2024
1 parent 58f8e1b commit 30dc7bc
Show file tree
Hide file tree
Showing 7 changed files with 721 additions and 25 deletions.
156 changes: 142 additions & 14 deletions docs/resources/port_action.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,60 @@ description: |-
Action resource
Docs for the Action resource can be found here https://docs.getport.io/create-self-service-experiences/.
Example Usage
```hcl
resource "portaction" "createmicroservice" {
hcl
resource "port_action" "create_microservice" {
title = "Create Microservice"
identifier = "create-microservice"
icon = "Terraform"
selfservicetrigger = {
self_service_trigger = {
operation = "CREATE"
blueprintidentifier = portblueprint.microservice.identifier
userproperties = {
stringprops = {
blueprint_identifier = port_blueprint.microservice.identifier
user_properties = {
string_props = {
myStringIdentifier = {
title = "My String Identifier"
required = true
format = "entity"
blueprint = portblueprint.parent.identifier
blueprint = port_blueprint.parent.identifier
dataset = {
combinator = "and"
rules = [{
property = "$title"
operator = "contains"
value = {
jqquery = "\"specificValue\""
jq_query = "\"specificValue\""
}
}]
}
}
}
numberprops = {
number_props = {
myNumberIdentifier = {
title = "My Number Identifier"
required = true
maximum = 100
minimum = 0
}
}
booleanprops = {
boolean_props = {
myBooleanIdentifier = {
title = "My Boolean Identifier"
required = true
}
}
objectprops = {
object_props = {
myObjectIdentifier = {
title = "My Object Identifier"
required = true
}
}
arrayprops = {
array_props = {
myArrayIdentifier = {
title = "My Array Identifier"
required = true
stringitems = {
string_items = {
format = "entity"
blueprint = portblueprint.parent.identifier
blueprint = port_blueprint.parent.identifier
dataset = jsonencode({
combinator = "and"
rules = [{
Expand All @@ -79,6 +79,26 @@ description: |-
})
}
}
Example Usage with Automation trigger
Port allows setting an automation trigger to an action, for executing an action based on event occurred to an entity in Port.
```hcl
resource "portaction" "deletetemporarymicroservice" {
title = "Delete Temporary Microservice"
identifier = "delete-temp-microservice"
icon = "Terraform"
automationtrigger = {
timerpropertyexpiredevent = {
blueprintidentifier = portblueprint.microservice.identifier
propertyidentifier = "ttl"
}
}
kafka_method = {
payload = jsonencode({
runId: "{{.run.id}}"
})
}
}
```
Example Usage With Condition
```hcl
Expand Down Expand Up @@ -198,6 +218,29 @@ resource "port_action" "create_microservice" {
})
}
}
```

## Example Usage with Automation trigger

Port allows setting an automation trigger to an action, for executing an action based on event occurred to an entity in Port.

```hcl
resource "port_action" "delete_temporary_microservice" {
title = "Delete Temporary Microservice"
identifier = "delete-temp-microservice"
icon = "Terraform"
automation_trigger = {
timer_property_expired_event = {
blueprint_identifier = port_blueprint.microservice.identifier
property_identifier = "ttl"
}
}
kafka_method = {
payload = jsonencode({
runId: "{{.run.id}}"
})
}
}
```

Expand Down Expand Up @@ -252,6 +295,7 @@ resource "port_action" "create_microservice" {

- `approval_email_notification` (Object) The email notification of the approval (see [below for nested schema](#nestedatt--approval_email_notification))
- `approval_webhook_notification` (Attributes) The webhook notification of the approval (see [below for nested schema](#nestedatt--approval_webhook_notification))
- `automation_trigger` (Attributes) Automation trigger for the action (see [below for nested schema](#nestedatt--automation_trigger))
- `azure_method` (Attributes) Azure DevOps invocation method (see [below for nested schema](#nestedatt--azure_method))
- `blueprint` (String, Deprecated) The blueprint identifier the action relates to
- `description` (String) Description
Expand All @@ -263,6 +307,7 @@ resource "port_action" "create_microservice" {
- `required_approval` (Boolean) Require approval before invoking the action
- `self_service_trigger` (Attributes) Self service trigger for the action (see [below for nested schema](#nestedatt--self_service_trigger))
- `title` (String) Title
- `upsert_entity_method` (Attributes) Upsert Entity invocation method (see [below for nested schema](#nestedatt--upsert_entity_method))
- `webhook_method` (Attributes) Webhook invocation method (see [below for nested schema](#nestedatt--webhook_method))

### Read-Only
Expand All @@ -288,6 +333,72 @@ Optional:
- `format` (String) The format to invoke the webhook


<a id="nestedatt--automation_trigger"></a>
### Nested Schema for `automation_trigger`

Optional:

- `any_entity_change_event` (Attributes) Any entity change event trigger (see [below for nested schema](#nestedatt--automation_trigger--any_entity_change_event))
- `entity_created_event` (Attributes) Entity created event trigger (see [below for nested schema](#nestedatt--automation_trigger--entity_created_event))
- `entity_deleted_event` (Attributes) Entity deleted event trigger (see [below for nested schema](#nestedatt--automation_trigger--entity_deleted_event))
- `entity_updated_event` (Attributes) Entity updated event trigger (see [below for nested schema](#nestedatt--automation_trigger--entity_updated_event))
- `jq_condition` (Attributes) JQ condition for automation trigger (see [below for nested schema](#nestedatt--automation_trigger--jq_condition))
- `timer_property_expired_event` (Attributes) Timer property expired event trigger (see [below for nested schema](#nestedatt--automation_trigger--timer_property_expired_event))

<a id="nestedatt--automation_trigger--any_entity_change_event"></a>
### Nested Schema for `automation_trigger.any_entity_change_event`

Required:

- `blueprint_identifier` (String) The blueprint identifier of the changed entity


<a id="nestedatt--automation_trigger--entity_created_event"></a>
### Nested Schema for `automation_trigger.entity_created_event`

Required:

- `blueprint_identifier` (String) The blueprint identifier of the created entity


<a id="nestedatt--automation_trigger--entity_deleted_event"></a>
### Nested Schema for `automation_trigger.entity_deleted_event`

Required:

- `blueprint_identifier` (String) The blueprint identifier of the deleted entity


<a id="nestedatt--automation_trigger--entity_updated_event"></a>
### Nested Schema for `automation_trigger.entity_updated_event`

Required:

- `blueprint_identifier` (String) The blueprint identifier of the updated entity


<a id="nestedatt--automation_trigger--jq_condition"></a>
### Nested Schema for `automation_trigger.jq_condition`

Required:

- `expressions` (List of String) The jq expressions of the condition

Optional:

- `combinator` (String) The combinator of the condition


<a id="nestedatt--automation_trigger--timer_property_expired_event"></a>
### Nested Schema for `automation_trigger.timer_property_expired_event`

Required:

- `blueprint_identifier` (String) The blueprint identifier of the expired timer property
- `property_identifier` (String) The property identifier of the expired timer property



<a id="nestedatt--azure_method"></a>
### Nested Schema for `azure_method`

Expand Down Expand Up @@ -535,6 +646,23 @@ Optional:



<a id="nestedatt--upsert_entity_method"></a>
### Nested Schema for `upsert_entity_method`

Required:

- `blueprint_identifier` (String) Required when selecting type Upsert Entity. The blueprint identifier of the entity for the upsert
- `identifier` (String) Required when selecting type Upsert Entity. The entity identifier for the upsert

Optional:

- `icon` (String) The icon of the entity
- `properties` (String) The properties of the entity (key-value object encoded to a string)
- `relations` (String) The relations of the entity (key-value object encoded to a string)
- `teams` (List of String) The teams the entity belongs to
- `title` (String) The title of the entity


<a id="nestedatt--webhook_method"></a>
### Nested Schema for `webhook_method`

Expand Down
16 changes: 10 additions & 6 deletions internal/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ type (
Required []string `json:"required,omitempty"`
}

MappingSchema struct {
Identifier *string `json:"identifier,omitempty"`
Title *string `json:"title,omitempty"`
Team any `json:"team,omitempty"`
Icon *string `json:"icon,omitempty"`
Properties map[string]any `json:"properties,omitempty"`
Relations map[string]any `json:"relations,omitempty"`
}

InvocationMethod struct {
Type string `json:"type"`
Payload any `json:"payload,omitempty"`
Expand All @@ -166,13 +175,8 @@ type (
DefaultRef *string `json:"defaultRef,omitempty"`
PipelineVariables map[string]any `json:"pipelineVariables,omitempty"`
Webhook *string `json:"webhook,omitempty"`
Identifier *string `json:"identifier,omitempty"`
Title *string `json:"title,omitempty"`
BlueprintIdentifier *string `json:"blueprintIdentifier,omitempty"`
Team any `json:"team,omitempty"`
Icon *string `json:"icon,omitempty"`
Properties map[string]any `json:"properties,omitempty"`
Relations map[string]any `json:"relations,omitempty"`
Mapping *MappingSchema `json:"mapping,omitempty"`
}

ApprovalNotification struct {
Expand Down
89 changes: 89 additions & 0 deletions port/action/actionStateToPortBody.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
"github.com/port-labs/terraform-provider-port-labs/v2/internal/consts"
"github.com/port-labs/terraform-provider-port-labs/v2/internal/flex"
"github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
)

Expand Down Expand Up @@ -110,6 +111,58 @@ func triggerToBody(ctx context.Context, data *ActionModel) (*cli.Trigger, error)
return selfServiceTrigger, nil
}

if data.AutomationTrigger != nil {
automationTrigger := &cli.Trigger{
Type: consts.Automation,
}

if data.AutomationTrigger.JqCondition != nil {
automationTrigger.Condition = &cli.TriggerCondition{
Type: consts.JqCondition,
Expressions: flex.TerraformStringListToGoArray(data.AutomationTrigger.JqCondition.Expressions),
Combinator: data.AutomationTrigger.JqCondition.Combinator.ValueStringPointer(),
}
}

if data.AutomationTrigger.EntityCreatedEvent != nil {
automationTrigger.Event = &cli.TriggerEvent{
Type: consts.EntityCreated,
BlueprintIdentifier: data.AutomationTrigger.EntityCreatedEvent.BlueprintIdentifier.ValueStringPointer(),
}
}

if data.AutomationTrigger.EntityUpdatedEvent != nil {
automationTrigger.Event = &cli.TriggerEvent{
Type: consts.EntityUpdated,
BlueprintIdentifier: data.AutomationTrigger.EntityUpdatedEvent.BlueprintIdentifier.ValueStringPointer(),
}
}

if data.AutomationTrigger.EntityDeletedEvent != nil {
automationTrigger.Event = &cli.TriggerEvent{
Type: consts.EntityDeleted,
BlueprintIdentifier: data.AutomationTrigger.EntityDeletedEvent.BlueprintIdentifier.ValueStringPointer(),
}
}

if data.AutomationTrigger.AnyEntityChangeEvent != nil {
automationTrigger.Event = &cli.TriggerEvent{
Type: consts.AnyEntityChange,
BlueprintIdentifier: data.AutomationTrigger.AnyEntityChangeEvent.BlueprintIdentifier.ValueStringPointer(),
}
}

if data.AutomationTrigger.TimerPropertyExpiredEvent != nil {
automationTrigger.Event = &cli.TriggerEvent{
Type: consts.TimerPropertyExpired,
BlueprintIdentifier: data.AutomationTrigger.TimerPropertyExpiredEvent.BlueprintIdentifier.ValueStringPointer(),
PropertyIdentifier: data.AutomationTrigger.TimerPropertyExpiredEvent.PropertyIdentifier.ValueStringPointer(),
}
}

return automationTrigger, nil
}

return nil, nil
}

Expand Down Expand Up @@ -257,5 +310,41 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca
return azureInvocation, nil
}

if data.UpsertEntityMethod != nil {
var mapping cli.MappingSchema
if data.UpsertEntityMethod.Mapping != nil {
var team interface{}
if data.UpsertEntityMethod.Mapping.Teams != nil {
team = flex.TerraformStringListToGoArray(data.UpsertEntityMethod.Mapping.Teams)
}
properties, err := utils.TerraformJsonStringToGoObject(data.UpsertEntityMethod.Mapping.Properties.ValueStringPointer())
if err != nil {
return nil, err
}

relations, err := utils.TerraformJsonStringToGoObject(data.UpsertEntityMethod.Mapping.Relations.ValueStringPointer())
if err != nil {
return nil, err
}

mapping = cli.MappingSchema{
Team: team,
Identifier: data.UpsertEntityMethod.Mapping.Identifier.ValueStringPointer(),
Title: data.UpsertEntityMethod.Title.ValueStringPointer(),
Icon: data.UpsertEntityMethod.Mapping.Icon.ValueStringPointer(),
Properties: *properties,
Relations: *relations,
}
}

upsertEntityInvocation := &cli.InvocationMethod{
Type: consts.UpsertEntity,
BlueprintIdentifier: data.UpsertEntityMethod.BlueprintIdentifier.ValueStringPointer(),
Mapping: &mapping,
}

return upsertEntityInvocation, nil
}

return nil, nil
}
Loading

0 comments on commit 30dc7bc

Please sign in to comment.