Skip to content

Import Container Nodepool doesn't work with 4 parts as documented #2021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jamielennox opened this issue Sep 11, 2018 · 5 comments
Closed

Import Container Nodepool doesn't work with 4 parts as documented #2021

jamielennox opened this issue Sep 11, 2018 · 5 comments

Comments

@jamielennox
Copy link
Contributor

The import documentation says we can import nodepools like:

$ terraform import google_container_node_pool.mainpool my-gcp-project/us-east1-a/my-cluster/main-pool

$ terraform import google_container_node_pool.mainpool us-east1-a/my-cluster/main-pool

I'm using the 4 part version and the import step completes successfully. Terraform then runs the exists check and in my case the d.Id() is:

<project>/<zone>/<cluster>/<nodepool-name>

which when run through:

name := getNodePoolName(d.Id())
func getNodePoolName(id string) string {
	// name can be specified with name, name_prefix, or neither, so read it from the id.
	return strings.Split(id, "/")[2]
}

means that it's returning my cluster name instead of the nodepool name.

I can then see it try and check the existance of the nodepool:

GET /v1beta1/projects/<project>/locations/<zone>/clusters/<cluster>/nodePools/<cluster>?alt=json

which obviously fails and prevents my import from working. In my case I don't have a provider project configured so I can't use the 3 param form.

google_container_node_pool.nodepool: project: required field is not set

I'll investigate a little further, but somewhere along the line the ID you end up with from import is wrong - or the getNodePoolName function needs to be updated to support it. I'm not sure which direction you're going in here.

I'm running master 65669fc.

@jamielennox
Copy link
Contributor Author

This quick patch is getting me past my problem. I don't know if it's actually a good idea or not.

From 0566c6abb98399a4547c0466a1fc50c427730859 Mon Sep 17 00:00:00 2001
From: Jamie Lennox <[email protected]>
Date: Tue, 11 Sep 2018 12:33:24 +1000
Subject: [PATCH] Support nodepool container name having 3 or 4 segments

Using nodepool import lets us use a 3 or 4 segment version. When you use
the 4 segment version the ID that gets created contains more information
than a regular ID and so we can't use the same portion of the path to
identify name.

Fix it temporarily to unblock myself until upstream confirms a
direction.
---
 google/resource_container_node_pool.go | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/google/resource_container_node_pool.go b/google/resource_container_node_pool.go
index 17c4b482..d0307c9d 100644
--- a/google/resource_container_node_pool.go
+++ b/google/resource_container_node_pool.go
@@ -724,5 +724,15 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node

 func getNodePoolName(id string) string {
 	// name can be specified with name, name_prefix, or neither, so read it from the id.
-	return strings.Split(id, "/")[2]
+	parts := strings.Split(id, "/")
+
+	switch len(parts) {
+	case 3:
+		return parts[2]
+	case 4:
+		return parts[3]
+	default:
+		log.Printf("[ERR] Unexpected nodepool name segments: %s", id)
+		return ""
+	}
 }
--
2.17.1

@jamielennox
Copy link
Contributor Author

Originally created in #1653 notifying @dtan4 and @danawillow based on that. (Hope that's ok)

@jamielennox
Copy link
Contributor Author

Actually looking at this a bit later, almost certainly what we want is a:

d.SetId(strings.Join(parts[1:], "/"))

in case 4: of the importer to drop the project from the start and use the standard id.

@danawillow
Copy link
Contributor

I'm going to go ahead and close this as a duplicate of #1846, but thanks for the report! Just a note that if you do have a solution in mind, we'd be happy to look at a PR for it :)

@ghost
Copy link

ghost commented Nov 16, 2018

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 [email protected]. Thanks!

@ghost ghost locked and limited conversation to collaborators Nov 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants