Skip to content

Commit

Permalink
✨ : add terraform blocs support
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Apr 21, 2021
1 parent 346497f commit d478ebb
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/main/antlr4/io/gaia_app/hcl/antlr/hcl.g4
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ file

directive
: providerDirective
| terraformDirective
| resourceDirective
| variableDirective
| outputDirective
Expand All @@ -15,6 +16,10 @@ providerDirective
: 'provider' STRING object
;

terraformDirective
: 'terraform' object
;

resourceDirective
: 'resource' STRING STRING object
;
Expand Down Expand Up @@ -64,7 +69,10 @@ object
| '{' field+ '}'
;

field: IDENTIFIER '=' expression;
field
: IDENTIFIER '=' expression
| IDENTIFIER object // for dynamic blocks
;

variableDescription
: 'description' '=' STRING
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/io/gaia_app/hcl/HclVisitor.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package io.gaia_app.hcl

import io.gaia_app.hcl.antlr.hclParser
import io.gaia_app.modules.bo.Output
import io.gaia_app.modules.bo.Variable
import java.util.*
import kotlin.NoSuchElementException

class HclVisitor : io.gaia_app.hcl.antlr.hclBaseVisitor<Unit>() {

Expand Down Expand Up @@ -72,4 +74,19 @@ class HclVisitor : io.gaia_app.hcl.antlr.hclBaseVisitor<Unit>() {
provider = resourceProvider
}
}

override fun visitTerraformDirective(ctx: hclParser.TerraformDirectiveContext) {
try {
// find required providers field
val requiredProviders = ctx.`object`().field()
.first { it.IDENTIFIER().text == "required_providers" }
// find first required provider that should not be ignored
val firstValidProvider = requiredProviders.`object`().field()
.first { ! IGNORED_PROVIDERS.contains(it.IDENTIFIER().text) }
provider = firstValidProvider.IDENTIFIER().text
}
catch (_: NoSuchElementException){
// NoSuchElementException is raised when no provider is found in the terraform directive
}
}
}
60 changes: 59 additions & 1 deletion src/test/java/io/gaia_app/hcl/HCLParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class HCLParserTest {

@Test
@Throws(IOException::class)
fun parsing_provider_shouldIgnoreDummyProviders() {
fun parsing_provider_fromResources_shouldIgnoreDummyProviders() {
// given
val fileContent = """
resource "random_id" "id" {
Expand All @@ -149,6 +149,64 @@ class HCLParserTest {
assertThat(provider).isEqualTo("google")
}

@Test
@Throws(IOException::class)
fun parsing_provider_shouldAnalyseRequiredProvidersBlock() {
// given
val fileContent = """
terraform {
required_version = ">= 0.14"
required_providers {
random = ">= 3.1.0"
google = ">= 3.64.0"
}
}
""".trimIndent()

// when
val provider: String = hclParser.parseProvider(fileContent)

// then
assertThat(provider).isEqualTo("google")
}

@Test
@Throws(IOException::class)
fun parsing_provider_shouldAnalyseRequiredProvidersBlock_withoutRelevantProvider() {
// given
val fileContent = """
terraform {
required_version = ">= 0.14"
required_providers {
random = ">= 3.1.0"
}
}
""".trimIndent()

// when
val provider: String = hclParser.parseProvider(fileContent)

// then
assertThat(provider).isEqualTo("unknown")
}

@Test
@Throws(IOException::class)
fun parsing_provider_shouldAnalyseRequiredProvidersBlock_withoutRequiredProviders() {
// given
val fileContent = """
terraform {
required_version = ">= 0.14"
}
""".trimIndent()

// when
val provider: String = hclParser.parseProvider(fileContent)

// then
assertThat(provider).isEqualTo("unknown")
}

@Test
fun dummyProvidersShouldBeIgnored() {
assertThat(hclParser.parseProvider("""provider "null" {} """)).isEqualTo("unknown")
Expand Down

0 comments on commit d478ebb

Please sign in to comment.