Skip to content

Commit fa7eec1

Browse files
authored
Merge pull request #268 from port-labs/add_pathFilter_tf_support
Add path filter tf support
2 parents 7d5d0cd + 7d369b9 commit fa7eec1

File tree

8 files changed

+505
-12
lines changed

8 files changed

+505
-12
lines changed

docs/resources/port_aggregation_properties.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,7 @@ Optional:
606606

607607
- `description` (String) The description of the aggregation property
608608
- `icon` (String) The icon of the aggregation property
609+
- `path_filter` (Attributes List) Path filter to filter entities based on relation path (see [below for nested schema](#nestedatt--properties--path_filter))
609610
- `query` (String) Query to filter the target entities
610611
- `title` (String) The title of the aggregation property
611612

@@ -645,3 +646,16 @@ Optional:
645646

646647
- `average_of` (String) The time periods to calculate the average of, e.g. hour, day, week, month
647648
- `measure_time_by` (String) The property name on which to calculate the the time periods, e.g. $createdAt, $updated_at or any other date property
649+
650+
651+
652+
<a id="nestedatt--properties--path_filter"></a>
653+
### Nested Schema for `properties.path_filter`
654+
655+
Required:
656+
657+
- `path` (List of String) The path array of relations to filter by
658+
659+
Optional:
660+
661+
- `from_blueprint` (String) The blueprint to start the path from. Should be the target blueprint or undefined to start from the source blueprint

examples/resources/port_aggregation_property/main.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ resource "port_aggregation_properties" "parent_aggregation_properties" {
4242
method = {
4343
count_entities = true
4444
}
45+
path_filter = [
46+
{
47+
from_blueprint = port_blueprint.child_blueprint.identifier
48+
path = ["parent"]
49+
}
50+
]
4551
}
4652
}
4753
}

internal/cli/models.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,18 @@ type (
146146
}
147147

148148
BlueprintAggregationProperty struct {
149-
Title *string `json:"title,omitempty"`
150-
Description *string `json:"description,omitempty"`
151-
Icon *string `json:"icon,omitempty"`
152-
Target string `json:"target,omitempty"`
153-
CalculationSpec map[string]string `json:"calculationSpec,omitempty"`
154-
Query any `json:"query,omitempty"`
149+
Title *string `json:"title,omitempty"`
150+
Description *string `json:"description,omitempty"`
151+
Icon *string `json:"icon,omitempty"`
152+
Target string `json:"target,omitempty"`
153+
CalculationSpec map[string]string `json:"calculationSpec,omitempty"`
154+
Query any `json:"query,omitempty"`
155+
PathFilter []AggregationPropertyPathFilter `json:"pathFilter,omitempty"`
156+
}
157+
158+
AggregationPropertyPathFilter struct {
159+
FromBlueprint string `json:"fromBlueprint,omitempty"`
160+
Path []string `json:"path"`
155161
}
156162

157163
BlueprintMirrorProperty struct {

port/aggregation-properties/model.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ type AggregationPropertiesModel struct {
99
}
1010

1111
type AggregationPropertyModel struct {
12-
Title types.String `tfsdk:"title"`
13-
Icon types.String `tfsdk:"icon"`
14-
Description types.String `tfsdk:"description"`
15-
TargetBlueprintIdentifier types.String `tfsdk:"target_blueprint_identifier"`
16-
Method *AggregationMethodsModel `tfsdk:"method"`
17-
Query types.String `tfsdk:"query"`
12+
Title types.String `tfsdk:"title"`
13+
Icon types.String `tfsdk:"icon"`
14+
Description types.String `tfsdk:"description"`
15+
TargetBlueprintIdentifier types.String `tfsdk:"target_blueprint_identifier"`
16+
Method *AggregationMethodsModel `tfsdk:"method"`
17+
Query types.String `tfsdk:"query"`
18+
PathFilter []AggregationPropertyPathFilterModel `tfsdk:"path_filter"`
1819
}
1920

2021
type AggregationMethodsModel struct {
@@ -39,3 +40,8 @@ type AggregateByPropertyModel struct {
3940
Func types.String `tfsdk:"func"`
4041
Property types.String `tfsdk:"property"`
4142
}
43+
44+
type AggregationPropertyPathFilterModel struct {
45+
FromBlueprint types.String `tfsdk:"from_blueprint"`
46+
Path types.List `tfsdk:"path"`
47+
}

port/aggregation-properties/readStateToPortBody.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aggregation_properties
22

33
import (
4+
"github.com/hashicorp/terraform-plugin-framework/types"
45
"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
56
"github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
67
)
@@ -61,6 +62,26 @@ func aggregationPropertiesToBody(state *AggregationPropertiesModel) (*map[string
6162
newAggregationProperty.Query = *query
6263
}
6364

65+
if len(aggregationProperty.PathFilter) > 0 {
66+
pathFilter := make([]cli.AggregationPropertyPathFilter, len(aggregationProperty.PathFilter))
67+
for i, pf := range aggregationProperty.PathFilter {
68+
pathFilter[i] = cli.AggregationPropertyPathFilter{}
69+
70+
if !pf.FromBlueprint.IsNull() {
71+
pathFilter[i].FromBlueprint = pf.FromBlueprint.ValueString()
72+
}
73+
74+
var pathStrings []string
75+
for _, pathElement := range pf.Path.Elements() {
76+
if pathStr, ok := pathElement.(types.String); ok {
77+
pathStrings = append(pathStrings, pathStr.ValueString())
78+
}
79+
}
80+
pathFilter[i].Path = pathStrings
81+
}
82+
newAggregationProperty.PathFilter = pathFilter
83+
}
84+
6485
aggregationProperties[aggregationPropertyIdentifier] = newAggregationProperty
6586
}
6687

port/aggregation-properties/refreshAggregationPropertyToState.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package aggregation_properties
22

33
import (
4+
"github.com/hashicorp/terraform-plugin-framework/attr"
45
"github.com/hashicorp/terraform-plugin-framework/types"
56
"github.com/port-labs/terraform-provider-port-labs/v2/internal/cli"
67
"github.com/port-labs/terraform-provider-port-labs/v2/internal/utils"
@@ -20,6 +21,7 @@ func (r *AggregationPropertiesResource) refreshAggregationPropertiesState(state
2021
TargetBlueprintIdentifier: types.StringValue(aggregationProperty.Target),
2122
Method: nil,
2223
Query: types.StringPointerValue(nil),
24+
PathFilter: nil,
2325
}
2426

2527
query, err := utils.GoObjectToTerraformString(aggregationProperty.Query, r.portClient.JSONEscapeHTML)
@@ -28,6 +30,27 @@ func (r *AggregationPropertiesResource) refreshAggregationPropertiesState(state
2830
}
2931
state.Properties[aggregationPropertyIdentifier].Query = query
3032

33+
if len(aggregationProperty.PathFilter) > 0 {
34+
pathFilter := make([]AggregationPropertyPathFilterModel, len(aggregationProperty.PathFilter))
35+
for i, pf := range aggregationProperty.PathFilter {
36+
pathFilter[i] = AggregationPropertyPathFilterModel{}
37+
38+
if pf.FromBlueprint != "" {
39+
pathFilter[i].FromBlueprint = types.StringValue(pf.FromBlueprint)
40+
} else {
41+
pathFilter[i].FromBlueprint = types.StringNull()
42+
}
43+
44+
pathElements := make([]attr.Value, len(pf.Path))
45+
for j, pathStr := range pf.Path {
46+
pathElements[j] = types.StringValue(pathStr)
47+
}
48+
pathList, _ := types.ListValue(types.StringType, pathElements)
49+
pathFilter[i].Path = pathList
50+
}
51+
state.Properties[aggregationPropertyIdentifier].PathFilter = pathFilter
52+
}
53+
3154
if aggregationProperty.CalculationSpec != nil {
3255
if calculationBy, ok := aggregationProperty.CalculationSpec["calculationBy"]; ok {
3356
if calculationBy == "entities" {

0 commit comments

Comments
 (0)