Skip to content

Commit 43a6575

Browse files
authored
terraform: Add support for Terraform v1.1 syntax (#1276)
* Bump Terraform version to v1.1 * Follow up of refactoring module source addresses hashicorp/terraform@1a8da65 * Assign unknown value to variables when it does not have default * Fix broken tests
1 parent 46eb8ee commit 43a6575

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2953
-177
lines changed

plugin/encode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func (s *Server) encodeConfig(config *tfconfigs.Config) (*tfplugin.Config, error
2727
Path: addrs.Module(config.Path),
2828
Module: module,
2929
CallRange: config.CallRange,
30-
SourceAddr: config.SourceAddr,
30+
SourceAddr: config.SourceAddrRaw,
3131
SourceAddrRange: config.SourceAddrRange,
3232
Version: versionStr,
3333
}, nil
@@ -214,7 +214,7 @@ func (s *Server) encodeModuleCall(call *tfconfigs.ModuleCall) *tfplugin.ModuleCa
214214
return &tfplugin.ModuleCall{
215215
Name: call.Name,
216216

217-
SourceAddr: call.SourceAddr,
217+
SourceAddr: call.SourceAddrRaw,
218218
SourceAddrRange: call.SourceAddrRange,
219219
SourceSet: call.SourceSet,
220220

rules/terraformrules/terraform_module_pinned_source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func (r *TerraformModulePinnedSourceRule) Check(runner *tflint.Runner) error {
7878
func (r *TerraformModulePinnedSourceRule) checkModule(runner *tflint.Runner, module *configs.ModuleCall, config terraformModulePinnedSourceRuleConfig) error {
7979
log.Printf("[DEBUG] Walk `%s` attribute", module.Name+".source")
8080

81-
source, err := getter.Detect(module.SourceAddr, filepath.Dir(module.DeclRange.Filename), []getter.Detector{
81+
source, err := getter.Detect(module.SourceAddrRaw, filepath.Dir(module.DeclRange.Filename), []getter.Detector{
8282
// https://github.com/hashicorp/terraform/blob/51b0aee36cc2145f45f5b04051a01eb6eb7be8bf/internal/getmodules/getter.go#L30-L52
8383
new(getter.GitHubDetector),
8484
new(getter.GitDetector),

rules/terraformrules/terraform_module_pinned_source_test.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ module "unpinned" {
143143
Expected: tflint.Issues{
144144
{
145145
Rule: NewTerraformModulePinnedSourceRule(),
146-
Message: "Module source \"github.com/hashicorp/consul\" is not pinned",
146+
Message: "Module source \"git::https://github.com/hashicorp/consul.git\" is not pinned",
147147
Range: hcl.Range{
148148
Filename: "module.tf",
149149
Start: hcl.Pos{Line: 3, Column: 12},
@@ -161,7 +161,7 @@ module "unpinned" {
161161
Expected: tflint.Issues{
162162
{
163163
Rule: NewTerraformModulePinnedSourceRule(),
164-
Message: "Module source \"git@github.com:hashicorp/consul.git\" is not pinned",
164+
Message: "Module source \"git::ssh://git@github.com/hashicorp/consul.git\" is not pinned",
165165
Range: hcl.Range{
166166
Filename: "module.tf",
167167
Start: hcl.Pos{Line: 3, Column: 12},
@@ -179,7 +179,7 @@ module "default_git" {
179179
Expected: tflint.Issues{
180180
{
181181
Rule: NewTerraformModulePinnedSourceRule(),
182-
Message: "Module source \"github.com/hashicorp/consul.git?ref=master\" uses a default branch as ref (master)",
182+
Message: "Module source \"git::https://github.com/hashicorp/consul.git?ref=master\" uses a default branch as ref (master)",
183183
Range: hcl.Range{
184184
Filename: "module.tf",
185185
Start: hcl.Pos{Line: 3, Column: 12},
@@ -218,7 +218,7 @@ rule "terraform_module_pinned_source" {
218218
Expected: tflint.Issues{
219219
{
220220
Rule: NewTerraformModulePinnedSourceRule(),
221-
Message: "Module source \"github.com/hashicorp/consul.git?ref=pinned\" uses a ref which is not a semantic version string",
221+
Message: "Module source \"git::https://github.com/hashicorp/consul.git?ref=pinned\" uses a ref which is not a semantic version string",
222222
Range: hcl.Range{
223223
Filename: "module.tf",
224224
Start: hcl.Pos{Line: 3, Column: 12},
@@ -249,7 +249,7 @@ module "unpinned" {
249249
Expected: tflint.Issues{
250250
{
251251
Rule: NewTerraformModulePinnedSourceRule(),
252-
Message: "Module source \"bitbucket.org/hashicorp/tf-test-git\" is not pinned",
252+
Message: "Module source \"git::https://bitbucket.org/hashicorp/tf-test-git.git\" is not pinned",
253253
Range: hcl.Range{
254254
Filename: "module.tf",
255255
Start: hcl.Pos{Line: 3, Column: 12},
@@ -267,7 +267,7 @@ module "default_git" {
267267
Expected: tflint.Issues{
268268
{
269269
Rule: NewTerraformModulePinnedSourceRule(),
270-
Message: "Module source \"bitbucket.org/hashicorp/tf-test-git.git?ref=master\" uses a default branch as ref (master)",
270+
Message: "Module source \"git::https://bitbucket.org/hashicorp/tf-test-git.git?ref=master\" uses a default branch as ref (master)",
271271
Range: hcl.Range{
272272
Filename: "module.tf",
273273
Start: hcl.Pos{Line: 3, Column: 12},
@@ -298,7 +298,7 @@ rule "terraform_module_pinned_source" {
298298
Expected: tflint.Issues{
299299
{
300300
Rule: NewTerraformModulePinnedSourceRule(),
301-
Message: "Module source \"bitbucket.org/hashicorp/tf-test-git.git?ref=pinned\" uses a ref which is not a semantic version string",
301+
Message: "Module source \"git::https://bitbucket.org/hashicorp/tf-test-git.git?ref=pinned\" uses a ref which is not a semantic version string",
302302
Range: hcl.Range{
303303
Filename: "module.tf",
304304
Start: hcl.Pos{Line: 3, Column: 12},
@@ -428,7 +428,7 @@ rule "terraform_module_pinned_source" {
428428
Expected: tflint.Issues{
429429
{
430430
Rule: NewTerraformModulePinnedSourceRule(),
431-
Message: "Module source \"github.com/hashicorp/consul.git?ref=foo\" uses a default branch as ref (foo)",
431+
Message: "Module source \"git::https://github.com/hashicorp/consul.git?ref=foo\" uses a default branch as ref (foo)",
432432
Range: hcl.Range{
433433
Filename: "module.tf",
434434
Start: hcl.Pos{Line: 3, Column: 12},
@@ -504,15 +504,6 @@ rule "terraform_module_pinned_source" {
504504
},
505505
},
506506
},
507-
{
508-
Name: "gitlab registry module",
509-
Content: `
510-
module "m" {
511-
source = "gitlab.com/namespace/module_name/module_system"
512-
version = "1.0.0"
513-
}`,
514-
Expected: tflint.Issues{},
515-
},
516507
}
517508

518509
rule := NewTerraformModulePinnedSourceRule()

rules/terraformrules/terraform_module_version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (r *TerraformModuleVersionRule) Check(runner *tflint.Runner) error {
7474
func (r *TerraformModuleVersionRule) checkModule(runner *tflint.Runner, module *configs.ModuleCall, config TerraformModuleVersionRuleConfig) error {
7575
log.Printf("[DEBUG] Walk `%s` attribute", module.Name+".source")
7676

77-
source, err := addrs.ParseModuleSource(module.SourceAddr)
77+
source, err := addrs.ParseModuleSource(module.SourceAddrRaw)
7878
if err != nil {
7979
return err
8080
}

rules/terraformrules/terraform_naming_convention_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ func testModuleSnakeCase(t *testing.T, testType string, formatName string, confi
11351135
Name: fmt.Sprintf("module: %s - Invalid snake_case with dash", testType),
11361136
Content: `
11371137
module "dash-name" {
1138-
source = ""
1138+
source = "./module"
11391139
}`,
11401140
Config: config,
11411141
Expected: tflint.Issues{
@@ -1154,7 +1154,7 @@ module "dash-name" {
11541154
Name: fmt.Sprintf("module: %s - Invalid snake_case with camelCase", testType),
11551155
Content: `
11561156
module "camelCased" {
1157-
source = ""
1157+
source = "./module"
11581158
}`,
11591159
Config: config,
11601160
Expected: tflint.Issues{
@@ -1173,7 +1173,7 @@ module "camelCased" {
11731173
Name: fmt.Sprintf("module: %s - Invalid snake_case with double underscore", testType),
11741174
Content: `
11751175
module "foo__bar" {
1176-
source = ""
1176+
source = "./module"
11771177
}`,
11781178
Config: config,
11791179
Expected: tflint.Issues{
@@ -1192,7 +1192,7 @@ module "foo__bar" {
11921192
Name: fmt.Sprintf("module: %s - Invalid snake_case with underscore tail", testType),
11931193
Content: `
11941194
module "foo_bar_" {
1195-
source = ""
1195+
source = "./module"
11961196
}`,
11971197
Config: config,
11981198
Expected: tflint.Issues{
@@ -1211,7 +1211,7 @@ module "foo_bar_" {
12111211
Name: fmt.Sprintf("module: %s - Invalid snake_case with Mixed_Snake_Case", testType),
12121212
Content: `
12131213
module "Foo_Bar" {
1214-
source = ""
1214+
source = "./module"
12151215
}`,
12161216
Config: config,
12171217
Expected: tflint.Issues{
@@ -1230,7 +1230,7 @@ module "Foo_Bar" {
12301230
Name: fmt.Sprintf("module: %s - Valid snake_case", testType),
12311231
Content: `
12321232
module "foo_bar" {
1233-
source = ""
1233+
source = "./module"
12341234
}`,
12351235
Config: config,
12361236
Expected: tflint.Issues{},
@@ -1239,7 +1239,7 @@ module "foo_bar" {
12391239
Name: fmt.Sprintf("module: %s - Valid single word", testType),
12401240
Content: `
12411241
module "foo" {
1242-
source = ""
1242+
source = "./module"
12431243
}`,
12441244
Config: config,
12451245
Expected: tflint.Issues{},
@@ -1270,7 +1270,7 @@ func testModuleMixedSnakeCase(t *testing.T, testType string, config string) {
12701270
Name: fmt.Sprintf("module: %s - Invalid mixed_snake_case with dash", testType),
12711271
Content: `
12721272
module "dash-name" {
1273-
source = ""
1273+
source = "./module"
12741274
}`,
12751275
Config: config,
12761276
Expected: tflint.Issues{
@@ -1289,7 +1289,7 @@ module "dash-name" {
12891289
Name: fmt.Sprintf("module: %s - Invalid mixed_snake_case with double underscore", testType),
12901290
Content: `
12911291
module "Foo__Bar" {
1292-
source = ""
1292+
source = "./module"
12931293
}`,
12941294
Config: config,
12951295
Expected: tflint.Issues{
@@ -1308,7 +1308,7 @@ module "Foo__Bar" {
13081308
Name: fmt.Sprintf("module: %s - Invalid mixed_snake_case with underscore tail", testType),
13091309
Content: `
13101310
module "Foo_Bar_" {
1311-
source = ""
1311+
source = "./module"
13121312
}`,
13131313
Config: config,
13141314
Expected: tflint.Issues{
@@ -1327,7 +1327,7 @@ module "Foo_Bar_" {
13271327
Name: fmt.Sprintf("module: %s - Valid snake_case", testType),
13281328
Content: `
13291329
module "foo_bar" {
1330-
source = ""
1330+
source = "./module"
13311331
}`,
13321332
Config: config,
13331333
Expected: tflint.Issues{},
@@ -1336,7 +1336,7 @@ module "foo_bar" {
13361336
Name: fmt.Sprintf("module: %s - Valid single word", testType),
13371337
Content: `
13381338
module "foo" {
1339-
source = ""
1339+
source = "./module"
13401340
}`,
13411341
Config: config,
13421342
Expected: tflint.Issues{},
@@ -1345,7 +1345,7 @@ module "foo" {
13451345
Name: fmt.Sprintf("module: %s - Valid Mixed_Snake_Case", testType),
13461346
Content: `
13471347
module "Foo_Bar" {
1348-
source = ""
1348+
source = "./module"
13491349
}`,
13501350
Config: config,
13511351
Expected: tflint.Issues{},
@@ -1354,7 +1354,7 @@ module "Foo_Bar" {
13541354
Name: fmt.Sprintf("module: %s - Valid single word with upper characters", testType),
13551355
Content: `
13561356
module "foo" {
1357-
source = ""
1357+
source = "./module"
13581358
}`,
13591359
Config: config,
13601360
Expected: tflint.Issues{},
@@ -1363,7 +1363,7 @@ module "foo" {
13631363
Name: fmt.Sprintf("module: %s - Valid PascalCase", testType),
13641364
Content: `
13651365
module "PascalCase" {
1366-
source = ""
1366+
source = "./module"
13671367
}`,
13681368
Config: config,
13691369
Expected: tflint.Issues{},
@@ -1372,7 +1372,7 @@ module "PascalCase" {
13721372
Name: fmt.Sprintf("module: %s - Valid camelCase", testType),
13731373
Content: `
13741374
module "camelCase" {
1375-
source = ""
1375+
source = "./module"
13761376
}`,
13771377
Config: config,
13781378
Expected: tflint.Issues{},
@@ -1403,7 +1403,7 @@ func testModuleDisabled(t *testing.T, testType string, config string) {
14031403
Name: fmt.Sprintf("module: %s - Valid mixed_snake_case with dash", testType),
14041404
Content: `
14051405
module "dash-name" {
1406-
source = ""
1406+
source = "./module"
14071407
}`,
14081408
Config: config,
14091409
Expected: tflint.Issues{},
@@ -1412,7 +1412,7 @@ module "dash-name" {
14121412
Name: fmt.Sprintf("module: %s - Valid snake_case", testType),
14131413
Content: `
14141414
module "foo_bar" {
1415-
source = ""
1415+
source = "./module"
14161416
}`,
14171417
Config: config,
14181418
Expected: tflint.Issues{},
@@ -1421,7 +1421,7 @@ module "foo_bar" {
14211421
Name: fmt.Sprintf("module: %s - Valid single word", testType),
14221422
Content: `
14231423
module "foo" {
1424-
source = ""
1424+
source = "./module"
14251425
}`,
14261426
Config: config,
14271427
Expected: tflint.Issues{},
@@ -1430,7 +1430,7 @@ module "foo" {
14301430
Name: fmt.Sprintf("module: %s - Valid Mixed_Snake_Case", testType),
14311431
Content: `
14321432
module "Foo_Bar" {
1433-
source = ""
1433+
source = "./module"
14341434
}`,
14351435
Config: config,
14361436
Expected: tflint.Issues{},
@@ -1439,7 +1439,7 @@ module "Foo_Bar" {
14391439
Name: fmt.Sprintf("module: %s - Valid single word upper characters", testType),
14401440
Content: `
14411441
module "Foo" {
1442-
source = ""
1442+
source = "./module"
14431443
}`,
14441444
Config: config,
14451445
Expected: tflint.Issues{},
@@ -1448,7 +1448,7 @@ module "Foo" {
14481448
Name: fmt.Sprintf("module: %s - Valid PascalCase", testType),
14491449
Content: `
14501450
module "PascalCase" {
1451-
source = ""
1451+
source = "./module"
14521452
}`,
14531453
Config: config,
14541454
Expected: tflint.Issues{},
@@ -1457,7 +1457,7 @@ module "PascalCase" {
14571457
Name: fmt.Sprintf("module: %s - Valid camelCase", testType),
14581458
Content: `
14591459
module "camelCase" {
1460-
source = ""
1460+
source = "./module"
14611461
}`,
14621462
Config: config,
14631463
Expected: tflint.Issues{},

rules/terraformrules/terraform_unused_declarations_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ resource "null_resource" "n" {
9393
Content: `
9494
variable "used" {}
9595
module "m" {
96-
source = "."
96+
source = "./module"
9797
u = var.used
9898
}
9999
`,
@@ -104,7 +104,7 @@ module "m" {
104104
Content: `
105105
variable "used" {}
106106
module "m" {
107-
source = "."
107+
source = "./module"
108108
u = var.used
109109
}
110110
`,
@@ -115,7 +115,7 @@ module "m" {
115115
Content: `
116116
locals { used = "used" }
117117
module "m" {
118-
source = "."
118+
source = "./module"
119119
u = local.used
120120
}
121121
`,

rules/terraformrules/terraform_workspace_remote_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ terraform {
9090
backend "remote" {}
9191
}
9292
module "a" {
93-
source = "."
93+
source = "./module"
9494
w = terraform.workspace
9595
}`,
9696
Expected: tflint.Issues{

terraform/addrs/count_attr.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ type CountAttr struct {
1010
func (ca CountAttr) String() string {
1111
return "count." + ca.Name
1212
}
13+
14+
func (ca CountAttr) UniqueKey() UniqueKey {
15+
return ca // A CountAttr is its own UniqueKey
16+
}
17+
18+
func (ca CountAttr) uniqueKeySigil() {}

terraform/addrs/for_each_attr.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ type ForEachAttr struct {
1010
func (f ForEachAttr) String() string {
1111
return "each." + f.Name
1212
}
13+
14+
func (f ForEachAttr) UniqueKey() UniqueKey {
15+
return f // A ForEachAttr is its own UniqueKey
16+
}
17+
18+
func (f ForEachAttr) uniqueKeySigil() {}

terraform/addrs/input_variable.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ func (v InputVariable) String() string {
1414
return "var." + v.Name
1515
}
1616

17+
func (v InputVariable) UniqueKey() UniqueKey {
18+
return v // A InputVariable is its own UniqueKey
19+
}
20+
21+
func (v InputVariable) uniqueKeySigil() {}
22+
1723
// Absolute converts the receiver into an absolute address within the given
1824
// module instance.
1925
func (v InputVariable) Absolute(m ModuleInstance) AbsInputVariableInstance {

0 commit comments

Comments
 (0)