Skip to content

Commit

Permalink
Merge pull request #200 from port-labs/devin/1736675744-add-pattern-s…
Browse files Browse the repository at this point in the history
…tring-items

feat: add pattern validation for string array items in blueprint resource
  • Loading branch information
OmriGez authored Jan 13, 2025
2 parents f443c01 + b7c62d4 commit f8b450f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
34 changes: 34 additions & 0 deletions docs/resources/port_blueprint.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,26 @@ resource "port_blueprint" "environment" {
```

## Example Usage with String Array Pattern Validation

```hcl
resource "port_blueprint" "regex" {
title = "Regex"
identifier = "regex"
properties = {
array_props = {
"stringArrayProp" = {
title = "String Array"
string_items = {
pattern = "^[a-zA-Z0-9]*$"
default = ["abc", "123", "test"]
}
}
}
}
}
```

## Example Usage with Relations

```hcl
Expand Down Expand Up @@ -284,6 +304,7 @@ resource "port_blueprint" "microservice" {
- `icon` (String) The icon of the blueprint
- `kafka_changelog_destination` (Object) The changelog destination of the blueprint (see [below for nested schema](#nestedatt--kafka_changelog_destination))
- `mirror_properties` (Attributes Map) The mirror properties of the blueprint (see [below for nested schema](#nestedatt--mirror_properties))
- `ownership` (Attributes) Optional ownership field for Blueprint. 'type' can be Inherited or Direct. If 'Inherited', then 'path' is required and must be a valid relation identifiers path. (see [below for nested schema](#nestedatt--ownership))
- `properties` (Attributes) The properties of the blueprint (see [below for nested schema](#nestedatt--properties))
- `relations` (Attributes Map) The relations of the blueprint (see [below for nested schema](#nestedatt--relations))
- `team_inheritance` (Attributes) The team inheritance of the blueprint (see [below for nested schema](#nestedatt--team_inheritance))
Expand Down Expand Up @@ -334,6 +355,18 @@ Optional:
- `title` (String) The title of the mirror property


<a id="nestedatt--ownership"></a>
### Nested Schema for `ownership`

Required:

- `type` (String) Ownership type: either 'Inherited' or 'Direct'.

Optional:

- `path` (String) Path for the Inherited ownership type. Required when type is 'Inherited'. Must be a valid relation identifiers path.


<a id="nestedatt--properties"></a>
### Nested Schema for `properties`

Expand Down Expand Up @@ -392,6 +425,7 @@ Optional:

- `default` (List of String) The default of the items
- `format` (String) The format of the items
- `pattern` (String) The pattern of the string array items



Expand Down
6 changes: 6 additions & 0 deletions port/blueprint/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func arrayPropResourceToBody(ctx context.Context, state *BlueprintModel, props m
if !prop.StringItems.Format.IsNull() {
items["format"] = prop.StringItems.Format.ValueString()
}
if !prop.StringItems.Pattern.IsNull() {
items["pattern"] = prop.StringItems.Pattern.ValueString()
}
if !prop.StringItems.Default.IsNull() {
defaultList, err := utils.TerraformListToGoArray(ctx, prop.StringItems.Default, "string")
if err != nil {
Expand Down Expand Up @@ -138,6 +141,9 @@ func addArrayPropertiesToState(v *cli.BlueprintProperty) *ArrayPropModel {
if value, ok := v.Items["format"]; ok && value != nil {
arrayProp.StringItems.Format = types.StringValue(v.Items["format"].(string))
}
if value, ok := v.Items["pattern"]; ok && value != nil {
arrayProp.StringItems.Pattern = types.StringValue(v.Items["pattern"].(string))
}
case "number":
arrayProp.NumberItems = &NumberItems{}
if v.Default != nil {
Expand Down
1 change: 1 addition & 0 deletions port/blueprint/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type BooleanPropModel struct {
type StringItems struct {
Format types.String `tfsdk:"format"`
Default types.List `tfsdk:"default"`
Pattern types.String `tfsdk:"pattern"`
}

type NumberItems struct {
Expand Down
4 changes: 3 additions & 1 deletion port/blueprint/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ func TestAccPortBlueprintArrayProperty(t *testing.T) {
min_items = 1
max_items = 10
string_items = {
default = ["a", "b", "c"]
default = ["a", "b", "c"],
pattern = "^[a-zA-Z0-9]*$"
}
}
myNumberArrayIdentifier = {
Expand Down Expand Up @@ -266,6 +267,7 @@ func TestAccPortBlueprintArrayProperty(t *testing.T) {
resource.TestCheckResourceAttr("port_blueprint.microservice", "properties.array_props.myStringArrayIdentifier.string_items.default.0", "a"),
resource.TestCheckResourceAttr("port_blueprint.microservice", "properties.array_props.myStringArrayIdentifier.string_items.default.1", "b"),
resource.TestCheckResourceAttr("port_blueprint.microservice", "properties.array_props.myStringArrayIdentifier.string_items.default.2", "c"),
resource.TestCheckResourceAttr("port_blueprint.microservice", "properties.array_props.myStringArrayIdentifier.string_items.pattern", "^[a-zA-Z0-9]*$"),
resource.TestCheckResourceAttr("port_blueprint.microservice", "properties.array_props.myNumberArrayIdentifier.number_items.default.0", "1"),
resource.TestCheckResourceAttr("port_blueprint.microservice", "properties.array_props.myNumberArrayIdentifier.number_items.default.1", "2"),
resource.TestCheckResourceAttr("port_blueprint.microservice", "properties.array_props.myNumberArrayIdentifier.number_items.default.2", "3"),
Expand Down
8 changes: 8 additions & 0 deletions port/blueprint/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package blueprint

import (
"context"
"regexp"

"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
"github.com/hashicorp/terraform-plugin-framework-validators/listvalidator"
Expand Down Expand Up @@ -205,6 +206,13 @@ func ArrayPropertySchema() schema.MapNestedAttribute {
Optional: true,
ElementType: types.StringType,
},
"pattern": schema.StringAttribute{
MarkdownDescription: "The pattern of the string array items",
Optional: true,
Validators: []validator.String{
stringvalidator.RegexMatches(regexp.MustCompile(`^.*$`), "must be a valid regular expression"),
},
},
},
},
"number_items": schema.SingleNestedAttribute{
Expand Down

0 comments on commit f8b450f

Please sign in to comment.