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_maintenance_window #14087

Merged
merged 6 commits into from
May 4, 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
3 changes: 3 additions & 0 deletions builtin/providers/aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ func Provider() terraform.ResourceProvider {
"aws_ssm_activation": resourceAwsSsmActivation(),
"aws_ssm_association": resourceAwsSsmAssociation(),
"aws_ssm_document": resourceAwsSsmDocument(),
"aws_ssm_maintenance_window": resourceAwsSsmMaintenanceWindow(),
"aws_ssm_maintenance_window_target": resourceAwsSsmMaintenanceWindowTarget(),
"aws_ssm_maintenance_window_task": resourceAwsSsmMaintenanceWindowTask(),
"aws_spot_datafeed_subscription": resourceAwsSpotDataFeedSubscription(),
"aws_spot_instance_request": resourceAwsSpotInstanceRequest(),
"aws_spot_fleet_request": resourceAwsSpotFleetRequest(),
Expand Down
168 changes: 168 additions & 0 deletions builtin/providers/aws/resource_aws_ssm_maintenance_window.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
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 resourceAwsSsmMaintenanceWindow() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSsmMaintenanceWindowCreate,
Read: resourceAwsSsmMaintenanceWindowRead,
Update: resourceAwsSsmMaintenanceWindowUpdate,
Delete: resourceAwsSsmMaintenanceWindowDelete,

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

"schedule": {
Type: schema.TypeString,
Required: true,
},

"duration": {
Type: schema.TypeInt,
Required: true,
},

"cutoff": {
Type: schema.TypeInt,
Required: true,
},

"allow_unassociated_targets": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
},
}
}

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

params := &ssm.CreateMaintenanceWindowInput{
Name: aws.String(d.Get("name").(string)),
Schedule: aws.String(d.Get("schedule").(string)),
Duration: aws.Int64(int64(d.Get("duration").(int))),
Cutoff: aws.Int64(int64(d.Get("cutoff").(int))),
AllowUnassociatedTargets: aws.Bool(d.Get("allow_unassociated_targets").(bool)),
}

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

d.SetId(*resp.WindowId)

return resourceAwsSsmMaintenanceWindowRead(d, meta)
}

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

params := &ssm.UpdateMaintenanceWindowInput{
WindowId: aws.String(d.Id()),
}

if d.HasChange("name") {
params.Name = aws.String(d.Get("name").(string))
}

if d.HasChange("schedule") {
params.Schedule = aws.String(d.Get("schedule").(string))
}

if d.HasChange("duration") {
params.Duration = aws.Int64(int64(d.Get("duration").(int)))
}

if d.HasChange("cutoff") {
params.Cutoff = aws.Int64(int64(d.Get("cutoff").(int)))
}

if d.HasChange("allow_unassociated_targets") {
params.AllowUnassociatedTargets = aws.Bool(d.Get("allow_unassociated_targets").(bool))
}

if d.HasChange("enabled") {
params.Enabled = aws.Bool(d.Get("enabled").(bool))
}

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

return resourceAwsSsmMaintenanceWindowRead(d, meta)
}

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

params := &ssm.DescribeMaintenanceWindowsInput{
Filters: []*ssm.MaintenanceWindowFilter{
{
Key: aws.String("Name"),
Values: []*string{aws.String(d.Get("name").(string))},
},
},
}

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

found := false

for _, window := range resp.WindowIdentities {
if *window.WindowId == d.Id() {
found = true

d.Set("name", window.Name)
d.Set("cutoff", window.Cutoff)
d.Set("duration", window.Duration)
d.Set("enabled", window.Enabled)
}
}

if !found {
log.Printf("[INFO] Cannot find the SSM Maintenance Window %q. Removing from state", d.Get("name").(string))
d.SetId("")
return nil
}

return nil
}

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

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

params := &ssm.DeleteMaintenanceWindowInput{
WindowId: aws.String(d.Id()),
}

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

return nil
}
179 changes: 179 additions & 0 deletions builtin/providers/aws/resource_aws_ssm_maintenance_window_target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
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 resourceAwsSsmMaintenanceWindowTarget() *schema.Resource {
return &schema.Resource{
Create: resourceAwsSsmMaintenanceWindowTargetCreate,
Read: resourceAwsSsmMaintenanceWindowTargetRead,
Delete: resourceAwsSsmMaintenanceWindowTargetDelete,

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

"resource_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"targets": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
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},
},
},
},
},

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

func expandAwsSsmMaintenanceWindowTargets(d *schema.ResourceData) []*ssm.Target {
var targets []*ssm.Target

targetConfig := d.Get("targets").([]interface{})

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

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

targets = append(targets, target)
}

return targets
}

func flattenAwsSsmMaintenanceWindowTargets(targets []*ssm.Target) []map[string]interface{} {
if len(targets) == 0 {
return nil
}

result := make([]map[string]interface{}, 0, len(targets))
target := targets[0]

t := make(map[string]interface{})
t["key"] = *target.Key
t["values"] = flattenStringList(target.Values)

result = append(result, t)

return result
}

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

log.Printf("[INFO] Registering SSM Maintenance Window Target")

params := &ssm.RegisterTargetWithMaintenanceWindowInput{
WindowId: aws.String(d.Get("window_id").(string)),
ResourceType: aws.String(d.Get("resource_type").(string)),
Targets: expandAwsSsmMaintenanceWindowTargets(d),
}

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

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

d.SetId(*resp.WindowTargetId)

return resourceAwsSsmMaintenanceWindowTargetRead(d, meta)
}

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

params := &ssm.DescribeMaintenanceWindowTargetsInput{
WindowId: aws.String(d.Get("window_id").(string)),
Filters: []*ssm.MaintenanceWindowFilter{
{
Key: aws.String("WindowTargetId"),
Values: []*string{aws.String(d.Id())},
},
},
}

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

found := false
for _, t := range resp.Targets {
if *t.WindowTargetId == d.Id() {
found = true

d.Set("owner_information", t.OwnerInformation)
d.Set("window_id", t.WindowId)
d.Set("resource_type", t.ResourceType)

if err := d.Set("targets", flattenAwsSsmMaintenanceWindowTargets(t.Targets)); err != nil {
return fmt.Errorf("[DEBUG] Error setting targets error: %#v", err)
}
}
}

if !found {
log.Printf("[INFO] Maintenance Window Target not found. Removing from state")
d.SetId("")
return nil
}

return nil
}

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

log.Printf("[INFO] Deregistering SSM Maintenance Window Target: %s", d.Id())

params := &ssm.DeregisterTargetFromMaintenanceWindowInput{
WindowId: aws.String(d.Get("window_id").(string)),
WindowTargetId: aws.String(d.Id()),
}

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

return nil
}
Loading