Skip to content

Commit

Permalink
Merge branch 'dpldb-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Aug 30, 2018
2 parents a47ddea + 600ae8a commit 767cdb0
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 25 deletions.
51 changes: 26 additions & 25 deletions aws/resource_aws_ssm_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func resourceAwsSsmAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSsmAssociationCreate,
Read: resourceAwsSsmAssociationRead,
Update: resourceAwsSsmAssocationUpdate,
Update: resourceAwsSsmAssociationUpdate,
Delete: resourceAwsSsmAssociationDelete,

MigrateState: resourceAwsSsmAssociationMigrateState,
Expand Down Expand Up @@ -97,39 +97,39 @@ func resourceAwsSsmAssociationCreate(d *schema.ResourceData, meta interface{}) e

log.Printf("[DEBUG] SSM association create: %s", d.Id())

assosciationInput := &ssm.CreateAssociationInput{
associationInput := &ssm.CreateAssociationInput{
Name: aws.String(d.Get("name").(string)),
}

if v, ok := d.GetOk("association_name"); ok {
assosciationInput.AssociationName = aws.String(v.(string))
associationInput.AssociationName = aws.String(v.(string))
}

if v, ok := d.GetOk("instance_id"); ok {
assosciationInput.InstanceId = aws.String(v.(string))
associationInput.InstanceId = aws.String(v.(string))
}

if v, ok := d.GetOk("document_version"); ok {
assosciationInput.DocumentVersion = aws.String(v.(string))
associationInput.DocumentVersion = aws.String(v.(string))
}

if v, ok := d.GetOk("schedule_expression"); ok {
assosciationInput.ScheduleExpression = aws.String(v.(string))
associationInput.ScheduleExpression = aws.String(v.(string))
}

if v, ok := d.GetOk("parameters"); ok {
assosciationInput.Parameters = expandSSMDocumentParameters(v.(map[string]interface{}))
associationInput.Parameters = expandSSMDocumentParameters(v.(map[string]interface{}))
}

if _, ok := d.GetOk("targets"); ok {
assosciationInput.Targets = expandAwsSsmTargets(d.Get("targets").([]interface{}))
associationInput.Targets = expandAwsSsmTargets(d.Get("targets").([]interface{}))
}

if v, ok := d.GetOk("output_location"); ok {
assosciationInput.OutputLocation = expandSSMAssociationOutputLocation(v.([]interface{}))
associationInput.OutputLocation = expandSSMAssociationOutputLocation(v.([]interface{}))
}

resp, err := ssmconn.CreateAssociation(assosciationInput)
resp, err := ssmconn.CreateAssociation(associationInput)
if err != nil {
return fmt.Errorf("[ERROR] Error creating SSM association: %s", err)
}
Expand Down Expand Up @@ -186,37 +186,38 @@ func resourceAwsSsmAssociationRead(d *schema.ResourceData, meta interface{}) err
return nil
}

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

log.Printf("[DEBUG] SSM association update: %s", d.Id())
log.Printf("[DEBUG] SSM Association update: %s", d.Id())

associationInput := &ssm.UpdateAssociationInput{
AssociationId: aws.String(d.Get("association_id").(string)),
}

if d.HasChange("association_name") {
associationInput.AssociationName = aws.String(d.Get("association_name").(string))
// AWS creates a new version every time the association is updated, so everything should be passed in the update.
if v, ok := d.GetOk("association_name"); ok {
associationInput.AssociationName = aws.String(v.(string))
}

if d.HasChange("schedule_expression") {
associationInput.ScheduleExpression = aws.String(d.Get("schedule_expression").(string))
if v, ok := d.GetOk("document_version"); ok {
associationInput.DocumentVersion = aws.String(v.(string))
}

if d.HasChange("document_version") {
associationInput.DocumentVersion = aws.String(d.Get("document_version").(string))
if v, ok := d.GetOk("schedule_expression"); ok {
associationInput.ScheduleExpression = aws.String(v.(string))
}

if d.HasChange("parameters") {
associationInput.Parameters = expandSSMDocumentParameters(d.Get("parameters").(map[string]interface{}))
if v, ok := d.GetOk("parameters"); ok {
associationInput.Parameters = expandSSMDocumentParameters(v.(map[string]interface{}))
}

if d.HasChange("output_location") {
associationInput.OutputLocation = expandSSMAssociationOutputLocation(d.Get("output_location").([]interface{}))
if _, ok := d.GetOk("targets"); ok {
associationInput.Targets = expandAwsSsmTargets(d.Get("targets").([]interface{}))
}

if d.HasChange("targets") {
associationInput.Targets = expandAwsSsmTargets(d.Get("targets").([]interface{}))
if v, ok := d.GetOk("output_location"); ok {
associationInput.OutputLocation = expandSSMAssociationOutputLocation(v.([]interface{}))
}

_, err := ssmconn.UpdateAssociation(associationInput)
Expand All @@ -230,7 +231,7 @@ func resourceAwsSsmAssocationUpdate(d *schema.ResourceData, meta interface{}) er
func resourceAwsSsmAssociationDelete(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn

log.Printf("[DEBUG] Deleting SSM Assosciation: %s", d.Id())
log.Printf("[DEBUG] Deleting SSM Association: %s", d.Id())

params := &ssm.DeleteAssociationInput{
AssociationId: aws.String(d.Get("association_id").(string)),
Expand Down
70 changes: 70 additions & 0 deletions aws/resource_aws_ssm_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,38 @@ func TestAccAWSSSMAssociation_withAssociationName(t *testing.T) {
})
}

func TestAccAWSSSMAssociation_withAssociationNameAndScheduleExpression(t *testing.T) {
assocName := acctest.RandString(10)
rName := acctest.RandString(5)
resourceName := "aws_ssm_association.test"
scheduleExpression1 := "cron(0 16 ? * TUE *)"
scheduleExpression2 := "cron(0 16 ? * WED *)"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMAssociationDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMAssociationConfigWithAssociationNameAndScheduleExpression(rName, assocName, scheduleExpression1),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMAssociationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "association_name", assocName),
resource.TestCheckResourceAttr(resourceName, "schedule_expression", scheduleExpression1),
),
},
{
Config: testAccAWSSSMAssociationConfigWithAssociationNameAndScheduleExpression(rName, assocName, scheduleExpression2),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMAssociationExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "association_name", assocName),
resource.TestCheckResourceAttr(resourceName, "schedule_expression", scheduleExpression2),
),
},
},
})
}

func TestAccAWSSSMAssociation_withDocumentVersion(t *testing.T) {
name := acctest.RandString(10)
resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -817,3 +849,41 @@ resource "aws_ssm_association" "foo" {
}
`, rName, assocName)
}

func testAccAWSSSMAssociationConfigWithAssociationNameAndScheduleExpression(rName, associationName, scheduleExpression string) string {
return fmt.Sprintf(`
resource "aws_ssm_document" "test" {
name = "test_document_association-%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
}
resource "aws_ssm_association" "test" {
association_name = %q
name = "${aws_ssm_document.test.name}",
schedule_expression = %q
targets {
key = "tag:Name"
values = ["acceptanceTest"]
}
}
`, rName, associationName, scheduleExpression)
}

0 comments on commit 767cdb0

Please sign in to comment.