Skip to content

Commit

Permalink
Allow project to be specified when importing instance groups (#3591)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and Ty Larrabee committed May 8, 2019
1 parent cbc5344 commit 19402eb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
15 changes: 10 additions & 5 deletions google/resource_compute_instance_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
20 changes: 14 additions & 6 deletions google/resource_compute_instance_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ 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) },
Providers: testAccProviders,
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),
Expand All @@ -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),
},
},
})
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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 {
Expand Down

0 comments on commit 19402eb

Please sign in to comment.