From 9f7fa0c6b0962893c78c72bb69b1063923e271cb Mon Sep 17 00:00:00 2001 From: Zhenhua Li Date: Thu, 21 Sep 2023 15:47:27 -0700 Subject: [PATCH] Check if labels field exists in customDiff function --- .../terraform/tpgresource/annotations.go | 10 ++++++++++ mmv1/third_party/terraform/tpgresource/labels.go | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/mmv1/third_party/terraform/tpgresource/annotations.go b/mmv1/third_party/terraform/tpgresource/annotations.go index f6a14211c8e8..183356e1cb44 100644 --- a/mmv1/third_party/terraform/tpgresource/annotations.go +++ b/mmv1/third_party/terraform/tpgresource/annotations.go @@ -8,6 +8,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{}) @@ -34,6 +39,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/mmv1/third_party/terraform/tpgresource/labels.go b/mmv1/third_party/terraform/tpgresource/labels.go index c07cdfd486a5..0a241e14e1fe 100644 --- a/mmv1/third_party/terraform/tpgresource/labels.go +++ b/mmv1/third_party/terraform/tpgresource/labels.go @@ -36,7 +36,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) } @@ -71,6 +76,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 @@ -79,7 +89,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) }