diff --git a/internal/service/iam/policy_document_data_source.go b/internal/service/iam/policy_document_data_source.go index b54afefecbd..0331409dcd4 100644 --- a/internal/service/iam/policy_document_data_source.go +++ b/internal/service/iam/policy_document_data_source.go @@ -56,7 +56,7 @@ func DataSourcePolicyDocument() *schema.Resource { Optional: true, Elem: &schema.Schema{ Type: schema.TypeString, - ValidateFunc: validation.All(validation.StringIsNotEmpty, validation.StringIsJSON), + ValidateFunc: validation.StringIsJSON, }, }, "statement": { @@ -139,6 +139,10 @@ func dataSourcePolicyDocumentRead(d *schema.ResourceData, meta interface{}) erro // merge sourceDocs in order specified for sourceJSONIndex, sourceJSON := range v.([]interface{}) { + if sourceJSON == nil { + continue + } + sourceDoc := &IAMPolicyDoc{} if err := json.Unmarshal([]byte(sourceJSON.(string)), sourceDoc); err != nil { return err diff --git a/internal/service/iam/policy_document_data_source_test.go b/internal/service/iam/policy_document_data_source_test.go index 9d13585278c..4fae2d3cd88 100644 --- a/internal/service/iam/policy_document_data_source_test.go +++ b/internal/service/iam/policy_document_data_source_test.go @@ -223,19 +223,23 @@ func TestAccIAMPolicyDocumentDataSource_duplicateSid(t *testing.T) { }) } -func TestAccIAMPolicyDocumentDataSource_sourcePolicyValidJson(t *testing.T) { +func TestAccIAMPolicyDocumentDataSource_sourcePolicyValidJSON(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(t) }, ErrorCheck: acctest.ErrorCheck(t, iam.EndpointsID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { - Config: testAccPolicyDocumentDataSourceConfig_invalidJson, + Config: testAccPolicyDocumentDataSourceConfig_invalidJSON, ExpectError: regexp.MustCompile(`"source_policy_documents.0" contains an invalid JSON: unexpected end of JSON input`), }, { - Config: testAccPolicyDocumentDataSourceConfig_emptyString, - ExpectError: regexp.MustCompile(`expected "source_policy_documents.0" to not be an empty string, got`), + Config: testAccPolicyDocumentDataSourceConfig_emptyString, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.aws_iam_policy_document.test", "json", + testAccPolicyDocumentExpectedJSONNoStatement, + ), + ), }, }, }) @@ -1297,16 +1301,20 @@ data "aws_iam_policy_document" "test" { var testAccPolicyDocumentDataSourceConfig_emptyString = ` data "aws_iam_policy_document" "test" { - source_policy_documents = [""] + source_policy_documents = [""] } ` -var testAccPolicyDocumentDataSourceConfig_invalidJson = ` +var testAccPolicyDocumentDataSourceConfig_invalidJSON = ` data "aws_iam_policy_document" "test" { - source_policy_documents = ["{"] + source_policy_documents = ["{"] } ` +var testAccPolicyDocumentExpectedJSONNoStatement = `{ + "Version": "2012-10-17" +}` + func testAccPolicyDocumentExpectedJSONStatementPrincipalIdentifiersMultiplePrincipals() string { return fmt.Sprintf(`{ "Version": "2012-10-17", diff --git a/internal/service/iam/policy_model.go b/internal/service/iam/policy_model.go index ab404f9fa36..7739c25f6da 100644 --- a/internal/service/iam/policy_model.go +++ b/internal/service/iam/policy_model.go @@ -13,7 +13,7 @@ const ( type IAMPolicyDoc struct { Version string `json:",omitempty"` Id string `json:",omitempty"` - Statements []*IAMPolicyStatement `json:"Statement"` + Statements []*IAMPolicyStatement `json:"Statement,omitempty"` } type IAMPolicyStatement struct {