Skip to content

Commit

Permalink
container_node_pool : only store in state on create if actually creat…
Browse files Browse the repository at this point in the history
…ing the resource (#4904) (#9424)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jun 23, 2021
1 parent 88ccbc3 commit eb82ee9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/4904.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
container: fixed issue where creating a node pool with a name that already exists would import that resource. `google_container_node_pool`
```
20 changes: 16 additions & 4 deletions google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,22 @@ func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) e
timeout := d.Timeout(schema.TimeoutCreate)
startTime := time.Now()

// Set the ID before we attempt to create - that way, if we receive an error but
// the resource is created anyway, it will be refreshed on the next call to
// apply.
d.SetId(fmt.Sprintf("projects/%s/locations/%s/clusters/%s/nodePools/%s", nodePoolInfo.project, nodePoolInfo.location, nodePoolInfo.cluster, nodePool.Name))
// we attempt to prefetch the node pool to make sure it doesn't exist before creation
var id = fmt.Sprintf("projects/%s/locations/%s/clusters/%s/nodePools/%s", nodePoolInfo.project, nodePoolInfo.location, nodePoolInfo.cluster, nodePool.Name)
name := getNodePoolName(id)
clusterNodePoolsGetCall := config.NewContainerBetaClient(userAgent).Projects.Locations.Clusters.NodePools.Get(nodePoolInfo.fullyQualifiedName(name))
if config.UserProjectOverride {
clusterNodePoolsGetCall.Header().Add("X-Goog-User-Project", nodePoolInfo.project)
}
_, err = clusterNodePoolsGetCall.Do()
if err != nil && isGoogleApiErrorWithCode(err, 404) {
// Set the ID before we attempt to create if the resource doesn't exist. That
// way, if we receive an error but the resource is created anyway, it will be
// refreshed on the next call to apply.
d.SetId(fmt.Sprintf(id))
} else if err == nil {
return fmt.Errorf("resource - %s - already exists", id)
}

var operation *containerBeta.Operation
err = resource.Retry(timeout, func() *resource.RetryError {
Expand Down

0 comments on commit eb82ee9

Please sign in to comment.