Hi @maastha,
You are correct that switching to an attribute would represent a breaking change. You are also correct that the link I supplied for Blocks with Computed Fields is for “computed-only ” blocks, my apologies.
If you are using a SetNestedBlock
as you have illustrated, then the block is already effectively optional. If api_keys
is not defined in the Terraform configuration that is supplied then an empty set will be written into state.
However, if you need to be able to handle instances in which api_keys
is supplied in the Terraform configuration and you have values returned from your API which you need to store then you will need to add an additional computed attribute or attributes to hold this data as Terraform will not allow modification of data that was supplied through the configuration. Modification of this sort is what gives rise to the error that you reported (e.g., produced an unexpected new value....
).
Alternatively, if you do not need to store the additional information that is returned by your API call you can filter it out so that the only data that is written to state is the data supplied in the Terraform configuration.
In short:
-
You do not need to mark the block as
Optional
, the block is already effectively optional. If you have data that is “computed” (i.e., returned from an API call) that needs to be stored then you will need to alter your schema to add a computed attribute or attributes that will be used to hold this data. If you do not need to persist data that is returned from an API call you can filter it out to make sure that it doesn’t mutate the value inapi_keys
. -
You can avoid errors such as
unexpected new value: ...
by making sure that you do not attempt to mutate any value forapi_keys
that has been set in the configuration. Again you will need to use an additional computed attribute or attributes for data returned from your API call. -
You can avoid
terraform plan
from marking the attribute as changed by filtering out the data returned from the API call.
Note that addition of a computed attribute or attributes represents a breaking change but this may be unavoidable depending upon whether you need to persist the data returned from your API call or not.