Skip to content

Commit

Permalink
Fix: terratag inconsistent behaviour with the google-beta provider (#173
Browse files Browse the repository at this point in the history
)

* Fix: terratag inconsistent behaviour with the google-beta provider

* added test and fixed bug

* additional fixes

* fixed 0.12.x issues

* change order to remove warnings

* change order to remove warnings
  • Loading branch information
TomerHeber authored Apr 19, 2023
1 parent 4c0e957 commit 016ab4c
Show file tree
Hide file tree
Showing 27 changed files with 143 additions and 753 deletions.
30 changes: 8 additions & 22 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ jobs:
timeout-minutes: 10
runs-on: ubuntu-20.04
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Go
uses: actions/setup-go@v4
with:
go-version: 1.19
- name: Checkout code
uses: actions/checkout@v3
- name: Go fmt
run: |
! go fmt ./... | read
- name: Go vet
run: |
! go vet ./... | read
- name: Go staticcheck
uses: dominikh/staticcheck-action@v1.2.0
uses: dominikh/staticcheck-action@v1.3.0
with:
version: "2022.1"
version: "2023.1.3"
install-go: false
- name: Go Test
run: SKIP_INTEGRATION_TESTS=1 go test -v ./...
Expand All @@ -39,34 +39,28 @@ jobs:
terraform_version: ['12','13','14','15','latest']

steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.19
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: Get dependencies
run: |
go mod tidy
- name: Install tfenv
run: |
git clone https://github.com/tfutils/tfenv.git $GITHUB_WORKSPACE/.tfenv
echo "$GITHUB_WORKSPACE/.tfenv/bin" >> $GITHUB_PATH
- name: Install terraform
working-directory: test/tfenvconf/terraform_${{ matrix.terraform_version }}
run: |
tfenv install
tfenv use
- name: Print Terraform version
run: |
terraform --version
- name: Set Test-Suite
id: test-suite
env:
Expand All @@ -87,39 +81,31 @@ jobs:
terragrunt-integration-tests:
name: Terragrunt Integration Tests
runs-on: ubuntu-latest

steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.19
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: Get dependencies
run: |
go mod tidy
- name: Install Terraform
uses: hashicorp/setup-terraform@v2
with:
terraform_version: 1.2.5

- name: Print Terraform version
run: |
terraform --version
- name: Install Terragrunt
uses: autero1/[email protected]
with:
terragrunt_version: 0.38.5

- name: Print Terragrunt version
run: |
terragrunt --version
- name: Test
run: |
go test -v -run ^TestTerragruntWithCache$
10 changes: 2 additions & 8 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,33 @@ jobs:
runs-on: ubuntu-latest
needs: goreleaser
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3
- name: Set up Go 1.19
uses: actions/setup-go@v4
with:
go-version: 1.19
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v3

- name: Get dependencies
run: |
go get -v -t -d ./...
- name: Build project
run: |
mkdir out
CGO_ENABLED=0 go build -ldflags "-X main.version=${{github.ref_name}}" -a -installsuffix cgo -o ./out/terratag/terratag ./cmd/terratag
# Setup .npmrc file to publish to GitHub Packages
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '12.x'
registry-url: 'https://registry.npmjs.org'
scope: '@env0'

- run: |
npm init --yes --scope @env0
ref=${{ github.ref }}
tag=\"${ref#"refs/tags/v"}\"
echo "`jq .version="${tag}" package.json`" > package.json
working-directory: ./out/terratag
- run: npm publish --access public ./out/terratag
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
38 changes: 34 additions & 4 deletions internal/tfschema/tfschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/env0/terratag/internal/providers"
"github.com/env0/terratag/internal/tagging"
"github.com/env0/terratag/internal/terraform"
"github.com/thoas/go-funk"

"github.com/hashicorp/hcl/v2/hclwrite"
"github.com/zclconf/go-cty/cty"
Expand All @@ -27,7 +28,7 @@ var ErrResourceTypeNotFound = errors.New("resource type not found")
var providerSchemasMap map[string]*ProviderSchemas = map[string]*ProviderSchemas{}
var providerSchemasMapLock sync.Mutex

//var customSupportedProviderNames = [...]string{"google-beta"}
var customSupportedProviderNames = [...]string{"google-beta"}

type Attribute struct {
Type cty.Type `json:"type"`
Expand Down Expand Up @@ -58,7 +59,7 @@ func IsTaggable(dir string, iacType common.IACType, resource hclwrite.Block) (bo
resourceType := terraform.GetResourceType(resource)

if providers.IsSupportedResource(resourceType) {
resourceSchema, err := getResourceSchema(resourceType, dir, iacType)
resourceSchema, err := getResourceSchema(resourceType, resource, dir, iacType)
if err != nil {
if err == ErrResourceTypeNotFound {
log.Print("[WARN] Skipped ", resourceType, " as it is not YET supported")
Expand Down Expand Up @@ -109,7 +110,29 @@ func getTerragruntPluginPath(dir string) string {
return ret
}

func getResourceSchema(resourceType string, dir string, iacType common.IACType) (*ResourceSchema, error) {
func extractProviderNameFromResourceType(resourceType string) (string, error) {
s := strings.SplitN(resourceType, "_", 2)
if len(s) < 2 {
return "", fmt.Errorf("failed to detect a provider name: %s", resourceType)
}
return s[0], nil
}

func detectProviderName(resource hclwrite.Block) (string, error) {
providerAttribute := resource.Body().GetAttribute("provider")

if providerAttribute != nil {
providerTokens := providerAttribute.Expr().BuildTokens(hclwrite.Tokens{})
providerName := strings.Trim(string(providerTokens.Bytes()), "\" ")
if funk.Contains(customSupportedProviderNames, providerName) {
return providerName, nil
}
}

return extractProviderNameFromResourceType(terraform.GetResourceType(resource))
}

func getResourceSchema(resourceType string, resource hclwrite.Block, dir string, iacType common.IACType) (*ResourceSchema, error) {
if iacType == common.Terragrunt {
// which mode of terragrunt it is (with or without cache folder).
if _, err := os.Stat(dir + "/.terragrunt-cache"); err == nil {
Expand Down Expand Up @@ -150,7 +173,14 @@ func getResourceSchema(resourceType string, dir string, iacType common.IACType)
providerSchemasMap[dir] = providerSchemas
}

for _, providerSchema := range providerSchemas.ProviderSchemas {
detectedProviderName, _ := detectProviderName(resource)
// Search through all providers.
for providerName, providerSchema := range providerSchemas.ProviderSchemas {
if len(detectedProviderName) > 0 && providerName != detectedProviderName && !strings.HasSuffix(providerName, "/"+detectedProviderName) {
// Not the correct provider (based on name). Skip.
continue
}

resourceSchema, ok := providerSchema.ResourceSchemas[resourceType]
if ok {
return resourceSchema, nil
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/terraform_12/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ suites:
- git_issue_66_aws_aliased_provider
- git_issue_88__corrupt_hcl_file
- google_beta_resource_skip

- google_vs_google_beta
3 changes: 2 additions & 1 deletion test/fixture/terraform_13_14/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ suites:
- git_issue_88__corrupt_hcl_file
- google_beta_resource_skip
- google_container_cluster
- google_labels_map
- google_labels_map
- google_vs_google_beta
3 changes: 2 additions & 1 deletion test/fixture/terraform_15/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ suites:
- git_issue_88__corrupt_hcl_file
- google_beta_resource_skip
- google_container_cluster
- google_labels_map
- google_labels_map
- google_vs_google_beta
3 changes: 2 additions & 1 deletion test/fixture/terraform_latest/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ suites:
- git_issue_88__corrupt_hcl_file
- google_beta_resource_skip
- google_container_cluster
- google_labels_map
- google_labels_map
- google_vs_google_beta
31 changes: 0 additions & 31 deletions test/tests/aws_tags_map_11/expected/main.terratag.tf

This file was deleted.

39 changes: 0 additions & 39 deletions test/tests/aws_tags_map_11/expected/main.tf.bak

This file was deleted.

39 changes: 0 additions & 39 deletions test/tests/aws_tags_map_11/input/main.tf

This file was deleted.

Loading

0 comments on commit 016ab4c

Please sign in to comment.