From 99ec85e534e57c3c372d49b6b1f8a9dc20ba89c6 Mon Sep 17 00:00:00 2001 From: talset Date: Wed, 9 Feb 2022 17:03:04 +0100 Subject: [PATCH] azurerm: do not stop when service not enable fix #247 we filter the error from Azure and return a custom error type if it's an error that we want to skip azure seems to not return error interface. Use regex to get Code --- azurerm/provider.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/azurerm/provider.go b/azurerm/provider.go index bad69cd0eb..45f5103fd9 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -3,6 +3,7 @@ package azurerm import ( "context" "fmt" + "regexp" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" @@ -10,11 +11,21 @@ import ( tfazurerm "github.com/terraform-providers/terraform-provider-azurerm/azurerm" "github.com/cycloidio/terracognita/cache" + "github.com/cycloidio/terracognita/errcode" "github.com/cycloidio/terracognita/filter" "github.com/cycloidio/terracognita/log" "github.com/cycloidio/terracognita/provider" ) +// skippableCodes is a list of codes +// which won't make Terracognita failed +// but they will be printed on the output +// they are based on the err.Code() content +// of the Azure error +var skippableCodes = map[string]struct{}{ + "ParentResourceNotFound": struct{}{}, +} + type azurerm struct { tfAzureRMClient interface{} tfProvider *schema.Provider @@ -89,6 +100,18 @@ func (a *azurerm) Resources(ctx context.Context, t string, f *filter.Filter) ([] resources, err := rfn(ctx, a, t, f) if err != nil { + // we filter the error from Azure and return a custom error + // type if it's an error that we want to skip + // azure seems to not return error interface. Use regex to get Code + r, _ := regexp.Compile("\\sCode=\"([^\"]+)\"") + errorCode := r.FindStringSubmatch(err.Error()) + if len(errorCode) == 2 { + fmt.Println(errorCode[1]) + if _, ok := skippableCodes[errorCode[1]]; ok { + return nil, fmt.Errorf("%w: %v", errcode.ErrProviderAPI, err) + } + } + return nil, errors.Wrapf(err, "error while reading from resource %q", t) }