Skip to content

Commit 1d64cc5

Browse files
authored
allow importing a pubsub topic using its full id (#1142)
1 parent e57eef9 commit 1d64cc5

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

google/resource_pubsub_topic.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package google
22

33
import (
44
"fmt"
5+
"regexp"
56

67
"github.com/hashicorp/terraform/helper/schema"
78
"google.golang.org/api/pubsub/v1"
@@ -94,12 +95,16 @@ func resourcePubsubTopicDelete(d *schema.ResourceData, meta interface{}) error {
9495
func resourcePubsubTopicStateImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
9596
config := meta.(*Config)
9697

97-
project, err := getProject(d, config)
98-
if err != nil {
99-
return nil, err
98+
topicId := regexp.MustCompile("^projects/[^/]+/topics/[^/]+$")
99+
if topicId.MatchString(d.Id()) {
100+
return []*schema.ResourceData{d}, nil
101+
}
102+
103+
if config.Project == "" {
104+
return nil, fmt.Errorf("The default project for the provider must be set when using the `{name}` id format.")
100105
}
101106

102-
id := fmt.Sprintf("projects/%s/topics/%s", project, d.Id())
107+
id := fmt.Sprintf("projects/%s/topics/%s", config.Project, d.Id())
103108

104109
d.SetId(id)
105110

google/resource_pubsub_topic_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ func TestAccPubsubTopic_basic(t *testing.T) {
2222
resource.TestStep{
2323
Config: testAccPubsubTopic_basic(topicName),
2424
},
25+
// Check importing with just the topic name
2526
resource.TestStep{
2627
ResourceName: "google_pubsub_topic.foo",
2728
ImportStateId: topicName,
2829
ImportState: true,
2930
ImportStateVerify: true,
3031
ImportStateVerifyIgnore: []string{"force_destroy"},
3132
},
33+
// Check importing with the full resource id
34+
resource.TestStep{
35+
ResourceName: "google_pubsub_topic.foo",
36+
ImportState: true,
37+
ImportStateVerify: true,
38+
ImportStateVerifyIgnore: []string{"force_destroy"},
39+
},
3240
},
3341
})
3442
}

website/docs/r/pubsub_topic.html.markdown

+9-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: "google"
33
page_title: "Google: google_pubsub_topic"
44
sidebar_current: "docs-google-pubsub-topic-x"
55
description: |-
6-
Creates a topic in Google's pubsub queueing system
6+
Creates a topic in Google's pubsub queueing system
77
---
88

99
# google\_pubsub\_topic
@@ -16,7 +16,7 @@ Creates a topic in Google's pubsub queueing system. For more information see
1616
## Example Usage
1717

1818
```hcl
19-
resource "google_pubsub_topic" "default" {
19+
resource "google_pubsub_topic" "mytopic" {
2020
name = "default-topic"
2121
}
2222
```
@@ -25,7 +25,7 @@ resource "google_pubsub_topic" "default" {
2525

2626
The following arguments are supported:
2727

28-
* `name` - (Required) A unique name for the resource, required by pubsub.
28+
* `name` - (Required) A unique name for the pubsub topic.
2929
Changing this forces a new resource to be created.
3030

3131
- - -
@@ -39,8 +39,12 @@ Only the arguments listed above are exposed as attributes.
3939

4040
## Import
4141

42-
Pubsub topics can be imported using the `name`, e.g.
42+
Pubsub topics can be imported using the `name` or full topic id, e.g.
4343

4444
```
45-
$ terraform import google_pubsub_topic.mytopic mytopic
45+
$ terraform import google_pubsub_topic.mytopic default-topic
4646
```
47+
```
48+
$ terraform import google_pubsub_topic.mytopic projects/my-gcp-project/topics/default-topic
49+
```
50+
When importing using only the name, the provider project must be set.

0 commit comments

Comments
 (0)