From 3a43ce8f8ef4000fae56a672879257213f33384d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 21 Apr 2016 08:18:04 -0700 Subject: [PATCH] providers/aws: sns_topic id-only --- builtin/providers/aws/resource_aws_sns_topic.go | 13 ++++++++++++- .../providers/aws/resource_aws_sns_topic_test.go | 14 ++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/builtin/providers/aws/resource_aws_sns_topic.go b/builtin/providers/aws/resource_aws_sns_topic.go index 4174e8732..62f2450c0 100644 --- a/builtin/providers/aws/resource_aws_sns_topic.go +++ b/builtin/providers/aws/resource_aws_sns_topic.go @@ -18,6 +18,7 @@ import ( // Mutable attributes var SNSAttributeMap = map[string]string{ + "arn": "TopicArn", "display_name": "DisplayName", "policy": "Policy", "delivery_policy": "DeliveryPolicy", @@ -163,7 +164,6 @@ func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error { attributeOutput, err := snsconn.GetTopicAttributes(&sns.GetTopicAttributesInput{ TopicArn: aws.String(d.Id()), }) - if err != nil { if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFound" { log.Printf("[WARN] SNS Topic (%s) not found, error code (404)", d.Id()) @@ -198,6 +198,17 @@ func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error { } } + // If we have no name set (import) then determine it from the ARN. + // This is a bit of a heuristic for now since AWS provides no other + // way to get it. + if _, ok := d.GetOk("name"); !ok { + arn := d.Get("arn").(string) + idx := strings.LastIndex(arn, ":") + if idx > -1 { + d.Set("name", arn[idx+1:]) + } + } + return nil } diff --git a/builtin/providers/aws/resource_aws_sns_topic_test.go b/builtin/providers/aws/resource_aws_sns_topic_test.go index 2852c36fb..2b74c7abb 100644 --- a/builtin/providers/aws/resource_aws_sns_topic_test.go +++ b/builtin/providers/aws/resource_aws_sns_topic_test.go @@ -13,9 +13,10 @@ import ( func TestAccAWSSNSTopic_basic(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSSNSTopicDestroy, + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_sns_topic.test_topic", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSNSTopicDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAWSSNSTopicConfig, @@ -29,9 +30,10 @@ func TestAccAWSSNSTopic_basic(t *testing.T) { func TestAccAWSSNSTopic_withIAMRole(t *testing.T) { resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSSNSTopicDestroy, + PreCheck: func() { testAccPreCheck(t) }, + IDRefreshName: "aws_sns_topic.test_topic", + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSSNSTopicDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAWSSNSTopicConfig_withIAMRole,