Skip to content

Commit b5c32cf

Browse files
fix: (storage) fix inconsistent plan issue for custome_attributes field (GoogleCloudPlatform#17200)
1 parent e893ef8 commit b5c32cf

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

mmv1/third_party/terraform/services/storage/fw_resource_storage_notification.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
9+
"github.com/hashicorp/terraform-plugin-framework/attr"
910
"github.com/hashicorp/terraform-plugin-framework/diag"
1011
"github.com/hashicorp/terraform-plugin-framework/path"
1112
"github.com/hashicorp/terraform-plugin-framework/resource"
@@ -308,9 +309,22 @@ func (r *storageNotificationResource) refresh(ctx context.Context, model *storag
308309
model.EventTypes, eventTypesDiags = types.SetValueFrom(ctx, types.StringType, res.EventTypes)
309310
diags.Append(eventTypesDiags...)
310311

311-
var customAttrsDiags diag.Diagnostics
312-
model.CustomAttributes, customAttrsDiags = types.MapValueFrom(ctx, types.StringType, res.CustomAttributes)
313-
diags.Append(customAttrsDiags...)
312+
// Fix for "Inconsistent result after apply" regarding empty maps
313+
if res.CustomAttributes == nil {
314+
// If the API returns nil but the user has an empty map {} configured,
315+
// we keep it as an empty map to avoid the provider inconsistency error.
316+
if !model.CustomAttributes.IsNull() && !model.CustomAttributes.IsUnknown() {
317+
emptyMap, _ := types.MapValue(types.StringType, map[string]attr.Value{})
318+
model.CustomAttributes = emptyMap
319+
} else {
320+
model.CustomAttributes = types.MapNull(types.StringType)
321+
}
322+
} else {
323+
// API returned actual values, so we map them normally
324+
var customAttrsDiags diag.Diagnostics
325+
model.CustomAttributes, customAttrsDiags = types.MapValueFrom(ctx, types.StringType, res.CustomAttributes)
326+
diags.Append(customAttrsDiags...)
327+
}
314328

315329
return !diags.HasError()
316330
}

mmv1/third_party/terraform/services/storage/resource_storage_notification_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,11 @@ resource "google_pubsub_topic_iam_binding" "binding" {
211211
}
212212
213213
resource "google_storage_notification" "notification" {
214-
bucket = google_storage_bucket.bucket.name
215-
payload_format = "JSON_API_V1"
216-
topic = google_pubsub_topic.topic.id
217-
depends_on = [google_pubsub_topic_iam_binding.binding]
214+
bucket = google_storage_bucket.bucket.name
215+
payload_format = "JSON_API_V1"
216+
topic = google_pubsub_topic.topic.id
217+
depends_on = [google_pubsub_topic_iam_binding.binding]
218+
custom_attributes = {}
218219
}
219220
220221
resource "google_storage_notification" "notification_with_prefix" {

0 commit comments

Comments
 (0)