change isAwsErr to an exported func so I can use it in other aws modules.

This commit is contained in:
Megan Marsh 2020-07-15 09:47:07 -07:00
parent 036ea238bf
commit a56942d3c7
9 changed files with 16 additions and 17 deletions

View File

@ -188,7 +188,7 @@ func (c *AccessConfig) Session() (*session.Session, error) {
cp, err := c.session.Config.Credentials.Get() cp, err := c.session.Config.Credentials.Get()
if isAWSErr(err, "NoCredentialProviders", "") { if IsAWSErr(err, "NoCredentialProviders", "") {
return nil, fmt.Errorf("No valid credential sources found for AWS Builder. " + return nil, fmt.Errorf("No valid credential sources found for AWS Builder. " +
"Please see https://www.packer.io/docs/builders/amazon#specifying-amazon-credentials " + "Please see https://www.packer.io/docs/builders/amazon#specifying-amazon-credentials " +
"for more information on providing credentials for the AWS Builder.") "for more information on providing credentials for the AWS Builder.")

View File

@ -31,7 +31,7 @@ func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error {
err = retry.Config{ err = retry.Config{
Tries: 11, Tries: 11,
ShouldRetry: func(err error) bool { ShouldRetry: func(err error) bool {
return isAWSErr(err, "UnauthorizedOperation", "") return IsAWSErr(err, "UnauthorizedOperation", "")
}, },
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear, RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
}.Run(ctx, func(ctx context.Context) error { }.Run(ctx, func(ctx context.Context) error {
@ -54,7 +54,7 @@ func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error {
err = retry.Config{ err = retry.Config{
Tries: 11, Tries: 11,
ShouldRetry: func(err error) bool { ShouldRetry: func(err error) bool {
return isAWSErr(err, "UnauthorizedOperation", "") return IsAWSErr(err, "UnauthorizedOperation", "")
}, },
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear, RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
}.Run(ctx, func(ctx context.Context) error { }.Run(ctx, func(ctx context.Context) error {
@ -79,7 +79,7 @@ func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error {
// * err is of type awserr.Error // * err is of type awserr.Error
// * Error.Code() matches code // * Error.Code() matches code
// * Error.Message() contains message // * Error.Message() contains message
func isAWSErr(err error, code string, message string) bool { func IsAWSErr(err error, code string, message string) bool {
if err, ok := err.(awserr.Error); ok { if err, ok := err.(awserr.Error); ok {
return err.Code() == code && strings.Contains(err.Message(), message) return err.Code() == code && strings.Contains(err.Message(), message)
} }

View File

@ -50,7 +50,7 @@ func (d *SSMDriver) StartSession(ctx context.Context, input ssm.StartSessionInpu
var output *ssm.StartSessionOutput var output *ssm.StartSessionOutput
err := retry.Config{ err := retry.Config{
ShouldRetry: func(err error) bool { return isAWSErr(err, "TargetNotConnected", "") }, ShouldRetry: func(err error) bool { return IsAWSErr(err, "TargetNotConnected", "") },
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 60 * time.Second, Multiplier: 2}).Linear, RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 60 * time.Second, Multiplier: 2}).Linear,
}.Run(ctx, func(ctx context.Context) (err error) { }.Run(ctx, func(ctx context.Context) (err error) {
output, err = d.SvcClient.StartSessionWithContext(ctx, &input) output, err = d.SvcClient.StartSessionWithContext(ctx, &input)

View File

@ -91,7 +91,7 @@ func (s *StepCreateTags) Run(ctx context.Context, state multistep.StateBag) mult
// Retry creating tags for about 2.5 minutes // Retry creating tags for about 2.5 minutes
err = retry.Config{Tries: 11, ShouldRetry: func(error) bool { err = retry.Config{Tries: 11, ShouldRetry: func(error) bool {
if isAWSErr(err, "InvalidAMIID.NotFound", "") || isAWSErr(err, "InvalidSnapshot.NotFound", "") { if IsAWSErr(err, "InvalidAMIID.NotFound", "") || IsAWSErr(err, "InvalidSnapshot.NotFound", "") {
return true return true
} }
return false return false

View File

@ -39,7 +39,7 @@ func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) mul
err := retry.Config{ err := retry.Config{
Tries: 11, Tries: 11,
ShouldRetry: func(err error) bool { ShouldRetry: func(err error) bool {
if isAWSErr(err, "AuthFailure", "") { if IsAWSErr(err, "AuthFailure", "") {
log.Printf("Waiting for Vault-generated AWS credentials" + log.Printf("Waiting for Vault-generated AWS credentials" +
" to pass authentication... trying again.") " to pass authentication... trying again.")
return true return true
@ -131,7 +131,7 @@ func (s *StepPreValidate) checkVpc(conn ec2iface.EC2API) error {
} }
res, err := conn.DescribeVpcs(&ec2.DescribeVpcsInput{VpcIds: []*string{aws.String(s.VpcId)}}) res, err := conn.DescribeVpcs(&ec2.DescribeVpcsInput{VpcIds: []*string{aws.String(s.VpcId)}})
if isAWSErr(err, "InvalidVpcID.NotFound", "") || err != nil { if IsAWSErr(err, "InvalidVpcID.NotFound", "") || err != nil {
return fmt.Errorf("Error retrieving VPC information for vpc_id %s: %s", s.VpcId, err) return fmt.Errorf("Error retrieving VPC information for vpc_id %s: %s", s.VpcId, err)
} }

View File

@ -198,7 +198,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
err = retry.Config{ err = retry.Config{
Tries: 11, Tries: 11,
ShouldRetry: func(err error) bool { ShouldRetry: func(err error) bool {
if isAWSErr(err, "InvalidParameterValue", "iamInstanceProfile") { if IsAWSErr(err, "InvalidParameterValue", "iamInstanceProfile") {
return true return true
} }
return false return false
@ -209,7 +209,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
return err return err
}) })
if isAWSErr(err, "VPCIdNotSpecified", "No default VPC for this user") && subnetId == "" { if IsAWSErr(err, "VPCIdNotSpecified", "No default VPC for this user") && subnetId == "" {
err := fmt.Errorf("Error launching source instance: a valid Subnet Id was not specified") err := fmt.Errorf("Error launching source instance: a valid Subnet Id was not specified")
state.Put("error", err) state.Put("error", err)
ui.Error(err.Error()) ui.Error(err.Error())
@ -247,7 +247,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
var r *ec2.DescribeInstancesOutput var r *ec2.DescribeInstancesOutput
err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool { err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool {
if isAWSErr(err, "InvalidInstanceID.NotFound", "") { if IsAWSErr(err, "InvalidInstanceID.NotFound", "") {
return true return true
} }
return false return false
@ -292,7 +292,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
ec2Tags.Report(ui) ec2Tags.Report(ui)
// Retry creating tags for about 2.5 minutes // Retry creating tags for about 2.5 minutes
err = retry.Config{Tries: 11, ShouldRetry: func(error) bool { err = retry.Config{Tries: 11, ShouldRetry: func(error) bool {
if isAWSErr(err, "InvalidInstanceID.NotFound", "") { if IsAWSErr(err, "InvalidInstanceID.NotFound", "") {
return true return true
} }
return false return false

View File

@ -377,7 +377,7 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
// Retry creating tags for about 2.5 minutes // Retry creating tags for about 2.5 minutes
err = retry.Config{Tries: 11, ShouldRetry: func(error) bool { err = retry.Config{Tries: 11, ShouldRetry: func(error) bool {
if isAWSErr(err, "InvalidInstanceID.NotFound", "") { if IsAWSErr(err, "InvalidInstanceID.NotFound", "") {
return true return true
} }
return false return false

View File

@ -41,7 +41,7 @@ func (s *StepStopEBSBackedInstance) Run(ctx context.Context, state multistep.Sta
// Work around this by retrying a few times, up to about 5 minutes. // Work around this by retrying a few times, up to about 5 minutes.
err := retry.Config{Tries: 6, ShouldRetry: func(error) bool { err := retry.Config{Tries: 6, ShouldRetry: func(error) bool {
if isAWSErr(err, "InvalidInstanceID.NotFound", "") { if IsAWSErr(err, "InvalidInstanceID.NotFound", "") {
return true return true
} }
return false return false

View File

@ -7,7 +7,6 @@ import (
"time" "time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
awscommon "github.com/hashicorp/packer/builder/amazon/common" awscommon "github.com/hashicorp/packer/builder/amazon/common"
"github.com/hashicorp/packer/common/random" "github.com/hashicorp/packer/common/random"
@ -57,8 +56,8 @@ func (s *stepCreateAMI) Run(ctx context.Context, state multistep.StateBag) multi
err = retry.Config{ err = retry.Config{
Tries: 11, Tries: 11,
ShouldRetry: func(err error) bool { ShouldRetry: func(err error) bool {
if err, ok := err.(awserr.Error); ok { if awscommon.IsAWSErr(err, "InvalidParameterValue", "Instance is not in state") {
return err.Code() == "InvalidParameterValue" return true
} }
return false return false
}, },