github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/aws/resource_aws_ssm_association.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/ssm"
     9  	"github.com/hashicorp/errwrap"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceAwsSsmAssociation() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsSsmAssociationCreate,
    16  		Read:   resourceAwsSsmAssociationRead,
    17  		Delete: resourceAwsSsmAssociationDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"association_id": {
    21  				Type:     schema.TypeString,
    22  				Computed: true,
    23  			},
    24  			"instance_id": {
    25  				Type:     schema.TypeString,
    26  				ForceNew: true,
    27  				Optional: true,
    28  			},
    29  			"name": {
    30  				Type:     schema.TypeString,
    31  				ForceNew: true,
    32  				Required: true,
    33  			},
    34  			"parameters": {
    35  				Type:     schema.TypeMap,
    36  				Optional: true,
    37  				ForceNew: true,
    38  				Computed: true,
    39  			},
    40  			"targets": {
    41  				Type:     schema.TypeList,
    42  				Optional: true,
    43  				ForceNew: true,
    44  				Computed: true,
    45  				MaxItems: 1,
    46  				Elem: &schema.Resource{
    47  					Schema: map[string]*schema.Schema{
    48  						"key": {
    49  							Type:     schema.TypeString,
    50  							Required: true,
    51  						},
    52  						"values": {
    53  							Type:     schema.TypeList,
    54  							Required: true,
    55  							Elem:     &schema.Schema{Type: schema.TypeString},
    56  						},
    57  					},
    58  				},
    59  			},
    60  		},
    61  	}
    62  }
    63  
    64  func resourceAwsSsmAssociationCreate(d *schema.ResourceData, meta interface{}) error {
    65  	ssmconn := meta.(*AWSClient).ssmconn
    66  
    67  	log.Printf("[DEBUG] SSM association create: %s", d.Id())
    68  
    69  	assosciationInput := &ssm.CreateAssociationInput{
    70  		Name: aws.String(d.Get("name").(string)),
    71  	}
    72  
    73  	if v, ok := d.GetOk("instance_id"); ok {
    74  		assosciationInput.InstanceId = aws.String(v.(string))
    75  	}
    76  
    77  	if v, ok := d.GetOk("parameters"); ok {
    78  		assosciationInput.Parameters = expandSSMDocumentParameters(v.(map[string]interface{}))
    79  	}
    80  
    81  	if _, ok := d.GetOk("targets"); ok {
    82  		assosciationInput.Targets = expandAwsSsmTargets(d)
    83  	}
    84  
    85  	resp, err := ssmconn.CreateAssociation(assosciationInput)
    86  	if err != nil {
    87  		return errwrap.Wrapf("[ERROR] Error creating SSM association: {{err}}", err)
    88  	}
    89  
    90  	if resp.AssociationDescription == nil {
    91  		return fmt.Errorf("[ERROR] AssociationDescription was nil")
    92  	}
    93  
    94  	d.SetId(*resp.AssociationDescription.Name)
    95  	d.Set("association_id", resp.AssociationDescription.AssociationId)
    96  
    97  	return resourceAwsSsmAssociationRead(d, meta)
    98  }
    99  
   100  func resourceAwsSsmAssociationRead(d *schema.ResourceData, meta interface{}) error {
   101  	ssmconn := meta.(*AWSClient).ssmconn
   102  
   103  	log.Printf("[DEBUG] Reading SSM Association: %s", d.Id())
   104  
   105  	params := &ssm.DescribeAssociationInput{
   106  		AssociationId: aws.String(d.Get("association_id").(string)),
   107  	}
   108  
   109  	resp, err := ssmconn.DescribeAssociation(params)
   110  
   111  	if err != nil {
   112  		return errwrap.Wrapf("[ERROR] Error reading SSM association: {{err}}", err)
   113  	}
   114  	if resp.AssociationDescription == nil {
   115  		return fmt.Errorf("[ERROR] AssociationDescription was nil")
   116  	}
   117  
   118  	association := resp.AssociationDescription
   119  	d.Set("instance_id", association.InstanceId)
   120  	d.Set("name", association.Name)
   121  	d.Set("parameters", association.Parameters)
   122  	d.Set("association_id", association.AssociationId)
   123  
   124  	if err := d.Set("targets", flattenAwsSsmTargets(association.Targets)); err != nil {
   125  		return fmt.Errorf("[DEBUG] Error setting targets error: %#v", err)
   126  	}
   127  
   128  	return nil
   129  }
   130  
   131  func resourceAwsSsmAssociationDelete(d *schema.ResourceData, meta interface{}) error {
   132  	ssmconn := meta.(*AWSClient).ssmconn
   133  
   134  	log.Printf("[DEBUG] Deleting SSM Assosciation: %s", d.Id())
   135  
   136  	params := &ssm.DeleteAssociationInput{
   137  		AssociationId: aws.String(d.Get("association_id").(string)),
   138  	}
   139  
   140  	_, err := ssmconn.DeleteAssociation(params)
   141  
   142  	if err != nil {
   143  		return errwrap.Wrapf("[ERROR] Error deleting SSM association: {{err}}", err)
   144  	}
   145  
   146  	return nil
   147  }
   148  
   149  func expandSSMDocumentParameters(params map[string]interface{}) map[string][]*string {
   150  	var docParams = make(map[string][]*string)
   151  	for k, v := range params {
   152  		values := make([]*string, 1)
   153  		values[0] = aws.String(v.(string))
   154  		docParams[k] = values
   155  	}
   156  
   157  	return docParams
   158  }