forked from hashicorp/terraform-provider-aws
/
resource_aws_ssm_patch_group.go
95 lines (74 loc) · 2.08 KB
/
resource_aws_ssm_patch_group.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
package aws
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsSsmPatchGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSsmPatchGroupCreate,
Read: resourceAwsSsmPatchGroupRead,
Delete: resourceAwsSsmPatchGroupDelete,
Schema: map[string]*schema.Schema{
"baseline_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"patch_group": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsSsmPatchGroupCreate(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
params := &ssm.RegisterPatchBaselineForPatchGroupInput{
BaselineId: aws.String(d.Get("baseline_id").(string)),
PatchGroup: aws.String(d.Get("patch_group").(string)),
}
resp, err := ssmconn.RegisterPatchBaselineForPatchGroup(params)
if err != nil {
return err
}
d.SetId(*resp.PatchGroup)
return resourceAwsSsmPatchGroupRead(d, meta)
}
func resourceAwsSsmPatchGroupRead(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
params := &ssm.DescribePatchGroupsInput{}
resp, err := ssmconn.DescribePatchGroups(params)
if err != nil {
return err
}
found := false
for _, t := range resp.Mappings {
if *t.PatchGroup == d.Id() {
found = true
d.Set("patch_group", t.PatchGroup)
d.Set("baseline_id", t.BaselineIdentity.BaselineId)
}
}
if !found {
log.Printf("[INFO] Patch Group not found. Removing from state")
d.SetId("")
return nil
}
return nil
}
func resourceAwsSsmPatchGroupDelete(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
log.Printf("[INFO] Deleting SSM Patch Group: %s", d.Id())
params := &ssm.DeregisterPatchBaselineForPatchGroupInput{
BaselineId: aws.String(d.Get("baseline_id").(string)),
PatchGroup: aws.String(d.Get("patch_group").(string)),
}
_, err := ssmconn.DeregisterPatchBaselineForPatchGroup(params)
if err != nil {
return err
}
return nil
}