Skip to content

Commit 0d94b7c

Browse files
gursewak1997jlebon
authored andcommitted
mantle: Handle error 404 for gcloud
When the allowMissing flag is passed to the ore gcloud delete-images command, it allows the process to continue without erroring out when a 404 error is returned, indicating that the resource was not found.
1 parent dbcbbdd commit 0d94b7c

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

mantle/cmd/ore/gcloud/delete-images.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"os"
2020

2121
"github.com/spf13/cobra"
22+
"google.golang.org/api/googleapi"
2223

2324
"github.com/coreos/coreos-assembler/mantle/platform/api/gcloud"
2425
)
@@ -29,10 +30,12 @@ var (
2930
Short: "Delete GCP images",
3031
Run: runDeleteImage,
3132
}
33+
allowMissing bool
3234
)
3335

3436
func init() {
3537
GCloud.AddCommand(cmdDeleteImage)
38+
cmdDeleteImage.Flags().BoolVar(&allowMissing, "allow-missing", false, "Do not error out on the resource not existing")
3639
}
3740

3841
func runDeleteImage(cmd *cobra.Command, args []string) {
@@ -46,7 +49,14 @@ func runDeleteImage(cmd *cobra.Command, args []string) {
4649
for _, name := range args {
4750
pending, err := api.DeleteImage(name)
4851
if err != nil {
49-
fmt.Fprintf(os.Stderr, "%v\n", err)
52+
if gErr, ok := err.(*googleapi.Error); ok {
53+
// Skip on NotFound error only if allowMissing flag is set to True
54+
if gErr.Code == 404 && allowMissing {
55+
plog.Infof("%v\n", err)
56+
continue
57+
}
58+
}
59+
fmt.Fprintf(os.Stderr, "Deleting %q failed: %v\n", name, err)
5060
exit = 1
5161
continue
5262
}

mantle/platform/api/gcloud/image.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ func (a *API) DeprecateImage(name string, state DeprecationState, replacement st
223223
func (a *API) DeleteImage(name string) (*Pending, error) {
224224
op, err := a.compute.Images.Delete(a.options.Project, name).Do()
225225
if err != nil {
226-
return nil, fmt.Errorf("Deleting %s failed: %v", name, err)
226+
return nil, err
227227
}
228228
opReq := a.compute.GlobalOperations.Get(a.options.Project, op.Name)
229229
return a.NewPending(op.Name, opReq), nil

0 commit comments

Comments
 (0)