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

resource/aws_launch_configuration: Clean up code #4037

Merged
merged 3 commits into from
Apr 4, 2018
Merged
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
29 changes: 12 additions & 17 deletions aws/resource_aws_launch_configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import (
"encoding/hex"
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)

func resourceAwsLaunchConfiguration() *schema.Resource {
Expand All @@ -34,14 +33,14 @@ func resourceAwsLaunchConfiguration() *schema.Resource {
Computed: true,
ForceNew: true,
ConflictsWith: []string{"name_prefix"},
ValidateFunc: validateMaxLength(255),
ValidateFunc: validation.StringLenBetween(1, 255),
},

"name_prefix": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validateMaxLength(255 - resource.UniqueIDSuffixLength),
ValidateFunc: validation.StringLenBetween(1, 255-resource.UniqueIDSuffixLength),
},

"image_id": {
Expand Down Expand Up @@ -82,7 +81,7 @@ func resourceAwsLaunchConfiguration() *schema.Resource {
return ""
}
},
ValidateFunc: validateMaxLength(16384),
ValidateFunc: validation.StringLenBetween(1, 16384),
},

"security_groups": {
Expand Down Expand Up @@ -435,27 +434,23 @@ func resourceAwsLaunchConfigurationCreate(d *schema.ResourceData, meta interface
}
createLaunchConfigurationOpts.LaunchConfigurationName = aws.String(lcName)

log.Printf(
"[DEBUG] autoscaling create launch configuration: %s", createLaunchConfigurationOpts)
log.Printf("[DEBUG] autoscaling create launch configuration: %s", createLaunchConfigurationOpts)

// IAM profiles can take ~10 seconds to propagate in AWS:
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#launch-instance-with-role-console
err = resource.Retry(90*time.Second, func() *resource.RetryError {
_, err := autoscalingconn.CreateLaunchConfiguration(&createLaunchConfigurationOpts)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if strings.Contains(awsErr.Message(), "Invalid IamInstanceProfile") {
return resource.RetryableError(err)
}
if strings.Contains(awsErr.Message(), "You are not authorized to perform this operation") {
return resource.RetryableError(err)
}
if isAWSErr(err, "ValidationError", "Invalid IamInstanceProfile") {
return resource.RetryableError(err)
}
if isAWSErr(err, "ValidationError", "You are not authorized to perform this operation") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})

if err != nil {
return fmt.Errorf("Error creating launch configuration: %s", err)
}
Expand Down Expand Up @@ -488,6 +483,7 @@ func resourceAwsLaunchConfigurationRead(d *schema.ResourceData, meta interface{}
return fmt.Errorf("Error retrieving launch configuration: %s", err)
}
if len(describConfs.LaunchConfigurations) == 0 {
log.Printf("[WARN] Launch Configuration (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
Expand Down Expand Up @@ -532,8 +528,7 @@ func resourceAwsLaunchConfigurationDelete(d *schema.ResourceData, meta interface
LaunchConfigurationName: aws.String(d.Id()),
})
if err != nil {
autoscalingerr, ok := err.(awserr.Error)
if ok && (autoscalingerr.Code() == "InvalidConfiguration.NotFound" || autoscalingerr.Code() == "ValidationError") {
if isAWSErr(err, "InvalidConfiguration.NotFound", "") {
log.Printf("[DEBUG] Launch configuration (%s) not found", d.Id())
return nil
}
Expand Down