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

New Datasource: aws_ssm_document #6479

Merged
merged 4 commits into from
Nov 25, 2018
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
90 changes: 90 additions & 0 deletions aws/data_source_aws_ssm_document.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func dataSourceAwsSsmDocument() *schema.Resource {
return &schema.Resource{
Read: dataAwsSsmDocumentRead,
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"content": {
Type: schema.TypeString,
Computed: true,
},
"document_format": {
Type: schema.TypeString,
Optional: true,
Default: ssm.DocumentFormatJson,
ValidateFunc: validation.StringInSlice([]string{
ssm.DocumentFormatJson,
ssm.DocumentFormatYaml,
}, false),
},
"document_type": {
Type: schema.TypeString,
Computed: true,
},
"document_version": {
Type: schema.TypeString,
Optional: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataAwsSsmDocumentRead(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn

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

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

if docVersion, ok := d.GetOk("document_version"); ok {
docInput.DocumentVersion = aws.String(docVersion.(string))
}

log.Printf("[DEBUG] Reading SSM Document: %s", docInput)
resp, err := ssmconn.GetDocument(docInput)

if err != nil {
return fmt.Errorf("Error reading SSM Document: %s", err)
}

d.SetId(aws.StringValue(resp.Name))

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

d.Set("arn", arn)
d.Set("name", resp.Name)
d.Set("content", resp.Content)
d.Set("document_version", resp.DocumentVersion)
d.Set("document_format", resp.DocumentFormat)
d.Set("document_type", resp.DocumentType)

return nil
}
76 changes: 76 additions & 0 deletions aws/data_source_aws_ssm_document_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package aws

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAWSSsmDocumentDataSource_basic(t *testing.T) {
resourceName := "data.aws_ssm_document.test"
name := "test_document"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: We should randomize the naming to prevent testing collisions and enable future parallel testing.


resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckAwsSsmDocumentDataSourceConfig(name, "JSON"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "arn", "aws_ssm_document.test", "arn"),
resource.TestCheckResourceAttrPair(resourceName, "name", "aws_ssm_document.test", "name"),
resource.TestCheckResourceAttrPair(resourceName, "document_format", "aws_ssm_document.test", "document_format"),
resource.TestCheckResourceAttr(resourceName, "document_version", "1"),
resource.TestCheckResourceAttrPair(resourceName, "document_type", "aws_ssm_document.test", "document_type"),
resource.TestCheckResourceAttrPair(resourceName, "content", "aws_ssm_document.test", "content"),
),
},
{
Config: testAccCheckAwsSsmDocumentDataSourceConfig(name, "YAML"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(resourceName, "arn", "aws_ssm_document.test", "arn"),
resource.TestCheckResourceAttrPair(resourceName, "name", "aws_ssm_document.test", "name"),
resource.TestCheckResourceAttr(resourceName, "document_format", "YAML"),
resource.TestCheckResourceAttr(resourceName, "document_version", "1"),
resource.TestCheckResourceAttrPair(resourceName, "document_type", "aws_ssm_document.test", "document_type"),
resource.TestCheckResourceAttrSet(resourceName, "content"),
),
},
},
})
}

func testAccCheckAwsSsmDocumentDataSourceConfig(name string, documentFormat string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "test" {
name = "%s"
document_type = "Command"

content = <<DOC
{
"schemaVersion": "1.2",
"description": "Check ip configuration of a Linux instance.",
"parameters": {

},
"runtimeConfig": {
"aws:runShellScript": {
"properties": [
{
"id": "0.aws:runShellScript",
"runCommand": ["ifconfig"]
}
]
}
}
}
DOC
}

data "aws_ssm_document" "test" {
name = "${aws_ssm_document.test.name}"
document_format = "%s"
}
`, name, documentFormat)
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ func Provider() terraform.ResourceProvider {
"aws_secretsmanager_secret_version": dataSourceAwsSecretsManagerSecretVersion(),
"aws_sns_topic": dataSourceAwsSnsTopic(),
"aws_sqs_queue": dataSourceAwsSqsQueue(),
"aws_ssm_document": dataSourceAwsSsmDocument(),
"aws_ssm_parameter": dataSourceAwsSsmParameter(),
"aws_storagegateway_local_disk": dataSourceAwsStorageGatewayLocalDisk(),
"aws_subnet": dataSourceAwsSubnet(),
Expand Down
3 changes: 3 additions & 0 deletions website/aws.erb
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@
<li<%= sidebar_current("docs-aws-datasource-sns-topic") %>>
<a href="/docs/providers/aws/d/sns_topic.html">aws_sns_topic</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-ssm-document") %>>
<a href="/docs/providers/aws/d/ssm_document.html">aws_ssm_document</a>
</li>
<li<%= sidebar_current("docs-aws-datasource-ssm-parameter") %>>
<a href="/docs/providers/aws/d/ssm_parameter.html">aws_ssm_parameter</a>
</li>
Expand Down
51 changes: 51 additions & 0 deletions website/docs/d/ssm_document.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
layout: "aws"
page_title: "AWS: aws_ssm_document"
sidebar_current: "docs-aws-datasource-ssm-document"
description: |-
Provides a SSM Document datasource
---

# Data Source: aws_ssm_document

Gets the contents of the specified Systems Manager document.

## Example Usage

To get the contents of the document owned by AWS.

```hcl
data "aws_ssm_document" "foo" {
name = "AWS-GatherSoftwareInventory"
document_format = "YAML"
}

output "content" {
value = "${data.aws_ssm_document.foo.content}"
}
```
To get the contents of the custom document.

```hcl
data "aws_ssm_document" "test" {
name = "${aws_ssm_document.test.name}"
document_format = "JSON"
}
```


## Argument Reference

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_version` - (Optional) The document version for which you want information.

bflad marked this conversation as resolved.
Show resolved Hide resolved
## Attributes Reference

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

* `arn` - The ARN of the document.
* `content` - The contents of the document.
* `document_type` - The type of the document.