diff --git a/schema/module.go b/schema/module.go index 7503a3c8e..226c32bd9 100644 --- a/schema/module.go +++ b/schema/module.go @@ -36,12 +36,17 @@ func newModule(fileName string, pos token.Pos, moduleId string) *Module { func (m *Module) Load() error { files := map[string][]byte{} + modulePath := m.oldPath() - if _, err := os.Stat(m.path()); err != nil { - return fmt.Errorf("ERROR: module `%s` not found. Did you run `terraform get`?", m.ModuleSource) + if _, err := os.Stat(modulePath); err != nil { + // Since the digest has changed in Terraform v0.10.6, try to check the new path + modulePath = m.path() + if _, err := os.Stat(modulePath); err != nil { + return fmt.Errorf("ERROR: module `%s` not found. Did you run `terraform get`?", m.ModuleSource) + } } - filePaths, err := filepath.Glob(m.path() + "/*.tf") + filePaths, err := filepath.Glob(modulePath + "/*.tf") if err != nil { return err } @@ -52,7 +57,7 @@ func (m *Module) Load() error { return fmt.Errorf("ERROR: Cannot open file %s", filePath) } - fileName := strings.Replace(filePath, m.path(), "", 1) + fileName := strings.Replace(filePath, modulePath, "", 1) fileKey := m.ModuleSource + fileName files[fileKey] = b } @@ -64,8 +69,15 @@ func (m *Module) Load() error { return nil } -func (m *Module) path() string { +// Since the digest has changed in Terraform v0.10.6, this is the path up to v0.10.5 +func (m *Module) oldPath() string { base := "root." + m.Id + "-" + m.ModuleSource sum := md5.Sum([]byte(base)) // #nosec return ".terraform/modules/" + hex.EncodeToString(sum[:]) } + +func (m *Module) path() string { + base := "module." + m.Id + "-" + m.ModuleSource + sum := md5.Sum([]byte(base)) // #nosec + return ".terraform/modules/" + hex.EncodeToString(sum[:]) +} diff --git a/schema/module_test.go b/schema/module_test.go index eea738b28..fbebf902e 100644 --- a/schema/module_test.go +++ b/schema/module_test.go @@ -19,7 +19,7 @@ func TestLoad(t *testing.T) { Error bool }{ { - Name: "init module", + Name: "init module with old digest path", Input: ` module "ec2_instance" { source = "./tf_aws_ec2_instance" @@ -94,6 +94,82 @@ module "ec2_instance" { }, Error: false, }, + { + Name: "init module with new digest path", + Input: ` +module "ec2_instance" { + source = "./tf_aws_ec2_instance_v2" +}`, + Result: []*Template{ + { + File: "./tf_aws_ec2_instance_v2/test.tf", + Resources: []*Resource{ + { + Source: &Source{ + File: "./tf_aws_ec2_instance_v2/test.tf", + Pos: token.Pos{ + Filename: "./tf_aws_ec2_instance_v2/test.tf", + Offset: 30, + Line: 1, + Column: 31, + }, + Attrs: map[string]*Attribute{ + "instance_type": { + Poses: []token.Pos{ + { + Filename: "./tf_aws_ec2_instance_v2/test.tf", + Offset: 83, + Line: 3, + Column: 19, + }, + }, + Vals: []interface{}{ + token.Token{ + Type: 9, + Pos: token.Pos{ + Filename: "./tf_aws_ec2_instance_v2/test.tf", + Offset: 83, + Line: 3, + Column: 19, + }, + Text: "\"t2.micro\"", + JSON: false, + }, + }, + }, + "ami": { + Poses: []token.Pos{ + { + Filename: "./tf_aws_ec2_instance_v2/test.tf", + Offset: 50, + Line: 2, + Column: 19, + }, + }, + Vals: []interface{}{ + token.Token{ + Type: 9, + Pos: token.Pos{ + Filename: "./tf_aws_ec2_instance_v2/test.tf", + Offset: 50, + Line: 2, + Column: 19, + }, + Text: "\"ami-abcd1234\"", + JSON: false, + }, + }, + }, + }, + }, + Type: "aws_instance", + Id: "web", + }, + }, + }, + }, + Error: false, + }, { Name: "module not found", Input: ` diff --git a/schema/test-fixtures/.terraform/modules/a4fbc206f00a6da596866799d651304c/test.tf b/schema/test-fixtures/.terraform/modules/a4fbc206f00a6da596866799d651304c/test.tf new file mode 100644 index 000000000..de537f056 --- /dev/null +++ b/schema/test-fixtures/.terraform/modules/a4fbc206f00a6da596866799d651304c/test.tf @@ -0,0 +1,4 @@ +resource "aws_instance" "web" { + ami = "ami-abcd1234" + instance_type = "t2.micro" +}