From 20ce389acdda676c191c196c5aef3c078f357c5a Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Fri, 22 Sep 2023 00:23:22 +0000 Subject: [PATCH] Check if labels field exists in customDiff function (#9043) Signed-off-by: Modular Magician --- .changelog/9043.txt | 3 +++ google/tpgresource/annotations.go | 10 ++++++++++ google/tpgresource/labels.go | 14 ++++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 .changelog/9043.txt diff --git a/.changelog/9043.txt b/.changelog/9043.txt new file mode 100644 index 00000000000..4144fa5efcd --- /dev/null +++ b/.changelog/9043.txt @@ -0,0 +1,3 @@ +```release-note:bug +provider: fixed the bug that labels/annotations field not exists in GA for some resources +``` diff --git a/google/tpgresource/annotations.go b/google/tpgresource/annotations.go index 472ed8d9158..fa90bdf1ada 100644 --- a/google/tpgresource/annotations.go +++ b/google/tpgresource/annotations.go @@ -10,6 +10,11 @@ import ( ) func SetAnnotationsDiff(_ context.Context, d *schema.ResourceDiff, meta interface{}) error { + raw := d.Get("annotations") + if raw == nil { + return nil + } + o, n := d.GetChange("annotations") effectiveAnnotations := d.Get("effective_annotations").(map[string]interface{}) @@ -36,6 +41,11 @@ func SetMetadataAnnotationsDiff(_ context.Context, d *schema.ResourceDiff, meta return nil } + raw := d.Get("metadata.0.annotations") + if raw == nil { + return nil + } + o, n := d.GetChange("metadata.0.annotations") effectiveAnnotations := d.Get("metadata.0.effective_annotations").(map[string]interface{}) diff --git a/google/tpgresource/labels.go b/google/tpgresource/labels.go index 0775c0388b0..680beb57924 100644 --- a/google/tpgresource/labels.go +++ b/google/tpgresource/labels.go @@ -38,7 +38,12 @@ func SetLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta interface{}) terraformLabels[k] = v } - labels := d.Get("labels").(map[string]interface{}) + raw := d.Get("labels") + if raw == nil { + return nil + } + + labels := raw.(map[string]interface{}) for k, v := range labels { terraformLabels[k] = v.(string) } @@ -73,6 +78,11 @@ func SetMetadataLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta inter return nil } + raw := d.Get("metadata.0.labels") + if raw == nil { + return nil + } + config := meta.(*transport_tpg.Config) // Merge provider default labels with the user defined labels in the resource to get terraform managed labels @@ -81,7 +91,7 @@ func SetMetadataLabelsDiff(_ context.Context, d *schema.ResourceDiff, meta inter terraformLabels[k] = v } - labels := d.Get("metadata.0.labels").(map[string]interface{}) + labels := raw.(map[string]interface{}) for k, v := range labels { terraformLabels[k] = v.(string) }