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

codedeploy_deployment_group: Include autoscaling groups when updating blue green config. #5827

Merged
merged 6 commits into from
Sep 28, 2018
Merged
144 changes: 75 additions & 69 deletions aws/resource_aws_codedeploy_deployment_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,13 +375,15 @@ func resourceAwsCodeDeployDeploymentGroup() *schema.Resource {
func resourceAwsCodeDeployDeploymentGroupCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).codedeployconn

application := d.Get("app_name").(string)
deploymentGroup := d.Get("deployment_group_name").(string)
// required fields
applicationName := d.Get("app_name").(string)
deploymentGroupName := d.Get("deployment_group_name").(string)
serviceRoleArn := d.Get("service_role_arn").(string)

input := codedeploy.CreateDeploymentGroupInput{
ApplicationName: aws.String(application),
DeploymentGroupName: aws.String(deploymentGroup),
ServiceRoleArn: aws.String(d.Get("service_role_arn").(string)),
ApplicationName: aws.String(applicationName),
DeploymentGroupName: aws.String(deploymentGroupName),
ServiceRoleArn: aws.String(serviceRoleArn),
}

if attr, ok := d.GetOk("deployment_style"); ok {
Expand Down Expand Up @@ -430,36 +432,15 @@ func resourceAwsCodeDeployDeploymentGroupCreate(d *schema.ResourceData, meta int
input.BlueGreenDeploymentConfiguration = expandBlueGreenDeploymentConfig(attr.([]interface{}))
}

// Retry to handle IAM role eventual consistency.
log.Printf("[DEBUG] Creating CodeDeploy DeploymentGroup %s", applicationName)

var resp *codedeploy.CreateDeploymentGroupOutput
var err error
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
resp, err = conn.CreateDeploymentGroup(&input)
if err != nil {
retry := false
codedeployErr, ok := err.(awserr.Error)
if !ok {
return resource.NonRetryableError(err)
}
if codedeployErr.Code() == "InvalidRoleException" {
retry = true
}
if codedeployErr.Code() == "InvalidTriggerConfigException" {
r := regexp.MustCompile("^Topic ARN .+ is not valid$")
if r.MatchString(codedeployErr.Message()) {
retry = true
}
}
if retry {
log.Printf("[DEBUG] Trying to create deployment group again: %q",
codedeployErr.Message())
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}
return nil
return handleCreateError(err)
})

if err != nil {
return err
}
Expand All @@ -473,10 +454,12 @@ func resourceAwsCodeDeployDeploymentGroupRead(d *schema.ResourceData, meta inter
conn := meta.(*AWSClient).codedeployconn

log.Printf("[DEBUG] Reading CodeDeploy DeploymentGroup %s", d.Id())

resp, err := conn.GetDeploymentGroup(&codedeploy.GetDeploymentGroupInput{
ApplicationName: aws.String(d.Get("app_name").(string)),
DeploymentGroupName: aws.String(d.Get("deployment_group_name").(string)),
})

if err != nil {
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "DeploymentGroupDoesNotExistException" {
log.Printf("[INFO] CodeDeployment DeploymentGroup %s not found", d.Get("deployment_group_name").(string))
Expand Down Expand Up @@ -535,20 +518,15 @@ func resourceAwsCodeDeployDeploymentGroupRead(d *schema.ResourceData, meta inter
func resourceAwsCodeDeployDeploymentGroupUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).codedeployconn

input := codedeploy.UpdateDeploymentGroupInput{
ApplicationName: aws.String(d.Get("app_name").(string)),
CurrentDeploymentGroupName: aws.String(d.Get("deployment_group_name").(string)),
ServiceRoleArn: aws.String(d.Get("service_role_arn").(string)),
}

if d.HasChange("autoscaling_groups") {
_, n := d.GetChange("autoscaling_groups")
input.AutoScalingGroups = expandStringList(n.(*schema.Set).List())
}
// required fields
applicationName := d.Get("app_name").(string)
deploymentGroupName := d.Get("deployment_group_name").(string)
serviceRoleArn := d.Get("service_role_arn").(string)

if d.HasChange("deployment_config_name") {
_, n := d.GetChange("deployment_config_name")
input.DeploymentConfigName = aws.String(n.(string))
input := codedeploy.UpdateDeploymentGroupInput{
ApplicationName: aws.String(applicationName),
CurrentDeploymentGroupName: aws.String(deploymentGroupName),
ServiceRoleArn: aws.String(serviceRoleArn),
}

if d.HasChange("deployment_group_name") {
Expand All @@ -561,6 +539,17 @@ func resourceAwsCodeDeployDeploymentGroupUpdate(d *schema.ResourceData, meta int
input.DeploymentStyle = expandDeploymentStyle(n.([]interface{}))
}

if d.HasChange("deployment_config_name") {
_, n := d.GetChange("deployment_config_name")
input.DeploymentConfigName = aws.String(n.(string))
}

// include (original or new) autoscaling groups when blue_green_deployment_config changes
if d.HasChange("autoscaling_groups") || d.HasChange("blue_green_deployment_config") {
_, n := d.GetChange("autoscaling_groups")
input.AutoScalingGroups = expandStringList(n.(*schema.Set).List())
}

// TagFilters aren't like tags. They don't append. They simply replace.
if d.HasChange("on_premises_instance_tag_filter") {
_, n := d.GetChange("on_premises_instance_tag_filter")
Expand Down Expand Up @@ -607,33 +596,11 @@ func resourceAwsCodeDeployDeploymentGroupUpdate(d *schema.ResourceData, meta int
}

log.Printf("[DEBUG] Updating CodeDeploy DeploymentGroup %s", d.Id())
// Retry to handle IAM role eventual consistency.
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := conn.UpdateDeploymentGroup(&input)
if err != nil {
retry := false
codedeployErr, ok := err.(awserr.Error)
if !ok {
return resource.NonRetryableError(err)
}
if codedeployErr.Code() == "InvalidRoleException" {
retry = true
}
if codedeployErr.Code() == "InvalidTriggerConfigException" {
r := regexp.MustCompile("^Topic ARN .+ is not valid$")
if r.MatchString(codedeployErr.Message()) {
retry = true
}
}
if retry {
log.Printf("[DEBUG] Retrying Code Deployment Group Update: %q",
codedeployErr.Message())
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}
return nil
var err error
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err = conn.UpdateDeploymentGroup(&input)
return handleUpdateError(err)
})

if err != nil {
Expand All @@ -651,13 +618,52 @@ func resourceAwsCodeDeployDeploymentGroupDelete(d *schema.ResourceData, meta int
ApplicationName: aws.String(d.Get("app_name").(string)),
DeploymentGroupName: aws.String(d.Get("deployment_group_name").(string)),
})

if err != nil {
return err
}

return nil
}

func handleCreateError(err error) *resource.RetryError {
return handleCodeDeployApiError(err, "create")
}

func handleUpdateError(err error) *resource.RetryError {
return handleCodeDeployApiError(err, "update")
}

func handleCodeDeployApiError(err error, operation string) *resource.RetryError {
if err == nil {
return nil
}

retry := false
codedeployErr, ok := err.(awserr.Error)
if !ok {
return resource.NonRetryableError(err)
}

if codedeployErr.Code() == "InvalidRoleException" {
retry = true
}

if codedeployErr.Code() == "InvalidTriggerConfigException" {
r := regexp.MustCompile("^Topic ARN .+ is not valid$")
if r.MatchString(codedeployErr.Message()) {
retry = true
}
}

if retry {
log.Printf("[DEBUG] Trying to %s DeploymentGroup again: %q", operation, codedeployErr.Message())
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}

// buildOnPremTagFilters converts raw schema lists into a list of
// codedeploy.TagFilters.
func buildOnPremTagFilters(configured []interface{}) []*codedeploy.TagFilter {
Expand Down
Loading