Merge pull request #11915 from hashicorp/f-validate-policy

provider/aws: JSON Validate iam_role
This commit is contained in:
Jake Champlin 2017-02-13 14:45:29 -05:00 committed by GitHub
commit be2982af33
2 changed files with 39 additions and 1 deletions

View File

@ -82,10 +82,11 @@ func resourceAwsIamRole() *schema.Resource {
ForceNew: true, ForceNew: true,
}, },
"assume_role_policy": &schema.Schema{ "assume_role_policy": {
Type: schema.TypeString, Type: schema.TypeString,
Required: true, Required: true,
DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs, DiffSuppressFunc: suppressEquivalentAwsPolicyDiffs,
ValidateFunc: validateJsonString,
}, },
"create_date": { "create_date": {

View File

@ -5,6 +5,8 @@ import (
"strings" "strings"
"testing" "testing"
"regexp"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/iam" "github.com/aws/aws-sdk-go/service/iam"
@ -81,6 +83,20 @@ func TestAccAWSRole_testNameChange(t *testing.T) {
}) })
} }
func TestAccAWSRole_badJSON(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSRoleDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSRoleConfig_badJson,
ExpectError: regexp.MustCompile(`"assume_role_policy" contains an invalid JSON:.*`),
},
},
})
}
func testAccCheckAWSRoleDestroy(s *terraform.State) error { func testAccCheckAWSRoleDestroy(s *terraform.State) error {
iamconn := testAccProvider.Meta().(*AWSClient).iamconn iamconn := testAccProvider.Meta().(*AWSClient).iamconn
@ -279,3 +295,24 @@ resource "aws_iam_instance_profile" "role_update_test" {
} }
` `
const testAccAWSRoleConfig_badJson = `
resource "aws_iam_role" "my_instance_role" {
name = "test-role"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com",
},
"Effect": "Allow",
"Sid": ""
}
]
}
POLICY
}`