allow importing a pubsub topic using its full id (#1142)

This commit is contained in:
Dana Hoffman 2018-03-02 11:22:58 -08:00 committed by GitHub
parent e57eef944c
commit 1d64cc5d26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package google
import ( import (
"fmt" "fmt"
"regexp"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/pubsub/v1" "google.golang.org/api/pubsub/v1"
@ -94,12 +95,16 @@ func resourcePubsubTopicDelete(d *schema.ResourceData, meta interface{}) error {
func resourcePubsubTopicStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { func resourcePubsubTopicStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config) config := meta.(*Config)
project, err := getProject(d, config) topicId := regexp.MustCompile("^projects/[^/]+/topics/[^/]+$")
if err != nil { if topicId.MatchString(d.Id()) {
return nil, err return []*schema.ResourceData{d}, nil
} }
id := fmt.Sprintf("projects/%s/topics/%s", project, d.Id()) if config.Project == "" {
return nil, fmt.Errorf("The default project for the provider must be set when using the `{name}` id format.")
}
id := fmt.Sprintf("projects/%s/topics/%s", config.Project, d.Id())
d.SetId(id) d.SetId(id)

View File

@ -22,6 +22,7 @@ func TestAccPubsubTopic_basic(t *testing.T) {
resource.TestStep{ resource.TestStep{
Config: testAccPubsubTopic_basic(topicName), Config: testAccPubsubTopic_basic(topicName),
}, },
// Check importing with just the topic name
resource.TestStep{ resource.TestStep{
ResourceName: "google_pubsub_topic.foo", ResourceName: "google_pubsub_topic.foo",
ImportStateId: topicName, ImportStateId: topicName,
@ -29,6 +30,13 @@ func TestAccPubsubTopic_basic(t *testing.T) {
ImportStateVerify: true, ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"}, ImportStateVerifyIgnore: []string{"force_destroy"},
}, },
// Check importing with the full resource id
resource.TestStep{
ResourceName: "google_pubsub_topic.foo",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
}, },
}) })
} }

View File

@ -3,7 +3,7 @@ layout: "google"
page_title: "Google: google_pubsub_topic" page_title: "Google: google_pubsub_topic"
sidebar_current: "docs-google-pubsub-topic-x" sidebar_current: "docs-google-pubsub-topic-x"
description: |- description: |-
Creates a topic in Google's pubsub queueing system Creates a topic in Google's pubsub queueing system
--- ---
# google\_pubsub\_topic # google\_pubsub\_topic
@ -16,7 +16,7 @@ Creates a topic in Google's pubsub queueing system. For more information see
## Example Usage ## Example Usage
```hcl ```hcl
resource "google_pubsub_topic" "default" { resource "google_pubsub_topic" "mytopic" {
name = "default-topic" name = "default-topic"
} }
``` ```
@ -25,7 +25,7 @@ resource "google_pubsub_topic" "default" {
The following arguments are supported: The following arguments are supported:
* `name` - (Required) A unique name for the resource, required by pubsub. * `name` - (Required) A unique name for the pubsub topic.
Changing this forces a new resource to be created. Changing this forces a new resource to be created.
- - - - - -
@ -39,8 +39,12 @@ Only the arguments listed above are exposed as attributes.
## Import ## Import
Pubsub topics can be imported using the `name`, e.g. Pubsub topics can be imported using the `name` or full topic id, e.g.
``` ```
$ terraform import google_pubsub_topic.mytopic mytopic $ terraform import google_pubsub_topic.mytopic default-topic
``` ```
```
$ terraform import google_pubsub_topic.mytopic projects/my-gcp-project/topics/default-topic
```
When importing using only the name, the provider project must be set.