Skip to content

Commit 7672a6b

Browse files
authored
Port 3675 add support for min max items for array properties (#46)
1 parent ff83aa4 commit 7672a6b

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

docs/resources/blueprint.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ Optional:
5959
- `format` (String) The format of the Property
6060
- `icon` (String) The icon of the property
6161
- `items` (Map of String) A metadata of an array's items, in case the type is an array
62+
- `max_items` (Number) The maximum number of items in the property
6263
- `max_length` (Number) The maximum length of the property
64+
- `min_items` (Number) The minimum number of items in the property
6365
- `min_length` (Number) The minimum length of the property
6466
- `required` (Boolean) Whether or not the property is required
6567
- `spec` (String) The specification of the property, one of "async-api", "open-api", "embedded-url"

port/cli/models.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type (
3838
Format string `json:"format,omitempty"`
3939
MaxLength int `json:"maxLength,omitempty"`
4040
MinLength int `json:"minLength,omitempty"`
41+
MaxItems int `json:"maxItems,omitempty"`
42+
MinItems int `json:"minItems,omitempty"`
4143
Description string `json:"description,omitempty"`
4244
Blueprint string `json:"blueprint,omitempty"`
4345
Pattern string `json:"pattern,omitempty"`

port/resource_port_blueprint.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,16 @@ func newBlueprintResource() *schema.Resource {
155155
Optional: true,
156156
Description: "The minimum length of the property",
157157
},
158+
"min_items": {
159+
Type: schema.TypeInt,
160+
Optional: true,
161+
Description: "The minimum number of items in the property",
162+
},
163+
"max_items": {
164+
Type: schema.TypeInt,
165+
Optional: true,
166+
Description: "The maximum number of items in the property",
167+
},
158168
"spec": {
159169
Type: schema.TypeString,
160170
Optional: true,
@@ -443,6 +453,8 @@ func writeBlueprintFieldsToResource(d *schema.ResourceData, b *cli.Blueprint) {
443453
p["format"] = v.Format
444454
p["max_length"] = v.MaxLength
445455
p["min_length"] = v.MinLength
456+
p["max_items"] = v.MaxItems
457+
p["min_items"] = v.MinItems
446458
p["icon"] = v.Icon
447459
p["spec"] = v.Spec
448460
p["enum_colors"] = v.EnumColors
@@ -652,6 +664,22 @@ func blueprintResourceToBody(d *schema.ResourceData) (*cli.Blueprint, error) {
652664
propFields.MinLength = i.(int)
653665
}
654666

667+
if i, ok := p["min_items"]; ok && i != 0 {
668+
if propFields.Type != "array" {
669+
return nil, fmt.Errorf("min_items can only be used when type is array for property %s", p["identifier"].(string))
670+
}
671+
672+
propFields.MinItems = i.(int)
673+
}
674+
675+
if i, ok := p["max_items"]; ok && i != 0 {
676+
if propFields.Type != "array" {
677+
return nil, fmt.Errorf("max_items can only be used when type is array for property %s", p["identifier"].(string))
678+
}
679+
680+
propFields.MaxItems = i.(int)
681+
}
682+
655683
if i, ok := p["icon"]; ok && i != "" {
656684
propFields.Icon = i.(string)
657685
}

port/resource_port_blueprint_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ func TestAccPortBlueprint(t *testing.T) {
101101
}
102102
title = "array"
103103
default_items = ["https://getport.io", "https://app.getport.io"]
104+
min_items = 2
105+
max_items = 10
104106
}
105107
properties {
106108
identifier = "text"
@@ -130,6 +132,8 @@ func TestAccPortBlueprint(t *testing.T) {
130132
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.0.default_items.#", "2"),
131133
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.0.items.type", "string"),
132134
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.0.items.format", "url"),
135+
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.0.min_items", "2"),
136+
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.0.max_items", "10"),
133137
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.1.default", "1"),
134138
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.2.identifier", "text"),
135139
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.2.enum.0", "a"),

0 commit comments

Comments
 (0)