Skip to content

Commit

Permalink
Fix value conversion error when using unknown variable values (#365)
Browse files Browse the repository at this point in the history
* Add test to replicate issue

* changelog

* fix resource
  • Loading branch information
patrickcping authored Aug 21, 2024
1 parent b470054 commit 1740585
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/365.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
`resource/davinci_variable`: Fix "Value Conversion Error" when defining variables with unknown values.
```
15 changes: 10 additions & 5 deletions internal/service/davinci/resource_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/pingidentity/terraform-provider-davinci/internal/framework"
stringvalidatorinternal "github.com/pingidentity/terraform-provider-davinci/internal/framework/stringvalidator"
"github.com/pingidentity/terraform-provider-davinci/internal/sdk"
Expand Down Expand Up @@ -278,26 +279,30 @@ func (p *VariableResource) ModifyPlan(ctx context.Context, req resource.ModifyPl
return
}

var varValuePlan *string
var varValuePlan basetypes.StringValue
resp.Diagnostics.Append(resp.Plan.GetAttribute(ctx, path.Root("value"), &varValuePlan)...)
if resp.Diagnostics.HasError() {
return
}

var varEmptyValuePlan *bool
var varEmptyValuePlan basetypes.BoolValue
resp.Diagnostics.Append(resp.Plan.GetAttribute(ctx, path.Root("empty_value"), &varEmptyValuePlan)...)
if resp.Diagnostics.HasError() {
return
}

if varEmptyValuePlan != nil && *varEmptyValuePlan {
if !varEmptyValuePlan.IsNull() && !varEmptyValuePlan.IsUnknown() {
resp.Plan.SetAttribute(ctx, path.Root("value_service"), types.StringNull())
} else {

if varValuePlan != nil {
resp.Plan.SetAttribute(ctx, path.Root("value_service"), types.StringValue(*varValuePlan))
if !varValuePlan.IsNull() && !varValuePlan.IsUnknown() {
resp.Plan.SetAttribute(ctx, path.Root("value_service"), varValuePlan)
}
}

if varEmptyValuePlan.IsUnknown() || varValuePlan.IsUnknown() {
resp.Plan.SetAttribute(ctx, path.Root("value_service"), types.StringUnknown())
}
}

func (r *VariableResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
Expand Down
87 changes: 87 additions & 0 deletions internal/service/davinci/resource_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,50 @@ func testAccResourceVariable_Values(t *testing.T, context, variableName string)
})
}

func TestAccResourceVariable_UnknownValue(t *testing.T) {

resourceBase := "davinci_variable"
resourceName := acctest.ResourceNameGen()
resourceFullName := fmt.Sprintf("%s.%s", resourceBase, resourceName)

name := resourceName

withBootstrapConfig := false

step1 := resource.TestStep{
Config: testAccResourceVariable_UnknownValue1_Hcl(resourceName, name, withBootstrapConfig),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceFullName, "value", "testVariable##SK##company"),
resource.TestCheckNoResourceAttr(resourceFullName, "empty_value"),
resource.TestCheckResourceAttr(resourceFullName, "value_service", "testVariable##SK##company"),
),
}

step2 := resource.TestStep{
Config: testAccResourceVariable_UnknownValue2_Hcl(resourceName, name, withBootstrapConfig),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceFullName, "value", "testVariable3##SK##company"),
resource.TestCheckNoResourceAttr(resourceFullName, "empty_value"),
resource.TestCheckResourceAttr(resourceFullName, "value_service", "testVariable3##SK##company"),
),
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheckClient(t)
acctest.PreCheckNewEnvironment(t)
},
ProtoV6ProviderFactories: acctest.ProtoV6ProviderFactories,
ExternalProviders: acctest.ExternalProviders,
ErrorCheck: acctest.ErrorCheck(t),
CheckDestroy: davinci.Variable_CheckDestroy(),
Steps: []resource.TestStep{
step1,
step2,
},
})
}

func TestAccResourceVariable_BadParameters(t *testing.T) {

resourceBase := "davinci_variable"
Expand Down Expand Up @@ -845,6 +889,49 @@ resource "davinci_variable" "%[1]s-flow" {
return hcl
}

func testAccResourceVariable_UnknownValue1_Hcl(resourceName, name string, withBootstrapConfig bool) (hcl string) {
hcl = fmt.Sprintf(`
resource "davinci_variable" "%[1]s" {
environment_id = pingone_environment.%[1]s.id
name = "testVariable2"
context = "company"
description = "testVariable2 description"
type = "string"
value = davinci_variable.%[1]s-company.id
}
%s`, resourceName, testAccResourceVariable_StaticValue_Hcl(resourceName, name, withBootstrapConfig, "myVal123"))

return
}

func testAccResourceVariable_UnknownValue2_Hcl(resourceName, name string, withBootstrapConfig bool) (hcl string) {
hcl = fmt.Sprintf(`
resource "davinci_variable" "%[1]s" {
environment_id = pingone_environment.%[1]s.id
name = "testVariable2"
context = "company"
description = "testVariable2 description"
type = "string"
value = davinci_variable.%[1]s-test.id
}
resource "davinci_variable" "%[1]s-test" {
environment_id = pingone_environment.%[1]s.id
name = "testVariable3"
context = "company"
description = "testVariable3 description"
type = "string"
}
%s`, resourceName, testAccResourceVariable_StaticValue_Hcl(resourceName, name, withBootstrapConfig, "myVal123"))

return
}

func testAccResourceVariable_EmptyValue_Hcl(resourceName, name string, withBootstrapConfig bool) (hcl string) {
mainFlowJson, err := acctest.ReadFlowJsonFile("flows/full-basic-vars.json")
if err != nil {
Expand Down

0 comments on commit 1740585

Please sign in to comment.