Skip to content

Commit

Permalink
Add support in enumJqQuery in array + add tests + Fix some bugs (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
dvirsegev authored Aug 14, 2023
1 parent 7ddefc3 commit efeb535
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/resources/port_action.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ Optional:
Optional:

- `default` (List of Number) The default of the items
- `enum` (List of Number) The enum of the items
- `enum_jq_query` (String) The enum jq query of the number items


<a id="nestedatt--user_properties--array_props--object_items"></a>
Expand All @@ -196,6 +198,8 @@ Optional:

- `blueprint` (String) The blueprint identifier the property relates to
- `default` (List of String) The default of the items
- `enum` (List of String) The enum of the items
- `enum_jq_query` (String) The enum jq query of the string items
- `format` (String) The format of the items


Expand Down
80 changes: 77 additions & 3 deletions port/action/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package action
import (
"context"
"encoding/json"
"reflect"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
Expand All @@ -29,6 +30,14 @@ func handleArrayItemsToBody(ctx context.Context, property *cli.ActionProperty, p
property.Default = defaultList
}

if !prop.StringItems.Enum.IsNull() {
enumList, err := utils.TerraformListToGoArray(ctx, prop.StringItems.Enum, "string")
if err != nil {
return err
}
items["enum"] = enumList
}

if !prop.StringItems.Format.IsNull() {
items["format"] = prop.StringItems.Format.ValueString()
}
Expand All @@ -37,29 +46,51 @@ func handleArrayItemsToBody(ctx context.Context, property *cli.ActionProperty, p
items["blueprint"] = prop.StringItems.Blueprint.ValueString()
}

if !prop.StringItems.EnumJqQuery.IsNull() {
enumJqQueryMap := map[string]string{
"jqQuery": prop.StringItems.EnumJqQuery.ValueString(),
}
items["enum"] = enumJqQueryMap
}

property.Items = items
}

if prop.NumberItems != nil {
items := map[string]interface{}{}
items["type"] = "number"
if !prop.NumberItems.Default.IsNull() {
defaultList, err := utils.TerraformListToGoArray(ctx, prop.StringItems.Default, "float64")
defaultList, err := utils.TerraformListToGoArray(ctx, prop.NumberItems.Default, "float64")
if err != nil {
return err
}

items["default"] = defaultList
}

if !prop.NumberItems.Enum.IsNull() {
enumList, err := utils.TerraformListToGoArray(ctx, prop.NumberItems.Enum, "float64")
if err != nil {
return err
}
items["enum"] = enumList
}

if !prop.NumberItems.EnumJqQuery.IsNull() {
enumJqQueryMap := map[string]string{
"jqQuery": prop.NumberItems.EnumJqQuery.ValueString(),
}
items["enum"] = enumJqQueryMap
}

property.Items = items
}

if prop.BooleanItems != nil {
items := map[string]interface{}{}
items["type"] = "boolean"
if !prop.BooleanItems.Default.IsNull() {
defaultList, err := utils.TerraformListToGoArray(ctx, prop.StringItems.Default, "bool")
defaultList, err := utils.TerraformListToGoArray(ctx, prop.BooleanItems.Default, "bool")
if err != nil {
return err
}
Expand All @@ -74,7 +105,7 @@ func handleArrayItemsToBody(ctx context.Context, property *cli.ActionProperty, p
items := map[string]interface{}{}
items["type"] = "object"
if !prop.ObjectItems.Default.IsNull() {
defaultList, err := utils.TerraformListToGoArray(ctx, prop.StringItems.Default, "object")
defaultList, err := utils.TerraformListToGoArray(ctx, prop.ObjectItems.Default, "object")
if err != nil {
return err
}
Expand Down Expand Up @@ -191,6 +222,27 @@ 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["enum"]; ok && value != nil {
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Slice:
slice := v.Interface().([]interface{})
attrs := make([]attr.Value, 0, v.Len())
for _, value := range slice {
attrs = append(attrs, basetypes.NewStringValue(value.(string)))
}
arrayProp.StringItems.Enum, _ = types.ListValue(types.StringType, attrs)
case reflect.Map:
v := v.Interface().(map[string]interface{})
jqQueryValue := v["jqQuery"].(string)
arrayProp.StringItems.EnumJqQuery = flex.GoStringToFramework(&jqQueryValue)
arrayProp.StringItems.Enum = types.ListNull(types.StringType)
}
} else {
arrayProp.StringItems.Enum = types.ListNull(types.StringType)
}

case "number":
arrayProp.NumberItems = &NumberItems{}
if v.Default != nil && arrayProp.DefaultJqQuery.IsNull() {
Expand All @@ -200,6 +252,28 @@ func addArrayPropertiesToResource(v *cli.ActionProperty) (*ArrayPropModel, error
attrs = append(attrs, basetypes.NewFloat64Value(value.(float64)))
}
arrayProp.NumberItems.Default, _ = types.ListValue(types.Float64Type, attrs)
} else {
arrayProp.NumberItems.Default = types.ListNull(types.Float64Type)
}

if value, ok := v.Items["enum"]; ok && value != nil {
v := reflect.ValueOf(value)
switch v.Kind() {
case reflect.Slice:
slice := v.Interface().([]interface{})
attrs := make([]attr.Value, 0, v.Len())
for _, value := range slice {
attrs = append(attrs, basetypes.NewFloat64Value(value.(float64)))
}
arrayProp.NumberItems.Enum, _ = types.ListValue(types.Float64Type, attrs)
case reflect.Map:
v := v.Interface().(map[string]interface{})
jqQueryValue := v["jqQuery"].(string)
arrayProp.NumberItems.EnumJqQuery = flex.GoStringToFramework(&jqQueryValue)
arrayProp.NumberItems.Enum = types.ListNull(types.Float64Type)
}
} else {
arrayProp.NumberItems.Enum = types.ListNull(types.Float64Type)
}

case "boolean":
Expand Down
12 changes: 8 additions & 4 deletions port/action/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,17 @@ type ObjectPropModel struct {
}

type StringItems struct {
Blueprint types.String `tfsdk:"blueprint"`
Format types.String `tfsdk:"format"`
Default types.List `tfsdk:"default"`
Blueprint types.String `tfsdk:"blueprint"`
Format types.String `tfsdk:"format"`
Default types.List `tfsdk:"default"`
Enum types.List `tfsdk:"enum"`
EnumJqQuery types.String `tfsdk:"enum_jq_query"`
}

type NumberItems struct {
Default types.List `tfsdk:"default"`
Default types.List `tfsdk:"default"`
Enum types.List `tfsdk:"enum"`
EnumJqQuery types.String `tfsdk:"enum_jq_query"`
}

type BooleanItems struct {
Expand Down
10 changes: 9 additions & 1 deletion port/action/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ func numberPropResourceToBody(ctx context.Context, state *ActionModel, props map
property.Enum = enumList
}

if !prop.EnumJqQuery.IsNull() {
enumJqQueryMap := map[string]string{
"jqQuery": prop.EnumJqQuery.ValueString(),
}
property.Enum = enumJqQueryMap
}

if !prop.DependsOn.IsNull() {
dependsOn, err := utils.TerraformListToGoArray(ctx, prop.DependsOn, "string")
if err != nil {
Expand Down Expand Up @@ -109,10 +116,11 @@ func addNumberPropertiesToResource(ctx context.Context, v *cli.ActionProperty) *
v := v.Interface().(map[string]interface{})
jqQueryValue := v["jqQuery"].(string)
numberProp.EnumJqQuery = flex.GoStringToFramework(&jqQueryValue)
numberProp.Enum = types.ListNull(types.StringType)
numberProp.Enum = types.ListNull(types.Float64Type)
}
} else {
numberProp.Enum = types.ListNull(types.Float64Type)
numberProp.EnumJqQuery = types.StringNull()
}

return numberProp
Expand Down
126 changes: 126 additions & 0 deletions port/action/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,132 @@ func TestAccPortActionJqDefault(t *testing.T) {

}

func TestAccPortActionEnumJqQuery(t *testing.T) {
identifier := utils.GenID()
actionIdentifier := utils.GenID()
var testAccActionConfigCreate = testAccCreateBlueprintConfig(identifier) + fmt.Sprintf(`
resource "port_action" "create_microservice" {
title = "Action 1"
blueprint = port_blueprint.microservice.id
identifier = "%s"
trigger = "DAY-2"
description = "This is a test action"
kafka_method = {}
user_properties = {
string_props = {
myStringIdentifier = {
title = "myStringIdentifier"
enum_jq_query = "[\"test1\", \"test2\"]"
}
}
number_props = {
myNumberIdentifier = {
title = "myNumberIdentifier"
enum_jq_query = "[1, 2]"
}
}
array_props = {
myStringArrayIdentifier = {
title = "myStringArrayIdentifier"
string_items = {
enum_jq_query = "'example' | [ . ]"
}
}
myNumberArrayIdentifier = {
title = "myNumberArrayIdentifier"
number_items = {
enum_jq_query = "[1, 2]"
}
}
}
}
}`, actionIdentifier)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,

Steps: []resource.TestStep{
{
Config: acctest.ProviderConfig + testAccActionConfigCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("port_action.create_microservice", "title", "Action 1"),
resource.TestCheckResourceAttr("port_action.create_microservice", "identifier", actionIdentifier),
resource.TestCheckResourceAttr("port_action.create_microservice", "trigger", "DAY-2"),
resource.TestCheckResourceAttr("port_action.create_microservice", "description", "This is a test action"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.string_props.myStringIdentifier.title", "myStringIdentifier"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.string_props.myStringIdentifier.enum_jq_query", "[\"test1\", \"test2\"]"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.number_props.myNumberIdentifier.title", "myNumberIdentifier"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.number_props.myNumberIdentifier.enum_jq_query", "[1, 2]"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.array_props.myStringArrayIdentifier.title", "myStringArrayIdentifier"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.array_props.myStringArrayIdentifier.string_items.enum_jq_query", "'example' | [ . ]"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.array_props.myNumberArrayIdentifier.title", "myNumberArrayIdentifier"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.array_props.myNumberArrayIdentifier.number_items.enum_jq_query", "[1, 2]"),
),
},
},
})
}

func TestAccPortActionEnum(t *testing.T) {
identifier := utils.GenID()
actionIdentifier := utils.GenID()
var testAccActionConfigCreate = testAccCreateBlueprintConfig(identifier) + fmt.Sprintf(`
resource "port_action" "create_microservice" {
title = "Action 1"
blueprint = port_blueprint.microservice.id
identifier = "%s"
trigger = "DAY-2"
description = "This is a test action"
kafka_method = {}
user_properties = {
string_props = {
myStringIdentifier = {
title = "myStringIdentifier"
enum = ["test1", "test2"]
}
}
number_props = {
myNumberIdentifier = {
title = "myNumberIdentifier"
enum = [1, 2]
}
}
array_props = {
myStringArrayIdentifier = {
title = "myStringArrayIdentifier"
string_items = {
enum = ["example"]
}
}
}
}
}`, actionIdentifier)

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.TestAccPreCheck(t) },
ProtoV6ProviderFactories: acctest.TestAccProtoV6ProviderFactories,

Steps: []resource.TestStep{
{
Config: acctest.ProviderConfig + testAccActionConfigCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("port_action.create_microservice", "title", "Action 1"),
resource.TestCheckResourceAttr("port_action.create_microservice", "identifier", actionIdentifier),
resource.TestCheckResourceAttr("port_action.create_microservice", "trigger", "DAY-2"),
resource.TestCheckResourceAttr("port_action.create_microservice", "description", "This is a test action"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.string_props.myStringIdentifier.title", "myStringIdentifier"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.string_props.myStringIdentifier.enum.0", "test1"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.string_props.myStringIdentifier.enum.1", "test2"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.number_props.myNumberIdentifier.title", "myNumberIdentifier"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.number_props.myNumberIdentifier.enum.0", "1"),
resource.TestCheckResourceAttr("port_action.create_microservice", "user_properties.number_props.myNumberIdentifier.enum.1", "2"),
),
},
},
})
}
func TestAccPortActionOrderProperties(t *testing.T) {
identifier := utils.GenID()
actionIdentifier := utils.GenID()
Expand Down
Loading

0 comments on commit efeb535

Please sign in to comment.