Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

d/ssm_document - dont generate arn for aws managed docs #23757

Merged
merged 3 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.