Skip to content
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

Allow project to be specified when importing instance groups #3597

Merged
merged 1 commit into from
May 8, 2019
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
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
3 changes: 2 additions & 1 deletion website/docs/r/compute_instance_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ This resource provides the following

## Import

Instance group can be imported using the `zone` and `name`, e.g.
Instance group can be imported using the `zone` and `name` with an optional `project`, e.g.

```
$ terraform import google_compute_instance_group.webservers us-central1-a/terraform-webservers
$ terraform import google_compute_instance_group.webservers big-project/us-central1-a/terraform-webservers
```
10 changes: 5 additions & 5 deletions website/google.erb
Original file line number Diff line number Diff line change
Expand Up @@ -393,15 +393,15 @@

<li<%= sidebar_current("docs-google-compute-instance-iam") %>>
<a href="/docs/providers/google/r/compute_instance_iam.html">google_compute_instance_iam_binding</a>
</li>
</li>

<li<%= sidebar_current("docs-google-compute-instance-iam") %>>
<li<%= sidebar_current("docs-google-compute-instance-iam") %>>
<a href="/docs/providers/google/r/compute_instance_iam.html">google_compute_instance_iam_member</a>
</li>
</li>

<li<%= sidebar_current("docs-google-compute-instance-iam") %>>
<li<%= sidebar_current("docs-google-compute-instance-iam") %>>
<a href="/docs/providers/google/r/compute_instance_iam.html">google_compute_instance_iam_policy</a>
</li>
</li>

<li<%= sidebar_current("docs-google-compute-instance-from-template") %>>
<a href="/docs/providers/google/r/compute_instance_from_template.html">google_compute_instance_from_template</a>
Expand Down