github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 "instance_id": { 21 Type: schema.TypeString, 22 ForceNew: true, 23 Required: true, 24 }, 25 "name": { 26 Type: schema.TypeString, 27 ForceNew: true, 28 Required: true, 29 }, 30 "parameters": &schema.Schema{ 31 Type: schema.TypeMap, 32 Optional: true, 33 ForceNew: true, 34 Computed: true, 35 }, 36 }, 37 } 38 } 39 40 func resourceAwsSsmAssociationCreate(d *schema.ResourceData, meta interface{}) error { 41 ssmconn := meta.(*AWSClient).ssmconn 42 43 log.Printf("[DEBUG] SSM association create: %s", d.Id()) 44 45 assosciationInput := &ssm.CreateAssociationInput{ 46 Name: aws.String(d.Get("name").(string)), 47 InstanceId: aws.String(d.Get("instance_id").(string)), 48 } 49 50 if v, ok := d.GetOk("parameters"); ok { 51 assosciationInput.Parameters = expandSSMDocumentParameters(v.(map[string]interface{})) 52 } 53 54 resp, err := ssmconn.CreateAssociation(assosciationInput) 55 if err != nil { 56 return errwrap.Wrapf("[ERROR] Error creating SSM association: {{err}}", err) 57 } 58 59 if resp.AssociationDescription == nil { 60 return fmt.Errorf("[ERROR] AssociationDescription was nil") 61 } 62 63 d.SetId(*resp.AssociationDescription.Name) 64 65 return resourceAwsSsmAssociationRead(d, meta) 66 } 67 68 func resourceAwsSsmAssociationRead(d *schema.ResourceData, meta interface{}) error { 69 ssmconn := meta.(*AWSClient).ssmconn 70 71 log.Printf("[DEBUG] Reading SSM Assosciation: %s", d.Id()) 72 73 params := &ssm.DescribeAssociationInput{ 74 Name: aws.String(d.Get("name").(string)), 75 InstanceId: aws.String(d.Get("instance_id").(string)), 76 } 77 78 resp, err := ssmconn.DescribeAssociation(params) 79 80 if err != nil { 81 return errwrap.Wrapf("[ERROR] Error reading SSM association: {{err}}", err) 82 } 83 if resp.AssociationDescription == nil { 84 return fmt.Errorf("[ERROR] AssociationDescription was nil") 85 } 86 87 association := resp.AssociationDescription 88 d.Set("instance_id", association.InstanceId) 89 d.Set("name", association.Name) 90 d.Set("parameters", association.Parameters) 91 92 return nil 93 } 94 95 func resourceAwsSsmAssociationDelete(d *schema.ResourceData, meta interface{}) error { 96 ssmconn := meta.(*AWSClient).ssmconn 97 98 log.Printf("[DEBUG] Deleting SSM Assosciation: %s", d.Id()) 99 100 params := &ssm.DeleteAssociationInput{ 101 Name: aws.String(d.Get("name").(string)), 102 InstanceId: aws.String(d.Get("instance_id").(string)), 103 } 104 105 _, err := ssmconn.DeleteAssociation(params) 106 107 if err != nil { 108 return errwrap.Wrapf("[ERROR] Error deleting SSM association: {{err}}", err) 109 } 110 111 return nil 112 } 113 114 func expandSSMDocumentParameters(params map[string]interface{}) map[string][]*string { 115 var docParams = make(map[string][]*string) 116 for k, v := range params { 117 values := make([]*string, 1) 118 values[0] = aws.String(v.(string)) 119 docParams[k] = values 120 } 121 122 return docParams 123 }