Skip to content

Commit

Permalink
feat: added condition for self_service trigger (#154)
Browse files Browse the repository at this point in the history
* feat: added condition for self_service trigger

* chore: field description

* chore: gen-docs
  • Loading branch information
danielsinai authored Jun 3, 2024
1 parent 77c940a commit 77084cc
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 31 deletions.
104 changes: 90 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 "port_action" "create_microservice" {
```hcl
resource "portaction" "createmicroservice" {
title = "Create Microservice"
identifier = "create-microservice"
icon = "Terraform"
self_service_trigger = {
selfservicetrigger = {
operation = "CREATE"
blueprint_identifier = port_blueprint.microservice.identifier
user_properties = {
string_props = {
blueprintidentifier = portblueprint.microservice.identifier
userproperties = {
stringprops = {
myStringIdentifier = {
title = "My String Identifier"
required = true
format = "entity"
blueprint = port_blueprint.parent.identifier
blueprint = portblueprint.parent.identifier
dataset = {
combinator = "and"
rules = [{
property = "$title"
operator = "contains"
value = {
jq_query = "\"specificValue\""
jqquery = "\"specificValue\""
}
}]
}
}
}
number_props = {
numberprops = {
myNumberIdentifier = {
title = "My Number Identifier"
required = true
maximum = 100
minimum = 0
}
}
boolean_props = {
booleanprops = {
myBooleanIdentifier = {
title = "My Boolean Identifier"
required = true
}
}
object_props = {
objectprops = {
myObjectIdentifier = {
title = "My Object Identifier"
required = true
}
}
array_props = {
arrayprops = {
myArrayIdentifier = {
title = "My Array Identifier"
required = true
string_items = {
stringitems = {
format = "entity"
blueprint = port_blueprint.parent.identifier
blueprint = portblueprint.parent.identifier
dataset = jsonencode({
combinator = "and"
rules = [{
Expand All @@ -79,6 +79,42 @@ description: |-
})
}
}
```
Example Usage With Condition
```hcl
resource "portaction" "createmicroservice" {
title = "Create Microservice"
identifier = "create-microservice"
icon = "Terraform"
selfservicetrigger = {
operation = "CREATE"
blueprintidentifier = portblueprint.microservice.identifier
condition = jsonencode({
type = "SEARCH"
combinator = "and"
rules = [
{
property = "$title"
operator = "!="
value = "Test"
}
]
})
userproperties = {
stringprops = {
myStringIdentifier = {
title = "My String Identifier"
required = true
}
}
}
}
kafka_method = {
payload = jsonencode({
runId: "{{.run.id}}"
})
}
```
---

# port_action (Resource)
Expand Down Expand Up @@ -162,6 +198,45 @@ resource "port_action" "create_microservice" {
})
}
}
```

## Example Usage With Condition

```hcl
resource "port_action" "create_microservice" {
title = "Create Microservice"
identifier = "create-microservice"
icon = "Terraform"
self_service_trigger = {
operation = "CREATE"
blueprint_identifier = port_blueprint.microservice.identifier
condition = jsonencode({
type = "SEARCH"
combinator = "and"
rules = [
{
property = "$title"
operator = "!="
value = "Test"
}
]
})
user_properties = {
string_props = {
myStringIdentifier = {
title = "My String Identifier"
required = true
}
}
}
}
kafka_method = {
payload = jsonencode({
runId: "{{.run.id}}"
})
}
```


Expand Down Expand Up @@ -273,6 +348,7 @@ Required:
Optional:

- `blueprint_identifier` (String) The ID of the blueprint
- `condition` (String) The `condition` field allows you to define rules using Port's [search & query syntax](https://docs.getport.io/search-and-query/#rules) to determine which entities the action will be available for.
- `order_properties` (List of String) Order properties
- `required_jq_query` (String) The required jq query of the property
- `user_properties` (Attributes) User properties (see [below for nested schema](#nestedatt--self_service_trigger--user_properties))
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ type (
}

TriggerCondition struct {
Type string `json:"type"`
Expressions []string `json:"expressions"`
Expressions []string `json:"expressions,omitempty"`
Combinator *string `json:"combinator,omitempty"`
Rules []any `json:"rules,omitempty"`
Type string `json:"type"`
}

Trigger struct {
Expand Down
11 changes: 6 additions & 5 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ func GoObjectToTerraformString(v interface{}) (types.String, error) {
return types.StringValue(value), nil
}

func TerraformStringToGoObject(s types.String) (interface{}, error) {
func TerraformStringToGoType[T any](s types.String) (T, error) {
var obj T

if s.IsNull() {
return nil, nil
return obj, nil
}

var obj interface{}
if err := json.Unmarshal([]byte(s.ValueString()), &obj); err != nil {
return nil, err
return obj, err
}

return obj, nil
Expand Down Expand Up @@ -136,7 +137,7 @@ func InterfaceToStringArray(o interface{}) []string {

func TFStringListToStringArray(list []types.String) []string {
res := make([]string, len(list))
for i, item := range list{
for i, item := range list {
res[i] = item.ValueString()
}

Expand Down
25 changes: 17 additions & 8 deletions port/action/actionStateToPortBody.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package action

import (
"context"

"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/utils"
Expand Down Expand Up @@ -98,6 +99,14 @@ func triggerToBody(ctx context.Context, data *ActionModel) (*cli.Trigger, error)
selfServiceTrigger.UserInputs.Order = orderString
}

if !data.SelfServiceTrigger.Condition.IsNull() {
condition, err := utils.TerraformStringToGoType[cli.TriggerCondition](data.SelfServiceTrigger.Condition)
if err != nil {
return nil, err
}
selfServiceTrigger.Condition = &condition
}

return selfServiceTrigger, nil
}

Expand Down Expand Up @@ -146,7 +155,7 @@ func actionPropertiesToBody(ctx context.Context, actionTrigger *cli.Trigger, dat

func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.InvocationMethod, error) {
if data.KafkaMethod != nil {
payload, err := utils.TerraformStringToGoObject(data.KafkaMethod.Payload)
payload, err := utils.TerraformStringToGoType[interface{}](data.KafkaMethod.Payload)
if err != nil {
return nil, err
}
Expand All @@ -155,11 +164,11 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca
}

if data.WebhookMethod != nil {
agent, err := utils.TerraformStringToGoObject(data.WebhookMethod.Agent)
agent, err := utils.TerraformStringToGoType[interface{}](data.WebhookMethod.Agent)
if err != nil {
return nil, err
}
synchronized, err := utils.TerraformStringToGoObject(data.WebhookMethod.Synchronized)
synchronized, err := utils.TerraformStringToGoType[interface{}](data.WebhookMethod.Synchronized)
if err != nil {
return nil, err
}
Expand All @@ -173,7 +182,7 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca
}
headers[key] = keyValue
}
body, err := utils.TerraformStringToGoObject(data.WebhookMethod.Body)
body, err := utils.TerraformStringToGoType[interface{}](data.WebhookMethod.Body)
if err != nil {
return nil, err
}
Expand All @@ -192,11 +201,11 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca
}

if data.GithubMethod != nil {
reportWorkflowStatus, err := utils.TerraformStringToGoObject(data.GithubMethod.ReportWorkflowStatus)
reportWorkflowStatus, err := utils.TerraformStringToGoType[interface{}](data.GithubMethod.ReportWorkflowStatus)
if err != nil {
return nil, err
}
wi, err := utils.TerraformStringToGoObject(data.GithubMethod.WorkflowInputs)
wi, err := utils.TerraformStringToGoType[interface{}](data.GithubMethod.WorkflowInputs)
if err != nil {
return nil, err
}
Expand All @@ -215,7 +224,7 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca
}

if data.GitlabMethod != nil {
pv, err := utils.TerraformStringToGoObject(data.GitlabMethod.PipelineVariables)
pv, err := utils.TerraformStringToGoType[interface{}](data.GitlabMethod.PipelineVariables)
if err != nil {
return nil, err
}
Expand All @@ -233,7 +242,7 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca
}

if data.AzureMethod != nil {
payload, err := utils.TerraformStringToGoObject(data.AzureMethod.Payload)
payload, err := utils.TerraformStringToGoType[interface{}](data.AzureMethod.Payload)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions port/action/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ type SelfServiceTriggerModel struct {
UserProperties *UserPropertiesModel `tfsdk:"user_properties"`
RequiredJqQuery types.String `tfsdk:"required_jq_query"`
OrderProperties types.List `tfsdk:"order_properties"`
Condition types.String `tfsdk:"condition"`
}

type EntityCreatedEventModel struct {
Expand Down
9 changes: 9 additions & 0 deletions port/action/refreshActionState.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package action

import (
"context"
"encoding/json"
"fmt"
"reflect"

Expand Down Expand Up @@ -297,6 +298,14 @@ func writeTriggerToResource(ctx context.Context, a *cli.Action, state *ActionMod
}
}

if a.Trigger.Condition != nil {
triggerCondition, err := json.Marshal(a.Trigger.Condition)
if err != nil {
return err
}
state.SelfServiceTrigger.Condition = types.StringValue(string(triggerCondition))
}

return nil
}

Expand Down
Loading

0 comments on commit 77084cc

Please sign in to comment.