From 2ff6fef92e4099cf94d07b4d4d817b10228310c1 Mon Sep 17 00:00:00 2001 From: Soulou Date: Thu, 10 Oct 2019 13:56:08 +0200 Subject: [PATCH] Display a nice message when creating an app in a disabled region --- Gopkg.lock | 6 ++-- apps/create.go | 31 +++++++++++++++++++ utils/errors.go | 15 +++++++++ .../Scalingo/go-scalingo/http/errors.go | 4 ++- 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 utils/errors.go diff --git a/Gopkg.lock b/Gopkg.lock index 7d7adb52d..d7d799802 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -18,7 +18,7 @@ revision = "8eb48cc6f27eafda8e3a9627edba494a1d229a01" [[projects]] - digest = "1:783cbace786278a2ffbde56d5aec37aa94e47c57ffba01967093b34c18b9b7d7" + digest = "1:2c5f09c29d50a9f8321bae28b6e376adf81116a069258f5889ab6860734ec524" name = "github.com/Scalingo/go-scalingo" packages = [ ".", @@ -29,8 +29,8 @@ "io", ] pruneopts = "NUT" - revision = "1bc7f634ed658b0a8499489673497715736ccb7a" - version = "v3.0.6" + revision = "11ac9402ccee4d1cc81dc6b89fa3d66d945bae0b" + version = "v3.0.7" [[projects]] digest = "1:a1dcc4e2d5a5980c31901ea884435b64917fdd523eacb5d6171023b80eaa25a8" diff --git a/apps/create.go b/apps/create.go index e65d73d85..7448769fb 100644 --- a/apps/create.go +++ b/apps/create.go @@ -18,6 +18,9 @@ func Create(appName string, remote string, buildpack string) error { app, err := c.AppsCreate(scalingo.AppsCreateOpts{Name: appName}) if err != nil { + if utils.IsRegionDisabledError(err) { + return handleRegionDisabledError(appName, c) + } if !utils.IsPaymentRequiredAndFreeTrialExceededError(err) { return errgo.Mask(err, errgo.Any) } @@ -43,3 +46,31 @@ func Create(appName string, remote string, buildpack string) error { } return nil } + +func handleRegionDisabledError(appName string, c *scalingo.Client) error { + regions, rerr := c.RegionsList() + if rerr != nil { + return errgo.Notef(rerr, "region is disabled, failed to list available regions") + } + if len(regions) <= 1 { + return errgo.New("region is disabled and there is no other available region") + } + firstRegion := regions[0] + if firstRegion.Name == config.C.ScalingoRegion { + firstRegion = regions[1] + } + + fmt.Printf("Application creation has been disabled on the currently used region: %v\n\n", config.C.ScalingoRegion) + fmt.Printf("Either configure your CLI to use another default region, then create your application:\n") + fmt.Printf(" scalingo config --region %s\n scalingo create %s\n", firstRegion.Name, appName) + fmt.Printf("\nOr use the region flag to specify the region explicitely for this command:\n") + fmt.Printf(" scalingo --region %s create %s\n", firstRegion.Name, appName) + fmt.Printf("\nList of available regions:\n") + for _, region := range regions { + if region.Name == config.C.ScalingoRegion { + continue + } + fmt.Printf("- %v (%v)\n", region.Name, region.DisplayName) + } + return nil +} diff --git a/utils/errors.go b/utils/errors.go new file mode 100644 index 000000000..a34cc2977 --- /dev/null +++ b/utils/errors.go @@ -0,0 +1,15 @@ +package utils + +import ( + "github.com/Scalingo/go-scalingo/http" + "github.com/Scalingo/go-utils/errors" +) + +func IsRegionDisabledError(err error) bool { + reqerr, ok := errors.ErrgoRoot(err).(*http.RequestFailedError) + if !ok || reqerr.Code != 403 { + return false + } + httperr, ok := reqerr.APIError.(http.ForbiddenError) + return ok && httperr.Code == "region_disabled" +} diff --git a/vendor/github.com/Scalingo/go-scalingo/http/errors.go b/vendor/github.com/Scalingo/go-scalingo/http/errors.go index dfdf6fb33..da2fbf2fb 100644 --- a/vendor/github.com/Scalingo/go-scalingo/http/errors.go +++ b/vendor/github.com/Scalingo/go-scalingo/http/errors.go @@ -13,6 +13,7 @@ import ( type ( BadRequestError struct { ErrMessage string `json:"error"` + Code string `json:"code"` } PaymentRequiredError struct { @@ -27,7 +28,8 @@ type ( } ForbiddenError struct { - Err string `json:"error"` + Err string `json:"error"` + Code string `json:"code"` } UnprocessableEntity struct {