Skip to content

Commit cfdd756

Browse files
committed
mantle/aws: simplify force upload handling of s3 objects
This will make it so we don't have to conditionalize the function call to API.RemoveImage based on if we are uploading WinLI images or not. This is prep for more simplification in the future.
1 parent b583a81 commit cfdd756

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

mantle/cmd/ore/aws/upload.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,16 +207,21 @@ func runUpload(cmd *cobra.Command, args []string) error {
207207
}
208208

209209
if uploadForce {
210-
var err error
211-
if !uploadCreateWinLIAMI {
212-
err = API.RemoveImage(uploadAMIName, s3BucketName, s3ObjectPath)
213-
} else {
214-
err = API.RemoveImage(uploadAMIName, "", "")
215-
}
210+
err := API.RemoveImage(uploadAMIName)
216211
if err != nil {
217212
fmt.Fprintf(os.Stderr, "%v\n", err)
218213
os.Exit(1)
219214
}
215+
// Since we are force uploading delete the s3 object used previously too
216+
// if it exists. A "NoSuchKey" err means it didn't exist and we can ignore.
217+
if uploadSourceObject == "" && uploadSourceSnapshot == "" {
218+
if err := API.DeleteObject(s3BucketName, s3ObjectPath); err != nil {
219+
if !strings.Contains(err.Error(), "NoSuchKey") {
220+
fmt.Fprintf(os.Stderr, "unable to delete object: %v\n", err)
221+
os.Exit(1)
222+
}
223+
}
224+
}
220225
}
221226

222227
// if no snapshot was specified, check for an existing one or a

mantle/platform/api/aws/images.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -478,23 +478,7 @@ func (a *API) deregisterImageIfExists(name string) error {
478478
}
479479

480480
// Remove all uploaded data associated with an AMI.
481-
func (a *API) RemoveImage(name, s3BucketName, s3ObjectPath string) error {
482-
// Delete S3 object if bucket and path are specified.
483-
if s3BucketName != "" && s3ObjectPath != "" {
484-
err := a.DeleteObject(s3BucketName, s3ObjectPath)
485-
if err != nil {
486-
if awsErr, ok := err.(awserr.Error); ok {
487-
if awsErr.Code() != "NoSuchKey" {
488-
return err
489-
}
490-
} else {
491-
return err
492-
}
493-
} else {
494-
plog.Infof("Deleted existing S3 object bucket:%s path:%s", s3BucketName, s3ObjectPath)
495-
}
496-
}
497-
481+
func (a *API) RemoveImage(name string) error {
498482
// compatibility with old versions of this code, which created HVM
499483
// AMIs with an "-hvm" suffix
500484
err := a.deregisterImageIfExists(name + "-hvm")

0 commit comments

Comments
 (0)