@@ -301,10 +301,22 @@ func resourceContainerNodePoolCreate(d *schema.ResourceData, meta interface{}) e
301301 timeout := d .Timeout (schema .TimeoutCreate )
302302 startTime := time .Now ()
303303
304- // Set the ID before we attempt to create - that way, if we receive an error but
305- // the resource is created anyway, it will be refreshed on the next call to
306- // apply.
307- d .SetId (fmt .Sprintf ("projects/%s/locations/%s/clusters/%s/nodePools/%s" , nodePoolInfo .project , nodePoolInfo .location , nodePoolInfo .cluster , nodePool .Name ))
304+ // we attempt to prefetch the node pool to make sure it doesn't exist before creation
305+ var id = fmt .Sprintf ("projects/%s/locations/%s/clusters/%s/nodePools/%s" , nodePoolInfo .project , nodePoolInfo .location , nodePoolInfo .cluster , nodePool .Name )
306+ name := getNodePoolName (id )
307+ clusterNodePoolsGetCall := config .NewContainerBetaClient (userAgent ).Projects .Locations .Clusters .NodePools .Get (nodePoolInfo .fullyQualifiedName (name ))
308+ if config .UserProjectOverride {
309+ clusterNodePoolsGetCall .Header ().Add ("X-Goog-User-Project" , nodePoolInfo .project )
310+ }
311+ _ , err = clusterNodePoolsGetCall .Do ()
312+ if err != nil && isGoogleApiErrorWithCode (err , 404 ) {
313+ // Set the ID before we attempt to create if the resource doesn't exist. That
314+ // way, if we receive an error but the resource is created anyway, it will be
315+ // refreshed on the next call to apply.
316+ d .SetId (fmt .Sprintf (id ))
317+ } else if err == nil {
318+ return fmt .Errorf ("resource - %s - already exists" , id )
319+ }
308320
309321 var operation * containerBeta.Operation
310322 err = resource .Retry (timeout , func () * resource.RetryError {
0 commit comments