Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new digest module path #144

Merged
merged 1 commit into from
Sep 30, 2017
Merged
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
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"
}