Skip to content

Commit

Permalink
Merge pull request #16 from port-labs/PORT-1843-terraform-provider-pr…
Browse files Browse the repository at this point in the history
…ovider-assumes-that-default-of-a-property-is-always-string

Port 1843 terraform provider support multiple types of default of a property
  • Loading branch information
talsabagport authored Nov 29, 2022
2 parents ad96656 + d6d570e commit bcb1750
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/resources/action.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Required:
Optional:

- `blueprint` (String) When selecting format 'entity', the identifier of the target blueprint
- `default_items` (List of String) The list of default items, in case the type of this property is a list
- `default` (String) A default value for this property in case an entity is created without explicitly providing a value.
- `description` (String) A description of the property. This value is visible to users when hovering on the info icon in the UI. It provides detailed information about the use of a specific property.
- `enum` (List of String) A list of allowed values for the property
Expand Down
1 change: 1 addition & 0 deletions docs/resources/blueprint.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Required:

Optional:

- `default_items` (List of String) The list of default items, in case the type of this property is a list
- `default` (String) The default value of the property
- `description` (String) The description of the property
- `enum` (List of String) A list of allowed values for the property
Expand Down
2 changes: 1 addition & 1 deletion port/cli/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type (
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Identifier string `json:"identifier,omitempty"`
Default string `json:"default,omitempty"`
Default interface{} `json:"default,omitempty"`
Icon string `json:"icon,omitempty"`
Format string `json:"format,omitempty"`
Description string `json:"description,omitempty"`
Expand Down
68 changes: 65 additions & 3 deletions port/resource_port_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package port

import (
"context"
"encoding/json"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -74,6 +76,14 @@ func newActionResource() *schema.Resource {
Optional: true,
Description: "A default value for this property in case an entity is created without explicitly providing a value.",
},
"default_items": {
Type: schema.TypeList,
Optional: true,
Description: "The list of items, in case the type of default property is a list",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"format": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -170,7 +180,6 @@ func writeActionFieldsToResource(d *schema.ResourceData, action *cli.Action) {
p["title"] = v.Title
p["type"] = v.Type
p["description"] = v.Description
p["default"] = v.Default
p["format"] = v.Format
p["pattern"] = v.Pattern
p["blueprint"] = v.Blueprint
Expand All @@ -180,6 +189,27 @@ func writeActionFieldsToResource(d *schema.ResourceData, action *cli.Action) {
} else {
p["required"] = false
}
if v.Default != nil {
switch t := v.Default.(type) {
case map[string]interface{}:
js, _ := json.Marshal(&t)
p["default"] = string(js)
case []interface{}:
p["default_items"] = t
case float64:
p["default"] = strconv.FormatFloat(t, 'f', -1, 64)
case int:
p["default"] = strconv.Itoa(t)
case string:
p["default"] = t
case bool:
p["default"] = "false"
if t {
p["default"] = "true"
}
}
}

properties.Add(p)
}
d.Set("user_properties", &properties)
Expand Down Expand Up @@ -220,8 +250,40 @@ func actionResourceToBody(d *schema.ResourceData) (*cli.Action, error) {
if d, ok := p["description"]; ok && d != "" {
propFields.Description = d.(string)
}
if d, ok := p["default"]; ok && d != "" {
propFields.Default = d.(string)
switch propFields.Type {
case "string":
if d, ok := p["default"]; ok && d.(string) != "" {
propFields.Default = d.(string)
}
case "number":
if d, ok := p["default"]; ok && d.(string) != "" {
defaultNum, err := strconv.ParseInt(d.(string), 10, 0)
if err != nil {
return nil, err
}
propFields.Default = defaultNum
}
case "boolean":
if d, ok := p["default"]; ok && d.(string) != "" {
defaultBool, err := strconv.ParseBool(d.(string))
if err != nil {
return nil, err
}
propFields.Default = defaultBool
}
case "array":
if d, ok := p["default_items"]; ok && d != nil {
propFields.Default = d
}
case "object":
if d, ok := p["default"]; ok && d.(string) != "" {
defaultObj := make(map[string]interface{})
err := json.Unmarshal([]byte(d.(string)), &defaultObj)
if err != nil {
return nil, err
}
propFields.Default = defaultObj
}
}
if f, ok := p["format"]; ok && f != "" {
propFields.Format = f.(string)
Expand Down
39 changes: 35 additions & 4 deletions port/resource_port_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,35 @@ func TestAccPortAction(t *testing.T) {
invocation_method {
type = "KAFKA"
}
user_properties {
identifier = "reason"
type = "string"
title = "Reason"
default = "test"
}
user_properties {
identifier = "delay"
type = "number"
title = "Delay"
default = 3
}
user_properties {
identifier = "clear_cache"
type = "boolean"
title = "Clear cache"
default = true
}
user_properties {
identifier = "services"
type = "array"
title = "Services"
default_items = ["api", "frontend"]
}
user_properties {
identifier = "config"
type = "object"
title = "Config"
default = jsonencode({"when":"immediate"})
}
}
`, identifier, actionIdentifier)
Expand Down Expand Up @@ -87,10 +112,16 @@ func TestAccPortAction(t *testing.T) {
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "blueprint_identifier", identifier),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "invocation_method.0.type", "KAFKA"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "trigger", "DAY-2"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.#", "1"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.0.identifier", "clear_cache"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.0.type", "boolean"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.0.title", "Clear cache"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.#", "5"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.0.default_items.0", "api"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.0.default_items.#", "2"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.1.default", "3"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.2.default", "test"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.3.identifier", "clear_cache"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.3.type", "boolean"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.3.title", "Clear cache"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.3.default", "true"),
resource.TestCheckResourceAttr("port-labs_action.restart_microservice", "user_properties.4.default", "{\"when\":\"immediate\"}"),
),
},
{
Expand Down
68 changes: 65 additions & 3 deletions port/resource_port_blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package port

import (
"context"
"encoding/json"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -118,6 +120,14 @@ func newBlueprintResource() *schema.Resource {
Optional: true,
Description: "The default value of the property",
},
"default_items": {
Type: schema.TypeList,
Optional: true,
Description: "The list of items, in case the type of default property is a list",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"format": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -283,7 +293,6 @@ func writeBlueprintFieldsToResource(d *schema.ResourceData, b *cli.Blueprint) {
p["title"] = v.Title
p["type"] = v.Type
p["description"] = v.Description
p["default"] = v.Default
p["format"] = v.Format
p["icon"] = v.Icon
p["enum"] = v.Enum
Expand All @@ -293,6 +302,27 @@ func writeBlueprintFieldsToResource(d *schema.ResourceData, b *cli.Blueprint) {
} else {
p["required"] = false
}
if v.Default != nil {
switch t := v.Default.(type) {
case map[string]interface{}:
js, _ := json.Marshal(&t)
p["default"] = string(js)
case []interface{}:
p["default_items"] = t
case float64:
p["default"] = strconv.FormatFloat(t, 'f', -1, 64)
case int:
p["default"] = strconv.Itoa(t)
case string:
p["default"] = t
case bool:
p["default"] = "false"
if t {
p["default"] = "true"
}
}
}

properties.Add(p)
}

Expand Down Expand Up @@ -356,8 +386,40 @@ func blueprintResourceToBody(d *schema.ResourceData) (*cli.Blueprint, error) {
if d, ok := p["description"]; ok && d != "" {
propFields.Description = d.(string)
}
if d, ok := p["default"]; ok && d != "" {
propFields.Default = d.(string)
switch propFields.Type {
case "string":
if d, ok := p["default"]; ok && d.(string) != "" {
propFields.Default = d.(string)
}
case "number":
if d, ok := p["default"]; ok && d.(string) != "" {
defaultNum, err := strconv.ParseInt(d.(string), 10, 0)
if err != nil {
return nil, err
}
propFields.Default = defaultNum
}
case "boolean":
if d, ok := p["default"]; ok && d.(string) != "" {
defaultBool, err := strconv.ParseBool(d.(string))
if err != nil {
return nil, err
}
propFields.Default = defaultBool
}
case "array":
if d, ok := p["default_items"]; ok && d != nil {
propFields.Default = d
}
case "object":
if d, ok := p["default"]; ok && d.(string) != "" {
defaultObj := make(map[string]interface{})
err := json.Unmarshal([]byte(d.(string)), &defaultObj)
if err != nil {
return nil, err
}
propFields.Default = defaultObj
}
}
if f, ok := p["format"]; ok && f != "" {
propFields.Format = f.(string)
Expand Down
17 changes: 14 additions & 3 deletions port/resource_port_blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,25 @@ func TestAccPortBlueprint(t *testing.T) {
identifier = "bool"
type = "boolean"
title = "boolean"
default = true
}
properties {
identifier = "number"
type = "number"
title = "number"
default = 1
}
properties {
identifier = "obj"
type = "object"
title = "object"
default = jsonencode({"a":"b"})
}
properties {
identifier = "array"
type = "array"
title = "array"
default_items = [1, 2, 3]
}
properties {
identifier = "text"
Expand All @@ -54,6 +58,7 @@ func TestAccPortBlueprint(t *testing.T) {
a = "red"
b = "blue"
}
default = "a"
}
}
`, identifier)
Expand All @@ -65,9 +70,15 @@ func TestAccPortBlueprint(t *testing.T) {
{
Config: testAccActionConfigCreate,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.0.identifier", "text"),
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.0.enum.0", "a"),
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.0.enum_colors.a", "red"),
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.0.default_items.0", "1"),
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.0.default_items.#", "3"),
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.1.default", "1"),
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.2.identifier", "text"),
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.2.enum.0", "a"),
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.2.enum_colors.a", "red"),
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.2.default", "a"),
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.3.default", "true"),
resource.TestCheckResourceAttr("port-labs_blueprint.microservice", "properties.4.default", "{\"a\":\"b\"}"),
),
},
},
Expand Down

0 comments on commit bcb1750

Please sign in to comment.