Skip to content

Commit

Permalink
prune: Make it work with old TF versions using 'depends_on'
Browse files Browse the repository at this point in the history
We fixed this issue on the 'generate' but not on the 'prune' so we imported the fix we did
  • Loading branch information
xescugc committed Jun 11, 2021
1 parent 689f89a commit e5b8c24
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## [Unreleased]

### Fixed
- Prune was not working with old TF versions because of the `depends_on`
([Issue #144](https://github.com/cycloidio/inframap/pull/144))

## [0.6.6] _2021-06-10_

### Changed
Expand Down
6 changes: 3 additions & 3 deletions generate/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func FromState(tfstate json.RawMessage, opt Options) (*graph.Graph, map[string]i
// replace from '"depends_on"' to '"dependencies"'
hasDependsOn := bytes.Contains(tfstate, []byte("\"depends_on\""))
tfstate = bytes.ReplaceAll(tfstate, []byte("\"depends_on\""), []byte("\"dependencies\""))
err := validateTFStateVersion(tfstate)
err := ValidateTFStateVersion(tfstate)
if err != nil {
return nil, nil, fmt.Errorf("error while validating TFStateVersion: %w", err)
}
Expand Down Expand Up @@ -273,9 +273,9 @@ func migrateVersions(src []byte, f *statefile.File) error {
return nil
}

// validateTFStateVersion validates that the version is the
// ValidateTFStateVersion validates that the version is the
// one we support which is only 3 and 4
func validateTFStateVersion(b []byte) error {
func ValidateTFStateVersion(b []byte) error {
v, err := tfStateVersion(b)
if err != nil {
return fmt.Errorf("could not read version: %w", err)
Expand Down
10 changes: 10 additions & 0 deletions prune/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/chr4/pwgen"
"github.com/cycloidio/inframap/errcode"
"github.com/cycloidio/inframap/generate"
"github.com/cycloidio/inframap/provider/factory"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/states/statefile"
Expand All @@ -23,7 +24,16 @@ var reARN = regexp.MustCompile("^arn:*")
// the resource canonicals will also be changed, for exmple 'aws_lb.front' will be changed to
// a random name like 'aws_lb.XptaK'
func Prune(tfstate json.RawMessage, replaceCanonicals bool) (json.RawMessage, error) {
// Since TF 0.13 'depends_on' has been dropped, so we do a manual
// replace from '"depends_on"' to '"dependencies"'
tfstate = bytes.ReplaceAll(tfstate, []byte("\"depends_on\""), []byte("\"dependencies\""))
err := generate.ValidateTFStateVersion(tfstate)
if err != nil {
return nil, fmt.Errorf("error while validating TFStateVersion: %w", err)
}

buf := bytes.NewBuffer(tfstate)

file, err := statefile.Read(buf)
if err != nil {
return nil, fmt.Errorf("error while reading TFState: %w", err)
Expand Down

0 comments on commit e5b8c24

Please sign in to comment.