Skip to content

Prevent panic when user specifies invalid ID during import fix #72 #73

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

Merged
merged 1 commit into from
Jan 29, 2021
Merged
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
5 changes: 5 additions & 0 deletions kustomize/resource_kustomization.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions kustomize/resource_kustomization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kustomize
import (
"context"
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
Expand Down Expand Up @@ -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
Expand Down