Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
github.com/google/go-cmp v0.5.1
github.com/hashicorp/go-terraform-address v0.0.0-20200814224554-3a484c5d5284
github.com/mattn/go-colorable v0.1.7
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hashicorp/go-terraform-address v0.0.0-20200814224554-3a484c5d5284 h1:XK/E8kLm/HGwik4GByK4tC9vTZ26Mp51OH3Q6Tv6Xqg=
github.com/hashicorp/go-terraform-address v0.0.0-20200814224554-3a484c5d5284/go.mod h1:xoy1vl2+4YvqSQEkKcFjNYxTk7cll+o1f1t2wxnHIX8=
github.com/mattn/go-colorable v0.1.7 h1:bQGKb3vps/j0E9GfJQ03JyhRuxsvdAanXlT9BTw3mdw=
github.com/mattn/go-colorable v0.1.7/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9 h1:yi1hN8dcqI9l8klZfy4B8mJvFmmAxJEePIQQFNSd7Cs=
golang.org/x/sys v0.0.0-20200808120158-1030fc2bf1d9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
55 changes: 10 additions & 45 deletions resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package tfplanparse

import (
"fmt"
"strconv"
"strings"

"github.com/hashicorp/go-terraform-address"
)

const (
Expand Down Expand Up @@ -131,53 +132,17 @@ func NewResourceChangeFromComment(comment string) (*ResourceChange, error) {
}

func (rc *ResourceChange) finalizeResourceInfo() error {
var address string

// parse index first in case the index contains a "."
addressIndex := strings.Split(rc.Address, "[")
address = addressIndex[0]

if len(addressIndex) == 2 {
index := dequote(strings.TrimSuffix(addressIndex[1], "]"))

if i, err := strconv.Atoi(index); err == nil {
rc.Index = i
} else {
rc.Index = index
}
} else if len(addressIndex) > 2 {
return fmt.Errorf("failed to parse resource info from address %s", rc.Address)
parsedInterface, err := address.Parse("", []byte(rc.Address))
if err != nil {
return err
}

values := strings.Split(address, ".")
parsed := parsedInterface.(*address.Address)

// TODO: handle module.module_name.data.type.name better
// TODO: eventually do something with "data"
// For now, since we're not handling it, we can just remove it
for k, v := range values {
var previous string
if k != 0 {
previous = values[k-1]
}

// don't remove "data" if any of the conditions are true:
// 1. Previous element was "module" or "data" (this means the module or data itself is named "data")
// 2. There are less than 2 elements left to parse (this means the resource name or type is "data")
if v == "data" && (previous != "module" && previous != "data") && (len(values)-k) > 2 {
values = append(values[:k], values[k+1:]...)
}
}

if len(values) == 2 {
rc.Name = values[1]
rc.Type = values[0]
} else if len(values) > 2 {
rc.Name = values[len(values)-1]
rc.Type = values[len(values)-2]
rc.ModuleAddress = fmt.Sprintf("%s.%s", values[0], values[1])
} else {
return fmt.Errorf("failed to parse resource info from address %s", rc.Address)
}
rc.Type = parsed.ResourceSpec.Type
rc.Name = parsed.ResourceSpec.Name
rc.ModuleAddress = parsed.ModulePath.String()
rc.Index = parsed.ResourceSpec.Index.Value

return nil
}
Expand Down
4 changes: 4 additions & 0 deletions resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ func TestNewResourceChangeFromComment(t *testing.T) {
t.Fatalf("Expected an error but didn't get one")
}

if err != nil && !tc.shouldError {
t.Fatalf("Unexpected error %v", err)
}

if !reflect.DeepEqual(got, tc.expected) {
t.Fatalf("Expected: %v but got %v", tc.expected, got)
}
Expand Down