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

Add tag group support for codedeploy-deployment-group #4324

Merged
merged 3 commits into from
Apr 25, 2018
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
106 changes: 100 additions & 6 deletions aws/resource_aws_codedeploy_deployment_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,40 @@ func resourceAwsCodeDeployDeploymentGroup() *schema.Resource {
ValidateFunc: validateMaxLength(100),
},

"ec2_tag_set": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ec2_tag_filter": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},

"type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateTagFilters,
},

"value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
Set: resourceAwsCodeDeployTagFilterHash,
},
},
},
Set: resourceAwsCodeDeployTagSetHash,
},

"ec2_tag_filter": &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -367,9 +401,12 @@ func resourceAwsCodeDeployDeploymentGroupCreate(d *schema.ResourceData, meta int
input.OnPremisesInstanceTagFilters = onPremFilters
}

if attr, ok := d.GetOk("ec2_tag_set"); ok {
input.Ec2TagSet = buildEC2TagSet(attr.(*schema.Set).List())
}

if attr, ok := d.GetOk("ec2_tag_filter"); ok {
ec2TagFilters := buildEC2TagFilters(attr.(*schema.Set).List())
input.Ec2TagFilters = ec2TagFilters
input.Ec2TagFilters = buildEC2TagFilters(attr.(*schema.Set).List())
}

if attr, ok := d.GetOk("trigger_configuration"); ok {
Expand Down Expand Up @@ -460,6 +497,10 @@ func resourceAwsCodeDeployDeploymentGroupRead(d *schema.ResourceData, meta inter
return err
}

if err := d.Set("ec2_tag_set", ec2TagSetToMap(resp.DeploymentGroupInfo.Ec2TagSet)); err != nil {
return err
}

if err := d.Set("ec2_tag_filter", ec2TagFiltersToMap(resp.DeploymentGroupInfo.Ec2TagFilters)); err != nil {
return err
}
Expand Down Expand Up @@ -527,6 +568,12 @@ func resourceAwsCodeDeployDeploymentGroupUpdate(d *schema.ResourceData, meta int
input.OnPremisesInstanceTagFilters = onPremFilters
}

if d.HasChange("ec2_tag_set") {
_, n := d.GetChange("ec2_tag_set")
ec2TagSet := buildEC2TagSet(n.(*schema.Set).List())
input.Ec2TagSet = ec2TagSet
}

if d.HasChange("ec2_tag_filter") {
_, n := d.GetChange("ec2_tag_filter")
ec2Filters := buildEC2TagFilters(n.(*schema.Set).List())
Expand Down Expand Up @@ -655,6 +702,18 @@ func buildEC2TagFilters(configured []interface{}) []*codedeploy.EC2TagFilter {
return filters
}

// buildEC2TagSet converts raw schema lists into a codedeploy.EC2TagSet.
func buildEC2TagSet(configured []interface{}) *codedeploy.EC2TagSet {
filterSets := make([][]*codedeploy.EC2TagFilter, 0)
for _, raw := range configured {
m := raw.(map[string]interface{})
rawFilters := m["ec2_tag_filter"].(*schema.Set)
filters := buildEC2TagFilters(rawFilters.List())
filterSets = append(filterSets, filters)
}
return &codedeploy.EC2TagSet{Ec2TagSetList: filterSets}
}

// buildTriggerConfigs converts a raw schema list into a list of
// codedeploy.TriggerConfig.
func buildTriggerConfigs(configured []interface{}) []*codedeploy.TriggerConfig {
Expand Down Expand Up @@ -846,11 +905,11 @@ func expandBlueGreenDeploymentConfig(list []interface{}) *codedeploy.BlueGreenDe
return blueGreenDeploymentConfig
}

// ec2TagFiltersToMap converts lists of tag filters into a []map[string]string.
func ec2TagFiltersToMap(list []*codedeploy.EC2TagFilter) []map[string]string {
result := make([]map[string]string, 0, len(list))
// ec2TagFiltersToMap converts lists of tag filters into a []map[string]interface{}.
func ec2TagFiltersToMap(list []*codedeploy.EC2TagFilter) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
for _, tf := range list {
l := make(map[string]string)
l := make(map[string]interface{})
if tf.Key != nil && *tf.Key != "" {
l["key"] = *tf.Key
}
Expand Down Expand Up @@ -884,6 +943,28 @@ func onPremisesTagFiltersToMap(list []*codedeploy.TagFilter) []map[string]string
return result
}

// ec2TagSetToMap converts lists of tag filters into a [][]map[string]string.
func ec2TagSetToMap(tagSet *codedeploy.EC2TagSet) []map[string]interface{} {
var result []map[string]interface{}
if tagSet == nil {
result = make([]map[string]interface{}, 0)
} else {
result = make([]map[string]interface{}, 0, len(tagSet.Ec2TagSetList))
for _, filterSet := range tagSet.Ec2TagSetList {
filters := ec2TagFiltersToMap(filterSet)
filtersAsIntfSlice := make([]interface{}, 0, len(filters))
for _, item := range filters {
filtersAsIntfSlice = append(filtersAsIntfSlice, item)
}
tagFilters := map[string]interface{}{
"ec2_tag_filter": schema.NewSet(resourceAwsCodeDeployTagFilterHash, filtersAsIntfSlice),
}
result = append(result, tagFilters)
}
}
return result
}

// triggerConfigsToMap converts a list of []*codedeploy.TriggerConfig into a []map[string]interface{}
func triggerConfigsToMap(list []*codedeploy.TriggerConfig) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
Expand Down Expand Up @@ -1067,6 +1148,19 @@ func resourceAwsCodeDeployTagFilterHash(v interface{}) int {
return hashcode.String(buf.String())
}

func resourceAwsCodeDeployTagSetHash(v interface{}) int {
tagSetMap := v.(map[string]interface{})
filterSet := tagSetMap["ec2_tag_filter"]
filterSetSlice := filterSet.(*schema.Set).List()

var x uint64
x = 1
for i, filter := range filterSetSlice {
x = ((x << 7) | (x >> (64 - 7))) ^ uint64(i) ^ uint64(resourceAwsCodeDeployTagFilterHash(filter))
}
return int(x)
}

func resourceAwsCodeDeployTriggerConfigHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
Expand Down
148 changes: 133 additions & 15 deletions aws/resource_aws_codedeploy_deployment_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestAccAWSCodeDeployDeploymentGroup_basic(t *testing.T) {
CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroup(rName),
Config: testAccAWSCodeDeployDeploymentGroup(rName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
resource.TestCheckResourceAttr(
Expand All @@ -41,6 +41,8 @@ func TestAccAWSCodeDeployDeploymentGroup_basic(t *testing.T) {
"aws_codedeploy_deployment_group.foo", "service_role_arn",
regexp.MustCompile("arn:aws:iam::[0-9]{12}:role/foo_role_.*")),

resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.#", "0"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.#", "1"),
resource.TestCheckResourceAttr(
Expand All @@ -59,7 +61,7 @@ func TestAccAWSCodeDeployDeploymentGroup_basic(t *testing.T) {
),
},
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroupModified(rName),
Config: testAccAWSCodeDeployDeploymentGroupModified(rName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
resource.TestCheckResourceAttr(
Expand All @@ -72,6 +74,8 @@ func TestAccAWSCodeDeployDeploymentGroup_basic(t *testing.T) {
"aws_codedeploy_deployment_group.foo", "service_role_arn",
regexp.MustCompile("arn:aws:iam::[0-9]{12}:role/bar_role_.*")),

resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.#", "0"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.#", "1"),
resource.TestCheckResourceAttr(
Expand All @@ -93,6 +97,90 @@ func TestAccAWSCodeDeployDeploymentGroup_basic(t *testing.T) {
})
}

func TestAccAWSCodeDeployDeploymentGroup_basic_tagSet(t *testing.T) {
var group codedeploy.DeploymentGroupInfo

rName := acctest.RandString(5)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroup(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "app_name", "foo_app_"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_group_name", "foo_"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_config_name", "CodeDeployDefault.OneAtATime"),
resource.TestMatchResourceAttr(
"aws_codedeploy_deployment_group.foo", "service_role_arn",
regexp.MustCompile("arn:aws:iam::[0-9]{12}:role/foo_role_.*")),

resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.#", "1"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.2916377593.ec2_tag_filter.#", "1"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.2916377593.ec2_tag_filter.2916377465.key", "filterkey"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.2916377593.ec2_tag_filter.2916377465.type", "KEY_AND_VALUE"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.2916377593.ec2_tag_filter.2916377465.value", "filtervalue"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.#", "0"),

resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "alarm_configuration.#", "0"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "auto_rollback_configuration.#", "0"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "trigger_configuration.#", "0"),
),
},
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroupModified(rName, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "app_name", "foo_app_"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_group_name", "bar_"+rName),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "deployment_config_name", "CodeDeployDefault.OneAtATime"),
resource.TestMatchResourceAttr(
"aws_codedeploy_deployment_group.foo", "service_role_arn",
regexp.MustCompile("arn:aws:iam::[0-9]{12}:role/bar_role_.*")),

resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.#", "1"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.2369538847.ec2_tag_filter.#", "1"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.2369538847.ec2_tag_filter.2369538975.key", "filterkey"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.2369538847.ec2_tag_filter.2369538975.type", "KEY_AND_VALUE"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_set.2369538847.ec2_tag_filter.2369538975.value", "anotherfiltervalue"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "ec2_tag_filter.#", "0"),

resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "alarm_configuration.#", "0"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "auto_rollback_configuration.#", "0"),
resource.TestCheckResourceAttr(
"aws_codedeploy_deployment_group.foo", "trigger_configuration.#", "0"),
),
},
},
})
}

func TestAccAWSCodeDeployDeploymentGroup_onPremiseTag(t *testing.T) {
var group codedeploy.DeploymentGroupInfo

Expand Down Expand Up @@ -138,7 +226,7 @@ func TestAccAWSCodeDeployDeploymentGroup_disappears(t *testing.T) {
CheckDestroy: testAccCheckAWSCodeDeployDeploymentGroupDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccAWSCodeDeployDeploymentGroup(rName),
Config: testAccAWSCodeDeployDeploymentGroup(rName, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeDeployDeploymentGroupExists("aws_codedeploy_deployment_group.foo", &group),
testAccAWSCodeDeployDeploymentGroupDisappears(&group),
Expand Down Expand Up @@ -1786,7 +1874,26 @@ func testAccCheckAWSCodeDeployDeploymentGroupExists(name string, group *codedepl
}
}

func testAccAWSCodeDeployDeploymentGroup(rName string) string {
func testAccAWSCodeDeployDeploymentGroup(rName string, tagGroup bool) string {
var tagGroupOrFilter string
if tagGroup {
tagGroupOrFilter = `ec2_tag_set {
ec2_tag_filter {
key = "filterkey"
type = "KEY_AND_VALUE"
value = "filtervalue"
}
}
`
} else {
tagGroupOrFilter = `ec2_tag_filter {
key = "filterkey"
type = "KEY_AND_VALUE"
value = "filtervalue"
}
`
}

return fmt.Sprintf(`
resource "aws_codedeploy_app" "foo_app" {
name = "foo_app_%s"
Expand Down Expand Up @@ -1845,15 +1952,30 @@ resource "aws_codedeploy_deployment_group" "foo" {
app_name = "${aws_codedeploy_app.foo_app.name}"
deployment_group_name = "foo_%s"
service_role_arn = "${aws_iam_role.foo_role.arn}"
ec2_tag_filter {
%s
}`, rName, rName, rName, rName, tagGroupOrFilter)
}

func testAccAWSCodeDeployDeploymentGroupModified(rName string, tagGroup bool) string {
var tagGroupOrFilter string
if tagGroup {
tagGroupOrFilter = `ec2_tag_set {
ec2_tag_filter {
key = "filterkey"
type = "KEY_AND_VALUE"
value = "anotherfiltervalue"
}
}
`
} else {
tagGroupOrFilter = `ec2_tag_filter {
key = "filterkey"
type = "KEY_AND_VALUE"
value = "filtervalue"
value = "anotherfiltervalue"
}
}`, rName, rName, rName, rName)
}
`
}

func testAccAWSCodeDeployDeploymentGroupModified(rName string) string {
return fmt.Sprintf(`
resource "aws_codedeploy_app" "foo_app" {
name = "foo_app_%s"
Expand Down Expand Up @@ -1912,12 +2034,8 @@ resource "aws_codedeploy_deployment_group" "foo" {
app_name = "${aws_codedeploy_app.foo_app.name}"
deployment_group_name = "bar_%s"
service_role_arn = "${aws_iam_role.bar_role.arn}"
ec2_tag_filter {
key = "filterkey"
type = "KEY_AND_VALUE"
value = "anotherfiltervalue"
}
}`, rName, rName, rName, rName)
%s
}`, rName, rName, rName, rName, tagGroupOrFilter)
}

func testAccAWSCodeDeployDeploymentGroupOnPremiseTags(rName string) string {
Expand Down
Loading