forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_aws_ssm_association.go
123 lines (96 loc) · 3.09 KB
/
resource_aws_ssm_association.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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
}