Skip to content

Commit 61e9b53

Browse files
container_node_pool : only store in state on create if actually creating the resource (#4904) (#3378)
Signed-off-by: Modular Magician <[email protected]>
1 parent 4b5f7f2 commit 61e9b53

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

.changelog/4904.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
container: fixed issue where creating a node pool with a name that already exists would import that resource. `google_container_node_pool`
3+
```

google-beta/resource_container_node_pool.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

google-beta/resource_dataproc_cluster_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1515

16-
dataproc "google.golang.org/api/dataproc/v1beta2"
1716
"google.golang.org/api/googleapi"
17+
18+
dataproc "google.golang.org/api/dataproc/v1beta2"
1819
)
1920

2021
func TestDataprocExtractInitTimeout(t *testing.T) {

google-beta/resource_gke_hub_feature_membership_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"testing"
77

8-
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
8+
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
99
gkehub "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/gkehub/beta"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

0 commit comments

Comments
 (0)