Terraform
Object Known Value Checks
The known value checks that are available for object values are:
ObjectExact
Check
The ObjectExact check tests that a resource attribute, or output value has a matching collection of attribute name, and attribute values.
Example usage of ObjectExact in an ExpectKnownValue plan check.
func TestExpectKnownValue_CheckPlan_Object(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed object attribute named "computed_attribute"
Config: `resource "test_resource" "one" {}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectKnownValue(
"test_resource.one",
tfjsonpath.New("computed_attribute"),
knownvalue.ObjectExact(map[string]knownvalue.Check{
"attr1": knownvalue.StringExact("value1"),
"attr2": knownvalue.StringExact("value2"),
}),
),
},
},
},
},
})
}
ObjectPartial
Check
The ObjectPartial check tests that a resource attribute, or output value has matching attribute values for the specified attribute names.
Example usage of ObjectPartial in an ExpectKnownValue plan check.
In this example, only the attribute value associated with the attribute name attr1
within the object is checked.
func TestExpectKnownValue_CheckPlan_ObjectPartial(t *testing.T) {
t.Parallel()
resource.Test(t, resource.TestCase{
// Provider definition omitted.
Steps: []resource.TestStep{
{
// Example resource containing a computed object attribute named "computed_attribute"
Config: `resource "test_resource" "one" {}`,
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectKnownValue(
"test_resource.one",
tfjsonpath.New("computed_attribute"),
knownvalue.ObjectPartial(map[string]knownvalue.Check{
"attr1": knownvalue.StringExact("value1"),
}),
),
},
},
},
},
})
}