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

nil checks to prevent crashes on empty config blocks #10713

Merged
merged 1 commit into from
Nov 6, 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
31 changes: 31 additions & 0 deletions aws/resource_aws_ssm_maintenance_window_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ func resourceAwsSsmMaintenanceWindowTask() *schema.Resource {
}

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

loggingConfig := config[0].(map[string]interface{})

Expand Down Expand Up @@ -382,6 +385,10 @@ func flattenAwsSsmTaskParameters(taskParameters map[string]*ssm.MaintenanceWindo
}

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

params := &ssm.MaintenanceWindowTaskInvocationParameters{}
for _, v := range config {
paramConfig := v.(map[string]interface{})
Expand Down Expand Up @@ -423,6 +430,10 @@ func flattenAwsSsmTaskInvocationParameters(parameters *ssm.MaintenanceWindowTask
}

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

params := &ssm.MaintenanceWindowAutomationParameters{}
configParam := config[0].(map[string]interface{})
if attr, ok := configParam["document_version"]; ok && len(attr.(string)) != 0 {
Expand All @@ -449,6 +460,10 @@ func flattenAwsSsmTaskInvocationAutomationParameters(parameters *ssm.Maintenance
}

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

params := &ssm.MaintenanceWindowLambdaParameters{}
configParam := config[0].(map[string]interface{})
if attr, ok := configParam["client_context"]; ok && len(attr.(string)) != 0 {
Expand Down Expand Up @@ -479,6 +494,10 @@ func flattenAwsSsmTaskInvocationLambdaParameters(parameters *ssm.MaintenanceWind
}

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

params := &ssm.MaintenanceWindowRunCommandParameters{}
configParam := config[0].(map[string]interface{})
if attr, ok := configParam["comment"]; ok && len(attr.(string)) != 0 {
Expand Down Expand Up @@ -546,6 +565,10 @@ func flattenAwsSsmTaskInvocationRunCommandParameters(parameters *ssm.Maintenance
}

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

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

Expand All @@ -572,6 +595,10 @@ func flattenAwsSsmTaskInvocationStepFunctionsParameters(parameters *ssm.Maintena
}

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

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

Expand Down Expand Up @@ -605,6 +632,10 @@ func flattenAwsSsmTaskInvocationRunCommandParametersNotificationConfig(config *s
}

func expandAwsSsmTaskInvocationCommonParameters(config []interface{}) map[string][]*string {
if len(config) == 0 || config[0] == nil {
return nil
}

params := make(map[string][]*string)

for _, v := range config {
Expand Down
82 changes: 66 additions & 16 deletions aws/resource_aws_ssm_maintenance_window_task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestAccAWSSSMMaintenanceWindowTask_basic(t *testing.T) {
var before, after ssm.MaintenanceWindowTask
resourceName := "aws_ssm_maintenance_window_task.target"
resourceName := "aws_ssm_maintenance_window_task.test"

name := acctest.RandString(10)
resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestAccAWSSSMMaintenanceWindowTask_basic(t *testing.T) {
func TestAccAWSSSMMaintenanceWindowTask_updateForcesNewResource(t *testing.T) {
var before, after ssm.MaintenanceWindowTask
name := acctest.RandString(10)
resourceName := "aws_ssm_maintenance_window_task.target"
resourceName := "aws_ssm_maintenance_window_task.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -89,7 +89,7 @@ func TestAccAWSSSMMaintenanceWindowTask_updateForcesNewResource(t *testing.T) {

func TestAccAWSSSMMaintenanceWindowTask_TaskInvocationAutomationParameters(t *testing.T) {
var task ssm.MaintenanceWindowTask
resourceName := "aws_ssm_maintenance_window_task.target"
resourceName := "aws_ssm_maintenance_window_task.test"

name := acctest.RandString(10)
resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -123,7 +123,7 @@ func TestAccAWSSSMMaintenanceWindowTask_TaskInvocationAutomationParameters(t *te

func TestAccAWSSSMMaintenanceWindowTask_TaskInvocationLambdaParameters(t *testing.T) {
var task ssm.MaintenanceWindowTask
resourceName := "aws_ssm_maintenance_window_task.target"
resourceName := "aws_ssm_maintenance_window_task.test"
rString := acctest.RandString(8)
rInt := acctest.RandInt()

Expand Down Expand Up @@ -155,7 +155,7 @@ func TestAccAWSSSMMaintenanceWindowTask_TaskInvocationLambdaParameters(t *testin

func TestAccAWSSSMMaintenanceWindowTask_TaskInvocationRunCommandParameters(t *testing.T) {
var task ssm.MaintenanceWindowTask
resourceName := "aws_ssm_maintenance_window_task.target"
resourceName := "aws_ssm_maintenance_window_task.test"
serviceRoleResourceName := "aws_iam_role.test"
s3BucketResourceName := "aws_s3_bucket.foo"

Expand Down Expand Up @@ -196,7 +196,7 @@ func TestAccAWSSSMMaintenanceWindowTask_TaskInvocationRunCommandParameters(t *te

func TestAccAWSSSMMaintenanceWindowTask_TaskInvocationStepFunctionParameters(t *testing.T) {
var task ssm.MaintenanceWindowTask
resourceName := "aws_ssm_maintenance_window_task.target"
resourceName := "aws_ssm_maintenance_window_task.test"
rString := acctest.RandString(8)

resource.ParallelTest(t, resource.TestCase{
Expand Down Expand Up @@ -246,6 +246,27 @@ func TestAccAWSSSMMaintenanceWindowTask_TaskParameters(t *testing.T) {
})
}

func TestAccAWSSSMMaintenanceWindowTask_emptyNotificationConfig(t *testing.T) {
var task ssm.MaintenanceWindowTask
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_ssm_maintenance_window_task.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMMaintenanceWindowTaskDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMMaintenanceWindowTaskConfigEmptyNotifcationConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMMaintenanceWindowTaskExists(resourceName, &task),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckAwsSsmWindowsTaskNotRecreated(t *testing.T,
before, after *ssm.MaintenanceWindowTask) resource.TestCheckFunc {
return func(s *terraform.State) error {
Expand Down Expand Up @@ -397,7 +418,7 @@ POLICY
func testAccAWSSSMMaintenanceWindowTaskBasicConfig(rName string) string {
return fmt.Sprintf(testAccAWSSSMMaintenanceWindowTaskConfigBase(rName) + `

resource "aws_ssm_maintenance_window_task" "target" {
resource "aws_ssm_maintenance_window_task" "test" {
window_id = "${aws_ssm_maintenance_window.test.id}"
task_type = "RUN_COMMAND"
task_arn = "AWS-RunShellScript"
Expand All @@ -423,7 +444,7 @@ resource "aws_ssm_maintenance_window_task" "target" {
func testAccAWSSSMMaintenanceWindowTaskBasicConfigUpdate(rName, description, taskType, taskArn string, priority, maxConcurrency, maxErrors int) string {
return fmt.Sprintf(testAccAWSSSMMaintenanceWindowTaskConfigBase(rName)+`

resource "aws_ssm_maintenance_window_task" "target" {
resource "aws_ssm_maintenance_window_task" "test" {
window_id = "${aws_ssm_maintenance_window.test.id}"
task_type = "%[2]s"
task_arn = "%[3]s"
Expand Down Expand Up @@ -486,7 +507,7 @@ EOF
func testAccAWSSSMMaintenanceWindowTaskBasicConfigUpdated(rName string) string {
return fmt.Sprintf(testAccAWSSSMMaintenanceWindowTaskConfigBase(rName) + `

resource "aws_ssm_maintenance_window_task" "target" {
resource "aws_ssm_maintenance_window_task" "test" {
window_id = "${aws_ssm_maintenance_window.test.id}"
task_type = "RUN_COMMAND"
task_arn = "AWS-RunShellScript"
Expand All @@ -511,10 +532,39 @@ resource "aws_ssm_maintenance_window_task" "target" {
`)
}

func testAccAWSSSMMaintenanceWindowTaskConfigEmptyNotifcationConfig(rName string) string {
return fmt.Sprintf(testAccAWSSSMMaintenanceWindowTaskConfigBase(rName) + `

resource "aws_ssm_maintenance_window_task" "test" {
window_id = "${aws_ssm_maintenance_window.test.id}"
task_type = "RUN_COMMAND"
task_arn = "AWS-CreateImage"
priority = 1
service_role_arn = "${aws_iam_role.test.arn}"
max_concurrency = "2"
max_errors = "1"
targets {
key = "WindowTargetIds"
values = ["${aws_ssm_maintenance_window_target.test.id}"]
}
task_invocation_parameters {
run_command_parameters {
timeout_seconds = 600
notification_config {}
parameter {
name = "Operation"
values = ["Install"]
}
}
}
}
`)
}

func testAccAWSSSMMaintenanceWindowTaskAutomationConfig(rName, version string) string {
return fmt.Sprintf(testAccAWSSSMMaintenanceWindowTaskConfigBase(rName)+`

resource "aws_ssm_maintenance_window_task" "target" {
resource "aws_ssm_maintenance_window_task" "test" {
window_id = "${aws_ssm_maintenance_window.test.id}"
task_type = "AUTOMATION"
task_arn = "AWS-CreateImage"
Expand Down Expand Up @@ -552,7 +602,7 @@ resource "aws_s3_bucket" "foo" {
force_destroy = true
}

resource "aws_ssm_maintenance_window_task" "target" {
resource "aws_ssm_maintenance_window_task" "test" {
window_id = "${aws_ssm_maintenance_window.test.id}"
task_type = "AUTOMATION"
task_arn = "AWS-CreateImage"
Expand Down Expand Up @@ -585,10 +635,10 @@ func testAccAWSSSMMaintenanceWindowTaskLambdaConfig(funcName, policyName, roleNa
return fmt.Sprintf(testAccAWSLambdaConfigBasic(funcName, policyName, roleName, sgName)+
testAccAWSSSMMaintenanceWindowTaskConfigBase(rName)+`

resource "aws_ssm_maintenance_window_task" "target" {
resource "aws_ssm_maintenance_window_task" "test" {
window_id = "${aws_ssm_maintenance_window.test.id}"
task_type = "LAMBDA"
task_arn = "${aws_lambda_function.lambda_function_test.arn}"
task_arn = "${aws_lambda_function.test.arn}"
priority = 1
service_role_arn = "${aws_iam_role.test.arn}"
max_concurrency = "2"
Expand Down Expand Up @@ -616,7 +666,7 @@ resource "aws_ssm_maintenance_window_task" "target" {
func testAccAWSSSMMaintenanceWindowTaskRunCommandConfig(rName, comment string, timeoutSeconds int) string {
return fmt.Sprintf(testAccAWSSSMMaintenanceWindowTaskConfigBase(rName)+`

resource "aws_ssm_maintenance_window_task" "target" {
resource "aws_ssm_maintenance_window_task" "test" {
window_id = "${aws_ssm_maintenance_window.test.id}"
task_type = "RUN_COMMAND"
task_arn = "AWS-RunShellScript"
Expand Down Expand Up @@ -654,7 +704,7 @@ resource "aws_s3_bucket" "foo" {
force_destroy = true
}

resource "aws_ssm_maintenance_window_task" "target" {
resource "aws_ssm_maintenance_window_task" "test" {
window_id = "${aws_ssm_maintenance_window.test.id}"
task_type = "RUN_COMMAND"
task_arn = "AWS-RunShellScript"
Expand Down Expand Up @@ -693,7 +743,7 @@ resource "aws_sfn_activity" "test" {
name = %[1]q
}

resource "aws_ssm_maintenance_window_task" "target" {
resource "aws_ssm_maintenance_window_task" "test" {
window_id = "${aws_ssm_maintenance_window.test.id}"
task_type = "STEP_FUNCTIONS"
task_arn = "${aws_sfn_activity.test.id}"
Expand Down