Skip to content

Commit

Permalink
osbuild-worker: switch to aws sdk v2 for errors in ami copy jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
croissanne committed Aug 20, 2024
1 parent 2624516 commit 54820a8
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions cmd/osbuild-worker/jobimpl-awsec2.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"errors"
"fmt"

"github.com/aws/aws-sdk-go/aws/awserr"
smithy "github.com/aws/smithy-go"
"github.com/sirupsen/logrus"

"github.com/osbuild/osbuild-composer/internal/cloud/awscloud"
Expand Down Expand Up @@ -51,8 +52,10 @@ func (impl *AWSEC2CopyJobImpl) Run(job worker.Job) error {
if err != nil {
logWithId.Errorf("Error copying ami: %v", err)
result.JobError = clienterrors.New(clienterrors.ErrorSharingTarget, fmt.Sprintf("Error copying ami %s", args.Ami), nil)
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {

var apiErr smithy.APIError
if errors.As(err, &apiErr) {
switch apiErr.ErrorCode() {
case "InvalidRegion":
result.JobError = clienterrors.New(clienterrors.ErrorSharingTarget, fmt.Sprintf("Invalid source region '%s'", args.SourceRegion), nil)
case "InvalidAMIID.Malformed":
Expand All @@ -62,7 +65,10 @@ func (impl *AWSEC2CopyJobImpl) Run(job worker.Job) error {
case "InvalidRequest":
result.JobError = clienterrors.New(clienterrors.ErrorSharingTarget, fmt.Sprintf("Source ami '%s' not found", args.Ami), nil)
}
} else {
result.JobError = clienterrors.New(clienterrors.ErrorSharingTarget, fmt.Sprintf("Unknown error copying ami '%s'", args.Ami), err.Error())
}

return err
}

Expand Down Expand Up @@ -125,16 +131,20 @@ func (impl *AWSEC2ShareJobImpl) Run(job worker.Job) error {
if err != nil {
logWithId.Errorf("Error sharing image: %v", err)
result.JobError = clienterrors.New(clienterrors.ErrorSharingTarget, fmt.Sprintf("Error sharing image with target %v", args.ShareWithAccounts), nil)
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
switch apiErr.ErrorCode() {
case "InvalidAMIID.Malformed":
result.JobError = clienterrors.New(clienterrors.ErrorSharingTarget, fmt.Sprintf("Malformed ami id '%s'", args.Ami), nil)
case "InvalidAMIID.NotFound":
result.JobError = clienterrors.New(clienterrors.ErrorSharingTarget, fmt.Sprintf("Ami '%s' not found in region '%s'", args.Ami, args.Region), nil)
case "InvalidAMIAttributeItemValue":
result.JobError = clienterrors.New(clienterrors.ErrorSharingTarget, fmt.Sprintf("Invalid user id to share ami with: %v", args.ShareWithAccounts), nil)
}
} else {
result.JobError = clienterrors.New(clienterrors.ErrorSharingTarget, fmt.Sprintf("Unknown error sharing ami '%s' with %v", args.Ami, args.ShareWithAccounts), err.Error())
}

return err
}

Expand Down

0 comments on commit 54820a8

Please sign in to comment.