From b058de072ad4392878c6f6c19b43c6ee5b44d905 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Thu, 29 Oct 2020 11:57:14 +0100 Subject: [PATCH] move packer/builder/amazon/common.IsAWSErr to builder/amazon/common/awserrors.Matches to avoid cyclic dependency issues --- builder/amazon/common/access_config.go | 3 ++- .../{error => common/awserrors}/utils.go | 2 +- builder/amazon/common/helper_funcs.go | 18 +++--------------- builder/amazon/common/ssm/driver.go | 16 ++-------------- builder/amazon/common/step_create_tags.go | 3 ++- builder/amazon/common/step_pre_validate.go | 5 +++-- .../amazon/common/step_run_source_instance.go | 9 +++++---- .../amazon/common/step_run_spot_instance.go | 3 ++- .../amazon/common/step_stop_ebs_instance.go | 3 ++- builder/amazon/ebs/step_create_ami.go | 3 ++- 10 files changed, 24 insertions(+), 41 deletions(-) rename builder/amazon/{error => common/awserrors}/utils.go (95%) diff --git a/builder/amazon/common/access_config.go b/builder/amazon/common/access_config.go index 6ebd372d3..cd6aafb44 100644 --- a/builder/amazon/common/access_config.go +++ b/builder/amazon/common/access_config.go @@ -17,6 +17,7 @@ import ( "github.com/aws/aws-sdk-go/service/ec2/ec2iface" awsbase "github.com/hashicorp/aws-sdk-go-base" cleanhttp "github.com/hashicorp/go-cleanhttp" + "github.com/hashicorp/packer/builder/amazon/common/awserrors" "github.com/hashicorp/packer/template/interpolate" vaultapi "github.com/hashicorp/vault/api" ) @@ -271,7 +272,7 @@ func (c *AccessConfig) Session() (*session.Session, error) { cp, err := c.session.Config.Credentials.Get() - if IsAWSErr(err, "NoCredentialProviders", "") { + if awserrors.Matches(err, "NoCredentialProviders", "") { return nil, c.NewNoValidCredentialSourcesError(err) } diff --git a/builder/amazon/error/utils.go b/builder/amazon/common/awserrors/utils.go similarity index 95% rename from builder/amazon/error/utils.go rename to builder/amazon/common/awserrors/utils.go index dcf98a825..676ef9b4a 100644 --- a/builder/amazon/error/utils.go +++ b/builder/amazon/common/awserrors/utils.go @@ -1,4 +1,4 @@ -package error +package awserrors import ( "strings" diff --git a/builder/amazon/common/helper_funcs.go b/builder/amazon/common/helper_funcs.go index 5f75e2568..12dfe715e 100644 --- a/builder/amazon/common/helper_funcs.go +++ b/builder/amazon/common/helper_funcs.go @@ -4,12 +4,11 @@ import ( "context" "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/ec2" + "github.com/hashicorp/packer/builder/amazon/common/awserrors" "github.com/hashicorp/packer/common/retry" ) @@ -31,7 +30,7 @@ func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error { err = retry.Config{ Tries: 11, ShouldRetry: func(err error) bool { - return IsAWSErr(err, "UnauthorizedOperation", "") + return awserrors.Matches(err, "UnauthorizedOperation", "") }, RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear, }.Run(ctx, func(ctx context.Context) error { @@ -54,7 +53,7 @@ func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error { err = retry.Config{ Tries: 11, ShouldRetry: func(err error) bool { - return IsAWSErr(err, "UnauthorizedOperation", "") + return awserrors.Matches(err, "UnauthorizedOperation", "") }, RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear, }.Run(ctx, func(ctx context.Context) error { @@ -74,14 +73,3 @@ func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error { } 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 -} diff --git a/builder/amazon/common/ssm/driver.go b/builder/amazon/common/ssm/driver.go index 79e662eb3..5c8ad9819 100644 --- a/builder/amazon/common/ssm/driver.go +++ b/builder/amazon/common/ssm/driver.go @@ -6,13 +6,12 @@ import ( "fmt" "log" "os/exec" - "strings" "time" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/ssm" "github.com/aws/aws-sdk-go/service/ssm/ssmiface" + "github.com/hashicorp/packer/builder/amazon/common/awserrors" "github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/helper/builder/localexec" "github.com/hashicorp/packer/packer" @@ -24,22 +23,11 @@ type Session struct { Input ssm.StartSessionInput } -// 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 -} - // getCommand return a valid ordered set of arguments to pass to the driver command. func (s Session) getCommand(ctx context.Context) ([]string, string, error) { var session *ssm.StartSessionOutput err := retry.Config{ - ShouldRetry: func(err error) bool { return isAWSErr(err, "TargetNotConnected", "") }, + ShouldRetry: func(err error) bool { return awserrors.Matches(err, "TargetNotConnected", "") }, RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 60 * time.Second, Multiplier: 2}).Linear, }.Run(ctx, func(ctx context.Context) (err error) { session, err = s.SvcClient.StartSessionWithContext(ctx, &s.Input) diff --git a/builder/amazon/common/step_create_tags.go b/builder/amazon/common/step_create_tags.go index 15ed105d0..4805b1255 100644 --- a/builder/amazon/common/step_create_tags.go +++ b/builder/amazon/common/step_create_tags.go @@ -8,6 +8,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/packer/builder/amazon/common/awserrors" "github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" @@ -91,7 +92,7 @@ func (s *StepCreateTags) Run(ctx context.Context, state multistep.StateBag) mult // Retry creating tags for about 2.5 minutes err = retry.Config{Tries: 11, ShouldRetry: func(error) bool { - if IsAWSErr(err, "InvalidAMIID.NotFound", "") || IsAWSErr(err, "InvalidSnapshot.NotFound", "") { + if awserrors.Matches(err, "InvalidAMIID.NotFound", "") || awserrors.Matches(err, "InvalidSnapshot.NotFound", "") { return true } return false diff --git a/builder/amazon/common/step_pre_validate.go b/builder/amazon/common/step_pre_validate.go index b86e04f61..94ea7af57 100644 --- a/builder/amazon/common/step_pre_validate.go +++ b/builder/amazon/common/step_pre_validate.go @@ -9,6 +9,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2/ec2iface" + "github.com/hashicorp/packer/builder/amazon/common/awserrors" "github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" @@ -39,7 +40,7 @@ func (s *StepPreValidate) Run(ctx context.Context, state multistep.StateBag) mul err := retry.Config{ Tries: 11, ShouldRetry: func(err error) bool { - if IsAWSErr(err, "AuthFailure", "") { + if awserrors.Matches(err, "AuthFailure", "") { log.Printf("Waiting for Vault-generated AWS credentials" + " to pass authentication... trying again.") return true @@ -131,7 +132,7 @@ func (s *StepPreValidate) checkVpc(conn ec2iface.EC2API) error { } res, err := conn.DescribeVpcs(&ec2.DescribeVpcsInput{VpcIds: []*string{aws.String(s.VpcId)}}) - if IsAWSErr(err, "InvalidVpcID.NotFound", "") || err != nil { + if awserrors.Matches(err, "InvalidVpcID.NotFound", "") || err != nil { return fmt.Errorf("Error retrieving VPC information for vpc_id %s: %s", s.VpcId, err) } diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index 71e42c261..ed228a128 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -11,6 +11,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/packer/builder/amazon/common/awserrors" "github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/helper/communicator" "github.com/hashicorp/packer/helper/multistep" @@ -204,7 +205,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa err = retry.Config{ Tries: 11, ShouldRetry: func(err error) bool { - if IsAWSErr(err, "InvalidParameterValue", "iamInstanceProfile") { + if awserrors.Matches(err, "InvalidParameterValue", "iamInstanceProfile") { return true } return false @@ -215,7 +216,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa return err }) - if IsAWSErr(err, "VPCIdNotSpecified", "No default VPC for this user") && subnetId == "" { + if awserrors.Matches(err, "VPCIdNotSpecified", "No default VPC for this user") && subnetId == "" { err := fmt.Errorf("Error launching source instance: a valid Subnet Id was not specified") state.Put("error", err) ui.Error(err.Error()) @@ -253,7 +254,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa var r *ec2.DescribeInstancesOutput err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool { - if IsAWSErr(err, "InvalidInstanceID.NotFound", "") { + if awserrors.Matches(err, "InvalidInstanceID.NotFound", "") { return true } return false @@ -298,7 +299,7 @@ func (s *StepRunSourceInstance) Run(ctx context.Context, state multistep.StateBa ec2Tags.Report(ui) // Retry creating tags for about 2.5 minutes err = retry.Config{Tries: 11, ShouldRetry: func(error) bool { - if IsAWSErr(err, "InvalidInstanceID.NotFound", "") { + if awserrors.Matches(err, "InvalidInstanceID.NotFound", "") { return true } return false diff --git a/builder/amazon/common/step_run_spot_instance.go b/builder/amazon/common/step_run_spot_instance.go index fae1dc4bc..75ec401e9 100644 --- a/builder/amazon/common/step_run_spot_instance.go +++ b/builder/amazon/common/step_run_spot_instance.go @@ -11,6 +11,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/packer/builder/amazon/common/awserrors" "github.com/hashicorp/packer/common/random" "github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/helper/communicator" @@ -396,7 +397,7 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag) // Retry creating tags for about 2.5 minutes err = retry.Config{Tries: 11, ShouldRetry: func(error) bool { - if IsAWSErr(err, "InvalidInstanceID.NotFound", "") { + if awserrors.Matches(err, "InvalidInstanceID.NotFound", "") { return true } return false diff --git a/builder/amazon/common/step_stop_ebs_instance.go b/builder/amazon/common/step_stop_ebs_instance.go index f02f15bfc..f3ed58473 100644 --- a/builder/amazon/common/step_stop_ebs_instance.go +++ b/builder/amazon/common/step_stop_ebs_instance.go @@ -6,6 +6,7 @@ import ( "time" "github.com/aws/aws-sdk-go/service/ec2" + "github.com/hashicorp/packer/builder/amazon/common/awserrors" "github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" @@ -42,7 +43,7 @@ func (s *StepStopEBSBackedInstance) Run(ctx context.Context, state multistep.Sta // Work around this by retrying a few times, up to about 5 minutes. err := retry.Config{Tries: 6, ShouldRetry: func(error) bool { - if IsAWSErr(err, "InvalidInstanceID.NotFound", "") { + if awserrors.Matches(err, "InvalidInstanceID.NotFound", "") { return true } return false diff --git a/builder/amazon/ebs/step_create_ami.go b/builder/amazon/ebs/step_create_ami.go index 74a3a419a..5cbbc5ca8 100644 --- a/builder/amazon/ebs/step_create_ami.go +++ b/builder/amazon/ebs/step_create_ami.go @@ -9,6 +9,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" awscommon "github.com/hashicorp/packer/builder/amazon/common" + "github.com/hashicorp/packer/builder/amazon/common/awserrors" "github.com/hashicorp/packer/common/random" "github.com/hashicorp/packer/common/retry" "github.com/hashicorp/packer/helper/multistep" @@ -61,7 +62,7 @@ func (s *stepCreateAMI) Run(ctx context.Context, state multistep.StateBag) multi err = retry.Config{ Tries: 0, ShouldRetry: func(err error) bool { - if awscommon.IsAWSErr(err, "InvalidParameterValue", "Instance is not in state") { + if awserrors.Matches(err, "InvalidParameterValue", "Instance is not in state") { return true } return false