Skip to content

Commit

Permalink
provider: Added a new function 'FilterByTags' to move the function aw…
Browse files Browse the repository at this point in the history
…ay from the general implementation (#370)
  • Loading branch information
xescugc authored Apr 13, 2023
1 parent 552a10f commit 87154f5
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
- Ability declare module variables on map types
([Issue #365](https://github.com/cycloidio/terracognita/issues/365))

### Changed

- Validation for specific provider tags to ignore is done now on the Provider implementation
([Issue #358](https://github.com/cycloidio/terracognita/issues/358))

## [0.8.3] _2023-03-14_

### Fixed
Expand Down
18 changes: 18 additions & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aws
import (
"context"
"fmt"
"regexp"

"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/cycloidio/terracognita/aws/reader"
Expand Down Expand Up @@ -128,3 +129,20 @@ func (a *aws) HasResourceType(t string) bool {
func (a *aws) Source() string { return "hashicorp/aws" }
func (a *aws) Configuration() map[string]interface{} { return a.configuration }
func (a *aws) FixResource(t string, v cty.Value) (cty.Value, error) { return v, nil }

var (
autogeneratedAWSResourcesRe = regexp.MustCompile(`^aws:(?:autoscaling|cloudformation)`)
)

func (a *aws) FilterByTags(tags interface{}) error {
ts, ok := tags.(map[string]interface{})
if !ok {
return nil
}
for k := range ts {
if autogeneratedAWSResourcesRe.MatchString(k) {
return errors.WithStack(errcode.ErrProviderResourceAutogenerated)
}
}
return nil
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,4 @@ func (a *azurerm) FixResource(t string, v cty.Value) (cty.Value, error) {
}
return v, nil
}
func (a *azurerm) FilterByTags(tags interface{}) error { return nil }
1 change: 1 addition & 0 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,4 @@ func (g *google) TFProvider() *schema.Provider {
return g.tfProvider
}
func (g *google) FixResource(t string, v cty.Value) (cty.Value, error) { return v, nil }
func (g *google) FilterByTags(tags interface{}) error { return nil }
14 changes: 14 additions & 0 deletions mock/provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,9 @@ type Provider interface {
// depending on other conditions.
// This could also be issues that the Providers have and may not fix
FixResource(t string, v cty.Value) (cty.Value, error)

// FilterByTags will check the tags for autogenerated provider tags
// that identify generated resources that should not be imported
// like instances in autoscaling
FilterByTags(tags interface{}) error
}
9 changes: 3 additions & 6 deletions provider/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,7 @@ type resource struct {
}

var (
autogeneratedAWSResourcesRe = regexp.MustCompile(`^aws:(?:autoscaling|cloudformation)`)
providerResources = map[string]getResource{
providerResources = map[string]getResource{
"aws": awsdocs.GetResource,
"azurerm": azuredocs.GetResource,
"google": googledocs.GetResource,
Expand Down Expand Up @@ -318,10 +317,8 @@ func (r *resource) Read(f *filter.Filter) error {

// Filter out autogenerated resources from AWS
if v, ok := r.data.GetOk(r.Provider().TagKey()); ok {
for k := range v.(map[string]interface{}) {
if autogeneratedAWSResourcesRe.MatchString(k) {
return errors.WithStack(errcode.ErrProviderResourceAutogenerated)
}
if err = r.provider.FilterByTags(v); err != nil {
return err
}
}

Expand Down
1 change: 1 addition & 0 deletions vsphere/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,4 @@ func (vs vsphere) Source() string { return "hashicorp/vsphere" }

func (vs vsphere) Configuration() map[string]interface{} { return vs.configuration }
func (vs vsphere) FixResource(t string, v cty.Value) (cty.Value, error) { return v, nil }
func (vs vsphere) FilterByTags(tags interface{}) error { return nil }

0 comments on commit 87154f5

Please sign in to comment.