github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_ssm_document_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/aws/awserr"
     9  	"github.com/aws/aws-sdk-go/service/ssm"
    10  	"github.com/hashicorp/terraform/helper/acctest"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestAccAWSSSMDocument_basic(t *testing.T) {
    16  	name := acctest.RandString(10)
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSSSMDocumentDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSSSMDocumentBasicConfig(name),
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSSSMDocumentExists("aws_ssm_document.foo"),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccAWSSSMDocument_permission(t *testing.T) {
    33  	name := acctest.RandString(10)
    34  	resource.Test(t, resource.TestCase{
    35  		PreCheck:     func() { testAccPreCheck(t) },
    36  		Providers:    testAccProviders,
    37  		CheckDestroy: testAccCheckAWSSSMDocumentDestroy,
    38  		Steps: []resource.TestStep{
    39  			resource.TestStep{
    40  				Config: testAccAWSSSMDocumentPermissionConfig(name),
    41  				Check: resource.ComposeTestCheckFunc(
    42  					testAccCheckAWSSSMDocumentExists("aws_ssm_document.foo"),
    43  					resource.TestCheckResourceAttr(
    44  						"aws_ssm_document.foo", "permissions.type", "Share"),
    45  					resource.TestCheckResourceAttr(
    46  						"aws_ssm_document.foo", "permissions.account_ids", "all"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func TestAccAWSSSMDocument_params(t *testing.T) {
    54  	name := acctest.RandString(10)
    55  	resource.Test(t, resource.TestCase{
    56  		PreCheck:     func() { testAccPreCheck(t) },
    57  		Providers:    testAccProviders,
    58  		CheckDestroy: testAccCheckAWSSSMDocumentDestroy,
    59  		Steps: []resource.TestStep{
    60  			resource.TestStep{
    61  				Config: testAccAWSSSMDocumentParamConfig(name),
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testAccCheckAWSSSMDocumentExists("aws_ssm_document.foo"),
    64  					resource.TestCheckResourceAttr(
    65  						"aws_ssm_document.foo", "parameter.0.name", "commands"),
    66  					resource.TestCheckResourceAttr(
    67  						"aws_ssm_document.foo", "parameter.0.type", "StringList"),
    68  					resource.TestCheckResourceAttr(
    69  						"aws_ssm_document.foo", "parameter.1.name", "workingDirectory"),
    70  					resource.TestCheckResourceAttr(
    71  						"aws_ssm_document.foo", "parameter.1.type", "String"),
    72  					resource.TestCheckResourceAttr(
    73  						"aws_ssm_document.foo", "parameter.2.name", "executionTimeout"),
    74  					resource.TestCheckResourceAttr(
    75  						"aws_ssm_document.foo", "parameter.2.type", "String"),
    76  				),
    77  			},
    78  		},
    79  	})
    80  }
    81  
    82  func testAccCheckAWSSSMDocumentExists(n string) resource.TestCheckFunc {
    83  	return func(s *terraform.State) error {
    84  		rs, ok := s.RootModule().Resources[n]
    85  		if !ok {
    86  			return fmt.Errorf("Not found: %s", n)
    87  		}
    88  
    89  		if rs.Primary.ID == "" {
    90  			return fmt.Errorf("No SSM Document ID is set")
    91  		}
    92  
    93  		conn := testAccProvider.Meta().(*AWSClient).ssmconn
    94  
    95  		_, err := conn.DescribeDocument(&ssm.DescribeDocumentInput{
    96  			Name: aws.String(rs.Primary.ID),
    97  		})
    98  		if err != nil {
    99  			return err
   100  		}
   101  
   102  		return nil
   103  	}
   104  }
   105  
   106  func testAccCheckAWSSSMDocumentDestroy(s *terraform.State) error {
   107  	conn := testAccProvider.Meta().(*AWSClient).ssmconn
   108  
   109  	for _, rs := range s.RootModule().Resources {
   110  		if rs.Type != "aws_ssm_document" {
   111  			continue
   112  		}
   113  
   114  		out, err := conn.DescribeDocument(&ssm.DescribeDocumentInput{
   115  			Name: aws.String(rs.Primary.Attributes["name"]),
   116  		})
   117  
   118  		if err != nil {
   119  			// InvalidDocument means it's gone, this is good
   120  			if wserr, ok := err.(awserr.Error); ok && wserr.Code() == "InvalidDocument" {
   121  				return nil
   122  			}
   123  			return err
   124  		}
   125  
   126  		if out != nil {
   127  			return fmt.Errorf("Expected AWS SSM Document to be gone, but was still found")
   128  		}
   129  
   130  		return nil
   131  	}
   132  
   133  	return fmt.Errorf("Default error in SSM Document Test")
   134  }
   135  
   136  /*
   137  Based on examples from here: https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/create-ssm-doc.html
   138  */
   139  
   140  func testAccAWSSSMDocumentBasicConfig(rName string) string {
   141  	return fmt.Sprintf(`
   142  resource "aws_ssm_document" "foo" {
   143    name = "test_document-%s"
   144  
   145    content = <<DOC
   146      {
   147        "schemaVersion": "1.2",
   148        "description": "Check ip configuration of a Linux instance.",
   149        "parameters": {
   150  
   151        },
   152        "runtimeConfig": {
   153          "aws:runShellScript": {
   154            "properties": [
   155              {
   156                "id": "0.aws:runShellScript",
   157                "runCommand": ["ifconfig"]
   158              }
   159            ]
   160          }
   161        }
   162      }
   163  DOC
   164  }
   165  
   166  `, rName)
   167  }
   168  
   169  func testAccAWSSSMDocumentPermissionConfig(rName string) string {
   170  	return fmt.Sprintf(`
   171  resource "aws_ssm_document" "foo" {
   172    name = "test_document-%s"
   173  
   174    permissions = {
   175      type        = "Share"
   176      account_ids = "all"
   177    }
   178  
   179    content = <<DOC
   180      {
   181        "schemaVersion": "1.2",
   182        "description": "Check ip configuration of a Linux instance.",
   183        "parameters": {
   184  
   185        },
   186        "runtimeConfig": {
   187          "aws:runShellScript": {
   188            "properties": [
   189              {
   190                "id": "0.aws:runShellScript",
   191                "runCommand": ["ifconfig"]
   192              }
   193            ]
   194          }
   195        }
   196      }
   197  DOC
   198  }
   199  `, rName)
   200  }
   201  
   202  func testAccAWSSSMDocumentParamConfig(rName string) string {
   203  	return fmt.Sprintf(`
   204  resource "aws_ssm_document" "foo" {
   205    name = "test_document-%s"
   206  
   207    content = <<DOC
   208  		{
   209  		    "schemaVersion":"1.2",
   210  		    "description":"Run a PowerShell script or specify the paths to scripts to run.",
   211  		    "parameters":{
   212  		        "commands":{
   213  		            "type":"StringList",
   214  		            "description":"(Required) Specify the commands to run or the paths to existing scripts on the instance.",
   215  		            "minItems":1,
   216  		            "displayType":"textarea"
   217  		        },
   218  		        "workingDirectory":{
   219  		            "type":"String",
   220  		            "default":"",
   221  		            "description":"(Optional) The path to the working directory on your instance.",
   222  		            "maxChars":4096
   223  		        },
   224  		        "executionTimeout":{
   225  		            "type":"String",
   226  		            "default":"3600",
   227  		            "description":"(Optional) The time in seconds for a command to be completed before it is considered to have failed. Default is 3600 (1 hour). Maximum is 28800 (8 hours).",
   228  		            "allowedPattern":"([1-9][0-9]{0,3})|(1[0-9]{1,4})|(2[0-7][0-9]{1,3})|(28[0-7][0-9]{1,2})|(28800)"
   229  		        }
   230  		    },
   231  		    "runtimeConfig":{
   232  		        "aws:runPowerShellScript":{
   233  		            "properties":[
   234  		                {
   235  		                    "id":"0.aws:runPowerShellScript",
   236  		                    "runCommand":"{{ commands }}",
   237  		                    "workingDirectory":"{{ workingDirectory }}",
   238  		                    "timeoutSeconds":"{{ executionTimeout }}"
   239  		                }
   240  		            ]
   241  		        }
   242  		    }
   243  		}
   244  DOC
   245  }
   246  
   247  `, rName)
   248  }