Skip to content

Commit

Permalink
feat: added condition for self_service trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsinai committed Jun 2, 2024
1 parent 77c940a commit d9c9480
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 16 deletions.
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
78 changes: 78 additions & 0 deletions port/action/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1734,3 +1734,81 @@ func TestAccPortActionNoUserPropertiesConditional(t *testing.T) {
},
})
}

func TestAccPortActionConditionalTrigger(t *testing.T) {
identifier := utils.GenID()
actionIdentifier := utils.GenID()
var testAccActionConfigCreate = testAccCreateBlueprintConfig(identifier) + fmt.Sprintf(`
resource "port_action" "create_microservice" {
title = "TF Provider Test"
identifier = "%s"
icon = "Terraform"
self_service_trigger = {
operation = "DAY-2"
blueprint_identifier = port_blueprint.microservice.identifier
condition = jsonencode({
type = "SEARCH"
combinator = "and"
rules = [
{
property = "$identifier"
operator = "="
value = "Test"
}
]
})
}
kafka_method = {}
}`, actionIdentifier)

var testAccActionConfigUpdate = testAccCreateBlueprintConfig(identifier) + fmt.Sprintf(`
resource "port_action" "create_microservice" {
title = "TF Provider Test"
identifier = "%s"
icon = "Terraform"
self_service_trigger = {
operation = "DAY-2"
blueprint_identifier = port_blueprint.microservice.identifier
condition = jsonencode({
type = "SEARCH"
combinator = "and"
rules = [
{
property = "$title"
operator = "="
value = "Test"
}
]
})
}
kafka_method = {}
}`, 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", "self_service_trigger.blueprint_identifier", identifier),
resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.operation", "DAY-2"),
resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.condition", `{"combinator":"and","rules":[{"operator":"=","property":"$identifier","value":"Test"}],"type":"SEARCH"}`),
),
},
{
Config: acctest.ProviderConfig + testAccActionConfigUpdate,
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", "self_service_trigger.blueprint_identifier", identifier),
resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.operation", "DAY-2"),
resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.condition", `{"combinator":"and","rules":[{"operator":"=","property":"$title","value":"Test"}],"type":"SEARCH"}`),
),
},
},
})
}
19 changes: 18 additions & 1 deletion port/action/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package action
import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectdefault"

"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/boolvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
Expand All @@ -19,7 +22,6 @@ import (
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
"regexp"
)

func MetadataProperties() map[string]schema.Attribute {
Expand Down Expand Up @@ -126,6 +128,10 @@ func ActionSchema() map[string]schema.Attribute {
Optional: true,
ElementType: types.StringType,
},
"condition": schema.StringAttribute{
Description: "The condition of the availability of the action on a specific entity",
Optional: true,
},
},
Validators: []validator.Object{
objectvalidator.ExactlyOneOf(
Expand Down Expand Up @@ -902,6 +908,17 @@ resource "port_action" "create_microservice" {
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 = {
Expand Down

0 comments on commit d9c9480

Please sign in to comment.