Merge pull request #10704 from Ninir/r-aws-sns_topic-protocols
provider/aws: Improved the SNS topic subscription protocols validation
This commit is contained in:
commit
56b0e87f5e
|
@ -30,22 +30,10 @@ func resourceAwsSnsTopicSubscription() *schema.Resource {
|
||||||
|
|
||||||
Schema: map[string]*schema.Schema{
|
Schema: map[string]*schema.Schema{
|
||||||
"protocol": &schema.Schema{
|
"protocol": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
Required: true,
|
Required: true,
|
||||||
ForceNew: false,
|
ForceNew: false,
|
||||||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
|
ValidateFunc: validateSNSSubscriptionProtocol,
|
||||||
value := v.(string)
|
|
||||||
forbidden := []string{"email", "sms"}
|
|
||||||
for _, f := range forbidden {
|
|
||||||
if strings.Contains(value, f) {
|
|
||||||
errors = append(
|
|
||||||
errors,
|
|
||||||
fmt.Errorf("Unsupported protocol (%s) for SNS Topic", value),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
"endpoint": &schema.Schema{
|
"endpoint": &schema.Schema{
|
||||||
Type: schema.TypeString,
|
Type: schema.TypeString,
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/service/s3"
|
"github.com/aws/aws-sdk-go/service/s3"
|
||||||
|
@ -573,3 +574,17 @@ func validateSQSFifoQueueName(v interface{}, k string) (errors []error) {
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func validateSNSSubscriptionProtocol(v interface{}, k string) (ws []string, errors []error) {
|
||||||
|
value := strings.ToLower(v.(string))
|
||||||
|
forbidden := []string{"email", "sms"}
|
||||||
|
for _, f := range forbidden {
|
||||||
|
if strings.Contains(value, f) {
|
||||||
|
errors = append(
|
||||||
|
errors,
|
||||||
|
fmt.Errorf("Unsupported protocol (%s) for SNS Topic", value),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -865,3 +865,33 @@ func TestValidateSQSFifoQueueName(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateSNSSubscriptionProtocol(t *testing.T) {
|
||||||
|
validProtocols := []string{
|
||||||
|
"lambda",
|
||||||
|
"sqs",
|
||||||
|
"sqs",
|
||||||
|
"application",
|
||||||
|
"http",
|
||||||
|
"https",
|
||||||
|
}
|
||||||
|
for _, v := range validProtocols {
|
||||||
|
if _, errors := validateSNSSubscriptionProtocol(v, "protocol"); len(errors) > 0 {
|
||||||
|
t.Fatalf("%q should be a valid SNS Subscription protocol: %v", v, errors)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
invalidProtocols := []string{
|
||||||
|
"Email",
|
||||||
|
"email",
|
||||||
|
"Email-JSON",
|
||||||
|
"email-json",
|
||||||
|
"SMS",
|
||||||
|
"sms",
|
||||||
|
}
|
||||||
|
for _, v := range invalidProtocols {
|
||||||
|
if _, errors := validateSNSSubscriptionProtocol(v, "protocol"); len(errors) == 0 {
|
||||||
|
t.Fatalf("%q should be an invalid SNS Subscription protocol: %v", v, errors)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue