From cd0116acc50ee1747d6bd4f562ab591b9d70d1b6 Mon Sep 17 00:00:00 2001 From: nvnyale Date: Thu, 21 Jul 2022 11:25:33 -0400 Subject: [PATCH] moved error type check into Iam package --- api/orchestration_instanceprofiles.go | 8 +------- iam/instanceprofile.go | 6 +++++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/api/orchestration_instanceprofiles.go b/api/orchestration_instanceprofiles.go index 2ebcad9..5b63922 100644 --- a/api/orchestration_instanceprofiles.go +++ b/api/orchestration_instanceprofiles.go @@ -4,10 +4,8 @@ import ( "context" "fmt" - "github.com/YaleSpinup/apierror" "github.com/YaleSpinup/ec2-api/common" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/iam" ) @@ -16,11 +14,7 @@ import ( func (o *iamOrchestrator) deleteInstanceProfile(ctx context.Context, name string) error { ip, err := o.iamClient.GetInstanceProfile(ctx, &iam.GetInstanceProfileInput{InstanceProfileName: aws.String(name)}) if err != nil { - if aerr, ok := err.(awserr.Error); ok && aerr.Code() == iam.ErrCodeNoSuchEntityException { - return apierror.New(apierror.ErrNotFound, "Instance profile not found", nil) - } else { - return common.ErrCode("failed to get instance profiles", err) - } + return err // do not modify this error } // detach policies from role(s) and delete the role(s) diff --git a/iam/instanceprofile.go b/iam/instanceprofile.go index f57a3d6..1d655a5 100644 --- a/iam/instanceprofile.go +++ b/iam/instanceprofile.go @@ -6,6 +6,7 @@ import ( "github.com/YaleSpinup/apierror" "github.com/YaleSpinup/ec2-api/common" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/iam" log "github.com/sirupsen/logrus" ) @@ -18,7 +19,10 @@ func (i *Iam) GetInstanceProfile(ctx context.Context, input *iam.GetInstanceProf out, err := i.Service.GetInstanceProfileWithContext(ctx, input) if err != nil { - return nil, common.ErrCode("failed to get instanceprofiles", err) + if aerr, ok := err.(awserr.Error); ok && aerr.Code() == iam.ErrCodeNoSuchEntityException { + return nil, apierror.New(apierror.ErrNotFound, "Instance profile not found", err) + } + return nil, common.ErrCode("failed to get instance profiles", err) } log.Debugf("got output instanceprofiles: %+v", out)