Skip to content

Commit 3a43ce8

Browse files
committed
providers/aws: sns_topic id-only
1 parent edd14e4 commit 3a43ce8

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

builtin/providers/aws/resource_aws_sns_topic.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818

1919
// Mutable attributes
2020
var SNSAttributeMap = map[string]string{
21+
"arn": "TopicArn",
2122
"display_name": "DisplayName",
2223
"policy": "Policy",
2324
"delivery_policy": "DeliveryPolicy",
@@ -163,7 +164,6 @@ func resourceAwsSnsTopicRead(d *schema.ResourceData, meta interface{}) error {
163164
attributeOutput, err := snsconn.GetTopicAttributes(&sns.GetTopicAttributesInput{
164165
TopicArn: aws.String(d.Id()),
165166
})
166-
167167
if err != nil {
168168
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFound" {
169169
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 {
198198
}
199199
}
200200

201+
// If we have no name set (import) then determine it from the ARN.
202+
// This is a bit of a heuristic for now since AWS provides no other
203+
// way to get it.
204+
if _, ok := d.GetOk("name"); !ok {
205+
arn := d.Get("arn").(string)
206+
idx := strings.LastIndex(arn, ":")
207+
if idx > -1 {
208+
d.Set("name", arn[idx+1:])
209+
}
210+
}
211+
201212
return nil
202213
}
203214

builtin/providers/aws/resource_aws_sns_topic_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import (
1313

1414
func TestAccAWSSNSTopic_basic(t *testing.T) {
1515
resource.Test(t, resource.TestCase{
16-
PreCheck: func() { testAccPreCheck(t) },
17-
Providers: testAccProviders,
18-
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
16+
PreCheck: func() { testAccPreCheck(t) },
17+
IDRefreshName: "aws_sns_topic.test_topic",
18+
Providers: testAccProviders,
19+
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
1920
Steps: []resource.TestStep{
2021
resource.TestStep{
2122
Config: testAccAWSSNSTopicConfig,
@@ -29,9 +30,10 @@ func TestAccAWSSNSTopic_basic(t *testing.T) {
2930

3031
func TestAccAWSSNSTopic_withIAMRole(t *testing.T) {
3132
resource.Test(t, resource.TestCase{
32-
PreCheck: func() { testAccPreCheck(t) },
33-
Providers: testAccProviders,
34-
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
33+
PreCheck: func() { testAccPreCheck(t) },
34+
IDRefreshName: "aws_sns_topic.test_topic",
35+
Providers: testAccProviders,
36+
CheckDestroy: testAccCheckAWSSNSTopicDestroy,
3537
Steps: []resource.TestStep{
3638
resource.TestStep{
3739
Config: testAccAWSSNSTopicConfig_withIAMRole,

0 commit comments

Comments
 (0)