Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nasir Rabbani committed Nov 29, 2022
1 parent 795ef69 commit af7dcd4
Show file tree
Hide file tree
Showing 29 changed files with 478 additions and 208 deletions.
11 changes: 11 additions & 0 deletions pkg/iac-providers/terraform/commons/load-dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,9 @@ func (t TerraformDirectoryLoader) loadDirNonRecursive() (output.AllResourceConfi
t.addError(errMessage, t.absRootDir)
}

// getting provider version for the root module
providerVersion := GetModuleProviderVersion(rootMod)

// get unified config for the current directory
unified, diags := t.buildUnifiedConfig(rootMod, t.absRootDir)

Expand Down Expand Up @@ -357,6 +360,14 @@ func (t TerraformDirectoryLoader) loadDirNonRecursive() (output.AllResourceConfi
return allResourcesConfig, multierror.Append(t.errIacLoadDirs, results.DirScanErr{IacType: "terraform", Directory: t.absRootDir, ErrMessage: errMessage})
}

resourceConfig.TerraformVersion = t.terraformVersion
resourceConfig.ProviderVersion = providerVersion

// if root module do not have provider contraints fetch the latest compatible version
if resourceConfig.ProviderVersion == "" {
resourceConfig.ProviderVersion = LatestProviderVersion(managedResource.Provider, t.terraformVersion)
}

if isRemoteModule {
resourceConfig.IsRemoteModule = &isRemoteModule
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/iac-providers/terraform/commons/terraform-provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ func GetModuleProviderVersion(m *configs.Module) string {
return version
}
for _, requiredProvider := range m.ProviderRequirements.RequiredProviders {
version = requiredProvider.Requirement.Required[0].String()
if requiredProvider != nil && len(requiredProvider.Requirement.Required) > 0 {
version = requiredProvider.Requirement.Required[0].String()
}
}

// trim version string
Expand All @@ -146,11 +148,13 @@ func GetModuleProviderVersion(m *configs.Module) string {
// if the 'required_providers' is not defined, it returns empty string
func GetFileProviderVersion(f *configs.File) string {
version := ""
if f == nil || f.RequiredProviders == nil {
if f == nil || f.RequiredProviders == nil || len(f.RequiredProviders) == 0 {
return version
}
for _, requiredProvider := range f.RequiredProviders[0].RequiredProviders {
version = requiredProvider.Requirement.Required[0].String()
if requiredProvider != nil && len(requiredProvider.Requirement.Required) > 0 {
version = requiredProvider.Requirement.Required[0].String()
}
}

// trim version string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,51 @@
{
"aws_ecs_task_definition": [
{
"id": "aws_ecs_task_definition.demo-ecs-jsoncode-task-definition",
"name": "demo-ecs-jsoncode-task-definition",
"module_name": "root",
"source": "terraform-aws-provider/jsoncode_in_task_definition.tf",
"plan_root": "terraform-aws-provider",
"line": 1,
"type": "aws_ecs_task_definition",
"config": {
"container_definitions": "${jsonencode([\n {\n name = \"first\"\n image = \"service-first\"\n cpu = 10\n memory = 512\n essential = true\n portMappings = [\n {\n containerPort = 80\n hostPort = 80\n }\n ]\n },\n {\n name = \"second\"\n image = \"service-second\"\n cpu = 10\n memory = 256\n essential = true\n portMappings = [\n {\n containerPort = 443\n hostPort = 443\n }\n ]\n }\n ])}",
"cpu": "512",
"execution_role_arn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"family": "ecs-task-definition-demo",
"memory": "1024",
"network_mode": "awsvpc",
"requires_compatibilities": [
"FARGATE"
]
},
"line_config": {
"container_definitions": 8,
"cpu": 6,
"execution_role_arn": 7,
"family": 2,
"memory": 5,
"network_mode": 3,
"requires_compatibilities": 4
},
"skip_rules": null,
"max_severity": "",
"min_severity": "",
"container_images": [
{
"name": "first",
"image": "service-first",
"vulnerabilities": null
},
{
"name": "second",
"image": "service-second",
"vulnerabilities": null
}
],
"terraform_version": "0.15.0",
"provider_version": "4.42.0"
},
{
"id": "aws_ecs_task_definition.demo-ecs-task-definition",
"name": "demo-ecs-task-definition",
Expand Down Expand Up @@ -39,7 +85,7 @@
}
],
"terraform_version": "0.15.0",
"provider_version": "4.41.0"
"provider_version": "4.42.0"
},
{
"id": "aws_ecs_task_definition.service",
Expand Down Expand Up @@ -94,53 +140,7 @@
}
],
"terraform_version": "0.15.0",
"provider_version": "4.41.0"
},
{
"id": "aws_ecs_task_definition.demo-ecs-jsoncode-task-definition",
"name": "demo-ecs-jsoncode-task-definition",
"module_name": "root",
"source": "terraform-aws-provider/jsoncode_in_task_definition.tf",
"plan_root": "terraform-aws-provider",
"line": 1,
"type": "aws_ecs_task_definition",
"config": {
"container_definitions": "${jsonencode([\n {\n name = \"first\"\n image = \"service-first\"\n cpu = 10\n memory = 512\n essential = true\n portMappings = [\n {\n containerPort = 80\n hostPort = 80\n }\n ]\n },\n {\n name = \"second\"\n image = \"service-second\"\n cpu = 10\n memory = 256\n essential = true\n portMappings = [\n {\n containerPort = 443\n hostPort = 443\n }\n ]\n }\n ])}",
"cpu": "512",
"execution_role_arn": "arn:aws:iam::123456789012:role/ecsTaskExecutionRole",
"family": "ecs-task-definition-demo",
"memory": "1024",
"network_mode": "awsvpc",
"requires_compatibilities": [
"FARGATE"
]
},
"line_config": {
"container_definitions": 8,
"cpu": 6,
"execution_role_arn": 7,
"family": 2,
"memory": 5,
"network_mode": 3,
"requires_compatibilities": 4
},
"skip_rules": null,
"max_severity": "",
"min_severity": "",
"container_images": [
{
"name": "first",
"image": "service-first",
"vulnerabilities": null
},
{
"name": "second",
"image": "service-second",
"vulnerabilities": null
}
],
"terraform_version": "0.15.0",
"provider_version": "4.41.0"
"provider_version": "4.42.0"
}
],
"azurerm_container_group": [
Expand Down
32 changes: 24 additions & 8 deletions pkg/iac-providers/terraform/v12/testdata/tfjson/config1.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "0.0.0"
}
],
"aws_internet_gateway": [
Expand All @@ -92,7 +94,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "0.0.0"
}
],
"aws_key_pair": [
Expand All @@ -113,7 +117,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "0.0.0"
}
],
"aws_route_table": [
Expand Down Expand Up @@ -150,7 +156,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "0.0.0"
}
],
"aws_route_table_association": [
Expand All @@ -171,7 +179,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "0.0.0"
}
],
"aws_security_group": [
Expand Down Expand Up @@ -248,7 +258,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "0.0.0"
}
],
"aws_subnet": [
Expand Down Expand Up @@ -277,7 +289,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "0.0.0"
}
],
"aws_vpc": [
Expand Down Expand Up @@ -306,7 +320,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "0.0.0"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,46 @@
{
"id": "aws_s3_bucket.bucket",
"name": "bucket",
"module_name": "m1",
"source": "modules/m1/main.tf",
"module_name": "m4",
"source": "modules/m4/main.tf",
"plan_root": "./",
"line": 20,
"line": 11,
"type": "aws_s3_bucket",
"config": {
"bucket": "${module.m3.fullbucketname}",
"policy": "${module.m2.fullbucketpolicy}"
"bucket": "tf-test-project-2",
"policy": "${module.m4a.fullbucketpolicy}"
},
"line_config": {
"bucket": 21,
"policy": 22
"bucket": 12,
"policy": 13
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "3.37.0"
},
{
"id": "aws_s3_bucket.bucket",
"name": "bucket",
"module_name": "m4",
"source": "modules/m4/main.tf",
"module_name": "m1",
"source": "modules/m1/main.tf",
"plan_root": "./",
"line": 11,
"line": 20,
"type": "aws_s3_bucket",
"config": {
"bucket": "tf-test-project-2",
"policy": "${module.m4a.fullbucketpolicy}"
"bucket": "${module.m3.fullbucketname}",
"policy": "${module.m2.fullbucketpolicy}"
},
"line_config": {
"bucket": 12,
"policy": 13
"bucket": 21,
"policy": 22
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "3.37.0"
},
{
"id": "aws_s3_bucket.bucket4a",
Expand All @@ -58,7 +62,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "3.37.0"
},
{
"id": "aws_s3_bucket.bucket",
Expand All @@ -78,7 +84,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "3.37.0"
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "3.37.0"
},
{
"id": "aws_s3_bucket.bucket",
Expand All @@ -38,7 +40,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "3.37.0"
},
{
"id": "aws_s3_bucket.bucket4a",
Expand All @@ -58,7 +62,9 @@
},
"skip_rules": null,
"max_severity": "",
"min_severity": ""
"min_severity": "",
"terraform_version": "0.12.0",
"provider_version": "3.37.0"
}
]
}
Loading

0 comments on commit af7dcd4

Please sign in to comment.