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 moudle path for v0.10.7 #149

Merged
merged 1 commit into from
Oct 13, 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
37 changes: 22 additions & 15 deletions schema/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,9 @@ 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(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)
}
modulePath, err := m.path()
if err != nil {
return err
}

filePaths, err := filepath.Glob(modulePath + "/*.tf")
Expand All @@ -69,15 +64,27 @@ func (m *Module) Load() error {
return nil
}

// 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, error) {
// before v0.10.5
base := m.pathname("root." + m.Id + "-" + m.ModuleSource)
if _, err := os.Stat(base); err == nil {
return base, nil
}
// for v0.10.6
base = m.pathname("module." + m.Id + "-" + m.ModuleSource)
if _, err := os.Stat(base); err == nil {
return base, nil
}
// after v0.10.7
base = m.pathname("0.root." + m.Id + "-" + m.ModuleSource)
if _, err := os.Stat(base); err == nil {
return base, nil
}

return "", fmt.Errorf("ERROR: module `%s` not found. Did you run `terraform get`?", m.ModuleSource)
}

func (m *Module) path() string {
base := "module." + m.Id + "-" + m.ModuleSource
func (m *Module) pathname(base string) string {
sum := md5.Sum([]byte(base)) // #nosec
return ".terraform/modules/" + hex.EncodeToString(sum[:])
}
80 changes: 78 additions & 2 deletions 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 with old digest path",
Name: "init module with v0.10.5",
Input: `
module "ec2_instance" {
source = "./tf_aws_ec2_instance"
Expand Down Expand Up @@ -95,7 +95,7 @@ module "ec2_instance" {
Error: false,
},
{
Name: "init module with new digest path",
Name: "init module with v0.10.6",
Input: `
module "ec2_instance" {
source = "./tf_aws_ec2_instance_v2"
Expand Down Expand Up @@ -170,6 +170,82 @@ module "ec2_instance" {
},
Error: false,
},
{
Name: "init module with v0.10.7",
Input: `
module "ec2_instance" {
source = "./tf_aws_ec2_instance_v3"
}`,
Result: []*Template{
{
File: "./tf_aws_ec2_instance_v3/test.tf",
Resources: []*Resource{
{
Source: &Source{
File: "./tf_aws_ec2_instance_v3/test.tf",
Pos: token.Pos{
Filename: "./tf_aws_ec2_instance_v3/test.tf",
Offset: 30,
Line: 1,
Column: 31,
},
Attrs: map[string]*Attribute{
"instance_type": {
Poses: []token.Pos{
{
Filename: "./tf_aws_ec2_instance_v3/test.tf",
Offset: 83,
Line: 3,
Column: 19,
},
},
Vals: []interface{}{
token.Token{
Type: 9,
Pos: token.Pos{
Filename: "./tf_aws_ec2_instance_v3/test.tf",
Offset: 83,
Line: 3,
Column: 19,
},
Text: "\"m3.large\"",
JSON: false,
},
},
},
"ami": {
Poses: []token.Pos{
{
Filename: "./tf_aws_ec2_instance_v3/test.tf",
Offset: 50,
Line: 2,
Column: 19,
},
},
Vals: []interface{}{
token.Token{
Type: 9,
Pos: token.Pos{
Filename: "./tf_aws_ec2_instance_v3/test.tf",
Offset: 50,
Line: 2,
Column: 19,
},
Text: "\"ami-9876abcd\"",
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-9876abcd"
instance_type = "m3.large"
}