I’m using the terraform plugin framework.
I have this resource schema:
func (g *GSI) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
...
"with": schema.SingleNestedAttribute{
Optional: true,
Computed: true,
Default: objectdefault.StaticValue(
types.ObjectValueMust(
map[string]attr.Type{
"defer_build": types.BoolType,
"num_replica": types.Int64Type,
"num_partition": types.Int64Type,
},
map[string]attr.Value{
"defer_build": types.BoolValue(false),
"num_replica": types.Int64Value(0),
"num_partition": types.Int64Null(),
},
)),
Attributes: map[string]schema.Attribute{
"defer_build": schema.BoolAttribute{
Optional: true,
Computed: true,
},
"num_replica": schema.Int64Attribute{
Optional: true,
Computed: true,
},
"num_partition": schema.Int64Attribute{
Optional: true,
Validators: []validator.Int64{
int64validator.AtLeast(1),
},
},
},
}
}
}
This doesn’t work. For example, if I omit num_replica
from the HCL, the state file shows a value of null
, but I’m expecting 0.
Also I’m a bit nervous about using types.ObjectValueMust
. The doc says:
This creation function is only recommended to create Object values which will not potentially affect practitioners, such as testing, or exhaustively tested provider logic.
Is there a way to do this without types.ObjectValueMust
?