Skip to content

Commit

Permalink
Merge pull request #144 from wata727/new_digest_module_path
Browse files Browse the repository at this point in the history
Support new digest module path
  • Loading branch information
wata727 committed Sep 30, 2017
2 parents 0dcc20c + e5ebcdd commit 5865a74
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
22 changes: 17 additions & 5 deletions schema/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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[:])
}
78 changes: 77 additions & 1 deletion schema/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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: `
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "aws_instance" "web" {
ami = "ami-abcd1234"
instance_type = "t2.micro"
}

0 comments on commit 5865a74

Please sign in to comment.