We are migrating from SDKv2 to Plugin Framework. In SDKv2 implementation of a resource we have a schema.TypeSet which is Optional and Computed.
This means the value of this set could be different than what the user might have in the config. For example, if the user did not provide this set in the configuration, the API could possibly return the set with one default element object, or another case can be, if user specified 2 elements the API can possibly return 3 elements during.
In the migrated resource, we specified this Set as a NestedBlock:
"api_keys": schema.SetNestedBlock{
NestedObject: schema.NestedBlockObject{
Attributes: map[string]schema.Attribute{
"api_key_id": schema.StringAttribute{
Required: true,
},
"role_names": schema.SetAttribute{
Required: true,
ElementType: types.StringType,
},
},
},
PlanModifiers: []planmodifier.Set{
},
},
- Do I need to mark the block as Computed and Optional? If so, how can I do that? My understanding is blocks don’t support those attributes.
- In the Create handler of the resource, how do I handle the different incoming values/elements of this resource than the configuration to avoid this error:
│ Error: Provider produced inconsistent result after apply
│ When applying changes to ... provider ... produced an unexpected new value: .api_keys: block set length changed from 2 to 3.
- How do I handle this Set during Read handler such that every time user runs
terraform plan
it doesn’t mark this attribute as changed?
I believe there is a way to do this using PlanModifiers but I’m not sure how.
Please let us know how this should be approached, would really appreciate an example.
Thank you!