Skip to content

Commit c079921

Browse files
authored
Add JQ required to Action Resource (#96)
1 parent 62f6a33 commit c079921

File tree

7 files changed

+296
-38
lines changed

7 files changed

+296
-38
lines changed

docs/resources/port_action.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Action resource
3434
- `kafka_method` (Object) The invocation method of the action (see [below for nested schema](#nestedatt--kafka_method))
3535
- `order_properties` (List of String) Order properties
3636
- `required_approval` (Boolean) Require approval before invoking the action
37+
- `required_jq_query` (String) The required jq query of the property
3738
- `user_properties` (Attributes) User properties (see [below for nested schema](#nestedatt--user_properties))
3839
- `webhook_method` (Attributes) The invocation method of the action (see [below for nested schema](#nestedatt--webhook_method))
3940

@@ -130,7 +131,7 @@ Optional:
130131
- `min_items` (Number) The min items of the array property
131132
- `number_items` (Attributes) The items of the array property (see [below for nested schema](#nestedatt--user_properties--array_props--number_items))
132133
- `object_items` (Attributes) The items of the array property (see [below for nested schema](#nestedatt--user_properties--array_props--object_items))
133-
- `required` (Boolean) Whether the property is required
134+
- `required` (Boolean) Whether the property is required, by default not required, this property can't be set at the same time if `required_jq_query` is set, and only supports true as value
134135
- `string_items` (Attributes) The items of the array property (see [below for nested schema](#nestedatt--user_properties--array_props--string_items))
135136
- `title` (String) The title of the property
136137
- `visible` (Boolean) The visibility of the array property
@@ -217,7 +218,7 @@ Optional:
217218
- `depends_on` (List of String) The properties that this property depends on
218219
- `description` (String) The description of the property
219220
- `icon` (String) The icon of the property
220-
- `required` (Boolean) Whether the property is required
221+
- `required` (Boolean) Whether the property is required, by default not required, this property can't be set at the same time if `required_jq_query` is set, and only supports true as value
221222
- `title` (String) The title of the property
222223
- `visible` (Boolean) The visibility of the boolean property
223224
- `visible_jq_query` (String) The visibility condition jq query of the boolean property
@@ -269,7 +270,7 @@ Optional:
269270
- `icon` (String) The icon of the property
270271
- `maximum` (Number) The min of the number property
271272
- `minimum` (Number) The max of the number property
272-
- `required` (Boolean) Whether the property is required
273+
- `required` (Boolean) Whether the property is required, by default not required, this property can't be set at the same time if `required_jq_query` is set, and only supports true as value
273274
- `title` (String) The title of the property
274275
- `visible` (Boolean) The visibility of the number property
275276
- `visible_jq_query` (String) The visibility condition jq query of the number property
@@ -318,7 +319,7 @@ Optional:
318319
- `description` (String) The description of the property
319320
- `encryption` (String) The algorithm to encrypt the property with
320321
- `icon` (String) The icon of the property
321-
- `required` (Boolean) Whether the property is required
322+
- `required` (Boolean) Whether the property is required, by default not required, this property can't be set at the same time if `required_jq_query` is set, and only supports true as value
322323
- `title` (String) The title of the property
323324
- `visible` (Boolean) The visibility of the object property
324325
- `visible_jq_query` (String) The visibility condition jq query of the object property
@@ -374,7 +375,7 @@ Optional:
374375
- `max_length` (Number) The max length of the string property
375376
- `min_length` (Number) The min length of the string property
376377
- `pattern` (String) The pattern of the string property
377-
- `required` (Boolean) Whether the property is required
378+
- `required` (Boolean) Whether the property is required, by default not required, this property can't be set at the same time if `required_jq_query` is set, and only supports true as value
378379
- `title` (String) The title of the property
379380
- `visible` (Boolean) The visibility of the string property
380381
- `visible_jq_query` (String) The visibility condition jq query of the string property

internal/cli/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ type (
161161

162162
ActionUserInputs = struct {
163163
Properties map[string]ActionProperty `json:"properties"`
164-
Required []string `json:"required,omitempty"`
164+
Required any `json:"required,omitempty"`
165165
Order []string `json:"order,omitempty"`
166166
}
167167

port/action/actionStateToPortBody.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,16 @@ func actionPropertiesToBody(ctx context.Context, action *cli.Action, data *Actio
124124
}
125125

126126
action.UserInputs.Properties = props
127-
action.UserInputs.Required = required
127+
128+
// if requiredJqQuery is set, required shouldn't be set and vice versa
129+
if !data.RequiredJqQuery.IsNull() {
130+
RequiredJqQueryMap := map[string]string{
131+
"jqQuery": data.RequiredJqQuery.ValueString(),
132+
}
133+
action.UserInputs.Required = RequiredJqQueryMap
134+
} else {
135+
action.UserInputs.Required = required
136+
}
128137

129138
return nil
130139
}

port/action/model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,5 @@ type ActionModel struct {
189189
ApprovalWebhookNotification *ApprovalWebhookNotificationModel `tfsdk:"approval_webhook_notification"`
190190
ApprovalEmailNotification types.Object `tfsdk:"approval_email_notification"`
191191
OrderProperties types.List `tfsdk:"order_properties"`
192+
RequiredJqQuery types.String `tfsdk:"required_jq_query"`
192193
}

port/action/refreshActionState.go

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ package action
33
import (
44
"context"
55
"fmt"
6+
"github.com/samber/lo"
67
"reflect"
78

89
"github.com/hashicorp/terraform-plugin-framework/types"
910
"github.com/port-labs/terraform-provider-port-labs/internal/cli"
1011
"github.com/port-labs/terraform-provider-port-labs/internal/consts"
1112
"github.com/port-labs/terraform-provider-port-labs/internal/flex"
1213
"github.com/port-labs/terraform-provider-port-labs/internal/utils"
13-
"github.com/samber/lo"
1414
)
1515

1616
func writeInvocationMethodToResource(a *cli.Action, state *ActionModel) {
@@ -103,9 +103,37 @@ func writeVisibleToResource(v cli.ActionProperty) (types.Bool, types.String) {
103103
return types.BoolNull(), types.StringNull()
104104
}
105105

106+
func writeRequiredToResource(v cli.ActionUserInputs) (types.String, []string) {
107+
// If required is nil, return an empty string and nil
108+
if v.Required == nil {
109+
return types.StringNull(), nil
110+
}
111+
112+
required := reflect.ValueOf(v.Required)
113+
switch required.Kind() {
114+
// if required is a slice of strings that means that the user has specified which properties are required
115+
case reflect.Slice:
116+
slice := required.Interface().([]interface{})
117+
attrs := make([]string, 0, required.Len())
118+
for _, value := range slice {
119+
attrs = append(attrs, value.(string))
120+
}
121+
return types.StringNull(), attrs
122+
// if required is a map, that means that the user has specified a jq query to determine which properties are required
123+
case reflect.Map:
124+
jq := required.Interface().(map[string]any)
125+
jqQueryValue := jq["jqQuery"].(string)
126+
return types.StringValue(jqQueryValue), nil
127+
}
128+
129+
// if required is not a slice or a map, return an empty string and nil
130+
return types.StringNull(), nil
131+
}
132+
106133
func writeInputsToResource(ctx context.Context, a *cli.Action, state *ActionModel) error {
107134
if len(a.UserInputs.Properties) > 0 {
108135
properties := &UserPropertiesModel{}
136+
requiredJq, required := writeRequiredToResource(a.UserInputs)
109137
for k, v := range a.UserInputs.Properties {
110138
switch v.Type {
111139
case "string":
@@ -114,10 +142,8 @@ func writeInputsToResource(ctx context.Context, a *cli.Action, state *ActionMode
114142
}
115143
stringProp := addStringPropertiesToResource(ctx, &v)
116144

117-
if lo.Contains(a.UserInputs.Required, k) {
145+
if requiredJq.IsNull() && lo.Contains(required, k) {
118146
stringProp.Required = types.BoolValue(true)
119-
} else {
120-
stringProp.Required = types.BoolValue(false)
121147
}
122148

123149
err := setCommonProperties(ctx, v, stringProp)
@@ -134,10 +160,8 @@ func writeInputsToResource(ctx context.Context, a *cli.Action, state *ActionMode
134160

135161
numberProp := addNumberPropertiesToResource(ctx, &v)
136162

137-
if lo.Contains(a.UserInputs.Required, k) {
163+
if requiredJq.IsNull() && lo.Contains(required, k) {
138164
numberProp.Required = types.BoolValue(true)
139-
} else {
140-
numberProp.Required = types.BoolValue(false)
141165
}
142166

143167
err := setCommonProperties(ctx, v, numberProp)
@@ -157,10 +181,8 @@ func writeInputsToResource(ctx context.Context, a *cli.Action, state *ActionMode
157181
return err
158182
}
159183

160-
if lo.Contains(a.UserInputs.Required, k) {
184+
if requiredJq.IsNull() && lo.Contains(required, k) {
161185
arrayProp.Required = types.BoolValue(true)
162-
} else {
163-
arrayProp.Required = types.BoolValue(false)
164186
}
165187

166188
err = setCommonProperties(ctx, v, arrayProp)
@@ -182,10 +204,8 @@ func writeInputsToResource(ctx context.Context, a *cli.Action, state *ActionMode
182204
return err
183205
}
184206

185-
if lo.Contains(a.UserInputs.Required, k) {
207+
if requiredJq.IsNull() && lo.Contains(required, k) {
186208
booleanProp.Required = types.BoolValue(true)
187-
} else {
188-
booleanProp.Required = types.BoolValue(false)
189209
}
190210

191211
properties.BooleanProps[k] = *booleanProp
@@ -197,10 +217,8 @@ func writeInputsToResource(ctx context.Context, a *cli.Action, state *ActionMode
197217

198218
objectProp := addObjectPropertiesToResource(&v)
199219

200-
if lo.Contains(a.UserInputs.Required, k) {
220+
if requiredJq.IsNull() && lo.Contains(required, k) {
201221
objectProp.Required = types.BoolValue(true)
202-
} else {
203-
objectProp.Required = types.BoolValue(false)
204222
}
205223

206224
err := setCommonProperties(ctx, v, objectProp)
@@ -246,6 +264,10 @@ func refreshActionState(ctx context.Context, state *ActionModel, a *cli.Action,
246264
}
247265
}
248266

267+
requiredJq, _ := writeRequiredToResource(a.UserInputs)
268+
269+
state.RequiredJqQuery = requiredJq
270+
249271
writeInvocationMethodToResource(a, state)
250272

251273
err := writeInputsToResource(ctx, a, state)

0 commit comments

Comments
 (0)