Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling a null response for certain values from PCA #2137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/service/core/core_instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,9 @@ func (s *CoreInstanceResourceCrud) SetData() error {

if s.Res.ExtendedMetadata != nil {
s.D.Set("extended_metadata", tfresource.GenericMapToJsonMap(s.Res.ExtendedMetadata))
} else {
extended_metadata := map[string]interface{}{}
s.D.Set("extended_metadata", extended_metadata)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does setting the values to an empty map fix the issue you are facing?

Copy link
Author

@cam-mclaren cam-mclaren Jun 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi thanks for reviewing.

Problem Description

At the moment we are having trouble with aggregate data types like maps being returned as null by the PCA API. When stored as null in the state file these aggregate data type variables report a change as being required every plan despite the values not being specified in our terraform configuration.

To give a concrete example, below I have an image of the state file for vm instance created with the OCI Terraform provider on a Private Cloud Appliance. You can see that the value for system tags is set to null which is due to the behavior of the PCA API.
image
Image 1: system_tags set to null

Whenever this system_tags variable is set to null in the state file a terraform plan will always report (known after apply) as the value it will be changed to. With the null value Terraform seems to always detect changes to be made despite no actual Terraform configuration changes. This is okay when dealing with a single vm instance. However larger scales these reported changes become very problematic and make it very difficult to determine legitimate drift in your configuration.
image
Image 2: terraform plan reports the system_tags value as requiring a change despite its value being unspecified.

We can fix this false system_tags change by setting it to an empty map {} through a manual edit of the state file. Below I have an image of the state file after being manually edited.
image
Image 3: system_tags is manually set to {} in the state file.

The resulting terraform plan no longer reports the system_tags attribute as requiring a change as can be seen in Image 4.
image
Image 4: There is no required change detected for system_tags.

We can fix the extended_metadata attribute in the same way. By changing it in the state file from null to {} this will remove the false change from the plan entirely.
image
Image 5: After setting the values in the state file from null to {} no change is detected.

As seen above setting the values of extended_metadata and system_tags to a value of empty map entirely removes the problem of the provider falsely reporting changes for PCA users.

I suspect for those without a PCA to access you will be able to confirm this problematic behavior from null by manually editing a state file for a vm instance on OCI so that its system_tags attribute is set to null.

}

if s.Res.FaultDomain != nil {
Expand Down Expand Up @@ -1728,6 +1731,9 @@ func (s *CoreInstanceResourceCrud) SetData() error {

if s.Res.SystemTags != nil {
s.D.Set("system_tags", tfresource.SystemTagsToMap(s.Res.SystemTags))
} else {
system_tags := map[string]interface{}{}
s.D.Set("system_tags", system_tags)
}

if s.Res.TimeCreated != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/service/core/core_volume_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,9 @@ func (s *CoreVolumeResourceCrud) SetData() error {

if s.Res.SystemTags != nil {
s.D.Set("system_tags", tfresource.SystemTagsToMap(s.Res.SystemTags))
} else {
system_tags := map[string]interface{}{}
s.D.Set("system_tags", system_tags)
}

if s.Res.TimeCreated != nil {
Expand Down