Skip to content

Commit

Permalink
Merge pull request #373 from acc-jon/issue365
Browse files Browse the repository at this point in the history
properly handle nested submodules (#365)
  • Loading branch information
kanchwala-yusuf authored Nov 10, 2020
2 parents ba331e0 + 557294c commit d375f0e
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 3 deletions.
12 changes: 9 additions & 3 deletions pkg/iac-providers/terraform/v12/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,15 @@ func (*TfV12) LoadIacDir(absRootDir string) (allResourcesConfig output.AllResour
var pathToModule string
if isLocalSourceAddr(req.SourceAddr) {
// determine the absolute path from root module to the sub module
// using *configs.ModuleRequest.Path field
pathToModule = filepath.Join(absRootDir, req.Parent.SourceAddr, req.SourceAddr)
zap.S().Debugf("processing local module %q", req.SourceAddr)
// since we start at the end of the path, we need to assemble
// the parts in reverse order

pathToModule = req.SourceAddr
for p := req.Parent; p != nil; p = p.Parent {
pathToModule = filepath.Join(p.SourceAddr, pathToModule)
}
pathToModule = filepath.Join(absRootDir, pathToModule)
zap.S().Debugf("processing local module %q", pathToModule)
} else {
// temp dir to download the remote repo
tempDir := filepath.Join(os.TempDir(), utils.GenRandomString(6))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
variable "m4projectid" {
type = string
default = "asdfasdf"
}

module "m4a" {
source = "./modules/m4a"
m4aprojectid = var.m4projectid
}

resource "aws_s3_bucket" "bucket" {
bucket = var.m4projectid
policy = module.m4a.fullbucketpolicy
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
variable "m4aprojectid" {
type = string
default = "asdfasdf"
}

module "m4b" {
source = "../m4b"
m4bversionyear = "2012"
m4bversionmonth = "10"
m4bversionday = "17"
m4bbucketname = module.m4c.fullbucketname
}
module "m4c" {
source = "../m4c"
m4cbucketname = var.m4aprojectid
m4cenvironment = "dev"
}


resource "aws_s3_bucket" "bucket4a" {
bucket = module.m4c.fullbucketname
policy = module.m4b.fullbucketpolicy
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
variable "m4bversionyear" {
type = string
}
variable "m4bversionmonth" {
type = string
}
variable "m4bversionday" {
type = string
}
variable "m4bbucketname" {
type = string
}
data "aws_iam_policy_document" "readbuckets" {
source_json = <<EOF
{
"Version":"${var.m4bversionyear}-${var.m4bversionmonth}-${var.m4bversionday}",
"Statement":[
{
"Sid": "PublicRead",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject"],
"Resource": ["arn:aws:s3:::${var.m4bbucketname}/*"]
}
]
}
EOF
}

output "fullbucketpolicy" {
value = data.aws_iam_policy_document.readbuckets.json
}
output "BucketARN" {
value = var.m4bbucketname
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
variable "m4cbucketname" {
type = string
}
variable "m4cenvironment" {
type = string
}

output "fullbucketname" {
value = "${var.m4cbucketname}-${var.m4cenvironment}"
}
output "sourcebucketname" {
value = var.m4cbucketname
}
output "sourceenvironment" {
value = var.m4cenvironment
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ module "m1" {
source = "./modules/m1"
m1projectid = "tf-test-project"
}

module "m4" {
source = "./modules/m4"
m4projectid = "tf-test-project-2"
}
22 changes: 22 additions & 0 deletions pkg/iac-providers/terraform/v12/testdata/tfjson/deep-modules.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@
"bucket": "${module.m3.fullbucketname}",
"policy": "${module.m2.fullbucketpolicy}"
}
},
{
"id": "aws_s3_bucket.bucket",
"name": "bucket",
"source": "modules/m4/main.tf",
"line": 11,
"type": "aws_s3_bucket",
"config": {
"bucket": "tf-test-project-2",
"policy": "${module.m4a.fullbucketpolicy}"
}
},
{
"id": "aws_s3_bucket.bucket4a",
"name": "bucket4a",
"source": "modules/m4/modules/m4a/main.tf",
"line": 20,
"type": "aws_s3_bucket",
"config": {
"bucket": "${module.m4c.fullbucketname}",
"policy": "${module.m4b.fullbucketpolicy}"
}
}
]
}

0 comments on commit d375f0e

Please sign in to comment.