opentofu/builtin/providers/aws/resource_aws_ssm_association.go
Paul Stack 9fc577547c provider/aws: Fix the panic in ssm_association with parameters (#12215)
Fixes: #12205

You cannot use an index of an empty slide therefore, we got a panic as follows:

```
aws_ssm_association.foo: Creating...
  instance_id:              "" => "i-002f3898dc95350e7"
  name:                     "" => "test_document_association-%s"
  parameters.%:             "" => "2"
  parameters.directoryId:   "" => "d-926720980b"
  parameters.directoryName: "" => "corp.mydomain.com"
Error applying plan:

1 error(s) occurred:

* aws_ssm_association.foo: 1 error(s) occurred:

* aws_ssm_association.foo: unexpected EOF

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.
panic: runtime error: index out of range
2017/02/23 21:41:45 [DEBUG] plugin: terraform-provider-aws:
2017/02/23 21:41:45 [DEBUG] plugin: terraform-provider-aws: goroutine 1419 [running]:
2017/02/23 21:41:45 [DEBUG] plugin: terraform-provider-aws: panic(0x1567480, 0xc42000c110)
2017/02/23 21:41:45 [DEBUG] plugin: terraform-provider-aws: 	/usr/local/Cellar/go/1.7.4_1/libexec/src/runtime/panic.go:500 +0x1a1
```
2017-02-23 22:22:14 +02:00

124 lines
3.1 KiB
Go

package aws
import (
"fmt"
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsSsmAssociation() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSsmAssociationCreate,
Read: resourceAwsSsmAssociationRead,
Delete: resourceAwsSsmAssociationDelete,
Schema: map[string]*schema.Schema{
"instance_id": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"parameters": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
ForceNew: true,
Computed: true,
},
},
}
}
func resourceAwsSsmAssociationCreate(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
log.Printf("[DEBUG] SSM association create: %s", d.Id())
assosciationInput := &ssm.CreateAssociationInput{
Name: aws.String(d.Get("name").(string)),
InstanceId: aws.String(d.Get("instance_id").(string)),
}
if v, ok := d.GetOk("parameters"); ok {
assosciationInput.Parameters = expandSSMDocumentParameters(v.(map[string]interface{}))
}
resp, err := ssmconn.CreateAssociation(assosciationInput)
if err != nil {
return errwrap.Wrapf("[ERROR] Error creating SSM association: {{err}}", err)
}
if resp.AssociationDescription == nil {
return fmt.Errorf("[ERROR] AssociationDescription was nil")
}
d.SetId(*resp.AssociationDescription.Name)
return resourceAwsSsmAssociationRead(d, meta)
}
func resourceAwsSsmAssociationRead(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
log.Printf("[DEBUG] Reading SSM Assosciation: %s", d.Id())
params := &ssm.DescribeAssociationInput{
Name: aws.String(d.Get("name").(string)),
InstanceId: aws.String(d.Get("instance_id").(string)),
}
resp, err := ssmconn.DescribeAssociation(params)
if err != nil {
return errwrap.Wrapf("[ERROR] Error reading SSM association: {{err}}", err)
}
if resp.AssociationDescription == nil {
return fmt.Errorf("[ERROR] AssociationDescription was nil")
}
association := resp.AssociationDescription
d.Set("instance_id", association.InstanceId)
d.Set("name", association.Name)
d.Set("parameters", association.Parameters)
return nil
}
func resourceAwsSsmAssociationDelete(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
log.Printf("[DEBUG] Deleting SSM Assosciation: %s", d.Id())
params := &ssm.DeleteAssociationInput{
Name: aws.String(d.Get("name").(string)),
InstanceId: aws.String(d.Get("instance_id").(string)),
}
_, err := ssmconn.DeleteAssociation(params)
if err != nil {
return errwrap.Wrapf("[ERROR] Error deleting SSM association: {{err}}", err)
}
return nil
}
func expandSSMDocumentParameters(params map[string]interface{}) map[string][]*string {
var docParams = make(map[string][]*string)
for k, v := range params {
values := make([]*string, 1)
values[0] = aws.String(v.(string))
docParams[k] = values
}
return docParams
}