builder/amazon: Add helper function for checking against AWSError

Replace all straight forward occurrences of `if err, ok := err.(awserr.Error)` with the `isAWSErr` helper function
This commit is contained in:
nywilken 2019-11-12 15:27:47 -05:00
parent ffc4876812
commit 488e539f63
7 changed files with 55 additions and 70 deletions

View File

@ -11,7 +11,6 @@ import (
"strings" "strings"
"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/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
@ -181,15 +180,17 @@ func (c *AccessConfig) Session() (*session.Session, error) {
c.session = sess c.session = sess
cp, err := c.session.Config.Credentials.Get() cp, err := c.session.Config.Credentials.Get()
if err != nil {
if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "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.html#specifying-amazon-credentials " + "Please see https://www.packer.io/docs/builders/amazon.html#specifying-amazon-credentials " +
"for more information on providing credentials for the AWS Builder.") "for more information on providing credentials for the AWS Builder.")
} else {
return nil, fmt.Errorf("Error loading credentials for AWS Provider: %s", err)
}
} }
if err != nil {
return nil, fmt.Errorf("Error loading credentials for AWS Provider: %s", err)
}
log.Printf("[INFO] AWS Auth provider used: %q", cp.ProviderName) log.Printf("[INFO] AWS Auth provider used: %q", cp.ProviderName)
if c.DecodeAuthZMessages { if c.DecodeAuthZMessages {

View File

@ -3,11 +3,14 @@ package common
import ( import (
"fmt" "fmt"
"log" "log"
"strings"
"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"
) )
// DestroyAMIs deregisters the AWS machine images in imageids from an active AWS account
func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error { func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error {
resp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{ resp, err := ec2conn.DescribeImages(&ec2.DescribeImagesInput{
ImageIds: imageids, ImageIds: imageids,
@ -47,3 +50,14 @@ func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error {
} }
return nil return nil
} }
// Returns true if the error matches all these conditions:
// * err is of type awserr.Error
// * Error.Code() matches code
// * Error.Message() contains message
func isAWSErr(err error, code string, message string) bool {
if err, ok := err.(awserr.Error); ok {
return err.Code() == code && strings.Contains(err.Message(), message)
}
return false
}

View File

@ -6,7 +6,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/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/common/retry"
@ -91,17 +90,12 @@ func (s *StepCreateTags) Run(ctx context.Context, state multistep.StateBag) mult
snapshotTags.Report(ui) snapshotTags.Report(ui)
// Retry creating tags for about 2.5 minutes // Retry creating tags for about 2.5 minutes
err = retry.Config{ err = retry.Config{Tries: 11, ShouldRetry: func(error) bool {
Tries: 11, if isAWSErr(err, "InvalidAMIID.NotFound", "") || isAWSErr(err, "InvalidSnapshot.NotFound", "") {
ShouldRetry: func(error) bool { return true
if awsErr, ok := err.(awserr.Error); ok { }
switch awsErr.Code() { return false
case "InvalidAMIID.NotFound", "InvalidSnapshot.NotFound": },
return true
}
}
return false
},
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 {
// Tag images and snapshots // Tag images and snapshots

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"
"github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/common/retry"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
@ -36,7 +35,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 awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "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

View File

@ -9,7 +9,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"
"github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/common/retry"
@ -207,17 +206,12 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
// will fail. Retry a couple of times to try to mitigate that race. // will fail. Retry a couple of times to try to mitigate that race.
var r *ec2.DescribeInstancesOutput var r *ec2.DescribeInstancesOutput
err = retry.Config{ err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool {
Tries: 11, if isAWSErr(err, "InvalidInstanceID.NotFound", "") {
ShouldRetry: func(err error) bool { return true
if awsErr, ok := err.(awserr.Error); ok { }
switch awsErr.Code() { return false
case "InvalidInstanceID.NotFound": },
return true
}
}
return false
},
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 {
r, err = ec2conn.DescribeInstances(describeInstance) r, err = ec2conn.DescribeInstances(describeInstance)
@ -254,17 +248,12 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa
if s.IsRestricted { if s.IsRestricted {
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{ err = retry.Config{Tries: 11, ShouldRetry: func(error) bool {
Tries: 11, if isAWSErr(err, "InvalidInstanceID.NotFound", "") {
ShouldRetry: func(error) bool { return true
if awsErr, ok := err.(awserr.Error); ok { }
switch awsErr.Code() { return false
case "InvalidInstanceID.NotFound": },
return true
}
}
return false
},
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 {
_, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{

View File

@ -9,7 +9,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"
"github.com/hashicorp/packer/common/random" "github.com/hashicorp/packer/common/random"
"github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/common/retry"
@ -353,17 +352,12 @@ 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{ err = retry.Config{Tries: 11, ShouldRetry: func(error) bool {
Tries: 11, if isAWSErr(err, "InvalidInstanceID.NotFound", "") {
ShouldRetry: func(error) bool { return true
if awsErr, ok := err.(awserr.Error); ok { }
switch awsErr.Code() { return false
case "InvalidInstanceID.NotFound": },
return true
}
}
return false
},
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 {
_, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/common/retry"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
@ -41,17 +40,12 @@ func (s *StepStopEBSBackedInstance) Run(ctx context.Context, state multistep.Sta
// does not exist. // does not exist.
// 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{ err := retry.Config{Tries: 6, ShouldRetry: func(error) bool {
Tries: 6, if isAWSErr(err, "InvalidInstanceID.NotFound", "") {
ShouldRetry: func(error) bool { return true
if awsErr, ok := err.(awserr.Error); ok { }
switch awsErr.Code() { return false
case "InvalidInstanceID.NotFound": },
return true
}
}
return false
},
RetryDelay: (&retry.Backoff{InitialBackoff: 10 * time.Second, MaxBackoff: 60 * time.Second, Multiplier: 2}).Linear, RetryDelay: (&retry.Backoff{InitialBackoff: 10 * time.Second, MaxBackoff: 60 * time.Second, Multiplier: 2}).Linear,
}.Run(ctx, func(ctx context.Context) error { }.Run(ctx, func(ctx context.Context) error {
ui.Message(fmt.Sprintf("Stopping instance")) ui.Message(fmt.Sprintf("Stopping instance"))