Skip to content

Commit 77084cc

Browse files
authored
feat: added condition for self_service trigger (#154)
* feat: added condition for self_service trigger * chore: field description * chore: gen-docs
1 parent 77c940a commit 77084cc

File tree

8 files changed

+253
-31
lines changed

8 files changed

+253
-31
lines changed

docs/resources/port_action.md

Lines changed: 90 additions & 14 deletions
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 "port_action" "create_microservice" {
9+
```hcl
10+
resource "portaction" "createmicroservice" {
1111
title = "Create Microservice"
1212
identifier = "create-microservice"
1313
icon = "Terraform"
14-
self_service_trigger = {
14+
selfservicetrigger = {
1515
operation = "CREATE"
16-
blueprint_identifier = port_blueprint.microservice.identifier
17-
user_properties = {
18-
string_props = {
16+
blueprintidentifier = portblueprint.microservice.identifier
17+
userproperties = {
18+
stringprops = {
1919
myStringIdentifier = {
2020
title = "My String Identifier"
2121
required = true
2222
format = "entity"
23-
blueprint = port_blueprint.parent.identifier
23+
blueprint = portblueprint.parent.identifier
2424
dataset = {
2525
combinator = "and"
2626
rules = [{
2727
property = "$title"
2828
operator = "contains"
2929
value = {
30-
jq_query = "\"specificValue\""
30+
jqquery = "\"specificValue\""
3131
}
3232
}]
3333
}
3434
}
3535
}
36-
number_props = {
36+
numberprops = {
3737
myNumberIdentifier = {
3838
title = "My Number Identifier"
3939
required = true
4040
maximum = 100
4141
minimum = 0
4242
}
4343
}
44-
boolean_props = {
44+
booleanprops = {
4545
myBooleanIdentifier = {
4646
title = "My Boolean Identifier"
4747
required = true
4848
}
4949
}
50-
object_props = {
50+
objectprops = {
5151
myObjectIdentifier = {
5252
title = "My Object Identifier"
5353
required = true
5454
}
5555
}
56-
array_props = {
56+
arrayprops = {
5757
myArrayIdentifier = {
5858
title = "My Array Identifier"
5959
required = true
60-
string_items = {
60+
stringitems = {
6161
format = "entity"
62-
blueprint = port_blueprint.parent.identifier
62+
blueprint = portblueprint.parent.identifier
6363
dataset = jsonencode({
6464
combinator = "and"
6565
rules = [{
@@ -79,6 +79,42 @@ description: |-
7979
})
8080
}
8181
}
82+
```
83+
Example Usage With Condition
84+
```hcl
85+
resource "portaction" "createmicroservice" {
86+
title = "Create Microservice"
87+
identifier = "create-microservice"
88+
icon = "Terraform"
89+
selfservicetrigger = {
90+
operation = "CREATE"
91+
blueprintidentifier = portblueprint.microservice.identifier
92+
condition = jsonencode({
93+
type = "SEARCH"
94+
combinator = "and"
95+
rules = [
96+
{
97+
property = "$title"
98+
operator = "!="
99+
value = "Test"
100+
}
101+
]
102+
})
103+
userproperties = {
104+
stringprops = {
105+
myStringIdentifier = {
106+
title = "My String Identifier"
107+
required = true
108+
}
109+
}
110+
}
111+
}
112+
kafka_method = {
113+
payload = jsonencode({
114+
runId: "{{.run.id}}"
115+
})
116+
}
117+
```
82118
---
83119

84120
# port_action (Resource)
@@ -162,6 +198,45 @@ resource "port_action" "create_microservice" {
162198
})
163199
}
164200
}
201+
202+
```
203+
204+
## Example Usage With Condition
205+
206+
```hcl
207+
resource "port_action" "create_microservice" {
208+
title = "Create Microservice"
209+
identifier = "create-microservice"
210+
icon = "Terraform"
211+
self_service_trigger = {
212+
operation = "CREATE"
213+
blueprint_identifier = port_blueprint.microservice.identifier
214+
condition = jsonencode({
215+
type = "SEARCH"
216+
combinator = "and"
217+
rules = [
218+
{
219+
property = "$title"
220+
operator = "!="
221+
value = "Test"
222+
}
223+
]
224+
})
225+
user_properties = {
226+
string_props = {
227+
myStringIdentifier = {
228+
title = "My String Identifier"
229+
required = true
230+
}
231+
}
232+
}
233+
}
234+
kafka_method = {
235+
payload = jsonencode({
236+
runId: "{{.run.id}}"
237+
})
238+
}
239+
165240
```
166241

167242

@@ -273,6 +348,7 @@ Required:
273348
Optional:
274349

275350
- `blueprint_identifier` (String) The ID of the blueprint
351+
- `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.
276352
- `order_properties` (List of String) Order properties
277353
- `required_jq_query` (String) The required jq query of the property
278354
- `user_properties` (Attributes) User properties (see [below for nested schema](#nestedatt--self_service_trigger--user_properties))

internal/cli/models.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,10 @@ type (
191191
}
192192

193193
TriggerCondition struct {
194-
Type string `json:"type"`
195-
Expressions []string `json:"expressions"`
194+
Expressions []string `json:"expressions,omitempty"`
196195
Combinator *string `json:"combinator,omitempty"`
196+
Rules []any `json:"rules,omitempty"`
197+
Type string `json:"type"`
197198
}
198199

199200
Trigger struct {

internal/utils/utils.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,15 @@ func GoObjectToTerraformString(v interface{}) (types.String, error) {
9898
return types.StringValue(value), nil
9999
}
100100

101-
func TerraformStringToGoObject(s types.String) (interface{}, error) {
101+
func TerraformStringToGoType[T any](s types.String) (T, error) {
102+
var obj T
103+
102104
if s.IsNull() {
103-
return nil, nil
105+
return obj, nil
104106
}
105107

106-
var obj interface{}
107108
if err := json.Unmarshal([]byte(s.ValueString()), &obj); err != nil {
108-
return nil, err
109+
return obj, err
109110
}
110111

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

137138
func TFStringListToStringArray(list []types.String) []string {
138139
res := make([]string, len(list))
139-
for i, item := range list{
140+
for i, item := range list {
140141
res[i] = item.ValueString()
141142
}
142143

port/action/actionStateToPortBody.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package action
22

33
import (
44
"context"
5+
56
"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
67
"github.com/port-labs/terraform-provider-port-labs/v2/internal/consts"
78
"github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
@@ -98,6 +99,14 @@ func triggerToBody(ctx context.Context, data *ActionModel) (*cli.Trigger, error)
9899
selfServiceTrigger.UserInputs.Order = orderString
99100
}
100101

102+
if !data.SelfServiceTrigger.Condition.IsNull() {
103+
condition, err := utils.TerraformStringToGoType[cli.TriggerCondition](data.SelfServiceTrigger.Condition)
104+
if err != nil {
105+
return nil, err
106+
}
107+
selfServiceTrigger.Condition = &condition
108+
}
109+
101110
return selfServiceTrigger, nil
102111
}
103112

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

147156
func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.InvocationMethod, error) {
148157
if data.KafkaMethod != nil {
149-
payload, err := utils.TerraformStringToGoObject(data.KafkaMethod.Payload)
158+
payload, err := utils.TerraformStringToGoType[interface{}](data.KafkaMethod.Payload)
150159
if err != nil {
151160
return nil, err
152161
}
@@ -155,11 +164,11 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca
155164
}
156165

157166
if data.WebhookMethod != nil {
158-
agent, err := utils.TerraformStringToGoObject(data.WebhookMethod.Agent)
167+
agent, err := utils.TerraformStringToGoType[interface{}](data.WebhookMethod.Agent)
159168
if err != nil {
160169
return nil, err
161170
}
162-
synchronized, err := utils.TerraformStringToGoObject(data.WebhookMethod.Synchronized)
171+
synchronized, err := utils.TerraformStringToGoType[interface{}](data.WebhookMethod.Synchronized)
163172
if err != nil {
164173
return nil, err
165174
}
@@ -173,7 +182,7 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca
173182
}
174183
headers[key] = keyValue
175184
}
176-
body, err := utils.TerraformStringToGoObject(data.WebhookMethod.Body)
185+
body, err := utils.TerraformStringToGoType[interface{}](data.WebhookMethod.Body)
177186
if err != nil {
178187
return nil, err
179188
}
@@ -192,11 +201,11 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca
192201
}
193202

194203
if data.GithubMethod != nil {
195-
reportWorkflowStatus, err := utils.TerraformStringToGoObject(data.GithubMethod.ReportWorkflowStatus)
204+
reportWorkflowStatus, err := utils.TerraformStringToGoType[interface{}](data.GithubMethod.ReportWorkflowStatus)
196205
if err != nil {
197206
return nil, err
198207
}
199-
wi, err := utils.TerraformStringToGoObject(data.GithubMethod.WorkflowInputs)
208+
wi, err := utils.TerraformStringToGoType[interface{}](data.GithubMethod.WorkflowInputs)
200209
if err != nil {
201210
return nil, err
202211
}
@@ -215,7 +224,7 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca
215224
}
216225

217226
if data.GitlabMethod != nil {
218-
pv, err := utils.TerraformStringToGoObject(data.GitlabMethod.PipelineVariables)
227+
pv, err := utils.TerraformStringToGoType[interface{}](data.GitlabMethod.PipelineVariables)
219228
if err != nil {
220229
return nil, err
221230
}
@@ -233,7 +242,7 @@ func invocationMethodToBody(ctx context.Context, data *ActionModel) (*cli.Invoca
233242
}
234243

235244
if data.AzureMethod != nil {
236-
payload, err := utils.TerraformStringToGoObject(data.AzureMethod.Payload)
245+
payload, err := utils.TerraformStringToGoType[interface{}](data.AzureMethod.Payload)
237246
if err != nil {
238247
return nil, err
239248
}

port/action/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ type SelfServiceTriggerModel struct {
275275
UserProperties *UserPropertiesModel `tfsdk:"user_properties"`
276276
RequiredJqQuery types.String `tfsdk:"required_jq_query"`
277277
OrderProperties types.List `tfsdk:"order_properties"`
278+
Condition types.String `tfsdk:"condition"`
278279
}
279280

280281
type EntityCreatedEventModel struct {

port/action/refreshActionState.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package action
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67
"reflect"
78

@@ -297,6 +298,14 @@ func writeTriggerToResource(ctx context.Context, a *cli.Action, state *ActionMod
297298
}
298299
}
299300

301+
if a.Trigger.Condition != nil {
302+
triggerCondition, err := json.Marshal(a.Trigger.Condition)
303+
if err != nil {
304+
return err
305+
}
306+
state.SelfServiceTrigger.Condition = types.StringValue(string(triggerCondition))
307+
}
308+
300309
return nil
301310
}
302311

0 commit comments

Comments
 (0)