Skip to content

Commit

Permalink
Merge pull request #118 from port-labs/PORT-6963-bug-ia-c-providers-a…
Browse files Browse the repository at this point in the history
…ctions-dataset-not-being-supported

Add support for dataset in arrayProps-stringItems
  • Loading branch information
pazhersh authored Mar 6, 2024
2 parents 8bfdf4d + 2fe6f29 commit 6898d74
Show file tree
Hide file tree
Showing 15 changed files with 759 additions and 325 deletions.
499 changes: 368 additions & 131 deletions docs/resources/port_action.md

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ func GoObjectToTerraformString(v interface{}) (types.String, error) {
return types.StringValue(value), nil
}

func TerraformJsonStringToGoObject(v *string) (*map[string]any, error) {
if v == nil || *v == "" {
return nil, nil
}

vMap := make(map[string]any)
if err := json.Unmarshal([]byte(*v), &vMap); err != nil {
return nil, err
}

return &vMap, nil
}

func InterfaceToStringArray(o interface{}) []string {
items := o.([]interface{})
res := make([]string, len(items))
Expand Down
22 changes: 3 additions & 19 deletions port/action-permissions/actionPermissionToPortBody.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
package action_permissions

import (
"encoding/json"
"github.com/port-labs/terraform-provider-port-labs/internal/cli"
"github.com/port-labs/terraform-provider-port-labs/internal/flex"
"github.com/port-labs/terraform-provider-port-labs/internal/utils"
)

func policyToPortBody(policy *string) (*map[string]any, error) {
// if policy is empty, set it to nil, so it will override the existing policy on server,
// as opposed to merging it, due to only having a PATCH endpoint

if policy == nil || *policy == "" {
return nil, nil
}

policyMap := make(map[string]any)
if err := json.Unmarshal([]byte(*policy), &policyMap); err != nil {
return nil, err
}

return &policyMap, nil
}

func actionPermissionsToPortBody(state *PermissionsModel) (*cli.ActionPermissions, error) {
if state == nil {
return nil, nil
Expand All @@ -41,12 +25,12 @@ func actionPermissionsToPortBody(state *PermissionsModel) (*cli.ActionPermission
},
}

approvePolicyMap, err := policyToPortBody(state.Approve.Policy.ValueStringPointer())
approvePolicyMap, err := utils.TerraformJsonStringToGoObject(state.Approve.Policy.ValueStringPointer())
if err != nil {
return nil, err
}

executePolicyMap, err := policyToPortBody(state.Execute.Policy.ValueStringPointer())
executePolicyMap, err := utils.TerraformJsonStringToGoObject(state.Execute.Policy.ValueStringPointer())
if err != nil {
return nil, err
}
Expand Down
19 changes: 16 additions & 3 deletions port/action/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ func handleArrayItemsToBody(ctx context.Context, property *cli.ActionProperty, p
items["enum"] = enumList
}

if !prop.StringItems.Dataset.IsNull() {
v, err := utils.TerraformJsonStringToGoObject(prop.StringItems.Dataset.ValueStringPointer())
if err != nil {
return err
}

items["dataset"] = v
}

if !prop.StringItems.Format.IsNull() {
items["format"] = prop.StringItems.Format.ValueString()
}
Expand Down Expand Up @@ -165,9 +174,6 @@ func arrayPropResourceToBody(ctx context.Context, d *ActionModel, props map[stri
property.DependsOn = utils.InterfaceToStringArray(dependsOn)

}
if prop.Dataset != nil {
property.Dataset = actionDataSetToPortBody(prop.Dataset)
}

err := handleArrayItemsToBody(ctx, &property, prop, required)
if err != nil {
Expand Down Expand Up @@ -233,6 +239,13 @@ func addArrayPropertiesToResource(v *cli.ActionProperty) (*ArrayPropModel, error
if value, ok := v.Items["blueprint"]; ok && value != nil {
arrayProp.StringItems.Blueprint = types.StringValue(v.Items["blueprint"].(string))
}
if value, ok := v.Items["dataset"]; ok && value != nil {
ds, err := utils.GoObjectToTerraformString(v.Items["dataset"])
if err != nil {
return nil, err
}
arrayProp.StringItems.Dataset = ds
}

if value, ok := v.Items["enum"]; ok && value != nil {
v := reflect.ValueOf(value)
Expand Down
3 changes: 0 additions & 3 deletions port/action/boolean.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ func booleanPropResourceToBody(ctx context.Context, d *ActionModel, props map[st
property.DependsOn = utils.InterfaceToStringArray(dependsOn)

}
if prop.Dataset != nil {
property.Dataset = actionDataSetToPortBody(prop.Dataset)
}

if !prop.Visible.IsNull() {
property.Visible = prop.Visible.ValueBoolPointer()
Expand Down
69 changes: 33 additions & 36 deletions port/action/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,14 @@ type StringPropModel struct {
}

type NumberPropModel struct {
Title types.String `tfsdk:"title"`
Icon types.String `tfsdk:"icon"`
Description types.String `tfsdk:"description"`
Required types.Bool `tfsdk:"required"`
DependsOn types.List `tfsdk:"depends_on"`
Dataset *DatasetModel `tfsdk:"dataset"`
DefaultJqQuery types.String `tfsdk:"default_jq_query"`
Visible types.Bool `tfsdk:"visible"`
VisibleJqQuery types.String `tfsdk:"visible_jq_query"`
Title types.String `tfsdk:"title"`
Icon types.String `tfsdk:"icon"`
Description types.String `tfsdk:"description"`
Required types.Bool `tfsdk:"required"`
DependsOn types.List `tfsdk:"depends_on"`
DefaultJqQuery types.String `tfsdk:"default_jq_query"`
Visible types.Bool `tfsdk:"visible"`
VisibleJqQuery types.String `tfsdk:"visible_jq_query"`

Default types.Float64 `tfsdk:"default"`
Maximum types.Float64 `tfsdk:"maximum"`
Expand All @@ -89,29 +88,27 @@ type NumberPropModel struct {
}

type BooleanPropModel struct {
Title types.String `tfsdk:"title"`
Icon types.String `tfsdk:"icon"`
Description types.String `tfsdk:"description"`
Required types.Bool `tfsdk:"required"`
DependsOn types.List `tfsdk:"depends_on"`
Dataset *DatasetModel `tfsdk:"dataset"`
DefaultJqQuery types.String `tfsdk:"default_jq_query"`
Visible types.Bool `tfsdk:"visible"`
VisibleJqQuery types.String `tfsdk:"visible_jq_query"`
Title types.String `tfsdk:"title"`
Icon types.String `tfsdk:"icon"`
Description types.String `tfsdk:"description"`
Required types.Bool `tfsdk:"required"`
DependsOn types.List `tfsdk:"depends_on"`
DefaultJqQuery types.String `tfsdk:"default_jq_query"`
Visible types.Bool `tfsdk:"visible"`
VisibleJqQuery types.String `tfsdk:"visible_jq_query"`

Default types.Bool `tfsdk:"default"`
}

type ArrayPropModel struct {
Title types.String `tfsdk:"title"`
Icon types.String `tfsdk:"icon"`
Description types.String `tfsdk:"description"`
Required types.Bool `tfsdk:"required"`
DependsOn types.List `tfsdk:"depends_on"`
Dataset *DatasetModel `tfsdk:"dataset"`
DefaultJqQuery types.String `tfsdk:"default_jq_query"`
Visible types.Bool `tfsdk:"visible"`
VisibleJqQuery types.String `tfsdk:"visible_jq_query"`
Title types.String `tfsdk:"title"`
Icon types.String `tfsdk:"icon"`
Description types.String `tfsdk:"description"`
Required types.Bool `tfsdk:"required"`
DependsOn types.List `tfsdk:"depends_on"`
DefaultJqQuery types.String `tfsdk:"default_jq_query"`
Visible types.Bool `tfsdk:"visible"`
VisibleJqQuery types.String `tfsdk:"visible_jq_query"`

MaxItems types.Int64 `tfsdk:"max_items"`
MinItems types.Int64 `tfsdk:"min_items"`
Expand All @@ -122,15 +119,14 @@ type ArrayPropModel struct {
}

type ObjectPropModel struct {
Title types.String `tfsdk:"title"`
Icon types.String `tfsdk:"icon"`
Description types.String `tfsdk:"description"`
Required types.Bool `tfsdk:"required"`
DependsOn types.List `tfsdk:"depends_on"`
Dataset *DatasetModel `tfsdk:"dataset"`
DefaultJqQuery types.String `tfsdk:"default_jq_query"`
Visible types.Bool `tfsdk:"visible"`
VisibleJqQuery types.String `tfsdk:"visible_jq_query"`
Title types.String `tfsdk:"title"`
Icon types.String `tfsdk:"icon"`
Description types.String `tfsdk:"description"`
Required types.Bool `tfsdk:"required"`
DependsOn types.List `tfsdk:"depends_on"`
DefaultJqQuery types.String `tfsdk:"default_jq_query"`
Visible types.Bool `tfsdk:"visible"`
VisibleJqQuery types.String `tfsdk:"visible_jq_query"`

Default types.String `tfsdk:"default"`
Encryption types.String `tfsdk:"encryption"`
Expand All @@ -142,6 +138,7 @@ type StringItems struct {
Default types.List `tfsdk:"default"`
Enum types.List `tfsdk:"enum"`
EnumJqQuery types.String `tfsdk:"enum_jq_query"`
Dataset types.String `tfsdk:"dataset"`
}

type NumberItems struct {
Expand Down
4 changes: 0 additions & 4 deletions port/action/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ func numberPropResourceToBody(ctx context.Context, state *ActionModel, props map

}

if prop.Dataset != nil {
property.Dataset = actionDataSetToPortBody(prop.Dataset)
}

if !prop.Visible.IsNull() {
property.Visible = prop.Visible.ValueBoolPointer()
}
Expand Down
4 changes: 0 additions & 4 deletions port/action/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func objectPropResourceToBody(ctx context.Context, d *ActionModel, props map[str
property.Encryption = &encryption
}

if prop.Dataset != nil {
property.Dataset = actionDataSetToPortBody(prop.Dataset)
}

if !prop.Visible.IsNull() {
property.Visible = prop.Visible.ValueBoolPointer()
}
Expand Down
30 changes: 6 additions & 24 deletions port/action/refreshActionState.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package action
import (
"context"
"fmt"
"github.com/samber/lo"
"reflect"

"github.com/samber/lo"

"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/port-labs/terraform-provider-port-labs/internal/cli"
"github.com/port-labs/terraform-provider-port-labs/internal/consts"
Expand Down Expand Up @@ -57,18 +58,16 @@ func writeInvocationMethodToResource(a *cli.Action, state *ActionModel) {
}
}

func writeDatasetToResource(v cli.ActionProperty) *DatasetModel {
if v.Dataset == nil {
func writeDatasetToResource(ds *cli.Dataset) *DatasetModel {
if ds == nil {
return nil
}

dataset := v.Dataset

datasetModel := &DatasetModel{
Combinator: types.StringValue(dataset.Combinator),
Combinator: types.StringValue(ds.Combinator),
}

for _, v := range dataset.Rules {
for _, v := range ds.Rules {
rule := &Rule{
Blueprint: flex.GoStringToFramework(v.Blueprint),
Property: flex.GoStringToFramework(v.Property),
Expand Down Expand Up @@ -390,23 +389,6 @@ func setCommonProperties(ctx context.Context, v cli.ActionProperty, prop interfa
p.DependsOn = flex.GoArrayStringToTerraformList(ctx, v.DependsOn)
}

case "Dataset":
dataset := writeDatasetToResource(v)
if dataset != nil {
switch p := prop.(type) {
case *StringPropModel:
p.Dataset = dataset
case *NumberPropModel:
p.Dataset = dataset
case *BooleanPropModel:
p.Dataset = dataset
case *ArrayPropModel:
p.Dataset = dataset
case *ObjectPropModel:
p.Dataset = dataset
}
}

case "Visible":
visible, visibleJq := writeVisibleToResource(v)
if !visible.IsNull() {
Expand Down
Loading

0 comments on commit 6898d74

Please sign in to comment.