'Shall I use d.Set() for a computer attribute?
I'm adding a new resource to Terraform Provider using Terraform SDKv2 (can't use the new Terraform Plugin).
One of new resource's attributes is the following:
"foo_enabled": {
Type: schema.TypeBool,
// Computed: true,
Optional: true,
Default: false,
},
I absolutely want this attribute to be optional and have a default value since it's not very important and only applicable for advanced users.
In my code, I only use d.Get("foo_enabled") in resourceCreate() and that's it. However when I started testing it, I figured if I don't set it to any value in Terraform configuration, it's being saved as foo_enabled = null in TF state file, when I set it to false / true in Terraform configuration, then it's being saved as foo_enabled = false / true (not 100% sure but I think it's accurate, don't remember what the value is after importing -- maybe it's false / true for create but null after importing).
To make things worse, I run into an issue with testing:
{
// https://www.terraform.io/docs/extend/resources/import.html
ResourceName: ...,
ImportState: true,
ImportStateVerify: true,
},
It used to work but now ImportStateVerify fails with actual: null, expected: true / false.
The main point is I'd rather have it as explicit true / false in any scenario to avoid drifts null -> false, or false -> null.
So I'm thinking about setting it in resourceRead() like
foo := d.Get("foo_enabled").(bool)
d.Set("foo_enabled", foo)
and marking a resource as Computed to allow these d.Set() calls. Is it a reasonable idea?
Related: Shall I set an empty string computed string attribute for Terraform resource?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
