From e1af0781309d6ee76f62d35d762096cde3e5b5d5 Mon Sep 17 00:00:00 2001 From: = Date: Mon, 11 Sep 2017 08:56:14 -0600 Subject: [PATCH] Add test for mistyped id --- azurerm/import_arm_public_ip_test.go | 27 +++++++++++++++++++++++++++ azurerm/resource_arm_public_ip.go | 12 +++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/azurerm/import_arm_public_ip_test.go b/azurerm/import_arm_public_ip_test.go index 0755c2350905..f1e5f1e03a49 100644 --- a/azurerm/import_arm_public_ip_test.go +++ b/azurerm/import_arm_public_ip_test.go @@ -1,6 +1,9 @@ package azurerm import ( + "fmt" + "os" + "regexp" "testing" "github.com/hashicorp/terraform/helper/acctest" @@ -29,3 +32,27 @@ func TestAccAzureRMPublicIpStatic_importBasic(t *testing.T) { }, }) } + +func TestAccAzureRMPublicIpStatic_importIdError(t *testing.T) { + resourceName := "azurerm_public_ip.test" + + ri := acctest.RandInt() + config := testAccAzureRMPublicIPStatic_basic(ri, testLocation()) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMPublicIpDestroy, + Steps: []resource.TestStep{ + { + Config: config, + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateId: fmt.Sprintf("/subscriptions/%s/resourceGroups/acctestRG-%d/providers/Microsoft.Network/publicIPAdresses/acctestpublicip-%d", os.Getenv("ARM_SUBSCRIPTION_ID"), ri, ri), + ExpectError: regexp.MustCompile("Error parsing supplied resource id."), + }, + }, + }) +} diff --git a/azurerm/resource_arm_public_ip.go b/azurerm/resource_arm_public_ip.go index 4526f19c9448..549e7381f387 100644 --- a/azurerm/resource_arm_public_ip.go +++ b/azurerm/resource_arm_public_ip.go @@ -18,7 +18,17 @@ func resourceArmPublicIp() *schema.Resource { Update: resourceArmPublicIpCreate, Delete: resourceArmPublicIpDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return nil, err + } + name := id.Path["publicIPAddresses"] + if name == "" { + return nil, fmt.Errorf("Error parsing supplied resource id. Please check it and rerun:\n %s", d.Id()) + } + return []*schema.ResourceData{d}, nil + }, }, Schema: map[string]*schema.Schema{