Skip to content

Commit

Permalink
azurerm: do not stop when service not enable fix #247
Browse files Browse the repository at this point in the history
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
  • Loading branch information
talset committed Feb 9, 2022
1 parent c680d2e commit 99ec85e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@ package azurerm
import (
"context"
"fmt"
"regexp"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/pkg/errors"
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
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 99ec85e

Please sign in to comment.