Skip to content

Commit fc90145

Browse files
authored
Port 3216 add support for enum of number (#42)
1 parent a687a77 commit fc90145

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

port/cli/models.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type (
4141
Description string `json:"description,omitempty"`
4242
Blueprint string `json:"blueprint,omitempty"`
4343
Pattern string `json:"pattern,omitempty"`
44-
Enum []string `json:"enum,omitempty"`
44+
Enum []interface{} `json:"enum,omitempty"`
4545
Spec string `json:"spec,omitempty"`
4646
SpecAuthentication *SpecAuthentication `json:"specAuthentication,omitempty"`
4747
EnumColors map[string]string `json:"enumColors,omitempty"`

port/resource_port_blueprint.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -445,14 +445,26 @@ func writeBlueprintFieldsToResource(d *schema.ResourceData, b *cli.Blueprint) {
445445
p["min_length"] = v.MinLength
446446
p["icon"] = v.Icon
447447
p["spec"] = v.Spec
448-
p["enum"] = v.Enum
449448
p["enum_colors"] = v.EnumColors
450449
if lo.Contains(b.Schema.Required, k) {
451450
p["required"] = true
452451
} else {
453452
p["required"] = false
454453
}
455454

455+
enumValue := []string{}
456+
457+
for _, value := range v.Enum {
458+
if v.Type == "number" {
459+
enumValue = append(enumValue, fmt.Sprintf("%v", value))
460+
}
461+
if v.Type == "string" {
462+
enumValue = append(enumValue, value.(string))
463+
}
464+
}
465+
466+
p["enum"] = enumValue
467+
456468
if v.Default != nil {
457469
writeDefaultFieldToResource(v, k, d, p)
458470
}
@@ -664,10 +676,25 @@ func blueprintResourceToBody(d *schema.ResourceData) (*cli.Blueprint, error) {
664676
if r, ok := p["required"]; ok && r.(bool) {
665677
required = append(required, p["identifier"].(string))
666678
}
667-
if e, ok := p["enum"]; ok && e != nil {
679+
if e, ok := p["enum"]; ok && len(e.([]interface{})) > 0 {
680+
if propFields.Type != "number" && propFields.Type != "string" {
681+
return nil, fmt.Errorf("enum can only be used when type is number or string for property %s", p["identifier"].(string))
682+
}
668683
for _, v := range e.([]interface{}) {
669-
propFields.Enum = append(propFields.Enum, v.(string))
684+
if propFields.Type == "number" {
685+
enumValue, err := strconv.ParseInt(v.(string), 10, 0)
686+
if err != nil {
687+
return nil, fmt.Errorf("enum value %s is not a valid number for property %s", v.(string), p["identifier"].(string))
688+
}
689+
propFields.Enum = append(propFields.Enum, enumValue)
690+
}
691+
if propFields.Type == "string" {
692+
enumValue := v.(string)
693+
propFields.Enum = append(propFields.Enum, enumValue)
694+
}
695+
670696
}
697+
671698
}
672699
if e, ok := p["enum_colors"]; ok && e != nil {
673700
enumColors := make(map[string]string)

port/resource_port_blueprint_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,56 @@ func genID() string {
1717
return fmt.Sprintf("t-%s", id[:18])
1818
}
1919

20+
func TestAccPortBlueprintEnum(t *testing.T) {
21+
identifier := genID()
22+
var testAccActionConfigCreate = fmt.Sprintf(`
23+
resource "port-labs_blueprint" "enumTest" {
24+
title = "TF Provider Test BP0"
25+
icon = "Terraform"
26+
identifier = "%s"
27+
properties {
28+
identifier = "text"
29+
type = "string"
30+
title = "text"
31+
icon = "Terraform"
32+
enum = ["a", "b", "c"]
33+
enum_colors = {
34+
a = "red"
35+
b = "blue"
36+
}
37+
}
38+
properties {
39+
identifier = "number"
40+
type = "number"
41+
title = "number"
42+
enum = [1, 2]
43+
44+
}
45+
}
46+
`, identifier)
47+
resource.Test(t, resource.TestCase{
48+
Providers: map[string]*schema.Provider{
49+
"port-labs": Provider(),
50+
},
51+
Steps: []resource.TestStep{
52+
{
53+
Config: testAccActionConfigCreate,
54+
Check: resource.ComposeTestCheckFunc(
55+
56+
resource.TestCheckResourceAttr("port-labs_blueprint.enumTest", "properties.1.enum.0", "a"),
57+
resource.TestCheckResourceAttr("port-labs_blueprint.enumTest", "properties.1.enum.1", "b"),
58+
resource.TestCheckResourceAttr("port-labs_blueprint.enumTest", "properties.1.enum.2", "c"),
59+
resource.TestCheckResourceAttr("port-labs_blueprint.enumTest", "properties.1.enum_colors.a", "red"),
60+
resource.TestCheckResourceAttr("port-labs_blueprint.enumTest", "properties.1.enum_colors.b", "blue"),
61+
62+
resource.TestCheckResourceAttr("port-labs_blueprint.enumTest", "properties.0.enum.0", "1"),
63+
resource.TestCheckResourceAttr("port-labs_blueprint.enumTest", "properties.0.enum.1", "2"),
64+
),
65+
},
66+
},
67+
})
68+
}
69+
2070
func TestAccPortBlueprint(t *testing.T) {
2171
identifier := genID()
2272
var testAccActionConfigCreate = fmt.Sprintf(`

0 commit comments

Comments
 (0)