Skip to content

Commit 7d5d0cd

Browse files
feat(actions): disabled properties (#265)
* feat(actions): disabled properties * fix docs and small refactor
1 parent 77b4cf8 commit 7d5d0cd

File tree

12 files changed

+404
-95
lines changed

12 files changed

+404
-95
lines changed

docs/resources/port_action.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,8 @@ Optional:
552552
- `default_jq_query` (String) The default jq query of the array property
553553
- `depends_on` (List of String) The properties that this property depends on
554554
- `description` (String) The description of the property
555+
- `disabled` (Boolean) The disabled state of the array property
556+
- `disabled_jq_query` (String) The disabled state jq query of the array property
555557
- `icon` (String) The icon of the property
556558
- `max_items` (Number) The max items of the array property
557559
- `min_items` (Number) The min items of the array property
@@ -625,6 +627,8 @@ Optional:
625627
- `default_jq_query` (String) The default jq query of the boolean property
626628
- `depends_on` (List of String) The properties that this property depends on
627629
- `description` (String) The description of the property
630+
- `disabled` (Boolean) The disabled state of the boolean property
631+
- `disabled_jq_query` (String) The disabled state jq query of the boolean property
628632
- `icon` (String) The icon of the property
629633
- `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
630634
- `title` (String) The title of the property
@@ -641,6 +645,8 @@ Optional:
641645
- `default_jq_query` (String) The default jq query of the number property
642646
- `depends_on` (List of String) The properties that this property depends on
643647
- `description` (String) The description of the property
648+
- `disabled` (Boolean) The disabled state of the number property
649+
- `disabled_jq_query` (String) The disabled state jq query of the number property
644650
- `enum` (List of Number) The enum of the number property
645651
- `enum_colors` (Map of String) The enum colors of the number property
646652
- `enum_jq_query` (String) The enum jq query of the string property
@@ -662,6 +668,8 @@ Optional:
662668
- `default_jq_query` (String) The default jq query of the object property
663669
- `depends_on` (List of String) The properties that this property depends on
664670
- `description` (String) The description of the property
671+
- `disabled` (Boolean) The disabled state of the object property
672+
- `disabled_jq_query` (String) The disabled state jq query of the object property
665673
- `encryption` (String) The algorithm to encrypt the property with. Accepted value: `aes256-gcm`
666674
- `icon` (String) The icon of the property
667675
- `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
@@ -681,6 +689,8 @@ Optional:
681689
- `default_jq_query` (String) The default jq query of the string property
682690
- `depends_on` (List of String) The properties that this property depends on
683691
- `description` (String) The description of the property
692+
- `disabled` (Boolean) The disabled state of the string property
693+
- `disabled_jq_query` (String) The disabled state jq query of the string property
684694
- `encryption` (String) The algorithm to encrypt the property with. Accepted value: `aes256-gcm`
685695
- `enum` (List of String) The enum of the string property
686696
- `enum_colors` (Map of String) The enum colors of the string property

examples/resources/port_action/main.tf

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,16 @@ resource "port_action" "restart_microservice" {
9595
format = "url"
9696
default = "https://example.com"
9797
pattern = "^https://.*"
98+
disabled = true
9899
}
99100
service = {
100-
title = "Service"
101-
description = "The service to restart"
102-
format = "entity"
103-
blueprint = "Service"
104-
sort = {
101+
title = "Service"
102+
description = "The service to restart"
103+
format = "entity"
104+
blueprint = "Service"
105+
disabled_jq_query = "1 == 1"
106+
107+
sort = {
105108
property = "$updatedAt"
106109
order = "DESC"
107110
}

internal/cli/models.go

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,28 +48,28 @@ type (
4848
}
4949

5050
BlueprintProperty struct {
51-
Type string `json:"type,omitempty"`
52-
Title *string `json:"title,omitempty"`
53-
Identifier string `json:"identifier,omitempty"`
54-
Items map[string]any `json:"items,omitempty"`
55-
Default any `json:"default,omitempty"`
56-
Icon *string `json:"icon,omitempty"`
57-
Format *string `json:"format,omitempty"`
58-
MaxLength *int `json:"maxLength,omitempty"`
59-
MinLength *int `json:"minLength,omitempty"`
60-
MaxItems *int `json:"maxItems,omitempty"`
61-
MinItems *int `json:"minItems,omitempty"`
62-
Maximum *float64 `json:"maximum,omitempty"`
63-
Minimum *float64 `json:"minimum,omitempty"`
64-
Description *string `json:"description,omitempty"`
65-
Blueprint *string `json:"blueprint,omitempty"`
66-
Pattern *string `json:"pattern,omitempty"`
67-
Enum []any `json:"enum,omitempty"`
68-
Spec *string `json:"spec,omitempty"`
69-
SpecAuthentication *SpecAuthentication `json:"specAuthentication,omitempty"`
70-
EnumColors map[string]string `json:"enumColors,omitempty"`
51+
Type string `json:"type,omitempty"`
52+
Title *string `json:"title,omitempty"`
53+
Identifier string `json:"identifier,omitempty"`
54+
Items map[string]any `json:"items,omitempty"`
55+
Default any `json:"default,omitempty"`
56+
Icon *string `json:"icon,omitempty"`
57+
Format *string `json:"format,omitempty"`
58+
MaxLength *int `json:"maxLength,omitempty"`
59+
MinLength *int `json:"minLength,omitempty"`
60+
MaxItems *int `json:"maxItems,omitempty"`
61+
MinItems *int `json:"minItems,omitempty"`
62+
Maximum *float64 `json:"maximum,omitempty"`
63+
Minimum *float64 `json:"minimum,omitempty"`
64+
Description *string `json:"description,omitempty"`
65+
Blueprint *string `json:"blueprint,omitempty"`
66+
Pattern *string `json:"pattern,omitempty"`
67+
Enum []any `json:"enum,omitempty"`
68+
Spec *string `json:"spec,omitempty"`
69+
SpecAuthentication *SpecAuthentication `json:"specAuthentication,omitempty"`
70+
EnumColors map[string]string `json:"enumColors,omitempty"`
7171
// UnknownFields captures any dynamic fields not explicitly defined above
72-
UnknownFields map[string]any `json:"-"`
72+
UnknownFields map[string]any `json:"-"`
7373
}
7474

7575
EntitiesSortModel struct {
@@ -102,6 +102,7 @@ type (
102102
Dataset *Dataset `json:"dataset,omitempty"`
103103
Encryption *string `json:"encryption,omitempty"`
104104
Visible any `json:"visible,omitempty"`
105+
Disabled any `json:"disabled,omitempty"`
105106
Sort *EntitiesSortModel `json:"sort,omitempty"`
106107
}
107108

@@ -502,87 +503,87 @@ type (
502503
func getKnownFields(bp *BlueprintProperty) map[string]bool {
503504
knownFields := make(map[string]bool)
504505
t := reflect.TypeOf(*bp)
505-
506+
506507
for i := 0; i < t.NumField(); i++ {
507508
field := t.Field(i)
508-
509+
509510
// Get the JSON tag
510511
jsonTag := field.Tag.Get("json")
511512
if jsonTag == "" || jsonTag == "-" {
512513
continue // Skip fields without JSON tags or with "-"
513514
}
514-
515+
515516
// Handle "fieldname,omitempty" format
516517
fieldName, _, _ := strings.Cut(jsonTag, ",")
517518
if fieldName != "" {
518519
knownFields[fieldName] = true
519520
}
520521
}
521-
522+
522523
return knownFields
523524
}
524525

525526
// Custom UnmarshalJSON for BlueprintProperty to capture dynamic fields
526527
func (bp *BlueprintProperty) UnmarshalJSON(data []byte) error {
527528
// Define an alias to avoid infinite recursion
528529
type Alias BlueprintProperty
529-
530+
530531
// First, unmarshal into the alias to populate known fields
531532
aux := &struct {
532533
*Alias
533534
}{
534535
Alias: (*Alias)(bp),
535536
}
536-
537+
537538
if err := json.Unmarshal(data, aux); err != nil {
538539
return err
539540
}
540-
541+
541542
// Now unmarshal into a map to capture all fields
542543
var all map[string]any
543544
if err := json.Unmarshal(data, &all); err != nil {
544545
return err
545546
}
546-
547+
547548
// Initialize UnknownFields map
548549
bp.UnknownFields = make(map[string]any)
549-
550+
550551
// Use reflection to get known fields instead of hardcoding
551552
knownFields := getKnownFields(bp)
552-
553+
553554
// Add any unknown fields to UnknownFields
554555
for key, value := range all {
555556
if !knownFields[key] {
556557
bp.UnknownFields[key] = value
557558
}
558559
}
559-
560+
560561
return nil
561562
}
562563

563564
// Custom MarshalJSON for BlueprintProperty to include dynamic fields
564565
func (bp BlueprintProperty) MarshalJSON() ([]byte, error) {
565566
// Define an alias to avoid infinite recursion
566567
type Alias BlueprintProperty
567-
568+
568569
// Marshal the known fields first
569570
aux := Alias(bp)
570571
aux.UnknownFields = nil // Don't marshal this field directly
571-
572+
572573
data, err := json.Marshal(aux)
573574
if err != nil {
574575
return nil, err
575576
}
576-
577+
577578
var result map[string]any
578579
if err := json.Unmarshal(data, &result); err != nil {
579580
return nil, err
580581
}
581-
582+
582583
for key, value := range bp.UnknownFields {
583584
result[key] = value
584585
}
585-
586+
586587
return json.Marshal(result)
587588
}
588589

port/action/array.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ func arrayPropResourceToBody(ctx context.Context, d *SelfServiceTriggerModel, pr
190190
property.Visible = VisibleJqQueryMap
191191
}
192192

193+
if !prop.Disabled.IsNull() {
194+
val := prop.Disabled.ValueBool()
195+
property.Disabled = &val
196+
}
197+
198+
if !prop.DisabledJqQuery.IsNull() {
199+
DisabledJqQuery := map[string]string{
200+
"jqQuery": prop.DisabledJqQuery.ValueString(),
201+
}
202+
property.Disabled = DisabledJqQuery
203+
}
204+
193205
if prop.Sort != nil {
194206
property.Sort = &cli.EntitiesSortModel{
195207
Property: prop.Sort.Property.ValueString(),

port/action/boolean.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ func booleanPropResourceToBody(ctx context.Context, d *SelfServiceTriggerModel,
4747
return err
4848
}
4949
property.DependsOn = utils.InterfaceToStringArray(dependsOn)
50-
5150
}
5251

5352
if !prop.Visible.IsNull() {
@@ -61,6 +60,18 @@ func booleanPropResourceToBody(ctx context.Context, d *SelfServiceTriggerModel,
6160
property.Visible = VisibleJqQueryMap
6261
}
6362

63+
if !prop.Disabled.IsNull() {
64+
val := prop.Disabled.ValueBool()
65+
property.Disabled = &val
66+
}
67+
68+
if !prop.DisabledJqQuery.IsNull() {
69+
DisabledJqQuery := map[string]string{
70+
"jqQuery": prop.DisabledJqQuery.ValueString(),
71+
}
72+
property.Disabled = DisabledJqQuery
73+
}
74+
6475
props[propIdentifier] = property
6576
}
6677
if prop.Required.ValueBool() {

0 commit comments

Comments
 (0)