Skip to content

Commit

Permalink
Merge pull request #182 from port-labs/add-sort-to-action-user-inputs
Browse files Browse the repository at this point in the history
Add sort to action user inputs
  • Loading branch information
Eyal-Be authored Sep 8, 2024
2 parents c027225 + eb32f29 commit 99381ce
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 15 deletions.
42 changes: 42 additions & 0 deletions docs/resources/port_action.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ description: |-
}
}]
}
sort = {
property = "$updatedAt"
order = "DESC"
}
}
}
number_props = {
Expand Down Expand Up @@ -69,6 +73,10 @@ description: |-
}]
})
}
sort = {
property = "$updatedAt"
order = "DESC"
}
}
}
}
Expand Down Expand Up @@ -169,6 +177,10 @@ resource "port_action" "create_microservice" {
jq_query = "\"specificValue\""
}
}]
}
sort = {
property = "$updatedAt"
order = "DESC"
}
}
}
Expand Down Expand Up @@ -208,6 +220,10 @@ resource "port_action" "create_microservice" {
}]
})
}
sort = {
property = "$updatedAt"
order = "DESC"
}
}
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -530,6 +547,18 @@ Optional:
- `default` (List of Map of String) The default values for the object items


<a id="nestedatt--self_service_trigger--user_properties--array_props--sort"></a>
### 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


<a id="nestedatt--self_service_trigger--user_properties--array_props--string_items"></a>
### Nested Schema for `self_service_trigger.user_properties.array_props.visible_jq_query`

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -652,6 +682,18 @@ Optional:



<a id="nestedatt--self_service_trigger--user_properties--string_props--sort"></a>
### 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





Expand Down
10 changes: 10 additions & 0 deletions examples/resources/port_action/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions internal/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions port/action/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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"] {
Expand Down
37 changes: 22 additions & 15 deletions port/action/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"`
Expand All @@ -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 {
Expand Down
70 changes: 70 additions & 0 deletions port/action/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
},
{
Expand Down Expand Up @@ -2192,3 +2193,72 @@ 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),
),
},
},
})
}
46 changes: 46 additions & 0 deletions port/action/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -1072,6 +1110,10 @@ resource "port_action" "create_microservice" {
jq_query = "\"specificValue\""
}
}]
}
sort = {
property = "$updatedAt"
order = "DESC"
}
}
}
Expand Down Expand Up @@ -1111,6 +1153,10 @@ resource "port_action" "create_microservice" {
}]
})
}
sort = {
property = "$updatedAt"
order = "DESC"
}
}
}
}
Expand Down
Loading

0 comments on commit 99381ce

Please sign in to comment.