Skip to content

Commit

Permalink
Merge pull request #23757 from DrFaust92/ssm-data-doc-arn
Browse files Browse the repository at this point in the history
d/ssm_document - dont generate `arn` for aws managed docs
  • Loading branch information
ewbankkit authored Mar 21, 2022
2 parents 214e58c + 7425f31 commit a9015a2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 23 deletions.
7 changes: 7 additions & 0 deletions .changelog/23757.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
data-source/aws_ssm_document: Support `TEXT` `document_format`
```

```release-note:bug
data-source/aws_ssm_document: Dont generate `arn` for AWS managed docs.
```
42 changes: 22 additions & 20 deletions internal/service/ssm/document_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ssm
import (
"fmt"
"log"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
Expand All @@ -25,13 +26,10 @@ func DataSourceDocument() *schema.Resource {
Computed: true,
},
"document_format": {
Type: schema.TypeString,
Optional: true,
Default: ssm.DocumentFormatJson,
ValidateFunc: validation.StringInSlice([]string{
ssm.DocumentFormatJson,
ssm.DocumentFormatYaml,
}, false),
Type: schema.TypeString,
Optional: true,
Default: ssm.DocumentFormatJson,
ValidateFunc: validation.StringInSlice(ssm.DocumentFormat_Values(), false),
},
"document_type": {
Type: schema.TypeString,
Expand All @@ -52,10 +50,8 @@ func DataSourceDocument() *schema.Resource {
func dataDocumentRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).SSMConn

name := d.Get("name").(string)

docInput := &ssm.GetDocumentInput{
Name: aws.String(name),
Name: aws.String(d.Get("name").(string)),
DocumentFormat: aws.String(d.Get("document_format").(string)),
}

Expand All @@ -70,18 +66,24 @@ func dataDocumentRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error reading SSM Document: %w", err)
}

d.SetId(aws.StringValue(resp.Name))
name := aws.StringValue(resp.Name)

arn := arn.ARN{
Partition: meta.(*conns.AWSClient).Partition,
Service: "ssm",
Region: meta.(*conns.AWSClient).Region,
AccountID: meta.(*conns.AWSClient).AccountID,
Resource: fmt.Sprintf("document/%s", aws.StringValue(resp.Name)),
}.String()
d.SetId(name)

if !strings.HasPrefix(name, "AWS-") {
arn := arn.ARN{
Partition: meta.(*conns.AWSClient).Partition,
Service: "ssm",
Region: meta.(*conns.AWSClient).Region,
AccountID: meta.(*conns.AWSClient).AccountID,
Resource: fmt.Sprintf("document/%s", name),
}.String()
d.Set("arn", arn)
} else {
d.Set("arn", name)
}

d.Set("arn", arn)
d.Set("name", resp.Name)
d.Set("name", name)
d.Set("content", resp.Content)
d.Set("document_version", resp.DocumentVersion)
d.Set("document_format", resp.DocumentFormat)
Expand Down
29 changes: 28 additions & 1 deletion internal/service/ssm/document_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,26 @@ func TestAccSSMDocumentDataSource_basic(t *testing.T) {
})
}

func testAccCheckDocumentDataSourceConfig(name string, documentFormat string) string {
func TestAccSSMDocumentDataSource_awsManaged(t *testing.T) {
resourceName := "data.aws_ssm_document.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, ssm.EndpointsID),
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccCheckDocumentDataSourceAWSManagedDocumentConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", "AWS-StartEC2Instance"),
resource.TestCheckResourceAttr(resourceName, "arn", "AWS-StartEC2Instance"),
),
},
},
})
}

func testAccCheckDocumentDataSourceConfig(name, documentFormat string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "test" {
name = "%s"
Expand Down Expand Up @@ -78,3 +97,11 @@ data "aws_ssm_document" "test" {
}
`, name, documentFormat)
}

func testAccCheckDocumentDataSourceAWSManagedDocumentConfig() string {
return `
data "aws_ssm_document" "test" {
name = "AWS-StartEC2Instance"
}
`
}
4 changes: 2 additions & 2 deletions website/docs/d/ssm_document.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ data "aws_ssm_document" "test" {
The following arguments are supported:

* `name` - (Required) The name of the Systems Manager document.
* `document_format` - (Optional) Returns the document in the specified format. The document format can be either JSON or YAML. JSON is the default format.
* `document_format` - (Optional) Returns the document in the specified format. The document format can be either `JSON`, `YAML` and `TEXT`. JSON is the default format.
* `document_version` - (Optional) The document version for which you want information.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - The ARN of the document.
* `arn` - The ARN of the document. If the document is an AWS managed document, this value will be set to the name of the document instead.
* `content` - The contents of the document.
* `document_type` - The type of the document.

0 comments on commit a9015a2

Please sign in to comment.