Skip to content

Commit

Permalink
Terraform version detection and v14 (#73)
Browse files Browse the repository at this point in the history
* 'terraform version' parsing switch to support wrapper as in setup-terraform github action

* Repeating v13 tests for v14

* CR: merge 13&14 test suites

Co-authored-by: Roni Frantchi <roni-frantchi@users.noreply.github.com>
  • Loading branch information
shlomimatichin and roni-frantchi authored Dec 20, 2020
1 parent 1126c12 commit 1712989
Show file tree
Hide file tree
Showing 54 changed files with 292 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false
matrix:
terraform_version: [11,12,13]
terraform_version: [11,12,13,14]

steps:
- name: Set up Go 1.x
Expand Down Expand Up @@ -41,7 +41,7 @@ jobs:
echo "$GITHUB_WORKSPACE/.tfenv/bin" >> $GITHUB_PATH
- name: Install terraform
working-directory: test/fixture/terraform_${{ matrix.terraform_version }}
working-directory: test/tfenvconf/terraform_${{ matrix.terraform_version }}
run: |
tfenv install
tfenv use
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Maintaining tags across your application is hard, especially when done manually.

## How?
### Prerequisites
- Terraform 0.11, 0.12 or 0.13
- Terraform 0.11, 0.12, 0.13 or 0.14

### Usage
1. Install from homebrew:
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ require (
github.com/hashicorp/go-hclog v0.9.2
github.com/hashicorp/hcl/v2 v2.8.0
github.com/hashicorp/logutils v1.0.0
github.com/minamijoyo/tfschema v0.5.1-0.20201012140620-226d0de9aed6
github.com/minamijoyo/tfschema v0.6.0
github.com/onsi/gomega v1.10.1
github.com/otiai10/copy v1.2.0
github.com/thoas/go-funk v0.5.0
github.com/zclconf/go-cty v1.5.1
golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect
google.golang.org/protobuf v1.24.0 // indirect
github.com/zclconf/go-cty v1.7.0
)
259 changes: 259 additions & 0 deletions go.sum

Large diffs are not rendered by default.

34 changes: 22 additions & 12 deletions terraform/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,43 @@ package terraform

import (
"encoding/json"
"github.com/bmatcuk/doublestar"
"github.com/env0/terratag/errors"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/thoas/go-funk"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strconv"
"strings"

"github.com/bmatcuk/doublestar"
"github.com/env0/terratag/errors"
"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/thoas/go-funk"
)

func GetTerraformVersion() int {
output, err := exec.Command("terraform", "version").Output()
outputAsString := strings.TrimSpace(string(output))
errors.PanicOnError(err, &outputAsString)

if strings.HasPrefix(outputAsString, "Terraform v0.11") {
return 11
} else if strings.HasPrefix(outputAsString, "Terraform v0.12") {
return 12
} else if strings.HasPrefix(outputAsString, "Terraform v0.13") {
return 13
regularExpression := regexp.MustCompile(`Terraform v0\.(\d+)\.\d+`)
matches := regularExpression.FindStringSubmatch(outputAsString)
if matches == nil {
log.Fatalln("Unable to parse 'terraform version'")
return -1
}
minorVersion, err := strconv.Atoi(matches[1])
if err != nil {
log.Fatalln("Unable to parse ", matches[1], "as integer")
return -1
}
if minorVersion < 11 || minorVersion > 14 {
log.Fatalln("Terratag only supports Terraform 0.11.x, 0.12.x, 0.13.x and 0.14.x - your version says ", outputAsString)
return -1
}

log.Fatalln("Terratag only supports Terraform 0.11.x, 0.12.x and 0.13.x - your version says ", outputAsString)
return -1
return minorVersion
}

func GetResourceType(resource hclwrite.Block) string {
Expand Down
6 changes: 5 additions & 1 deletion terratag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ func TestTerraform12(t *testing.T) {
}

func TestTerraform13(t *testing.T) {
testTerraform(t, "13")
testTerraform(t, "13_14")
}

func TestTerraform14(t *testing.T) {
testTerraform(t, "13_14")
}

func testTerraform(t *testing.T, version string) {
Expand Down
1 change: 1 addition & 0 deletions test/tfenvconf/terraform_14/.terraform-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.14.2

0 comments on commit 1712989

Please sign in to comment.