From dbdcdd1d6eb026c6f9fe2a82a385f0e900a3691b Mon Sep 17 00:00:00 2001 From: Eyal Benari Date: Thu, 5 Sep 2024 13:29:25 +0300 Subject: [PATCH 1/3] added sort to relevant models --- docs/resources/port_action.md | 42 +++++++++++++++++++++++ examples/resources/port_action/main.tf | 10 ++++++ internal/cli/models.go | 6 ++++ port/action/array.go | 14 ++++++++ port/action/model.go | 37 ++++++++++++--------- port/action/schema.go | 46 ++++++++++++++++++++++++++ port/action/string.go | 14 ++++++++ 7 files changed, 154 insertions(+), 15 deletions(-) diff --git a/docs/resources/port_action.md b/docs/resources/port_action.md index b32706d7..f3e6ae2f 100644 --- a/docs/resources/port_action.md +++ b/docs/resources/port_action.md @@ -31,6 +31,10 @@ description: |- } }] } + sort = { + property = "$updatedAt" + order = "DESC" + } } } number_props = { @@ -69,6 +73,10 @@ description: |- }] }) } + sort = { + property = "$updatedAt" + order = "DESC" + } } } } @@ -169,6 +177,10 @@ resource "port_action" "create_microservice" { jq_query = "\"specificValue\"" } }] + } + sort = { + property = "$updatedAt" + order = "DESC" } } } @@ -208,6 +220,10 @@ resource "port_action" "create_microservice" { }] }) } + sort = { + property = "$updatedAt" + order = "DESC" + } } } } @@ -499,6 +515,7 @@ Optional: - `number_items` (Attributes) An array of number items within the property (see [below for nested schema](#nestedatt--self_service_trigger--user_properties--array_props--number_items)) - `object_items` (Attributes) An array of object items within the property (see [below for nested schema](#nestedatt--self_service_trigger--user_properties--array_props--object_items)) - `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 +- `sort` (Attributes) How to sort entities when in the self service action form in the UI (see [below for nested schema](#nestedatt--self_service_trigger--user_properties--array_props--sort)) - `string_items` (Attributes) An array of string items within the property (see [below for nested schema](#nestedatt--self_service_trigger--user_properties--array_props--string_items)) - `title` (String) The title of the property - `visible` (Boolean) The visibility of the array property @@ -530,6 +547,18 @@ Optional: - `default` (List of Map of String) The default values for the object items + +### Nested Schema for `self_service_trigger.user_properties.array_props.visible_jq_query` + +Required: + +- `property` (String) The property to sort the entities by + +Optional: + +- `order` (String) The order to sort the entities in + + ### Nested Schema for `self_service_trigger.user_properties.array_props.visible_jq_query` @@ -617,6 +646,7 @@ Optional: - `min_length` (Number) The min length of the string property - `pattern` (String) The pattern of the string property - `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 +- `sort` (Attributes) How to sort entities when in the self service action form in the UI (see [below for nested schema](#nestedatt--self_service_trigger--user_properties--string_props--sort)) - `title` (String) The title of the property - `visible` (Boolean) The visibility of the string property - `visible_jq_query` (String) The visibility condition jq query of the string property @@ -652,6 +682,18 @@ Optional: + +### Nested Schema for `self_service_trigger.user_properties.string_props.visible_jq_query` + +Required: + +- `property` (String) The property to sort the entities by + +Optional: + +- `order` (String) The order to sort the entities in + + diff --git a/examples/resources/port_action/main.tf b/examples/resources/port_action/main.tf index 4b4145cb..e27d6cb8 100644 --- a/examples/resources/port_action/main.tf +++ b/examples/resources/port_action/main.tf @@ -88,6 +88,16 @@ resource "port_action" "restart_microservice" { default = "https://example.com" pattern = "^https://.*" } + service = { + title = "Service" + description = "The service to restart" + format = "entity" + blueprint = "service" + sort = { + property = "$updatedAt" + order = "DESC" + } + } } } } diff --git a/internal/cli/models.go b/internal/cli/models.go index 633b74a8..04c2cca7 100644 --- a/internal/cli/models.go +++ b/internal/cli/models.go @@ -66,6 +66,11 @@ type ( EnumColors map[string]string `json:"enumColors,omitempty"` } + EntitiesSortModel struct { + Property string `json:"property"` + Order string `json:"order"` + } + ActionProperty struct { Type string `json:"type,omitempty"` Title *string `json:"title,omitempty"` @@ -91,6 +96,7 @@ type ( Dataset *Dataset `json:"dataset,omitempty"` Encryption *string `json:"encryption,omitempty"` Visible any `json:"visible,omitempty"` + Sort *EntitiesSortModel `json:"sort,omitempty"` } SpecAuthentication struct { diff --git a/port/action/array.go b/port/action/array.go index 71d4b558..c38776b6 100644 --- a/port/action/array.go +++ b/port/action/array.go @@ -191,6 +191,13 @@ func arrayPropResourceToBody(ctx context.Context, d *SelfServiceTriggerModel, pr property.Visible = VisibleJqQueryMap } + if prop.Sort != nil { + property.Sort = &cli.EntitiesSortModel{ + Property: prop.Sort.Property.ValueString(), + Order: prop.Sort.Order.ValueString(), + } + } + props[propIdentifier] = property } @@ -215,6 +222,13 @@ func addArrayPropertiesToResource(v *cli.ActionProperty) (*ArrayPropModel, error } } + if v.Sort != nil { + arrayProp.Sort = &EntitiesSortModel{ + Property: types.StringValue(v.Sort.Property), + Order: types.StringValue(v.Sort.Order), + } + } + if v.Items != nil { if v.Items["type"] != "" { switch v.Items["type"] { diff --git a/port/action/model.go b/port/action/model.go index 1657ddea..681619f5 100644 --- a/port/action/model.go +++ b/port/action/model.go @@ -30,15 +30,16 @@ type StringPropModel struct { Visible types.Bool `tfsdk:"visible"` VisibleJqQuery types.String `tfsdk:"visible_jq_query"` - Default types.String `tfsdk:"default"` - Blueprint types.String `tfsdk:"blueprint"` - Format types.String `tfsdk:"format"` - MaxLength types.Int64 `tfsdk:"max_length"` - MinLength types.Int64 `tfsdk:"min_length"` - Pattern types.String `tfsdk:"pattern"` - Enum types.List `tfsdk:"enum"` - EnumJqQuery types.String `tfsdk:"enum_jq_query"` - Encryption types.String `tfsdk:"encryption"` + Default types.String `tfsdk:"default"` + Blueprint types.String `tfsdk:"blueprint"` + Format types.String `tfsdk:"format"` + MaxLength types.Int64 `tfsdk:"max_length"` + MinLength types.Int64 `tfsdk:"min_length"` + Pattern types.String `tfsdk:"pattern"` + Enum types.List `tfsdk:"enum"` + EnumJqQuery types.String `tfsdk:"enum_jq_query"` + Encryption types.String `tfsdk:"encryption"` + Sort *EntitiesSortModel `tfsdk:"sort"` } // StringPropValidationModel is a model used for the validation of StringPropModel resources @@ -206,6 +207,11 @@ type BooleanPropModel struct { Default types.Bool `tfsdk:"default"` } +type EntitiesSortModel struct { + Property types.String `tfsdk:"property"` + Order types.String `tfsdk:"order"` +} + type ArrayPropModel struct { Title types.String `tfsdk:"title"` Icon types.String `tfsdk:"icon"` @@ -216,12 +222,13 @@ type ArrayPropModel struct { Visible types.Bool `tfsdk:"visible"` VisibleJqQuery types.String `tfsdk:"visible_jq_query"` - MaxItems types.Int64 `tfsdk:"max_items"` - MinItems types.Int64 `tfsdk:"min_items"` - StringItems *StringItems `tfsdk:"string_items"` - NumberItems *NumberItems `tfsdk:"number_items"` - BooleanItems *BooleanItems `tfsdk:"boolean_items"` - ObjectItems *ObjectItems `tfsdk:"object_items"` + MaxItems types.Int64 `tfsdk:"max_items"` + MinItems types.Int64 `tfsdk:"min_items"` + StringItems *StringItems `tfsdk:"string_items"` + NumberItems *NumberItems `tfsdk:"number_items"` + BooleanItems *BooleanItems `tfsdk:"boolean_items"` + ObjectItems *ObjectItems `tfsdk:"object_items"` + Sort *EntitiesSortModel `tfsdk:"sort"` } type ObjectPropModel struct { diff --git a/port/action/schema.go b/port/action/schema.go index b32930e6..32b44101 100644 --- a/port/action/schema.go +++ b/port/action/schema.go @@ -556,6 +556,25 @@ func StringPropertySchema() schema.Attribute { }, }, }, + "sort": schema.SingleNestedAttribute{ + MarkdownDescription: "How to sort entities when in the self service action form in the UI", + Optional: true, + Attributes: map[string]schema.Attribute{ + "property": schema.StringAttribute{ + MarkdownDescription: "The property to sort the entities by", + Required: true, + }, + "order": schema.StringAttribute{ + MarkdownDescription: "The order to sort the entities in", + Computed: true, + Optional: true, + Default: stringdefault.StaticString("ASC"), + Validators: []validator.String{ + stringvalidator.OneOf("ASC", "DESC"), + }, + }, + }, + }, } utils.CopyMaps(stringPropertySchema, MetadataProperties()) @@ -831,6 +850,25 @@ func ArrayPropertySchema() schema.Attribute { stringvalidator.ConflictsWith(path.MatchRelative().AtParent().AtName("visible")), }, }, + "sort": schema.SingleNestedAttribute{ + MarkdownDescription: "How to sort entities when in the self service action form in the UI", + Optional: true, + Attributes: map[string]schema.Attribute{ + "property": schema.StringAttribute{ + MarkdownDescription: "The property to sort the entities by", + Required: true, + }, + "order": schema.StringAttribute{ + MarkdownDescription: "The order to sort the entities in", + Computed: true, + Optional: true, + Default: stringdefault.StaticString("ASC"), + Validators: []validator.String{ + stringvalidator.OneOf("ASC", "DESC"), + }, + }, + }, + }, } utils.CopyMaps(arrayPropertySchema, MetadataProperties()) @@ -1072,6 +1110,10 @@ resource "port_action" "create_microservice" { jq_query = "\"specificValue\"" } }] + } + sort = { + property = "$updatedAt" + order = "DESC" } } } @@ -1111,6 +1153,10 @@ resource "port_action" "create_microservice" { }] }) } + sort = { + property = "$updatedAt" + order = "DESC" + } } } } diff --git a/port/action/string.go b/port/action/string.go index 8622cc07..927a8372 100644 --- a/port/action/string.go +++ b/port/action/string.go @@ -114,6 +114,13 @@ func stringPropResourceToBody(ctx context.Context, d *SelfServiceTriggerModel, p property.Visible = VisibleJqQueryMap } + if prop.Sort != nil { + property.Sort = &cli.EntitiesSortModel{ + Property: prop.Sort.Property.ValueString(), + Order: prop.Sort.Order.ValueString(), + } + } + props[propIdentifier] = property if prop.Required.ValueBool() { @@ -157,5 +164,12 @@ func addStringPropertiesToResource(ctx context.Context, v *cli.ActionProperty) * stringProp.EnumJqQuery = types.StringNull() } + if v.Sort != nil { + stringProp.Sort = &EntitiesSortModel{ + Property: types.StringValue(v.Sort.Property), + Order: types.StringValue(v.Sort.Order), + } + } + return stringProp } From a74e1ee651297ad665cb561b728f393e7cc03ea6 Mon Sep 17 00:00:00 2001 From: Eyal Benari Date: Thu, 5 Sep 2024 16:29:35 +0300 Subject: [PATCH 2/3] added test --- port/action/resource_test.go | 70 ++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/port/action/resource_test.go b/port/action/resource_test.go index c896eee1..cf2bb8df 100644 --- a/port/action/resource_test.go +++ b/port/action/resource_test.go @@ -2192,3 +2192,73 @@ func TestRequiredApprovalAll(t *testing.T) { }, }) } + +func TestAccPortActionSort(t *testing.T) { + identifier := utils.GenID() + actionIdentifier := utils.GenID() + var testAccActionConfigCreate = testAccCreateBlueprintConfig(identifier) + fmt.Sprintf(` + resource "port_action" "create_microservice" { + title = "TF Provider Test" + identifier = "%[1]v" + icon = "Terraform" + self_service_trigger = { + operation = "DAY-2" + blueprint_identifier = port_blueprint.microservice.identifier + user_properties = { + "string_props" = { + "myEntityIdentifier" = { + "title" = "My entity Identifier" + "format" = "entity" + "blueprint" = "%[2]v" + "sort" = { + "order" = "DESC" + "property" = "timer" + } + } + } + "array_props" = { + "myEntityIdentifiers" = { + "title" = "My entity Identifiers" + string_items = { + "format" = "entity" + "blueprint" = "%[2]v" + } + "sort" = { + "order" = "ASC" + "property" = "timer" + } + } + } + } + } + kafka_method = {} + }`, actionIdentifier, identifier) + + 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", "TF Provider Test"), + resource.TestCheckResourceAttr("port_action.create_microservice", "identifier", actionIdentifier), + resource.TestCheckResourceAttr("port_action.create_microservice", "icon", "Terraform"), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.blueprint_identifier", identifier), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.operation", "DAY-2"), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.string_props.myEntityIdentifier.title", "My entity Identifier"), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.string_props.myEntityIdentifier.format", "entity"), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.string_props.myEntityIdentifier.blueprint", identifier), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.string_props.myEntityIdentifier.sort.order", "DESC"), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.string_props.myEntityIdentifier.sort.property", "timer"), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.array_props.myEntityIdentifiers.title", "My entity Identifiers"), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.array_props.myEntityIdentifiers.sort.order", "ASC"), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.array_props.myEntityIdentifiers.sort.property", "timer"), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.array_props.myEntityIdentifiers.string_items.format", "entity"), + resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.array_props.myEntityIdentifiers.string_items.blueprint", identifier), + ), + }, + }, + }) +} + From eb32f298c8ed8f2aa8ad732c60926d1260596aa0 Mon Sep 17 00:00:00 2001 From: Eyal Benari Date: Sun, 8 Sep 2024 13:23:26 +0300 Subject: [PATCH 3/3] added test for no sort --- port/action/resource_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/port/action/resource_test.go b/port/action/resource_test.go index cf2bb8df..ec238cce 100644 --- a/port/action/resource_test.go +++ b/port/action/resource_test.go @@ -489,6 +489,7 @@ func TestAccPortActionImport(t *testing.T) { resource.TestCheckResourceAttr("port_action.create_microservice", "webhook_method.url", "https://getport.io"), resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.string_props.myStringIdentifier.title", "My String Identifier"), resource.TestCheckResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.string_props.myStringIdentifier.required", "true"), + resource.TestCheckNoResourceAttr("port_action.create_microservice", "self_service_trigger.user_properties.string_props.myStringIdentifier.sort"), ), }, { @@ -2261,4 +2262,3 @@ func TestAccPortActionSort(t *testing.T) { }, }) } -