Skip to content

Commit b7f2025

Browse files
dtan4danawillow
authored andcommitted
Add project to container_node_pool import name (#1653)
## What As well as #1282 , make `resource_container_node_pool` importer accept `{project}/{zone}/{cluster}/{name}` format to specify the project where the node pool belongs to actually. ## Why Sometimes I want to import container pool in different project from default SA's. However, currently there is no way to specify project the target node pool belongs to, Terraform tries to retrieve node pool from SA's project, then it fails due to `You cannot import non-existent resources using Terraform import.` error.
1 parent 502e617 commit b7f2025

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

google/resource_container_node_pool.go

+25-11
Original file line numberDiff line numberDiff line change
@@ -386,19 +386,33 @@ func resourceContainerNodePoolExists(d *schema.ResourceData, meta interface{}) (
386386

387387
func resourceContainerNodePoolStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
388388
parts := strings.Split(d.Id(), "/")
389-
if len(parts) != 3 {
390-
return nil, fmt.Errorf("Invalid container cluster specifier. Expecting {zone}/{cluster}/{name}")
391-
}
392389

393-
location := parts[0]
394-
if isZone(location) {
395-
d.Set("zone", location)
396-
} else {
397-
d.Set("region", location)
398-
}
390+
switch len(parts) {
391+
case 3:
392+
location := parts[0]
393+
if isZone(location) {
394+
d.Set("zone", location)
395+
} else {
396+
d.Set("region", location)
397+
}
399398

400-
d.Set("cluster", parts[1])
401-
d.Set("name", parts[2])
399+
d.Set("cluster", parts[1])
400+
d.Set("name", parts[2])
401+
case 4:
402+
d.Set("project", parts[0])
403+
404+
location := parts[1]
405+
if isZone(location) {
406+
d.Set("zone", location)
407+
} else {
408+
d.Set("region", location)
409+
}
410+
411+
d.Set("cluster", parts[2])
412+
d.Set("name", parts[3])
413+
default:
414+
return nil, fmt.Errorf("Invalid container cluster specifier. Expecting {zone}/{cluster}/{name} or {project}/{zone}/{cluster}/{name}")
415+
}
402416

403417
return []*schema.ResourceData{d}, nil
404418
}

website/docs/r/container_node_pool.html.markdown

+4-1
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,11 @@ The `management` block supports:
163163

164164
## Import
165165

166-
Node pools can be imported using the `zone`, `cluster` and `name`, e.g.
166+
Node pools can be imported using the `project`, `zone`, `cluster` and `name`. If
167+
the project is omitted, the default provider value will be used. Examples:
167168

168169
```
170+
$ terraform import google_container_node_pool.mainpool my-gcp-project/us-east1-a/my-cluster/main-pool
171+
169172
$ terraform import google_container_node_pool.mainpool us-east1-a/my-cluster/main-pool
170173
```

0 commit comments

Comments
 (0)