Skip to content

Commit

Permalink
Merge pull request #21 from form3tech-oss/fmatavelli-fix-drp-provider
Browse files Browse the repository at this point in the history
fix: param conversion
  • Loading branch information
fabiomatavelli-f3 authored May 29, 2023
2 parents 73d215f + 3d077b7 commit 068663e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 35 deletions.
57 changes: 22 additions & 35 deletions drpv4/resource_drp_profile_param.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package drpv4

import (
"encoding/json"
"fmt"
"log"
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"gitlab.com/rackn/provision/v4/models"
Expand Down Expand Up @@ -71,11 +71,11 @@ func getParam(c *Config, name string) (*models.Param, error) {
func getParamSchemaType(c *Config, name string) string {
param, err := getParam(c, name)
if err != nil {
return "string"
return ""
}

if param.Schema == nil || param.Schema.(map[string]interface{})["type"] == nil {
return "string"
return ""
}

s := param.Schema.(map[string]interface{})["type"].(string)
Expand All @@ -84,39 +84,30 @@ func getParamSchemaType(c *Config, name string) string {
}

// convertParamToType returns the value in the correct type
func convertParamToType(value string, valueType string) (interface{}, error) {
if valueType == "string" {
return value, nil
}
func convertParamToType(c *Config, name string, value string) (interface{}, error) {
paramType := getParamSchemaType(c, name)

switch valueType {
case "integer":
return strconv.Atoi(value)
case "number":
return strconv.ParseFloat(value, 32)
case "boolean":
return strconv.ParseBool(value)
switch paramType {
case "string":
case "array":
case "map":
default:
return value, nil
default:
var out interface{}
err := json.Unmarshal([]byte(value), &out)
if err != nil {
return value, nil
}
return out, nil
}

return value, nil
}

// convertParamToType returns the value in the correct type
func convertParamFromType(value interface{}, valueType string) (string, error) {
switch valueType {
case "integer":
return strconv.FormatFloat(value.(float64), 'f', 0, 64), nil
case "number":
return strconv.FormatFloat(value.(float64), 'f', 6, 64), nil
case "boolean":
return strconv.FormatBool(value.(bool)), nil
// convertParamToString returns the value in the correct type
func convertParamToString(value interface{}) (string, error) {
switch value := value.(type) {
case string:
return value, nil
default:
return value.(string), nil
out, err := json.Marshal(value)
return string(out), err
}
}

Expand Down Expand Up @@ -175,9 +166,7 @@ func resourceProfileParamCreate(d *schema.ResourceData, m interface{}) error {
return err
}
} else {
paramType := getParamSchemaType(c, name)

convertedValue, err := convertParamToType(value, paramType)
convertedValue, err := convertParamToType(c, name, value)
if err != nil {
return err
}
Expand Down Expand Up @@ -211,9 +200,7 @@ func resourceProfileParamRead(d *schema.ResourceData, m interface{}) error {
d.Set("profile", profile)

if secureValue == "" {
paramType := getParamSchemaType(c, name)

value, err := convertParamFromType(p, paramType)
value, err := convertParamToString(p)
if err != nil {
return err
}
Expand Down
55 changes: 55 additions & 0 deletions drpv4/resource_drp_profile_param_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ func TestAccResourceProfileParam(t *testing.T) {
resource.TestCheckResourceAttr(fmt.Sprintf("drp_profile_param.%s", profileParamName), "value", "test2"),
),
},
{
Config: fmt.Sprintf(`
resource "drp_profile_param" "%s" {
profile = "global"
name = "test_json_value"
value = jsonencode({
test = "test"
})
}
`, profileParamName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fmt.Sprintf("drp_profile_param.%s", profileParamName), "value", `{"test":"test"}`),
),
},
{
Config: fmt.Sprintf(`
resource "drp_param" "%s" {
Expand All @@ -68,6 +82,47 @@ func TestAccResourceProfileParam(t *testing.T) {
resource.TestCheckResourceAttr(fmt.Sprintf("drp_profile_param.%s", profileParamName), "value", "test"),
),
},
{
Config: fmt.Sprintf(`
resource "drp_param" "%s" {
name = "test_nil_schema_json"
description = "Testing nil schema"
}
resource "drp_profile_param" "%s" {
profile = "global"
name = drp_param.%s.name
value = jsonencode({
test = "test"
})
}
`, profileParamName, profileParamName, profileParamName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fmt.Sprintf("drp_profile_param.%s", profileParamName), "value", `{"test":"test"}`),
),
},
{
Config: fmt.Sprintf(`
resource "drp_param" "%s" {
name = "test_string_schema_json"
description = "Testing string schema"
schema = {
type = "string"
}
}
resource "drp_profile_param" "%s" {
profile = "global"
name = drp_param.%s.name
value = jsonencode({
test = "test"
})
}
`, profileParamName, profileParamName, profileParamName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fmt.Sprintf("drp_profile_param.%s", profileParamName), "value", `{"test":"test"}`),
),
},
{
Config: fmt.Sprintf(`
resource "drp_param" "%s" {
Expand Down

0 comments on commit 068663e

Please sign in to comment.