Skip to content

Commit

Permalink
Support and test bool, number, and object
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchnielsen committed Oct 18, 2024
1 parent 535195c commit b8134ab
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
8 changes: 0 additions & 8 deletions internal/provider/datasources/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,6 @@ func (d *VariableDataSource) Read(ctx context.Context, req datasource.ReadReques

model.Value = types.DynamicValue(jsontypes.NewNormalizedValue(string(byteSlice)))

case []string: // array
list, diags := types.ListValueFrom(ctx, types.StringType, value)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
model.Value = types.DynamicValue(list)

default:
resp.Diagnostics.Append(helpers.ResourceClientErrorDiagnostic("Variable", "type", fmt.Errorf("unsupported type: %T", value)))
}
Expand Down
30 changes: 25 additions & 5 deletions internal/provider/resources/variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package resources

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

"github.com/avast/retry-go/v4"
Expand Down Expand Up @@ -165,13 +167,31 @@ func getUnderlyingValue(plan VariableResourceModel) (interface{}, diag.Diagnosti
var value interface{}

switch underlyingValue := plan.Value.UnderlyingValue().(type) {
case types.Bool:
value = underlyingValue.ValueBool()
// case types.Number:
// case types.List:
// case types.Object:
case types.String:
value = underlyingValue.ValueString()

case types.Number:
var err error
value, err = strconv.ParseFloat(underlyingValue.String(), 64)
if err != nil {
diags.Append(diag.NewErrorDiagnostic(
"unable to convert number to float64",
fmt.Sprintf("number: %v, error: %v", value, err),
))
}

case types.Bool:
value = underlyingValue.ValueBool()

case types.Object:
result := map[string]interface{}{}
if err := json.Unmarshal([]byte(underlyingValue.String()), &result); err != nil {
diags.Append(diag.NewErrorDiagnostic(
"unable to convert object to map[string]interface",
fmt.Sprintf("object: %v, error: %v", value, err),
))
}

default:
diags.Append(diag.NewErrorDiagnostic(
"unexpected value type",
Expand Down
49 changes: 38 additions & 11 deletions internal/provider/resources/variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ func TestAccResource_variable(t *testing.T) {

resourceName := "prefect_variable.test"

randomValue := testutils.NewRandomPrefixedString()
randomValueWithQuotes := fmt.Sprintf("%q", randomValue)
randomValue2 := false
valueString := "hello-world"
valueStringForResource := fmt.Sprintf("%q", valueString)
valueNumber := float64(123)
valueBool := true

valueObject := `{"foo" = "bar"}`
valueObjectForResource := fmt.Sprintf("%q", valueObject)

workspace, workspaceName := testutils.NewEphemeralWorkspace()
workspaceResourceName := "prefect_workspace." + workspaceName
Expand All @@ -62,28 +66,51 @@ func TestAccResource_variable(t *testing.T) {
Steps: []resource.TestStep{
{
// Check creation + existence of the variable resource
Config: fixtureAccVariableResource(workspace, workspaceName, randomName, randomValueWithQuotes),
Config: fixtureAccVariableResource(workspace, workspaceName, randomName, valueStringForResource),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckVariableExists(resourceName, workspaceResourceName, &variable),
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName, Value: randomValue}),
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName, Value: valueString}),
resource.TestCheckResourceAttr(resourceName, "name", randomName),
),
},
{
// Check updating name + value of the variable resource
Config: fixtureAccVariableResource(workspace, workspaceName, randomName2, randomValue2),
// Check updating name of the variable resource
Config: fixtureAccVariableResource(workspace, workspaceName, randomName2, valueStringForResource),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckVariableExists(resourceName, workspaceResourceName, &variable),
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName2, Value: randomValue2}),
resource.TestCheckResourceAttr(resourceName, "name", randomName2),
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName2, Value: valueString}),
),
},
{
// Check updating value of the variable resource to a number
Config: fixtureAccVariableResource(workspace, workspaceName, randomName2, valueNumber),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckVariableExists(resourceName, workspaceResourceName, &variable),
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName2, Value: valueNumber}),
),
},
{
// Check updating value of the variable resource to a boolean
Config: fixtureAccVariableResource(workspace, workspaceName, randomName2, valueBool),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckVariableExists(resourceName, workspaceResourceName, &variable),
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName2, Value: valueBool}),
),
},
{
// Check updating value of the variable resource to a object
Config: fixtureAccVariableResource(workspace, workspaceName, randomName2, valueObjectForResource),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckVariableExists(resourceName, workspaceResourceName, &variable),
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName2, Value: valueObject}),
),
},
{
// Check adding tags
Config: fixtureAccVariableResourceWithTags(workspace, workspaceName, randomName2, randomValue2),
Config: fixtureAccVariableResourceWithTags(workspace, workspaceName, randomName2, valueBool),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckVariableExists(resourceName, workspaceResourceName, &variable),
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName2, Value: randomValue2}),
testAccCheckVariableValues(&variable, &api.Variable{Name: randomName2, Value: valueBool}),
resource.TestCheckResourceAttr(resourceName, "name", randomName2),
resource.TestCheckResourceAttr(resourceName, "tags.#", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "foo"),
Expand Down

0 comments on commit b8134ab

Please sign in to comment.