From 3def84d2867f9e6d0d7eed65bee3df359731eebb Mon Sep 17 00:00:00 2001 From: Anders Bruun Olsen Date: Thu, 3 Aug 2017 14:53:42 +0200 Subject: [PATCH 1/9] Make google_container_node_pool resources importable. --- google/import_container_node_pool_test.go | 32 +++++++++++++++++++++++ google/resource_container_node_pool.go | 18 +++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 google/import_container_node_pool_test.go diff --git a/google/import_container_node_pool_test.go b/google/import_container_node_pool_test.go new file mode 100644 index 00000000000..d580d97a386 --- /dev/null +++ b/google/import_container_node_pool_test.go @@ -0,0 +1,32 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccGoogleContainerNodePool_import(t *testing.T) { + resourceName := "google_container_node_pool.np" + cluster := fmt.Sprintf("tf-nodepool-test-%s", acctest.RandString(10)) + np := fmt.Sprintf("tf-nodepool-test-%s", acctest.RandString(10)) + conf := testAccContainerNodePool_basic(cluster, np) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckContainerNodePoolDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: conf, + }, + + resource.TestStep{ + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} diff --git a/google/resource_container_node_pool.go b/google/resource_container_node_pool.go index 7fd301612f2..1ad9f2c722c 100644 --- a/google/resource_container_node_pool.go +++ b/google/resource_container_node_pool.go @@ -3,6 +3,7 @@ package google import ( "fmt" "log" + "strings" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" @@ -18,6 +19,10 @@ func resourceContainerNodePool() *schema.Resource { Delete: resourceContainerNodePoolDelete, Exists: resourceContainerNodePoolExists, + Importer: &schema.ResourceImporter{ + State: resourceContainerNodePoolStateImporter, + }, + Schema: map[string]*schema.Schema{ "project": &schema.Schema{ Type: schema.TypeString, @@ -360,3 +365,16 @@ func resourceContainerNodePoolExists(d *schema.ResourceData, meta interface{}) ( } return true, nil } + +func resourceContainerNodePoolStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + parts := strings.Split(d.Id(), "/") + if len(parts) != 3 { + return nil, fmt.Errorf("Invalid container cluster specifier. Expecting {zone}/{cluster}/{name}") + } + + d.Set("zone", parts[0]) + d.Set("cluster", parts[1]) + d.Set("name", parts[2]) + + return []*schema.ResourceData{d}, nil +} From fa58deeb83331e1727f8309b813d58c7c9c97c6d Mon Sep 17 00:00:00 2001 From: Anders Bruun Olsen Date: Sat, 5 Aug 2017 18:44:33 +0200 Subject: [PATCH 2/9] Add website documentation for importing google_container_node_pool. --- website/docs/r/container_node_pool.html.markdown | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/website/docs/r/container_node_pool.html.markdown b/website/docs/r/container_node_pool.html.markdown index 4476013ca19..631b99d6f22 100644 --- a/website/docs/r/container_node_pool.html.markdown +++ b/website/docs/r/container_node_pool.html.markdown @@ -111,3 +111,11 @@ The `autoscaling` block supports: <= `maxNodeCount`. * `maxNodeCount` - (Required) Maximum number of nodes in the NodePool. Must be >= minNodeCount. + +## Import + +Node pools can be imported using the `zone`, `cluster` and `name`, e.g. + +``` +$ terraform import google_container_node_pool.mainpool us-east1-a/my-cluster/main-pool +``` \ No newline at end of file From a5dd0ae6284cfaf46f8b5570a947e2b942dfdfcc Mon Sep 17 00:00:00 2001 From: Anders Bruun Olsen Date: Tue, 5 Sep 2017 12:50:07 +0200 Subject: [PATCH 3/9] Use ImportStatePassthrough importer for pubsub topics. --- google/resource_pubsub_topic.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/google/resource_pubsub_topic.go b/google/resource_pubsub_topic.go index ba78a6f74a1..90a92c9cc97 100644 --- a/google/resource_pubsub_topic.go +++ b/google/resource_pubsub_topic.go @@ -13,6 +13,10 @@ func resourcePubsubTopic() *schema.Resource { Read: resourcePubsubTopicRead, Delete: resourcePubsubTopicDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + Schema: map[string]*schema.Schema{ "name": &schema.Schema{ Type: schema.TypeString, From e0f663478b772705e9278b68396d7fc501fa012b Mon Sep 17 00:00:00 2001 From: Anders Bruun Olsen Date: Wed, 6 Sep 2017 14:13:25 +0200 Subject: [PATCH 4/9] Make pubsub_topic importable. --- google/import_pubsub_topic_test.go | 34 +++++++++++++++++++++++ google/resource_pubsub_topic.go | 19 ++++++++++++- website/docs/r/pubsub_topic.html.markdown | 8 ++++++ 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 google/import_pubsub_topic_test.go diff --git a/google/import_pubsub_topic_test.go b/google/import_pubsub_topic_test.go new file mode 100644 index 00000000000..c3c0e2acccd --- /dev/null +++ b/google/import_pubsub_topic_test.go @@ -0,0 +1,34 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccPubsubTopic_import(t *testing.T) { + topicName := fmt.Sprintf("tf-test-topic-%d", acctest.RandInt()) + conf := fmt.Sprintf(` + resource "google_pubsub_topic" "tf-test" { + name = "%s" + }`, topicName) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckPubsubTopicDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: conf, + }, + resource.TestStep{ + ResourceName: "google_pubsub_topic.tf-test", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"force_destroy"}, + }, + }, + }) +} diff --git a/google/resource_pubsub_topic.go b/google/resource_pubsub_topic.go index 90a92c9cc97..51b164d1d04 100644 --- a/google/resource_pubsub_topic.go +++ b/google/resource_pubsub_topic.go @@ -14,7 +14,7 @@ func resourcePubsubTopic() *schema.Resource { Delete: resourcePubsubTopicDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourcePubsubTopicStateImporter, }, Schema: map[string]*schema.Schema{ @@ -80,3 +80,20 @@ func resourcePubsubTopicDelete(d *schema.ResourceData, meta interface{}) error { return nil } + +func resourcePubsubTopicStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return nil, err + } + + name := d.Id() + id := fmt.Sprintf("projects/%s/topics/%s", project, name) + + d.Set("name", name) + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} diff --git a/website/docs/r/pubsub_topic.html.markdown b/website/docs/r/pubsub_topic.html.markdown index ceb3aebc22c..7ae22342d16 100644 --- a/website/docs/r/pubsub_topic.html.markdown +++ b/website/docs/r/pubsub_topic.html.markdown @@ -36,3 +36,11 @@ The following arguments are supported: ## Attributes Reference Only the arguments listed above are exposed as attributes. + +## Import + +Pubsub topics can be imported using the `name`, e.g. + +``` +$ terraform import google_pubsub_topic.mytopic mytopic +``` From d2c0f3436b8d353d3bf75d6a3465056c4893451a Mon Sep 17 00:00:00 2001 From: Anders Bruun Olsen Date: Thu, 7 Sep 2017 10:43:19 +0200 Subject: [PATCH 5/9] Don't set name during import of pubsub topic, but do it during read instead. --- google/resource_pubsub_topic.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/google/resource_pubsub_topic.go b/google/resource_pubsub_topic.go index 51b164d1d04..740d01c5c90 100644 --- a/google/resource_pubsub_topic.go +++ b/google/resource_pubsub_topic.go @@ -60,11 +60,13 @@ func resourcePubsubTopicRead(d *schema.ResourceData, meta interface{}) error { name := d.Id() call := config.clientPubsub.Projects.Topics.Get(name) - _, err := call.Do() + res, err := call.Do() if err != nil { return handleNotFoundError(err, d, fmt.Sprintf("Pubsub Topic %q", name)) } + d.Set("name", res.Name) + return nil } @@ -89,10 +91,8 @@ func resourcePubsubTopicStateImporter(d *schema.ResourceData, meta interface{}) return nil, err } - name := d.Id() - id := fmt.Sprintf("projects/%s/topics/%s", project, name) + id := fmt.Sprintf("projects/%s/topics/%s", project, d.Id()) - d.Set("name", name) d.SetId(id) return []*schema.ResourceData{d}, nil From 318d5f629ed06cd66df7b46dae5c84766cf7e928 Mon Sep 17 00:00:00 2001 From: Anders Bruun Olsen Date: Mon, 11 Sep 2017 15:04:29 +0200 Subject: [PATCH 6/9] Use passthrough import method instead of custom method. --- google/resource_pubsub_topic.go | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/google/resource_pubsub_topic.go b/google/resource_pubsub_topic.go index 740d01c5c90..c2066b826a0 100644 --- a/google/resource_pubsub_topic.go +++ b/google/resource_pubsub_topic.go @@ -14,7 +14,7 @@ func resourcePubsubTopic() *schema.Resource { Delete: resourcePubsubTopicDelete, Importer: &schema.ResourceImporter{ - State: resourcePubsubTopicStateImporter, + State: schema.ImportStatePassthrough, }, Schema: map[string]*schema.Schema{ @@ -82,18 +82,3 @@ func resourcePubsubTopicDelete(d *schema.ResourceData, meta interface{}) error { return nil } - -func resourcePubsubTopicStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - config := meta.(*Config) - - project, err := getProject(d, config) - if err != nil { - return nil, err - } - - id := fmt.Sprintf("projects/%s/topics/%s", project, d.Id()) - - d.SetId(id) - - return []*schema.ResourceData{d}, nil -} From 57f6cd147d31cda2eb95128788475742a90bd82a Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Mon, 11 Sep 2017 09:35:38 -0700 Subject: [PATCH 7/9] Revert "Use passthrough import method instead of custom method." This reverts commit 318d5f629ed06cd66df7b46dae5c84766cf7e928. --- google/resource_pubsub_topic.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/google/resource_pubsub_topic.go b/google/resource_pubsub_topic.go index c2066b826a0..740d01c5c90 100644 --- a/google/resource_pubsub_topic.go +++ b/google/resource_pubsub_topic.go @@ -14,7 +14,7 @@ func resourcePubsubTopic() *schema.Resource { Delete: resourcePubsubTopicDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourcePubsubTopicStateImporter, }, Schema: map[string]*schema.Schema{ @@ -82,3 +82,18 @@ func resourcePubsubTopicDelete(d *schema.ResourceData, meta interface{}) error { return nil } + +func resourcePubsubTopicStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + + project, err := getProject(d, config) + if err != nil { + return nil, err + } + + id := fmt.Sprintf("projects/%s/topics/%s", project, d.Id()) + + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} From 5638389cdddcb166f1b7b69a14642051ddec8fb9 Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Mon, 11 Sep 2017 09:42:18 -0700 Subject: [PATCH 8/9] Add diff supress on the topic name field --- google/resource_pubsub_topic.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/google/resource_pubsub_topic.go b/google/resource_pubsub_topic.go index 740d01c5c90..f245f8b3ac0 100644 --- a/google/resource_pubsub_topic.go +++ b/google/resource_pubsub_topic.go @@ -19,9 +19,10 @@ func resourcePubsubTopic() *schema.Resource { Schema: map[string]*schema.Schema{ "name": &schema.Schema{ - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: linkDiffSuppress, }, "project": &schema.Schema{ From 80bbe5dd4b930c3981bbec86772c5a5ad4599004 Mon Sep 17 00:00:00 2001 From: Vincent Roseberry Date: Mon, 11 Sep 2017 09:42:32 -0700 Subject: [PATCH 9/9] Use only name for the import id in test --- google/import_pubsub_topic_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/google/import_pubsub_topic_test.go b/google/import_pubsub_topic_test.go index c3c0e2acccd..335183838de 100644 --- a/google/import_pubsub_topic_test.go +++ b/google/import_pubsub_topic_test.go @@ -25,6 +25,7 @@ func TestAccPubsubTopic_import(t *testing.T) { }, resource.TestStep{ ResourceName: "google_pubsub_topic.tf-test", + ImportStateId: topicName, ImportState: true, ImportStateVerify: true, ImportStateVerifyIgnore: []string{"force_destroy"},