Pubsub importable (#392)

This commit is contained in:
Anders Bruun Olsen 2017-09-11 18:46:27 +02:00 committed by Vincent Roseberry
parent 7c884c7e09
commit bf51f26c07
3 changed files with 69 additions and 4 deletions

View File

@ -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"},
},
},
})
}

View File

@ -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{
@ -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
}
@ -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
}

View File

@ -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
```