Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: support aliased provider names #67

Merged
merged 2 commits into from
Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
provider "aws" {
region = "us-east-1"
alias = "custom"
version = "~> 3.0"
}

resource "aws_s3_bucket" "bucket" {
provider = aws.custom

bucket = "my-tf-test-bucket"
acl = "private"
tags = local.terratag_added_main
}
locals {
terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
provider "aws" {
region = "us-east-1"
alias = "custom"
version = "~> 3.0"
}

resource "aws_s3_bucket" "bucket" {
provider = aws.custom

bucket = "my-tf-test-bucket"
acl = "private"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
provider "aws" {
region = "us-east-1"
alias = "custom"
version = "~> 3.0"
}

resource "aws_s3_bucket" "bucket" {
provider = aws.custom

bucket = "my-tf-test-bucket"
acl = "private"
}
26 changes: 17 additions & 9 deletions tfschema/tfschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tfschema

import (
"fmt"
"github.com/thoas/go-funk"
"log"
"strings"

Expand All @@ -16,6 +17,8 @@ import (

var providerToClientMap = map[string]tfschema.Client{}

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

func IsTaggable(dir string, resource hclwrite.Block) bool {
var isTaggable bool
resourceType := terraform.GetResourceType(resource)
Expand Down Expand Up @@ -54,21 +57,26 @@ type TfSchemaAttribute struct {
Type string
}

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 {
resourceType := resource.Labels()[0]
s := strings.SplitN(resourceType, "_", 2)
if len(s) < 2 {
return "", fmt.Errorf("Failed to detect a provider name: %s", resourceType)
}
return s[0], nil
} else {
if providerAttribute != nil {
providerTokens := providerAttribute.Expr().BuildTokens(hclwrite.Tokens{})
providerName := strings.Trim(string(providerTokens.Bytes()), "\" ")
return providerName, nil
if funk.Contains(customSupportedProviderNames, providerName) {
return providerName, nil
}
}

return extractProviderNameFromResourceType(terraform.GetResourceType(resource))
}

func getClient(providerName string, dir string) tfschema.Client {
Expand Down