From 51d5e212a869b8265694081f51f61ba2add95979 Mon Sep 17 00:00:00 2001 From: Ryn Daniels Date: Tue, 10 Sep 2019 11:52:59 +0200 Subject: [PATCH] Add import for SSM association --- aws/resource_aws_ssm_association.go | 3 + aws/resource_aws_ssm_association_test.go | 252 ++++++++++++------- website/docs/r/ssm_association.html.markdown | 11 +- 3 files changed, 172 insertions(+), 94 deletions(-) diff --git a/aws/resource_aws_ssm_association.go b/aws/resource_aws_ssm_association.go index 397570d075b..2b434979b40 100644 --- a/aws/resource_aws_ssm_association.go +++ b/aws/resource_aws_ssm_association.go @@ -17,6 +17,9 @@ func resourceAwsSsmAssociation() *schema.Resource { Read: resourceAwsSsmAssociationRead, Update: resourceAwsSsmAssociationUpdate, Delete: resourceAwsSsmAssociationDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, MigrateState: resourceAwsSsmAssociationMigrateState, SchemaVersion: 1, diff --git a/aws/resource_aws_ssm_association_test.go b/aws/resource_aws_ssm_association_test.go index 36c65291591..fbfab05bfbc 100644 --- a/aws/resource_aws_ssm_association_test.go +++ b/aws/resource_aws_ssm_association_test.go @@ -15,6 +15,7 @@ import ( func TestAccAWSSSMAssociation_basic(t *testing.T) { name := fmt.Sprintf("tf-acc-ssm-association-%s", acctest.RandString(10)) + resourceName := "aws_ssm_association.test" deleteSsmAssociaton := func() { ec2conn := testAccProvider.Meta().(*AWSClient).ec2conn @@ -53,14 +54,19 @@ func TestAccAWSSSMAssociation_basic(t *testing.T) { { Config: testAccAWSSSMAssociationBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, { PreConfig: deleteSsmAssociaton, Config: testAccAWSSSMAssociationBasicConfig(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), ), }, }, @@ -69,6 +75,7 @@ func TestAccAWSSSMAssociation_basic(t *testing.T) { func TestAccAWSSSMAssociation_withTargets(t *testing.T) { name := acctest.RandString(10) + resourceName := "aws_ssm_association.test" oneTarget := ` targets { key = "tag:Name" @@ -91,41 +98,46 @@ func TestAccAWSSSMAssociation_withTargets(t *testing.T) { { Config: testAccAWSSSMAssociationBasicConfigWithTargets(name, oneTarget), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "targets.#", "1"), + resourceName, "targets.#", "1"), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "targets.0.key", "tag:Name"), + resourceName, "targets.0.key", "tag:Name"), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "targets.0.values.0", "acceptanceTest"), + resourceName, "targets.0.values.0", "acceptanceTest"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, { Config: testAccAWSSSMAssociationBasicConfigWithTargets(name, twoTargets), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "targets.#", "2"), + resourceName, "targets.#", "2"), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "targets.0.key", "tag:Name"), + resourceName, "targets.0.key", "tag:Name"), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "targets.0.values.0", "acceptanceTest"), + resourceName, "targets.0.values.0", "acceptanceTest"), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "targets.1.key", "tag:ExtraName"), + resourceName, "targets.1.key", "tag:ExtraName"), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "targets.1.values.0", "acceptanceTest"), + resourceName, "targets.1.values.0", "acceptanceTest"), ), }, { Config: testAccAWSSSMAssociationBasicConfigWithTargets(name, oneTarget), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "targets.#", "1"), + resourceName, "targets.#", "1"), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "targets.0.key", "tag:Name"), + resourceName, "targets.0.key", "tag:Name"), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "targets.0.values.0", "acceptanceTest"), + resourceName, "targets.0.values.0", "acceptanceTest"), ), }, }, @@ -134,6 +146,8 @@ func TestAccAWSSSMAssociation_withTargets(t *testing.T) { func TestAccAWSSSMAssociation_withParameters(t *testing.T) { name := acctest.RandString(10) + resourceName := "aws_ssm_association.test" + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -142,17 +156,23 @@ func TestAccAWSSSMAssociation_withParameters(t *testing.T) { { Config: testAccAWSSSMAssociationBasicConfigWithParameters(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "parameters.Directory", "myWorkSpace"), + resourceName, "parameters.Directory", "myWorkSpace"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"parameters"}, + }, { Config: testAccAWSSSMAssociationBasicConfigWithParametersUpdated(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "parameters.Directory", "myWorkSpaceUpdated"), + resourceName, "parameters.Directory", "myWorkSpaceUpdated"), ), }, }, @@ -163,6 +183,8 @@ func TestAccAWSSSMAssociation_withAssociationName(t *testing.T) { assocName1 := acctest.RandString(10) assocName2 := acctest.RandString(10) rName := acctest.RandString(5) + resourceName := "aws_ssm_association.test" + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -171,17 +193,22 @@ func TestAccAWSSSMAssociation_withAssociationName(t *testing.T) { { Config: testAccAWSSSMAssociationBasicConfigWithAssociationName(rName, assocName1), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "association_name", assocName1), + resourceName, "association_name", assocName1), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, { Config: testAccAWSSSMAssociationBasicConfigWithAssociationName(rName, assocName2), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "association_name", assocName2), + resourceName, "association_name", assocName2), ), }, }, @@ -208,6 +235,11 @@ func TestAccAWSSSMAssociation_withAssociationNameAndScheduleExpression(t *testin resource.TestCheckResourceAttr(resourceName, "schedule_expression", scheduleExpression1), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, { Config: testAccAWSSSMAssociationConfigWithAssociationNameAndScheduleExpression(rName, assocName, scheduleExpression2), Check: resource.ComposeTestCheckFunc( @@ -222,6 +254,8 @@ func TestAccAWSSSMAssociation_withAssociationNameAndScheduleExpression(t *testin func TestAccAWSSSMAssociation_withDocumentVersion(t *testing.T) { name := acctest.RandString(10) + resourceName := "aws_ssm_association.test" + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -230,17 +264,24 @@ func TestAccAWSSSMAssociation_withDocumentVersion(t *testing.T) { { Config: testAccAWSSSMAssociationBasicConfigWithDocumentVersion(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "document_version", "1"), + resourceName, "document_version", "1"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, }, }) } func TestAccAWSSSMAssociation_withOutputLocation(t *testing.T) { name := acctest.RandString(10) + resourceName := "aws_ssm_association.test" + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -249,31 +290,36 @@ func TestAccAWSSSMAssociation_withOutputLocation(t *testing.T) { { Config: testAccAWSSSMAssociationBasicConfigWithOutPutLocation(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "output_location.0.s3_bucket_name", fmt.Sprintf("tf-acc-test-ssmoutput-%s", name)), + resourceName, "output_location.0.s3_bucket_name", fmt.Sprintf("tf-acc-test-ssmoutput-%s", name)), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "output_location.0.s3_key_prefix", "SSMAssociation"), + resourceName, "output_location.0.s3_key_prefix", "SSMAssociation"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, { Config: testAccAWSSSMAssociationBasicConfigWithOutPutLocationUpdateBucketName(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "output_location.0.s3_bucket_name", fmt.Sprintf("tf-acc-test-ssmoutput-updated-%s", name)), + resourceName, "output_location.0.s3_bucket_name", fmt.Sprintf("tf-acc-test-ssmoutput-updated-%s", name)), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "output_location.0.s3_key_prefix", "SSMAssociation"), + resourceName, "output_location.0.s3_key_prefix", "SSMAssociation"), ), }, { Config: testAccAWSSSMAssociationBasicConfigWithOutPutLocationUpdateKeyPrefix(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "output_location.0.s3_bucket_name", fmt.Sprintf("tf-acc-test-ssmoutput-updated-%s", name)), + resourceName, "output_location.0.s3_bucket_name", fmt.Sprintf("tf-acc-test-ssmoutput-updated-%s", name)), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "output_location.0.s3_key_prefix", "UpdatedAssociation"), + resourceName, "output_location.0.s3_key_prefix", "UpdatedAssociation"), ), }, }, @@ -282,6 +328,8 @@ func TestAccAWSSSMAssociation_withOutputLocation(t *testing.T) { func TestAccAWSSSMAssociation_withScheduleExpression(t *testing.T) { name := acctest.RandString(10) + resourceName := "aws_ssm_association.test" + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -290,17 +338,22 @@ func TestAccAWSSSMAssociation_withScheduleExpression(t *testing.T) { { Config: testAccAWSSSMAssociationBasicConfigWithScheduleExpression(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "schedule_expression", "cron(0 16 ? * TUE *)"), + resourceName, "schedule_expression", "cron(0 16 ? * TUE *)"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, { Config: testAccAWSSSMAssociationBasicConfigWithScheduleExpressionUpdated(name), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "schedule_expression", "cron(0 16 ? * WED *)"), + resourceName, "schedule_expression", "cron(0 16 ? * WED *)"), ), }, }, @@ -312,6 +365,7 @@ func TestAccAWSSSMAssociation_withComplianceSeverity(t *testing.T) { rName := acctest.RandString(10) compSeverity1 := "HIGH" compSeverity2 := "LOW" + resourceName := "aws_ssm_association.test" resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -321,21 +375,26 @@ func TestAccAWSSSMAssociation_withComplianceSeverity(t *testing.T) { { Config: testAccAWSSSMAssociationBasicConfigWithComplianceSeverity(compSeverity1, rName, assocName), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "association_name", assocName), + resourceName, "association_name", assocName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "compliance_severity", compSeverity1), + resourceName, "compliance_severity", compSeverity1), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, { Config: testAccAWSSSMAssociationBasicConfigWithComplianceSeverity(compSeverity2, rName, assocName), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "association_name", assocName), + resourceName, "association_name", assocName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "compliance_severity", compSeverity2), + resourceName, "compliance_severity", compSeverity2), ), }, }, @@ -344,6 +403,8 @@ func TestAccAWSSSMAssociation_withComplianceSeverity(t *testing.T) { func TestAccAWSSSMAssociation_rateControl(t *testing.T) { name := acctest.RandString(10) + resourceName := "aws_ssm_association.test" + resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -352,21 +413,26 @@ func TestAccAWSSSMAssociation_rateControl(t *testing.T) { { Config: testAccAWSSSMAssociationRateControlConfig(name, "10%"), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "max_concurrency", "10%"), + resourceName, "max_concurrency", "10%"), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "max_errors", "10%"), + resourceName, "max_errors", "10%"), ), }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, { Config: testAccAWSSSMAssociationRateControlConfig(name, "20%"), Check: resource.ComposeTestCheckFunc( - testAccCheckAWSSSMAssociationExists("aws_ssm_association.foo"), + testAccCheckAWSSSMAssociationExists(resourceName), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "max_concurrency", "20%"), + resourceName, "max_concurrency", "20%"), resource.TestCheckResourceAttr( - "aws_ssm_association.foo", "max_errors", "20%"), + resourceName, "max_errors", "20%"), ), }, }, @@ -430,7 +496,7 @@ func testAccCheckAWSSSMAssociationDestroy(s *terraform.State) error { func testAccAWSSSMAssociationBasicConfigWithParameters(rName string) string { return fmt.Sprintf(` -resource "aws_ssm_document" "foo_document" { +resource "aws_ssm_document" "test" { name = "test_document_association-%s" document_type = "Command" @@ -460,8 +526,8 @@ resource "aws_ssm_document" "foo_document" { DOC } -resource "aws_ssm_association" "foo" { - name = "${aws_ssm_document.foo_document.name}" +resource "aws_ssm_association" "test" { + name = "${aws_ssm_document.test.name}" parameters = { Directory = "myWorkSpace" @@ -477,7 +543,7 @@ resource "aws_ssm_association" "foo" { func testAccAWSSSMAssociationBasicConfigWithParametersUpdated(rName string) string { return fmt.Sprintf(` -resource "aws_ssm_document" "foo_document" { +resource "aws_ssm_document" "test" { name = "test_document_association-%s" document_type = "Command" @@ -507,8 +573,8 @@ resource "aws_ssm_document" "foo_document" { DOC } -resource "aws_ssm_association" "foo" { - name = "${aws_ssm_document.foo_document.name}" +resource "aws_ssm_association" "test" { + name = "${aws_ssm_document.test.name}" parameters = { Directory = "myWorkSpaceUpdated" @@ -524,7 +590,7 @@ resource "aws_ssm_association" "foo" { func testAccAWSSSMAssociationBasicConfigWithTargets(rName, targetsStr string) string { return fmt.Sprintf(` -resource "aws_ssm_document" "foo_document" { +resource "aws_ssm_document" "test" { name = "test_document_association-%s" document_type = "Command" content = <