Skip to content

Commit 30dc7bc

Browse files
Add automations and upsert entity invocation method (#138)
* Add automation trigger support for actions * Add upsert entity invocation method for actions --------- Co-authored-by: Paz Hershberg <[email protected]>
1 parent 58f8e1b commit 30dc7bc

File tree

7 files changed

+721
-25
lines changed

7 files changed

+721
-25
lines changed

docs/resources/port_action.md

+142-14
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,60 @@ description: |-
66
Action resource
77
Docs for the Action resource can be found here https://docs.getport.io/create-self-service-experiences/.
88
Example Usage
9-
```hcl
10-
resource "portaction" "createmicroservice" {
9+
hcl
10+
resource "port_action" "create_microservice" {
1111
title = "Create Microservice"
1212
identifier = "create-microservice"
1313
icon = "Terraform"
14-
selfservicetrigger = {
14+
self_service_trigger = {
1515
operation = "CREATE"
16-
blueprintidentifier = portblueprint.microservice.identifier
17-
userproperties = {
18-
stringprops = {
16+
blueprint_identifier = port_blueprint.microservice.identifier
17+
user_properties = {
18+
string_props = {
1919
myStringIdentifier = {
2020
title = "My String Identifier"
2121
required = true
2222
format = "entity"
23-
blueprint = portblueprint.parent.identifier
23+
blueprint = port_blueprint.parent.identifier
2424
dataset = {
2525
combinator = "and"
2626
rules = [{
2727
property = "$title"
2828
operator = "contains"
2929
value = {
30-
jqquery = "\"specificValue\""
30+
jq_query = "\"specificValue\""
3131
}
3232
}]
3333
}
3434
}
3535
}
36-
numberprops = {
36+
number_props = {
3737
myNumberIdentifier = {
3838
title = "My Number Identifier"
3939
required = true
4040
maximum = 100
4141
minimum = 0
4242
}
4343
}
44-
booleanprops = {
44+
boolean_props = {
4545
myBooleanIdentifier = {
4646
title = "My Boolean Identifier"
4747
required = true
4848
}
4949
}
50-
objectprops = {
50+
object_props = {
5151
myObjectIdentifier = {
5252
title = "My Object Identifier"
5353
required = true
5454
}
5555
}
56-
arrayprops = {
56+
array_props = {
5757
myArrayIdentifier = {
5858
title = "My Array Identifier"
5959
required = true
60-
stringitems = {
60+
string_items = {
6161
format = "entity"
62-
blueprint = portblueprint.parent.identifier
62+
blueprint = port_blueprint.parent.identifier
6363
dataset = jsonencode({
6464
combinator = "and"
6565
rules = [{
@@ -79,6 +79,26 @@ description: |-
7979
})
8080
}
8181
}
82+
83+
Example Usage with Automation trigger
84+
Port allows setting an automation trigger to an action, for executing an action based on event occurred to an entity in Port.
85+
```hcl
86+
resource "portaction" "deletetemporarymicroservice" {
87+
title = "Delete Temporary Microservice"
88+
identifier = "delete-temp-microservice"
89+
icon = "Terraform"
90+
automationtrigger = {
91+
timerpropertyexpiredevent = {
92+
blueprintidentifier = portblueprint.microservice.identifier
93+
propertyidentifier = "ttl"
94+
}
95+
}
96+
kafka_method = {
97+
payload = jsonencode({
98+
runId: "{{.run.id}}"
99+
})
100+
}
101+
}
82102
```
83103
Example Usage With Condition
84104
```hcl
@@ -198,6 +218,29 @@ resource "port_action" "create_microservice" {
198218
})
199219
}
200220
}
221+
```
222+
223+
## Example Usage with Automation trigger
224+
225+
Port allows setting an automation trigger to an action, for executing an action based on event occurred to an entity in Port.
226+
227+
```hcl
228+
resource "port_action" "delete_temporary_microservice" {
229+
title = "Delete Temporary Microservice"
230+
identifier = "delete-temp-microservice"
231+
icon = "Terraform"
232+
automation_trigger = {
233+
timer_property_expired_event = {
234+
blueprint_identifier = port_blueprint.microservice.identifier
235+
property_identifier = "ttl"
236+
}
237+
}
238+
kafka_method = {
239+
payload = jsonencode({
240+
runId: "{{.run.id}}"
241+
})
242+
}
243+
}
201244
202245
```
203246

@@ -252,6 +295,7 @@ resource "port_action" "create_microservice" {
252295

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

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

290335

336+
<a id="nestedatt--automation_trigger"></a>
337+
### Nested Schema for `automation_trigger`
338+
339+
Optional:
340+
341+
- `any_entity_change_event` (Attributes) Any entity change event trigger (see [below for nested schema](#nestedatt--automation_trigger--any_entity_change_event))
342+
- `entity_created_event` (Attributes) Entity created event trigger (see [below for nested schema](#nestedatt--automation_trigger--entity_created_event))
343+
- `entity_deleted_event` (Attributes) Entity deleted event trigger (see [below for nested schema](#nestedatt--automation_trigger--entity_deleted_event))
344+
- `entity_updated_event` (Attributes) Entity updated event trigger (see [below for nested schema](#nestedatt--automation_trigger--entity_updated_event))
345+
- `jq_condition` (Attributes) JQ condition for automation trigger (see [below for nested schema](#nestedatt--automation_trigger--jq_condition))
346+
- `timer_property_expired_event` (Attributes) Timer property expired event trigger (see [below for nested schema](#nestedatt--automation_trigger--timer_property_expired_event))
347+
348+
<a id="nestedatt--automation_trigger--any_entity_change_event"></a>
349+
### Nested Schema for `automation_trigger.any_entity_change_event`
350+
351+
Required:
352+
353+
- `blueprint_identifier` (String) The blueprint identifier of the changed entity
354+
355+
356+
<a id="nestedatt--automation_trigger--entity_created_event"></a>
357+
### Nested Schema for `automation_trigger.entity_created_event`
358+
359+
Required:
360+
361+
- `blueprint_identifier` (String) The blueprint identifier of the created entity
362+
363+
364+
<a id="nestedatt--automation_trigger--entity_deleted_event"></a>
365+
### Nested Schema for `automation_trigger.entity_deleted_event`
366+
367+
Required:
368+
369+
- `blueprint_identifier` (String) The blueprint identifier of the deleted entity
370+
371+
372+
<a id="nestedatt--automation_trigger--entity_updated_event"></a>
373+
### Nested Schema for `automation_trigger.entity_updated_event`
374+
375+
Required:
376+
377+
- `blueprint_identifier` (String) The blueprint identifier of the updated entity
378+
379+
380+
<a id="nestedatt--automation_trigger--jq_condition"></a>
381+
### Nested Schema for `automation_trigger.jq_condition`
382+
383+
Required:
384+
385+
- `expressions` (List of String) The jq expressions of the condition
386+
387+
Optional:
388+
389+
- `combinator` (String) The combinator of the condition
390+
391+
392+
<a id="nestedatt--automation_trigger--timer_property_expired_event"></a>
393+
### Nested Schema for `automation_trigger.timer_property_expired_event`
394+
395+
Required:
396+
397+
- `blueprint_identifier` (String) The blueprint identifier of the expired timer property
398+
- `property_identifier` (String) The property identifier of the expired timer property
399+
400+
401+
291402
<a id="nestedatt--azure_method"></a>
292403
### Nested Schema for `azure_method`
293404

@@ -535,6 +646,23 @@ Optional:
535646

536647

537648

649+
<a id="nestedatt--upsert_entity_method"></a>
650+
### Nested Schema for `upsert_entity_method`
651+
652+
Required:
653+
654+
- `blueprint_identifier` (String) Required when selecting type Upsert Entity. The blueprint identifier of the entity for the upsert
655+
- `identifier` (String) Required when selecting type Upsert Entity. The entity identifier for the upsert
656+
657+
Optional:
658+
659+
- `icon` (String) The icon of the entity
660+
- `properties` (String) The properties of the entity (key-value object encoded to a string)
661+
- `relations` (String) The relations of the entity (key-value object encoded to a string)
662+
- `teams` (List of String) The teams the entity belongs to
663+
- `title` (String) The title of the entity
664+
665+
538666
<a id="nestedatt--webhook_method"></a>
539667
### Nested Schema for `webhook_method`
540668

internal/cli/models.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,15 @@ type (
146146
Required []string `json:"required,omitempty"`
147147
}
148148

149+
MappingSchema struct {
150+
Identifier *string `json:"identifier,omitempty"`
151+
Title *string `json:"title,omitempty"`
152+
Team any `json:"team,omitempty"`
153+
Icon *string `json:"icon,omitempty"`
154+
Properties map[string]any `json:"properties,omitempty"`
155+
Relations map[string]any `json:"relations,omitempty"`
156+
}
157+
149158
InvocationMethod struct {
150159
Type string `json:"type"`
151160
Payload any `json:"payload,omitempty"`
@@ -166,13 +175,8 @@ type (
166175
DefaultRef *string `json:"defaultRef,omitempty"`
167176
PipelineVariables map[string]any `json:"pipelineVariables,omitempty"`
168177
Webhook *string `json:"webhook,omitempty"`
169-
Identifier *string `json:"identifier,omitempty"`
170-
Title *string `json:"title,omitempty"`
171178
BlueprintIdentifier *string `json:"blueprintIdentifier,omitempty"`
172-
Team any `json:"team,omitempty"`
173-
Icon *string `json:"icon,omitempty"`
174-
Properties map[string]any `json:"properties,omitempty"`
175-
Relations map[string]any `json:"relations,omitempty"`
179+
Mapping *MappingSchema `json:"mapping,omitempty"`
176180
}
177181

178182
ApprovalNotification struct {

port/action/actionStateToPortBody.go

+89
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
77
"github.com/port-labs/terraform-provider-port-labs/v2/internal/consts"
8+
"github.com/port-labs/terraform-provider-port-labs/v2/internal/flex"
89
"github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
910
)
1011

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

114+
if data.AutomationTrigger != nil {
115+
automationTrigger := &cli.Trigger{
116+
Type: consts.Automation,
117+
}
118+
119+
if data.AutomationTrigger.JqCondition != nil {
120+
automationTrigger.Condition = &cli.TriggerCondition{
121+
Type: consts.JqCondition,
122+
Expressions: flex.TerraformStringListToGoArray(data.AutomationTrigger.JqCondition.Expressions),
123+
Combinator: data.AutomationTrigger.JqCondition.Combinator.ValueStringPointer(),
124+
}
125+
}
126+
127+
if data.AutomationTrigger.EntityCreatedEvent != nil {
128+
automationTrigger.Event = &cli.TriggerEvent{
129+
Type: consts.EntityCreated,
130+
BlueprintIdentifier: data.AutomationTrigger.EntityCreatedEvent.BlueprintIdentifier.ValueStringPointer(),
131+
}
132+
}
133+
134+
if data.AutomationTrigger.EntityUpdatedEvent != nil {
135+
automationTrigger.Event = &cli.TriggerEvent{
136+
Type: consts.EntityUpdated,
137+
BlueprintIdentifier: data.AutomationTrigger.EntityUpdatedEvent.BlueprintIdentifier.ValueStringPointer(),
138+
}
139+
}
140+
141+
if data.AutomationTrigger.EntityDeletedEvent != nil {
142+
automationTrigger.Event = &cli.TriggerEvent{
143+
Type: consts.EntityDeleted,
144+
BlueprintIdentifier: data.AutomationTrigger.EntityDeletedEvent.BlueprintIdentifier.ValueStringPointer(),
145+
}
146+
}
147+
148+
if data.AutomationTrigger.AnyEntityChangeEvent != nil {
149+
automationTrigger.Event = &cli.TriggerEvent{
150+
Type: consts.AnyEntityChange,
151+
BlueprintIdentifier: data.AutomationTrigger.AnyEntityChangeEvent.BlueprintIdentifier.ValueStringPointer(),
152+
}
153+
}
154+
155+
if data.AutomationTrigger.TimerPropertyExpiredEvent != nil {
156+
automationTrigger.Event = &cli.TriggerEvent{
157+
Type: consts.TimerPropertyExpired,
158+
BlueprintIdentifier: data.AutomationTrigger.TimerPropertyExpiredEvent.BlueprintIdentifier.ValueStringPointer(),
159+
PropertyIdentifier: data.AutomationTrigger.TimerPropertyExpiredEvent.PropertyIdentifier.ValueStringPointer(),
160+
}
161+
}
162+
163+
return automationTrigger, nil
164+
}
165+
113166
return nil, nil
114167
}
115168

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

313+
if data.UpsertEntityMethod != nil {
314+
var mapping cli.MappingSchema
315+
if data.UpsertEntityMethod.Mapping != nil {
316+
var team interface{}
317+
if data.UpsertEntityMethod.Mapping.Teams != nil {
318+
team = flex.TerraformStringListToGoArray(data.UpsertEntityMethod.Mapping.Teams)
319+
}
320+
properties, err := utils.TerraformJsonStringToGoObject(data.UpsertEntityMethod.Mapping.Properties.ValueStringPointer())
321+
if err != nil {
322+
return nil, err
323+
}
324+
325+
relations, err := utils.TerraformJsonStringToGoObject(data.UpsertEntityMethod.Mapping.Relations.ValueStringPointer())
326+
if err != nil {
327+
return nil, err
328+
}
329+
330+
mapping = cli.MappingSchema{
331+
Team: team,
332+
Identifier: data.UpsertEntityMethod.Mapping.Identifier.ValueStringPointer(),
333+
Title: data.UpsertEntityMethod.Title.ValueStringPointer(),
334+
Icon: data.UpsertEntityMethod.Mapping.Icon.ValueStringPointer(),
335+
Properties: *properties,
336+
Relations: *relations,
337+
}
338+
}
339+
340+
upsertEntityInvocation := &cli.InvocationMethod{
341+
Type: consts.UpsertEntity,
342+
BlueprintIdentifier: data.UpsertEntityMethod.BlueprintIdentifier.ValueStringPointer(),
343+
Mapping: &mapping,
344+
}
345+
346+
return upsertEntityInvocation, nil
347+
}
348+
260349
return nil, nil
261350
}

0 commit comments

Comments
 (0)