Closed jasmingacic closed 7 years ago
I was able to find what the issue is. Since my TypeSet rules
has one computed field and the other fields are required and optional. If I set only computed values in map[string]interface{}
that gets recorded in .tfstate
. So I've come up with following solution:
func readRules(d *schema.ResourceData, rules []oneandone.FirewallPolicyRule) interface{} {
rawRules := d.Get("rules").([]interface{})
counter := 0
for _, rR := range rawRules {
rawMap := rR.(map[string]interface{})
rawMap["rule_id"] = rules[counter].Id
rawMap["source_ip"] = rules[counter].SourceIp
counter++
}
return rawRules
}
Is there a better solution for this?
Hi @jasminSPC! Sorry for letting this question sit here unanswered for so long.
It looks like you found a reasonable solution, so I'm sure my answer here is pretty academic at this point but I'm going to leave one here for posterity in case anyone else finds this issue in future.
Things get quite complicated for nested structures, as you've seen. We will hopefully address some of these oddities in future versions of Terraform, but for now we usually suggest keeping Computed
and non-Computed
attributes separate, rather than mixing the two in nested structures, since Terraform's current support for reading values from nested sets and lists is rather fraught due to limitations of the interpolation language.
So practically speaking that could mean having a separate top-level attribute rule_ids
that is a Computed
list of strings, whose list element indices correspond with those in the rules
list. This way the list of ids is easier to use with the primitives in the interpolation language: the user of your resource can either refer to a specific element in the list using an expression like oneandone_firewall_policy.example.rule_ids[0]
or to the whole list of ids as oneandone_firewall_policy.example.rule_ids
. The latter would not be possible with the ids nested inside the rule
structure.
It's likely that we'll do some work to improve the collection-wrangling capabilities of the interpolation language in future. For now I'm going to close this.
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
I'm building a third party plugin for Terraform
And I'm running into a following problem:
I've got this resource
In case of the resource
rules
nothing gets persisted into tfstate except for the values I've provided.I have this in my code when i'm setting
rules
Output from looks like this:
As you can see
ips
are stored correctly butrules
are not.What am I doing wrong?