Skip to content

Commit

Permalink
resource/aws_sns_application: Incorporate resource feedback from #1101
Browse files Browse the repository at this point in the history
…and rename attributes to closer match SNS attributes
  • Loading branch information
bflad committed Feb 7, 2018
1 parent 21c4491 commit 86fc7a5
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 102 deletions.
79 changes: 34 additions & 45 deletions aws/resource_aws_sns_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import (
"fmt"
"log"

"github.com/hashicorp/terraform/helper/schema"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/hashicorp/terraform/helper/schema"
)

var SupportedPlatforms = map[string]bool{
Expand All @@ -22,14 +21,14 @@ var SupportedPlatforms = map[string]bool{
// Mutable attributes
// http://docs.aws.amazon.com/sns/latest/api/API_SetPlatformApplicationAttributes.html
var SNSPlatformAppAttributeMap = map[string]string{
"principal": "PlatformPrincipal",
"created_topic": "EventEndpointCreated",
"deleted_topic": "EventEndpointDeleted",
"updated_topic": "EventEndpointUpdated",
"failure_topic": "EventDeliveryFailure",
"success_iam_arn": "SuccessFeedbackRoleArn",
"failure_iam_arn": "FailureFeedbackRoleArn",
"success_sample_rate": "SuccessFeedbackSampleRate",
"event_delivery_failure_topic_arn": "EventDeliveryFailure",
"event_endpoint_created_topic_arn": "EventEndpointCreated",
"event_endpoint_deleted_topic_arn": "EventEndpointDeleted",
"event_endpoint_updated_topic_arn": "EventEndpointUpdated",
"failure_feedback_role_arn": "FailureFeedbackRoleArn",
"platform_principal": "PlatformPrincipal",
"success_feedback_role_arn": "SuccessFeedbackRoleArn",
"success_feedback_sample_rate": "SuccessFeedbackSampleRate",
}

func resourceAwsSnsApplication() *schema.Resource {
Expand All @@ -43,66 +42,57 @@ func resourceAwsSnsApplication() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"platform": &schema.Schema{
"platform": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"credential": &schema.Schema{
"platform_credential": {
Type: schema.TypeString,
Required: true,
ForceNew: false,
StateFunc: hashSum,
},
"principal": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: false,
StateFunc: hashSum,
},
"created_topic": &schema.Schema{
"arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
Computed: true,
},
"deleted_topic": &schema.Schema{
"event_delivery_failure_topic_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"updated_topic": &schema.Schema{
"event_endpoint_created_topic_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"failure_topic": &schema.Schema{
"event_endpoint_deleted_topic_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"success_iam_role": &schema.Schema{
"event_endpoint_updated_topic_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"failure_iam_role": &schema.Schema{
"failure_feedback_role_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"success_sample_rate": &schema.Schema{
"platform_principal": {
Type: schema.TypeString,
Optional: true,
StateFunc: hashSum,
},
"success_feedback_role_arn": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"arn": &schema.Schema{
"success_feedback_sample_rate": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
}
Expand All @@ -114,9 +104,9 @@ func resourceAwsSnsApplicationCreate(d *schema.ResourceData, meta interface{}) e
attributes := make(map[string]*string)
name := d.Get("name").(string)
platform := d.Get("platform").(string)
principal := d.Get("principal").(string)
principal := d.Get("platform_principal").(string)

attributes["PlatformCredential"] = aws.String(d.Get("credential").(string))
attributes["PlatformCredential"] = aws.String(d.Get("platform_credential").(string))

if _, ok := SupportedPlatforms[platform]; !ok {
return errors.New(fmt.Sprintf("Platform %s is not supported", platform))
Expand Down Expand Up @@ -145,9 +135,6 @@ func resourceAwsSnsApplicationCreate(d *schema.ResourceData, meta interface{}) e

d.SetId(*output.PlatformApplicationArn)

// Write the ARN to the 'arn' field for export
d.Set("arn", *output.PlatformApplicationArn)

return resourceAwsSnsApplicationUpdate(d, meta)
}

Expand All @@ -168,13 +155,13 @@ func resourceAwsSnsApplicationUpdate(d *schema.ResourceData, meta interface{}) e
}
}

if d.HasChange("credential") {
attributes["PlatformCredential"] = aws.String(d.Get("credential").(string))
if d.HasChange("platform_credential") {
attributes["PlatformCredential"] = aws.String(d.Get("platform_credential").(string))
// If the platform requires a principal it must also be specified, even if it didn't change
// since credential is stored as a hash, the only way to update principal is to update both
// as they must be specified together in the request.
if v, _ := SupportedPlatforms[d.Get("platform").(string)]; v {
attributes["PlatformPrincipal"] = aws.String(d.Get("principal").(string))
attributes["PlatformPrincipal"] = aws.String(d.Get("platform_principal").(string))
}
}

Expand All @@ -195,6 +182,8 @@ func resourceAwsSnsApplicationUpdate(d *schema.ResourceData, meta interface{}) e
func resourceAwsSnsApplicationRead(d *schema.ResourceData, meta interface{}) error {
snsconn := meta.(*AWSClient).snsconn

d.Set("arn", d.Id())

attributeOutput, err := snsconn.GetPlatformApplicationAttributes(&sns.GetPlatformApplicationAttributesInput{
PlatformApplicationArn: aws.String(d.Id()),
})
Expand Down
96 changes: 48 additions & 48 deletions aws/resource_aws_sns_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ func TestAccAWSSNSApplication_gcm_create_update(t *testing.T) {
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "platform", "GCM"),
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "success_sample_rate", "100"),
"aws_sns_application.gcm_test", "success_feedback_sample_rate", "100"),
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "created_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic"),
"aws_sns_application.gcm_test", "event_endpoint_created_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic"),
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "updated_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic"),
"aws_sns_application.gcm_test", "event_endpoint_updated_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic"),
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "failure_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic"),
"aws_sns_application.gcm_test", "event_delivery_failure_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic"),
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "deleted_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic"),
"aws_sns_application.gcm_test", "event_endpoint_deleted_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic"),
),
},
resource.TestStep{
Expand All @@ -57,15 +57,15 @@ func TestAccAWSSNSApplication_gcm_create_update(t *testing.T) {
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "platform", "GCM"),
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "success_sample_rate", "99"),
"aws_sns_application.gcm_test", "success_feedback_sample_rate", "99"),
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "created_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic-update"),
"aws_sns_application.gcm_test", "event_endpoint_created_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic-update"),
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "updated_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic-update"),
"aws_sns_application.gcm_test", "event_endpoint_updated_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic-update"),
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "failure_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic-update"),
"aws_sns_application.gcm_test", "event_delivery_failure_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic-update"),
resource.TestCheckResourceAttr(
"aws_sns_application.gcm_test", "deleted_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic-update"),
"aws_sns_application.gcm_test", "event_endpoint_deleted_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic-update"),
),
},
},
Expand Down Expand Up @@ -95,15 +95,15 @@ func TestAccAWSSNSApplication_apns_sandbox_create_update(t *testing.T) {
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "platform", "APNS_SANDBOX"),
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "success_sample_rate", "100"),
"aws_sns_application.apns_test", "success_feedback_sample_rate", "100"),
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "created_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic"),
"aws_sns_application.apns_test", "event_endpoint_created_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic"),
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "updated_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic"),
"aws_sns_application.apns_test", "event_endpoint_updated_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic"),
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "failure_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic"),
"aws_sns_application.apns_test", "event_delivery_failure_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic"),
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "deleted_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic"),
"aws_sns_application.apns_test", "event_endpoint_deleted_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic"),
),
},
resource.TestStep{
Expand All @@ -114,15 +114,15 @@ func TestAccAWSSNSApplication_apns_sandbox_create_update(t *testing.T) {
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "platform", "APNS_SANDBOX"),
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "success_sample_rate", "99"),
"aws_sns_application.apns_test", "success_feedback_sample_rate", "99"),
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "created_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic-update"),
"aws_sns_application.apns_test", "event_endpoint_created_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic-update"),
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "updated_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic-update"),
"aws_sns_application.apns_test", "event_endpoint_updated_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic-update"),
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "failure_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic-update"),
"aws_sns_application.apns_test", "event_delivery_failure_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic-update"),
resource.TestCheckResourceAttr(
"aws_sns_application.apns_test", "deleted_topic", "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic-update"),
"aws_sns_application.apns_test", "event_endpoint_deleted_topic_arn", "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic-update"),
),
},
},
Expand Down Expand Up @@ -151,54 +151,54 @@ func testAccCheckAWSSNSApplicationDestroy(s *terraform.State) error {

var testAccAWSSNSApplicationGCMConfig = `
resource "aws_sns_application" "gcm_test" {
event_delivery_failure_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic"
event_endpoint_created_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic"
event_endpoint_deleted_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic"
event_endpoint_updated_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic"
name = "aws_sns_application_test"
platform = "GCM"
created_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic"
deleted_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic"
updated_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic"
failure_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic"
success_sample_rate = 100
credential = "` + os.Getenv("GCM_API_KEY") + `"
platform_credential = "` + os.Getenv("GCM_API_KEY") + `"
success_feedback_sample_rate = 100
}
`

var testAccAWSSNSApplicationGCMConfigUpdate = `
resource "aws_sns_application" "gcm_test" {
event_delivery_failure_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic-update"
event_endpoint_created_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic-update"
event_endpoint_deleted_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic-update"
event_endpoint_updated_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic-update"
name = "aws_sns_application_test"
platform = "GCM"
created_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic-update"
deleted_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic-update"
updated_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic-update"
failure_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic-update"
success_sample_rate = 99
credential = "` + os.Getenv("GCM_API_KEY") + `"
platform_credential = "` + os.Getenv("GCM_API_KEY") + `"
resource "aws_sns_application" "gcm_test" {
success_feedback_sample_rate = 99
}
`

var testAccAWSSNSApplicationAPNSSandBoxConfig = `
resource "aws_sns_application" "apns_test" {
event_delivery_failure_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic"
event_endpoint_created_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic"
event_endpoint_deleted_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic"
event_endpoint_updated_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic"
name = "aws_sns_application_test"
platform = "APNS_SANDBOX"
created_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic"
deleted_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic"
updated_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic"
failure_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic"
success_sample_rate = 100
credential = "` + os.Getenv("APNS_SANDBOX_CREDENTIAL") + `"
principal = "` + os.Getenv("APNS_SANDBOX_PRINCIPAL") + `"
platform_credential = "` + os.Getenv("APNS_SANDBOX_CREDENTIAL") + `"
platform_principal = "` + os.Getenv("APNS_SANDBOX_PRINCIPAL") + `"
success_feedback_sample_rate = 100
}
`

var testAccAWSSNSApplicationAPNSSandBoxConfigUpdate = `
resource "aws_sns_application" "apns_test" {
resource "aws_sns_application" "apns_test" {
event_delivery_failure_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic-update"
event_endpoint_created_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic-update"
event_endpoint_deleted_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic-update"
event_endpoint_updated_topic_arn = "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic-update"
name = "aws_sns_application_test"
platform = "APNS_SANDBOX"
created_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-created-topic-update"
deleted_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-deleted-topic-update"
updated_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-updated-topic-update"
failure_topic = "arn:aws:sns:us-east-1:638386993804:endpoint-failure-topic-update"
success_sample_rate = 99
credential = "` + os.Getenv("APNS_SANDBOX_CREDENTIAL") + `"
principal = "` + os.Getenv("APNS_SANDBOX_PRINCIPAL") + `"
platform_credential = "` + os.Getenv("APNS_SANDBOX_CREDENTIAL") + `"
platform_principal = "` + os.Getenv("APNS_SANDBOX_PRINCIPAL") + `"
success_feedback_sample_rate = 99
}
`
18 changes: 9 additions & 9 deletions website/docs/r/sns_application.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ The following arguments are supported:

* `name` - (Required) The friendly name for the SNS application
* `platform` - (Required) The platform that the app is registered with. See [Platform][1] for supported platforms.
* `credential` - (Required) Application Platform credential. See [Credential][1] for type of credential required for platform.
* `principal` - (Optional) Application Platform principal. See [Principal][2] for type of principal required for platform.
* `created_topic` - (Optional) SNS Topic triggered when a new platform endpoint is added to your platform application.
* `deleted_topic` - (Optional) SNS Topic triggered when an existing platform endpoint is deleted from your platform application.
* `failure_iam_role` - (Optional) The IAM role permitted to receive failure feedback for this application.
* `failure_topic` - (Optional) SNS Topic triggered when a delivery to any of the platform endpoints associated with your platform application encounters a permanent failure.
* `success_iam_role` - (Optional) The IAM role permitted to receive success feedback for this application.
* `success_sample_rate` - (Optional) The percentage of success to sample (0-100)
* `updated_topic` - (Optional) SNS Topic triggered when an existing platform endpoint is changed from your platform application.
* `platform_credential` - (Required) Application Platform credential. See [Credential][1] for type of credential required for platform.
* `event_delivery_failure_topic_arn` - (Optional) SNS Topic triggered when a delivery to any of the platform endpoints associated with your platform application encounters a permanent failure.
* `event_endpoint_created_topic_arn` - (Optional) SNS Topic triggered when a new platform endpoint is added to your platform application.
* `event_endpoint_deleted_topic_arn` - (Optional) SNS Topic triggered when an existing platform endpoint is deleted from your platform application.
* `event_endpoint_updated_topic` - (Optional) SNS Topic triggered when an existing platform endpoint is changed from your platform application.
* `failure_feedback_role_arn` - (Optional) The IAM role permitted to receive failure feedback for this application.
* `platform_principal` - (Optional) Application Platform principal. See [Principal][2] for type of principal required for platform.
* `success_feedback_role_arn` - (Optional) The IAM role permitted to receive success feedback for this application.
* `success_feedback_sample_rate` - (Optional) The percentage of success to sample (0-100)

## Platforms supported

Expand Down

0 comments on commit 86fc7a5

Please sign in to comment.