From 19402eb4f0e91590f6b753cdfc35206d04eac301 Mon Sep 17 00:00:00 2001 From: The Magician Date: Wed, 8 May 2019 14:28:37 -0700 Subject: [PATCH] Allow project to be specified when importing instance groups (#3591) Signed-off-by: Modular Magician --- google/resource_compute_instance_group.go | 15 +++++++++----- .../resource_compute_instance_group_test.go | 20 +++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/google/resource_compute_instance_group.go b/google/resource_compute_instance_group.go index dc529604c23..75ea764dc1c 100644 --- a/google/resource_compute_instance_group.go +++ b/google/resource_compute_instance_group.go @@ -379,12 +379,17 @@ func resourceComputeInstanceGroupDelete(d *schema.ResourceData, meta interface{} func resourceComputeInstanceGroupImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { parts := strings.Split(d.Id(), "/") - if len(parts) != 2 { - return nil, fmt.Errorf("Invalid compute instance group specifier. Expecting {zone}/{name}") + if len(parts) == 2 { + d.Set("zone", parts[0]) + d.Set("name", parts[1]) + } else if len(parts) == 3 { + d.Set("project", parts[0]) + d.Set("zone", parts[1]) + d.Set("name", parts[2]) + d.SetId(parts[1] + "/" + parts[2]) + } else { + return nil, fmt.Errorf("Invalid compute instance group specifier. Expecting {zone}/{name} or {project}/{zone}/{name}") } - d.Set("zone", parts[0]) - d.Set("name", parts[1]) - return []*schema.ResourceData{d}, nil } diff --git a/google/resource_compute_instance_group_test.go b/google/resource_compute_instance_group_test.go index 0ea5f3224ae..4ade46d2c88 100644 --- a/google/resource_compute_instance_group_test.go +++ b/google/resource_compute_instance_group_test.go @@ -15,7 +15,9 @@ func TestAccComputeInstanceGroup_basic(t *testing.T) { t.Parallel() var instanceGroup compute.InstanceGroup + var resourceName = "google_compute_instance_group.basic" var instanceName = fmt.Sprintf("instancegroup-test-%s", acctest.RandString(10)) + var zone = "us-central1-c" resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -23,7 +25,7 @@ func TestAccComputeInstanceGroup_basic(t *testing.T) { CheckDestroy: testAccComputeInstanceGroup_destroy, Steps: []resource.TestStep{ { - Config: testAccComputeInstanceGroup_basic(instanceName), + Config: testAccComputeInstanceGroup_basic(zone, instanceName), Check: resource.ComposeTestCheckFunc( testAccComputeInstanceGroup_exists( "google_compute_instance_group.basic", &instanceGroup), @@ -32,9 +34,15 @@ func TestAccComputeInstanceGroup_basic(t *testing.T) { ), }, { - ResourceName: "google_compute_instance_group.basic", + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: resourceName, ImportState: true, ImportStateVerify: true, + ImportStateId: fmt.Sprintf("%s/%s/%s", getTestProjectFromEnv(), zone, instanceName), }, }, }) @@ -307,7 +315,7 @@ func testAccComputeInstanceGroup_hasCorrectNetwork(nInstanceGroup string, nNetwo } } -func testAccComputeInstanceGroup_basic(instance string) string { +func testAccComputeInstanceGroup_basic(zone, instance string) string { return fmt.Sprintf(` data "google_compute_image" "my_image" { family = "debian-9" @@ -334,7 +342,7 @@ func testAccComputeInstanceGroup_basic(instance string) string { resource "google_compute_instance_group" "basic" { description = "Terraform test instance group" name = "%s" - zone = "us-central1-c" + zone = "%s" instances = [ "${google_compute_instance.ig_instance.self_link}" ] named_port { name = "http" @@ -349,7 +357,7 @@ func testAccComputeInstanceGroup_basic(instance string) string { resource "google_compute_instance_group" "empty" { description = "Terraform test instance group empty" name = "%s-empty" - zone = "us-central1-c" + zone = "%s" named_port { name = "http" port = "8080" @@ -358,7 +366,7 @@ func testAccComputeInstanceGroup_basic(instance string) string { name = "https" port = "8443" } - }`, instance, instance, instance) + }`, instance, instance, zone, instance, zone) } func testAccComputeInstanceGroup_rename(instance, instanceGroup, backend, health string) string {