From 8309830cb00622cfd5f3d068904a7649b1a207c2 Mon Sep 17 00:00:00 2001 From: Jazel Canseco Date: Wed, 1 Apr 2020 06:31:24 -0700 Subject: [PATCH] Standardize google_compute_instance import ID This patch changes the import ID of google_compute_instance to have a format that is more consistent with other resources like google_compute_subnetwork while still supporting the previously supported import ID of {{project}}/{{zone}}/{{name}} in order to have backwards compatibility. --- google/resource_compute_instance.go | 22 +++++++++++-------- website/docs/r/compute_instance.html.markdown | 6 +++-- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/google/resource_compute_instance.go b/google/resource_compute_instance.go index 2dc1bacdd48..37bf750b261 100644 --- a/google/resource_compute_instance.go +++ b/google/resource_compute_instance.go @@ -1773,16 +1773,20 @@ func resourceComputeInstanceDelete(d *schema.ResourceData, meta interface{}) err } func resourceComputeInstanceImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - parts := strings.Split(d.Id(), "/") - - if len(parts) != 3 { - return nil, fmt.Errorf("Invalid import id %q. Expecting {project}/{zone}/{instance_name}", d.Id()) + config := meta.(*Config) + if err := parseImportId([]string{ + "projects/(?P[^/]+)/zones/(?P[^/]+)/instances/(?P[^/]+)", + "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", + "(?P[^/]+)", + }, d, config); err != nil { + return nil, err } - - d.Set("project", parts[0]) - d.Set("zone", parts[1]) - d.Set("name", parts[2]) - d.SetId(fmt.Sprintf("projects/%s/zones/%s/instances/%s", parts[0], parts[1], parts[2])) + // Replace import id for the resource id + id, err := replaceVars(d, config, "{{project}}/{{zone}}/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) return []*schema.ResourceData{d}, nil } diff --git a/website/docs/r/compute_instance.html.markdown b/website/docs/r/compute_instance.html.markdown index 093d4cd5f9e..66a16b725c8 100644 --- a/website/docs/r/compute_instance.html.markdown +++ b/website/docs/r/compute_instance.html.markdown @@ -394,10 +394,12 @@ This resource provides the following -> **Note:** The `desired_status` field will not be set on import. If you have it set, Terraform will update the field on the next `terraform apply`, bringing your instance to the desired status. -Instances can be imported using the `project`, `zone` and `name`, e.g. +Instances can be imported using any of these accepted formats: ``` -$ terraform import google_compute_instance.default gcp-project/us-central1-a/test +$ terraform import google_compute_instance.default projects/{{project}}/zones/{{zone}}/instances/{{name}} +$ terraform import google_compute_instance.default {{project}}/{{zone}}/{{name}} +$ terraform import google_compute_instance.default {{name}} ``` [custom-vm-types]: https://cloud.google.com/dataproc/docs/concepts/compute/custom-machine-types