Skip to content

Commit 665b641

Browse files
[Upstream] Standardize google_compute_instance import (#3329) (#1933)
* 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. * 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. Co-authored-by: Jazel Canseco <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Jazel Canseco <[email protected]>
1 parent 98fb374 commit 665b641

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

.changelog/3329.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: Added more import formats for `google_compute_instance`
3+
```

google-beta/resource_compute_instance.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1773,16 +1773,21 @@ func resourceComputeInstanceDelete(d *schema.ResourceData, meta interface{}) err
17731773
}
17741774

17751775
func resourceComputeInstanceImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
1776-
parts := strings.Split(d.Id(), "/")
1777-
1778-
if len(parts) != 3 {
1779-
return nil, fmt.Errorf("Invalid import id %q. Expecting {project}/{zone}/{instance_name}", d.Id())
1776+
config := meta.(*Config)
1777+
if err := parseImportId([]string{
1778+
"projects/(?P<project>[^/]+)/zones/(?P<zone>[^/]+)/instances/(?P<name>[^/]+)",
1779+
"(?P<project>[^/]+)/(?P<zone>[^/]+)/(?P<name>[^/]+)",
1780+
"(?P<name>[^/]+)",
1781+
}, d, config); err != nil {
1782+
return nil, err
17801783
}
17811784

1782-
d.Set("project", parts[0])
1783-
d.Set("zone", parts[1])
1784-
d.Set("name", parts[2])
1785-
d.SetId(fmt.Sprintf("projects/%s/zones/%s/instances/%s", parts[0], parts[1], parts[2]))
1785+
// Replace import id for the resource id
1786+
id, err := replaceVars(d, config, "projects/{{project}}/zones/{{zone}}/instances/{{name}}")
1787+
if err != nil {
1788+
return nil, fmt.Errorf("Error constructing id: %s", err)
1789+
}
1790+
d.SetId(id)
17861791

17871792
return []*schema.ResourceData{d}, nil
17881793
}

website/docs/r/compute_instance.html.markdown

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,12 @@ This resource provides the following
394394
-> **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.
395395

396396

397-
Instances can be imported using the `project`, `zone` and `name`, e.g.
397+
Instances can be imported using any of these accepted formats:
398398

399399
```
400-
$ terraform import google_compute_instance.default gcp-project/us-central1-a/test
400+
$ terraform import google_compute_instance.default projects/{{project}}/zones/{{zone}}/instances/{{name}}
401+
$ terraform import google_compute_instance.default {{project}}/{{zone}}/{{name}}
402+
$ terraform import google_compute_instance.default {{name}}
401403
```
402404

403405
[custom-vm-types]: https://cloud.google.com/dataproc/docs/concepts/compute/custom-machine-types

0 commit comments

Comments
 (0)