From 79dfbcde34f263fff86eb40ecebba28a6344b4dd Mon Sep 17 00:00:00 2001 From: Philipp Strube Date: Fri, 29 Jan 2021 12:18:18 +0100 Subject: [PATCH] Prevent panic when user specifies invalid ID during import fix #72 --- kustomize/resource_kustomization.go | 5 +++++ kustomize/resource_kustomization_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/kustomize/resource_kustomization.go b/kustomize/resource_kustomization.go index 699419a..4775912 100644 --- a/kustomize/resource_kustomization.go +++ b/kustomize/resource_kustomization.go @@ -477,6 +477,11 @@ func kustomizationResourceImport(d *schema.ResourceData, m interface{}) ([]*sche client := m.(*Config).Client cgvk := m.(*Config).CachedGroupVersionKind + // "|" must match resid.separator + if len(strings.Split(d.Id(), "|")) != 3 { + return nil, logError(fmt.Errorf("invalid ID: %q, valid IDs look like: \"~G_v1_Namespace|~X|example\"", d.Id())) + } + rid := resid.FromString(d.Id()) namespace := rid.Namespace diff --git a/kustomize/resource_kustomization_test.go b/kustomize/resource_kustomization_test.go index af5b32c..425cc11 100644 --- a/kustomize/resource_kustomization_test.go +++ b/kustomize/resource_kustomization_test.go @@ -3,6 +3,7 @@ package kustomize import ( "context" "fmt" + "regexp" "testing" "github.com/hashicorp/terraform-plugin-sdk/helper/resource" @@ -96,6 +97,29 @@ resource "kustomization_resource" "dep2" { ` } +// +// +// Import test invalid id +func TestAccResourceKustomization_importInvalidID(t *testing.T) { + + resource.Test(t, resource.TestCase{ + //PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + // + // + // Test state import + { + ResourceName: "kustomization_resource.test[\"~G_v1_Namespace|~X|test-basic\"]", + ImportStateId: "invalidID", + ImportState: true, + ImportStateVerify: true, + ExpectError: regexp.MustCompile("invalid ID: \"invalidID\", valid IDs look like: \"~G_v1_Namespace|~X|example\""), + }, + }, + }) +} + // // // Update_Inplace Test