Skip to content

Commit

Permalink
moved error type check into Iam package
Browse files Browse the repository at this point in the history
  • Loading branch information
nvnyale committed Jul 21, 2022
1 parent 4ec0918 commit cd0116a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
8 changes: 1 addition & 7 deletions api/orchestration_instanceprofiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion iam/instanceprofile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)

Expand Down

0 comments on commit cd0116a

Please sign in to comment.