Skip to content
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

Pubsub importable #392

Merged
merged 13 commits into from
Sep 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions google/import_pubsub_topic_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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",
ImportStateId: topicName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"force_destroy"},
},
},
})
}
30 changes: 26 additions & 4 deletions google/resource_pubsub_topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ func resourcePubsubTopic() *schema.Resource {
Read: resourcePubsubTopicRead,
Delete: resourcePubsubTopicDelete,

Importer: &schema.ResourceImporter{
State: resourcePubsubTopicStateImporter,
},

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{
Expand Down Expand Up @@ -56,11 +61,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
}

Expand All @@ -76,3 +83,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
}
8 changes: 8 additions & 0 deletions website/docs/r/pubsub_topic.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
```