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

resource/aws_ssm_maintenance_window: Add end_date, schedule_timezone, and start_date attributes #7040

Merged
merged 1 commit into from
Jan 7, 2019
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
94 changes: 68 additions & 26 deletions aws/resource_aws_ssm_maintenance_window.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ func resourceAwsSsmMaintenanceWindow() *schema.Resource {
Optional: true,
Default: true,
},

"end_date": {
Type: schema.TypeString,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want some validation on these checking the date format?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API parameter says it accepts ISO-8601 extended format, which has some slight differences with RFC3339, that would match our normal date validators. The time.RFC3339 format built into Go's standard library doesn't work with all ISO-8601 formats unfortunately.

It looks like we have ISO-8601 needs in other places where it is inconsistently handled, so I have created a technical debt issue to address all these at once (I would think preferably upstream in the provider SDK), but even a common solution in the provider itself is a good start: #7045

},

"schedule_timezone": {
Type: schema.TypeString,
Optional: true,
},

"start_date": {
Type: schema.TypeString,
Optional: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you want some validation on these checking the date format?

},
},
}
}
Expand All @@ -59,59 +74,83 @@ func resourceAwsSsmMaintenanceWindowCreate(d *schema.ResourceData, meta interfac
ssmconn := meta.(*AWSClient).ssmconn

params := &ssm.CreateMaintenanceWindowInput{
AllowUnassociatedTargets: aws.Bool(d.Get("allow_unassociated_targets").(bool)),
Cutoff: aws.Int64(int64(d.Get("cutoff").(int))),
Duration: aws.Int64(int64(d.Get("duration").(int))),
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)),
}

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

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

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

resp, err := ssmconn.CreateMaintenanceWindow(params)
if err != nil {
return err
return fmt.Errorf("error creating SSM Maintenance Window: %s", err)
}

d.SetId(*resp.WindowId)
return resourceAwsSsmMaintenanceWindowUpdate(d, meta)

if !d.Get("enabled").(bool) {
input := &ssm.UpdateMaintenanceWindowInput{
Enabled: aws.Bool(false),
WindowId: aws.String(d.Id()),
}

_, err := ssmconn.UpdateMaintenanceWindow(input)
if err != nil {
return fmt.Errorf("error disabling SSM Maintenance Window (%s): %s", d.Id(), err)
}
}

return resourceAwsSsmMaintenanceWindowRead(d, meta)
}

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

// Replace must be set otherwise its not possible to remove optional attributes, e.g.
// ValidationException: 1 validation error detected: Value '' at 'startDate' failed to satisfy constraint: Member must have length greater than or equal to 1
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))
AllowUnassociatedTargets: aws.Bool(d.Get("allow_unassociated_targets").(bool)),
Cutoff: aws.Int64(int64(d.Get("cutoff").(int))),
Duration: aws.Int64(int64(d.Get("duration").(int))),
Enabled: aws.Bool(d.Get("enabled").(bool)),
Name: aws.String(d.Get("name").(string)),
Replace: aws.Bool(true),
Schedule: aws.String(d.Get("schedule").(string)),
WindowId: aws.String(d.Id()),
}

if d.HasChange("duration") {
params.Duration = aws.Int64(int64(d.Get("duration").(int)))
if v, ok := d.GetOk("end_date"); ok {
params.EndDate = aws.String(v.(string))
}

if d.HasChange("cutoff") {
params.Cutoff = aws.Int64(int64(d.Get("cutoff").(int)))
if v, ok := d.GetOk("schedule_timezone"); ok {
params.ScheduleTimezone = aws.String(v.(string))
}

if d.HasChange("allow_unassociated_targets") {
params.AllowUnassociatedTargets = aws.Bool(d.Get("allow_unassociated_targets").(bool))
if v, ok := d.GetOk("start_date"); ok {
params.StartDate = aws.String(v.(string))
}

params.Enabled = aws.Bool(d.Get("enabled").(bool))

_, err := ssmconn.UpdateMaintenanceWindow(params)
if err != nil {
if isAWSErr(err, ssm.ErrCodeDoesNotExistException, "") {
log.Printf("[WARN] Maintenance Window %s not found, removing from state", d.Id())
d.SetId("")
return nil
}
return err
return fmt.Errorf("error updating SSM Maintenance Window (%s): %s", d.Id(), err)
}

return resourceAwsSsmMaintenanceWindowRead(d, meta)
Expand All @@ -131,15 +170,18 @@ func resourceAwsSsmMaintenanceWindowRead(d *schema.ResourceData, meta interface{
d.SetId("")
return nil
}
return err
return fmt.Errorf("error reading SSM Maintenance Window (%s): %s", d.Id(), err)
}

d.Set("name", resp.Name)
d.Set("allow_unassociated_targets", resp.AllowUnassociatedTargets)
d.Set("cutoff", resp.Cutoff)
d.Set("duration", resp.Duration)
d.Set("enabled", resp.Enabled)
d.Set("allow_unassociated_targets", resp.AllowUnassociatedTargets)
d.Set("end_date", resp.EndDate)
d.Set("name", resp.Name)
d.Set("schedule_timezone", resp.ScheduleTimezone)
d.Set("schedule", resp.Schedule)
d.Set("start_date", resp.StartDate)

return nil
}
Expand Down
Loading