Latest updates to aws_ssm_document resource. (#11671)

A parameter `document_type` and a few more attributes returned based
upon the new API.
This commit is contained in:
Liam Bennett 2017-02-08 12:45:38 +00:00 committed by Paul Stack
parent cb9102d550
commit d760f2102a
4 changed files with 198 additions and 16 deletions

View File

@ -106,6 +106,7 @@ resource "aws_instance" "foo" {
resource "aws_ssm_document" "foo_document" {
name = "test_document_association-%s",
document_type = "Command"
content = <<DOC
{
"schemaVersion": "1.2",

View File

@ -24,18 +24,25 @@ func resourceAwsSsmDocument() *schema.Resource {
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"content": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"document_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAwsSSMDocumentType,
},
"created_date": {
Type: schema.TypeString,
Computed: true,
},
"default_version": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
@ -48,6 +55,10 @@ func resourceAwsSsmDocument() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"latest_version": {
Type: schema.TypeString,
Computed: true,
},
"owner": {
Type: schema.TypeString,
Computed: true,
@ -56,9 +67,10 @@ func resourceAwsSsmDocument() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"platform_type": {
Type: schema.TypeString,
"platform_types": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"parameter": {
Type: schema.TypeList,
@ -110,18 +122,27 @@ func resourceAwsSsmDocumentCreate(d *schema.ResourceData, meta interface{}) erro
log.Printf("[INFO] Creating SSM Document: %s", d.Get("name").(string))
docInput := &ssm.CreateDocumentInput{
Name: aws.String(d.Get("name").(string)),
Content: aws.String(d.Get("content").(string)),
Name: aws.String(d.Get("name").(string)),
Content: aws.String(d.Get("content").(string)),
DocumentType: aws.String(d.Get("document_type").(string)),
}
resp, err := ssmconn.CreateDocument(docInput)
log.Printf("[DEBUG] Waiting for SSM Document %q to be created", d.Get("name").(string))
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, err := ssmconn.CreateDocument(docInput)
if err != nil {
return resource.NonRetryableError(err)
}
d.SetId(*resp.DocumentDescription.Name)
return nil
})
if err != nil {
return errwrap.Wrapf("[ERROR] Error creating SSM document: {{err}}", err)
}
d.SetId(*resp.DocumentDescription.Name)
if v, ok := d.GetOk("permissions"); ok && v != nil {
if err := setDocumentPermissions(d, meta); err != nil {
return err
@ -150,12 +171,21 @@ func resourceAwsSsmDocumentRead(d *schema.ResourceData, meta interface{}) error
doc := resp.Document
d.Set("created_date", doc.CreatedDate)
d.Set("default_version", doc.DefaultVersion)
d.Set("description", doc.Description)
if _, ok := d.GetOk("document_type"); ok {
d.Set("document_type", doc.DocumentType)
}
d.Set("document_version", doc.DocumentVersion)
d.Set("hash", doc.Hash)
d.Set("hash_type", doc.HashType)
d.Set("latest_version", doc.LatestVersion)
d.Set("name", doc.Name)
d.Set("owner", doc.Owner)
d.Set("platform_type", doc.PlatformTypes[0])
d.Set("platform_types", flattenStringList(doc.PlatformTypes))
d.Set("status", doc.Status)
gp, err := getDocumentPermissions(d, meta)
@ -344,3 +374,17 @@ func deleteDocumentPermissions(d *schema.ResourceData, meta interface{}) error {
return nil
}
func validateAwsSSMDocumentType(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
types := map[string]bool{
"Command": true,
"Policy": true,
"Automation": true,
}
if !types[value] {
errors = append(errors, fmt.Errorf("CodeBuild: Arifacts Namespace Type can only be NONE / BUILD_ID"))
}
return
}

View File

@ -79,6 +79,25 @@ func TestAccAWSSSMDocument_params(t *testing.T) {
})
}
func TestAccAWSSSMDocument_automation(t *testing.T) {
name := acctest.RandString(10)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMDocumentDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSSSMDocumentTypeAutomationConfig(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMDocumentExists("aws_ssm_document.foo"),
resource.TestCheckResourceAttr(
"aws_ssm_document.foo", "document_type", "Automation"),
),
},
},
})
}
func testAccCheckAWSSSMDocumentExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
@ -141,6 +160,7 @@ func testAccAWSSSMDocumentBasicConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "foo" {
name = "test_document-%s"
document_type = "Command"
content = <<DOC
{
@ -170,6 +190,7 @@ func testAccAWSSSMDocumentPermissionConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "foo" {
name = "test_document-%s"
document_type = "Command"
permissions = {
type = "Share"
@ -203,6 +224,7 @@ func testAccAWSSSMDocumentParamConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "foo" {
name = "test_document-%s"
document_type = "Command"
content = <<DOC
{
@ -246,3 +268,113 @@ DOC
`, rName)
}
func testAccAWSSSMDocumentTypeAutomationConfig(rName string) string {
return fmt.Sprintf(`
data "aws_ami" "ssm_ami" {
most_recent = true
filter {
name = "name"
values = ["*hvm-ssd/ubuntu-trusty-14.04*"]
}
}
resource "aws_iam_instance_profile" "ssm_profile" {
name = "ssm_profile-%s"
roles = ["${aws_iam_role.ssm_role.name}"]
}
resource "aws_iam_role" "ssm_role" {
name = "ssm_role-%s"
path = "/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_ssm_document" "foo" {
name = "test_document-%s"
document_type = "Automation"
content = <<DOC
{
"description": "Systems Manager Automation Demo",
"schemaVersion": "0.3",
"assumeRole": "${aws_iam_role.ssm_role.arn}",
"mainSteps": [
{
"name": "startInstances",
"action": "aws:runInstances",
"timeoutSeconds": 1200,
"maxAttempts": 1,
"onFailure": "Abort",
"inputs": {
"ImageId": "${data.aws_ami.ssm_ami.id}",
"InstanceType": "t2.small",
"MinInstanceCount": 1,
"MaxInstanceCount": 1,
"IamInstanceProfileName": "${aws_iam_instance_profile.ssm_profile.name}"
}
},
{
"name": "stopInstance",
"action": "aws:changeInstanceState",
"maxAttempts": 1,
"onFailure": "Continue",
"inputs": {
"InstanceIds": [
"{{ startInstances.InstanceIds }}"
],
"DesiredState": "stopped"
}
},
{
"name": "terminateInstance",
"action": "aws:changeInstanceState",
"maxAttempts": 1,
"onFailure": "Continue",
"inputs": {
"InstanceIds": [
"{{ startInstances.InstanceIds }}"
],
"DesiredState": "terminated"
}
}
]
}
DOC
}
`, rName, rName, rName)
}
func TestAccAWSSSMDocument_documentTypeValidation(t *testing.T) {
cases := []struct {
Value string
ErrCount int
}{
{Value: "Command", ErrCount: 0},
{Value: "Policy", ErrCount: 0},
{Value: "Automation", ErrCount: 0},
{Value: "XYZ", ErrCount: 1},
}
for _, tc := range cases {
_, errors := validateAwsSSMDocumentType(tc.Value, "aws_ssm_document")
if len(errors) != tc.ErrCount {
t.Fatalf("Expected the AWS SSM Document document_type to trigger a validation error")
}
}
}

View File

@ -14,7 +14,8 @@ Provides an SSM Document resource
```
resource "aws_ssm_document" "foo" {
name = "test_document",
name = "test_document"
document_type = "Command"
content = <<DOC
{
"schemaVersion": "1.2",
@ -43,23 +44,27 @@ The following arguments are supported:
* `name` - (Required) The name of the document.
* `content` - (Required) The json content of the document.
* `document_type` - (Required) The type of the document. Valid document types include: `Command`, `Policy` and `Automation`
* `permission` - (Optional) Additional Permissions to attach to the document. See [Permissions](#permissions) below for details.
## Attributes Reference
The following attributes are exported:
* `name` - The name of the document
* `content` - The json content of the document
* `created_date` - The date the document was created
* `description` - The description of the document
* `name` - The name of the document.
* `content` - The json content of the document.
* `created_date` - The date the document was created.
* `description` - The description of the document.
* `document_type` - The type of document created.
* `default_version` - The default version of the document.
* `hash` - The sha1 or sha256 of the document content
* `hash_type` - "Sha1" "Sha256". The hashing algorithm used when hashing the content.
* `latest_version` - The latest version of the document.
* `owner` - The AWS user account of the person who created the document.
* `status` - "Creating", "Active" or "Deleting". The current status of the document.
* `parameter` - The parameters that are available to this document.
* `permission` - The permissions of how this document should be shared.
* `platform_type` - "Windows" or "Linux". A list of OS platforms compatible with this SSM document.
* `platform_types` - A list of OS platforms compatible with this SSM document, either "Windows" or "Linux".
## Permissions