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

r/ssm_maint_windows_task - add CloudWatch config + plan time validations #11774

Merged
merged 10 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 9 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
12 changes: 12 additions & 0 deletions .changelog/11774.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
```release-note:enhancement
resource/aws_ssm_maintenance_window_task: Add `cloudwatch_config` argument
```

```release-note:enhancement
resource/aws_ssm_maintenance_window_task: Add `document_version` argument
bflad marked this conversation as resolved.
Show resolved Hide resolved
```

```release-note:enhancement
resource/aws_ssm_maintenance_window_task: Add plan time validation to `task_type`, `service_role_arn`, `targets.notification_arn`, `targets.service_role_arn`, `max_concurrency`, `max_errors`, `targets`, `priority`,
`task_invocation_parameters.run_command_parameters.comment`, `task_invocation_parameters.run_command_parameters.document_hash`, `task_invocation_parameters.run_command_parameters.timeout_seconds`, `task_invocation_parameters.run_command_parameters.notification_config.notification_events`
bflad marked this conversation as resolved.
Show resolved Hide resolved
```
179 changes: 133 additions & 46 deletions aws/resource_aws_ssm_maintenance_window_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ func resourceAwsSsmMaintenanceWindowTask() *schema.Resource {
},

"max_concurrency": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^([1-9][0-9]*|[1-9][0-9]%|[1-9]%|100%)$`), ""),
bflad marked this conversation as resolved.
Show resolved Hide resolved
},

"max_errors": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^([1-9][0-9]*|[0]|[1-9][0-9]%|[0-9]%|100%)$`), ""),
bflad marked this conversation as resolved.
Show resolved Hide resolved
},

"task_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(ssm.MaintenanceWindowTaskType_Values(), false),
},

"task_arn": {
Expand All @@ -52,13 +55,15 @@ func resourceAwsSsmMaintenanceWindowTask() *schema.Resource {
},

"service_role_arn": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},

"targets": {
Type: schema.TypeList,
Required: true,
Optional: true,
MaxItems: 5,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Expand All @@ -68,16 +73,18 @@ func resourceAwsSsmMaintenanceWindowTask() *schema.Resource {
"values": {
Type: schema.TypeList,
Required: true,
MaxItems: 50,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},

"name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateAwsSSMMaintenanceWindowTaskName,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[a-zA-Z0-9_\-.]{3,128}$`),
"Only alphanumeric characters, hyphens, dots & underscores allowed."),
},

"description": {
Expand All @@ -87,8 +94,9 @@ func resourceAwsSsmMaintenanceWindowTask() *schema.Resource {
},

"priority": {
Type: schema.TypeInt,
Optional: true,
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntAtLeast(0),
},

"task_invocation_parameters": {
Expand Down Expand Up @@ -165,22 +173,26 @@ func resourceAwsSsmMaintenanceWindowTask() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"comment": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 100),
},

"document_hash": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 256),
},

"document_hash_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
ssm.DocumentHashTypeSha256,
ssm.DocumentHashTypeSha1,
}, false),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(ssm.DocumentHashType_Values(), false),
},
"document_version": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`([$]LATEST|[$]DEFAULT|^[1-9][0-9]*$)`), ""),
bflad marked this conversation as resolved.
Show resolved Hide resolved
},

"notification_config": {
Expand All @@ -190,23 +202,31 @@ func resourceAwsSsmMaintenanceWindowTask() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"notification_arn": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},

"notification_events": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
"All",
"InProgress",
"Success",
"TimedOut",
"Cancelled",
"Failed",
}, false),
bflad marked this conversation as resolved.
Show resolved Hide resolved
},
},

"notification_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
ssm.NotificationTypeCommand,
ssm.NotificationTypeInvocation,
}, false),
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(ssm.NotificationType_Values(), false),
},
},
},
Expand Down Expand Up @@ -242,13 +262,33 @@ func resourceAwsSsmMaintenanceWindowTask() *schema.Resource {
},

"service_role_arn": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},

"timeout_seconds": {
Type: schema.TypeInt,
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(30, 2592000),
},
"cloudwatch_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cloudwatch_log_group_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"cloudwatch_output_enabled": {
Type: schema.TypeBool,
Optional: true,
},
},
},
},
},
},
Expand Down Expand Up @@ -407,6 +447,9 @@ func expandAwsSsmTaskInvocationRunCommandParameters(config []interface{}) *ssm.M
if attr, ok := configParam["document_hash_type"]; ok && len(attr.(string)) != 0 {
params.DocumentHashType = aws.String(attr.(string))
}
if attr, ok := configParam["document_version"]; ok && len(attr.(string)) != 0 {
params.DocumentVersion = aws.String(attr.(string))
}
if attr, ok := configParam["notification_config"]; ok && len(attr.([]interface{})) > 0 {
params.NotificationConfig = expandAwsSsmTaskInvocationRunCommandParametersNotificationConfig(attr.([]interface{}))
}
Expand All @@ -425,6 +468,10 @@ func expandAwsSsmTaskInvocationRunCommandParameters(config []interface{}) *ssm.M
if attr, ok := configParam["timeout_seconds"]; ok && attr.(int) != 0 {
params.TimeoutSeconds = aws.Int64(int64(attr.(int)))
}

if attr, ok := configParam["cloudwatch_config"]; ok && len(attr.([]interface{})) > 0 {
params.CloudWatchOutputConfig = expandAwsSsmTaskInvocationRunCommandParametersCloudWatchConfig(attr.([]interface{}))
}
return params
}

Expand All @@ -440,6 +487,9 @@ func flattenAwsSsmTaskInvocationRunCommandParameters(parameters *ssm.Maintenance
if parameters.DocumentHashType != nil {
result["document_hash_type"] = aws.StringValue(parameters.DocumentHashType)
}
if parameters.DocumentVersion != nil {
result["document_version"] = aws.StringValue(parameters.DocumentVersion)
}
if parameters.NotificationConfig != nil {
result["notification_config"] = flattenAwsSsmTaskInvocationRunCommandParametersNotificationConfig(parameters.NotificationConfig)
}
Expand All @@ -458,6 +508,9 @@ func flattenAwsSsmTaskInvocationRunCommandParameters(parameters *ssm.Maintenance
if parameters.TimeoutSeconds != nil {
result["timeout_seconds"] = aws.Int64Value(parameters.TimeoutSeconds)
}
if parameters.CloudWatchOutputConfig != nil {
result["cloudwatch_config"] = flattenAwsSsmTaskInvocationRunCommandParametersCloudWatchConfig(parameters.CloudWatchOutputConfig)
}

return []interface{}{result}
}
Expand Down Expand Up @@ -529,6 +582,37 @@ func flattenAwsSsmTaskInvocationRunCommandParametersNotificationConfig(config *s
return []interface{}{result}
}

func expandAwsSsmTaskInvocationRunCommandParametersCloudWatchConfig(config []interface{}) *ssm.CloudWatchOutputConfig {
if len(config) == 0 || config[0] == nil {
return nil
}

params := &ssm.CloudWatchOutputConfig{}
configParam := config[0].(map[string]interface{})

if attr, ok := configParam["cloudwatch_log_group_name"]; ok && len(attr.(string)) != 0 {
params.CloudWatchLogGroupName = aws.String(attr.(string))
}
if attr, ok := configParam["cloudwatch_output_enabled"]; ok {
params.CloudWatchOutputEnabled = aws.Bool(attr.(bool))
}

return params
}

func flattenAwsSsmTaskInvocationRunCommandParametersCloudWatchConfig(config *ssm.CloudWatchOutputConfig) []interface{} {
result := make(map[string]interface{})

if config.CloudWatchLogGroupName != nil {
result["cloudwatch_log_group_name"] = aws.StringValue(config.CloudWatchLogGroupName)
}
if config.CloudWatchOutputEnabled != nil {
result["cloudwatch_output_enabled"] = aws.BoolValue(config.CloudWatchOutputEnabled)
}

return []interface{}{result}
}

func expandAwsSsmTaskInvocationCommonParameters(config []interface{}) map[string][]*string {
if len(config) == 0 || config[0] == nil {
return nil
Expand Down Expand Up @@ -569,7 +653,7 @@ func flattenAwsSsmTaskInvocationCommonParameters(parameters map[string][]*string
}

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

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

Expand All @@ -580,7 +664,10 @@ func resourceAwsSsmMaintenanceWindowTaskCreate(d *schema.ResourceData, meta inte
TaskType: aws.String(d.Get("task_type").(string)),
ServiceRoleArn: aws.String(d.Get("service_role_arn").(string)),
TaskArn: aws.String(d.Get("task_arn").(string)),
Targets: expandAwsSsmTargets(d.Get("targets").([]interface{})),
}

if v, ok := d.GetOk("targets"); ok {
params.Targets = expandAwsSsmTargets(v.([]interface{}))
}

if v, ok := d.GetOk("name"); ok {
Expand All @@ -599,7 +686,7 @@ func resourceAwsSsmMaintenanceWindowTaskCreate(d *schema.ResourceData, meta inte
params.TaskInvocationParameters = expandAwsSsmTaskInvocationParameters(v.([]interface{}))
}

resp, err := ssmconn.RegisterTaskWithMaintenanceWindow(params)
resp, err := conn.RegisterTaskWithMaintenanceWindow(params)
if err != nil {
return err
}
Expand All @@ -610,14 +697,14 @@ func resourceAwsSsmMaintenanceWindowTaskCreate(d *schema.ResourceData, meta inte
}

func resourceAwsSsmMaintenanceWindowTaskRead(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
conn := meta.(*AWSClient).ssmconn
windowID := d.Get("window_id").(string)

params := &ssm.GetMaintenanceWindowTaskInput{
WindowId: aws.String(windowID),
WindowTaskId: aws.String(d.Id()),
}
resp, err := ssmconn.GetMaintenanceWindowTask(params)
resp, err := conn.GetMaintenanceWindowTask(params)
if isAWSErr(err, ssm.ErrCodeDoesNotExistException, "") {
log.Printf("[WARN] Maintenance Window (%s) Task (%s) not found, removing from state", windowID, d.Id())
d.SetId("")
Expand Down Expand Up @@ -651,7 +738,7 @@ func resourceAwsSsmMaintenanceWindowTaskRead(d *schema.ResourceData, meta interf
}

func resourceAwsSsmMaintenanceWindowTaskUpdate(d *schema.ResourceData, meta interface{}) error {
ssmconn := meta.(*AWSClient).ssmconn
conn := meta.(*AWSClient).ssmconn
windowID := d.Get("window_id").(string)

params := &ssm.UpdateMaintenanceWindowTaskInput{
Expand Down Expand Up @@ -681,7 +768,7 @@ func resourceAwsSsmMaintenanceWindowTaskUpdate(d *schema.ResourceData, meta inte
params.TaskInvocationParameters = expandAwsSsmTaskInvocationParameters(v.([]interface{}))
}

_, err := ssmconn.UpdateMaintenanceWindowTask(params)
_, err := conn.UpdateMaintenanceWindowTask(params)
if isAWSErr(err, ssm.ErrCodeDoesNotExistException, "") {
log.Printf("[WARN] Maintenance Window (%s) Task (%s) not found, removing from state", windowID, d.Id())
d.SetId("")
Expand All @@ -696,7 +783,7 @@ func resourceAwsSsmMaintenanceWindowTaskUpdate(d *schema.ResourceData, meta inte
}

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

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

Expand All @@ -705,7 +792,7 @@ func resourceAwsSsmMaintenanceWindowTaskDelete(d *schema.ResourceData, meta inte
WindowTaskId: aws.String(d.Id()),
}

_, err := ssmconn.DeregisterTaskFromMaintenanceWindow(params)
_, err := conn.DeregisterTaskFromMaintenanceWindow(params)
if isAWSErr(err, ssm.ErrCodeDoesNotExistException, "") {
return nil
}
Expand Down
Loading