Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/aws: Add support for aws_ssm_patch_baseline #14954

Merged
merged 3 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ func Provider() terraform.ResourceProvider {
"aws_ssm_maintenance_window": resourceAwsSsmMaintenanceWindow(),
"aws_ssm_maintenance_window_target": resourceAwsSsmMaintenanceWindowTarget(),
"aws_ssm_maintenance_window_task": resourceAwsSsmMaintenanceWindowTask(),
"aws_ssm_patch_baseline": resourceAwsSsmPatchBaseline(),
"aws_ssm_patch_group": resourceAwsSsmPatchGroup(),
"aws_spot_datafeed_subscription": resourceAwsSpotDataFeedSubscription(),
"aws_spot_instance_request": resourceAwsSpotInstanceRequest(),
"aws_spot_fleet_request": resourceAwsSpotFleetRequest(),
Expand Down
277 changes: 277 additions & 0 deletions builtin/providers/aws/resource_aws_ssm_patch_baseline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsSsmPatchBaseline() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSsmPatchBaselineCreate,
Read: resourceAwsSsmPatchBaselineRead,
Delete: resourceAwsSsmPatchBaselineDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},

"global_filter": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 4,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
},
"values": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},

"approval_rule": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"approve_after_days": {
Type: schema.TypeInt,
Required: true,
},

"patch_filter": {
Type: schema.TypeList,
Required: true,
MaxItems: 10,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
},
"values": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
},
},

"approved_patches": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"rejected_patches": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}

func resourceAwsSsmPatchBaselineCreate(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn

params := &ssm.CreatePatchBaselineInput{
Name: aws.String(d.Get("name").(string)),
}

if v, ok := d.GetOk("description"); ok {
params.Description = aws.String(v.(string))
}

if v, ok := d.GetOk("approved_patches"); ok && v.(*schema.Set).Len() > 0 {
params.ApprovedPatches = expandStringList(v.(*schema.Set).List())
}

if v, ok := d.GetOk("rejected_patches"); ok && v.(*schema.Set).Len() > 0 {
params.RejectedPatches = expandStringList(v.(*schema.Set).List())
}

if _, ok := d.GetOk("global_filter"); ok {
params.GlobalFilters = expandAwsSsmPatchFilterGroup(d)
}

if _, ok := d.GetOk("approval_rule"); ok {
params.ApprovalRules = expandAwsSsmPatchRuleGroup(d)
}

resp, err := ssmconn.CreatePatchBaseline(params)
if err != nil {
return err
}

d.SetId(*resp.BaselineId)
return resourceAwsSsmPatchBaselineRead(d, meta)
}

func resourceAwsSsmPatchBaselineRead(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn

params := &ssm.GetPatchBaselineInput{
BaselineId: aws.String(d.Id()),
}

resp, err := ssmconn.GetPatchBaseline(params)
if err != nil {
return err
}

d.Set("name", resp.Name)
d.Set("description", resp.Description)
d.Set("approved_patches", flattenStringList(resp.ApprovedPatches))
d.Set("rejected_patches", flattenStringList(resp.RejectedPatches))

if err := d.Set("global_filter", flattenAwsSsmPatchFilterGroup(resp.GlobalFilters)); err != nil {
return fmt.Errorf("[DEBUG] Error setting global filters error: %#v", err)
}

if err := d.Set("approval_rule", flattenAwsSsmPatchRuleGroup(resp.ApprovalRules)); err != nil {
return fmt.Errorf("[DEBUG] Error setting approval rules error: %#v", err)
}

return nil
}

func resourceAwsSsmPatchBaselineDelete(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn

log.Printf("[INFO] Deleting SSM Patch Baseline: %s", d.Id())

params := &ssm.DeletePatchBaselineInput{
BaselineId: aws.String(d.Id()),
}

_, err := ssmconn.DeletePatchBaseline(params)
if err != nil {
return err
}

return nil
}

func expandAwsSsmPatchFilterGroup(d *schema.ResourceData) *ssm.PatchFilterGroup {
var filters []*ssm.PatchFilter

filterConfig := d.Get("global_filter").([]interface{})

for _, fConfig := range filterConfig {
config := fConfig.(map[string]interface{})

filter := &ssm.PatchFilter{
Key: aws.String(config["key"].(string)),
Values: expandStringList(config["values"].([]interface{})),
}

filters = append(filters, filter)
}

return &ssm.PatchFilterGroup{
PatchFilters: filters,
}
}

func flattenAwsSsmPatchFilterGroup(group *ssm.PatchFilterGroup) []map[string]interface{} {
if len(group.PatchFilters) == 0 {
return nil
}

result := make([]map[string]interface{}, 0, len(group.PatchFilters))

for _, filter := range group.PatchFilters {
f := make(map[string]interface{})
f["key"] = *filter.Key
f["values"] = flattenStringList(filter.Values)

result = append(result, f)
}

return result
}

func expandAwsSsmPatchRuleGroup(d *schema.ResourceData) *ssm.PatchRuleGroup {
var rules []*ssm.PatchRule

ruleConfig := d.Get("approval_rule").([]interface{})

for _, rConfig := range ruleConfig {
rCfg := rConfig.(map[string]interface{})

var filters []*ssm.PatchFilter
filterConfig := rCfg["patch_filter"].([]interface{})

for _, fConfig := range filterConfig {
fCfg := fConfig.(map[string]interface{})

filter := &ssm.PatchFilter{
Key: aws.String(fCfg["key"].(string)),
Values: expandStringList(fCfg["values"].([]interface{})),
}

filters = append(filters, filter)
}

filterGroup := &ssm.PatchFilterGroup{
PatchFilters: filters,
}

rule := &ssm.PatchRule{
ApproveAfterDays: aws.Int64(int64(rCfg["approve_after_days"].(int))),
PatchFilterGroup: filterGroup,
}

rules = append(rules, rule)
}

return &ssm.PatchRuleGroup{
PatchRules: rules,
}
}

func flattenAwsSsmPatchRuleGroup(group *ssm.PatchRuleGroup) []map[string]interface{} {
if len(group.PatchRules) == 0 {
return nil
}

result := make([]map[string]interface{}, 0, len(group.PatchRules))

for _, rule := range group.PatchRules {
r := make(map[string]interface{})
r["approve_after_days"] = *rule.ApproveAfterDays
r["patch_filter"] = flattenAwsSsmPatchFilterGroup(rule.PatchFilterGroup)
result = append(result, r)
}

return result
}
Loading