provider/aws: `aws_codedeploy_deployment_group` Panics when setting (#6617)

`on_premises_instance_tag_filter`

When setting `on_premises_instance_tag_filter`, Terraform was not
pushing the changes on the cReate (due to a spelling mistake). A second
apply would push the tags and then cause a panic. Terraform was building
a ec2.Tagfilter struct without checking for optional values. When the
TagFilter was being dereferenced, it caused a panic
This commit is contained in:
Paul Stack 2016-05-11 18:38:03 +01:00
parent e33847c8a8
commit 06b5cf737c
2 changed files with 111 additions and 7 deletions

View File

@ -177,7 +177,7 @@ func resourceAwsCodeDeployDeploymentGroupCreate(d *schema.ResourceData, meta int
if attr, ok := d.GetOk("autoscaling_groups"); ok {
input.AutoScalingGroups = expandStringList(attr.(*schema.Set).List())
}
if attr, ok := d.GetOk("on_premises_instance_tag_filters"); ok {
if attr, ok := d.GetOk("on_premises_instance_tag_filter"); ok {
onPremFilters := buildOnPremTagFilters(attr.(*schema.Set).List())
input.OnPremisesInstanceTagFilters = onPremFilters
}
@ -331,9 +331,15 @@ func buildOnPremTagFilters(configured []interface{}) []*codedeploy.TagFilter {
var filter codedeploy.TagFilter
m := raw.(map[string]interface{})
filter.Key = aws.String(m["key"].(string))
filter.Type = aws.String(m["type"].(string))
filter.Value = aws.String(m["value"].(string))
if v, ok := m["key"]; ok {
filter.Key = aws.String(v.(string))
}
if v, ok := m["type"]; ok {
filter.Type = aws.String(v.(string))
}
if v, ok := m["value"]; ok {
filter.Value = aws.String(v.(string))
}
filters = append(filters, &filter)
}
@ -400,13 +406,13 @@ func onPremisesTagFiltersToMap(list []*codedeploy.TagFilter) []map[string]string
result := make([]map[string]string, 0, len(list))
for _, tf := range list {
l := make(map[string]string)
if *tf.Key != "" {
if tf.Key != nil && *tf.Key != "" {
l["key"] = *tf.Key
}
if *tf.Value != "" {
if tf.Value != nil && *tf.Value != "" {
l["value"] = *tf.Value
}
if *tf.Type != "" {
if tf.Type != nil && *tf.Type != "" {
l["type"] = *tf.Type
}
result = append(result, l)

View File

@ -69,6 +69,39 @@ func TestAccAWSCodeDeployDeploymentGroup_basic(t *testing.T) {
})
}
func TestAccAWSCodeDeployDeploymentGroup_onPremiseTag(t *testing.T) {
var group codedeploy.DeploymentGroupInfo
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroupOnPremiseTags,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "app_name", "foo_app"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_group_name", "foo"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_config_name", "CodeDeployDefault.OneAtATime"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.#", "1"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.2916377465.key", "filterkey"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.2916377465.type", "KEY_AND_VALUE"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "on_premises_instance_tag_filter.2916377465.value", "filtervalue"),
),
},
},
})
}
func TestAccAWSCodeDeployDeploymentGroup_triggerConfiguration_basic(t *testing.T) {
var group codedeploy.DeploymentGroupInfo
@ -519,6 +552,71 @@ resource "aws_codedeploy_deployment_group" "foo" {
}
}`
var testAccAWSCodeDeployDeploymentGroupOnPremiseTags = `
resource "aws_codedeploy_app" "foo_app" {
name = "foo_app"
}
resource "aws_iam_role_policy" "foo_policy" {
name = "foo_policy"
role = "${aws_iam_role.foo_role.id}"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"autoscaling:CompleteLifecycleAction",
"autoscaling:DeleteLifecycleHook",
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeLifecycleHooks",
"autoscaling:PutLifecycleHook",
"autoscaling:RecordLifecycleActionHeartbeat",
"ec2:DescribeInstances",
"ec2:DescribeInstanceStatus",
"tag:GetTags",
"tag:GetResources"
],
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role" "foo_role" {
name = "foo_role"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": [
"codedeploy.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}
resource "aws_codedeploy_deployment_group" "foo" {
app_name = "${aws_codedeploy_app.foo_app.name}"
deployment_group_name = "foo"
service_role_arn = "${aws_iam_role.foo_role.arn}"
on_premises_instance_tag_filter {
key = "filterkey"
type = "KEY_AND_VALUE"
value = "filtervalue"
}
}`
const baseCodeDeployConfig = `
resource "aws_codedeploy_app" "foo_app" {
name = "foo-app"