diff --git a/test/fixture/terraform_13/git_issue_66_aws_aliased_provider/expected/main.terratag.tf b/test/fixture/terraform_13/git_issue_66_aws_aliased_provider/expected/main.terratag.tf new file mode 100644 index 0000000..e25863f --- /dev/null +++ b/test/fixture/terraform_13/git_issue_66_aws_aliased_provider/expected/main.terratag.tf @@ -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"} +} + diff --git a/test/fixture/terraform_13/git_issue_66_aws_aliased_provider/expected/main.tf.bak b/test/fixture/terraform_13/git_issue_66_aws_aliased_provider/expected/main.tf.bak new file mode 100644 index 0000000..c64b9e9 --- /dev/null +++ b/test/fixture/terraform_13/git_issue_66_aws_aliased_provider/expected/main.tf.bak @@ -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" +} \ No newline at end of file diff --git a/test/fixture/terraform_13/git_issue_66_aws_aliased_provider/input/main.tf b/test/fixture/terraform_13/git_issue_66_aws_aliased_provider/input/main.tf new file mode 100644 index 0000000..c64b9e9 --- /dev/null +++ b/test/fixture/terraform_13/git_issue_66_aws_aliased_provider/input/main.tf @@ -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" +} \ No newline at end of file diff --git a/tfschema/tfschema.go b/tfschema/tfschema.go index b5c3277..85ad833 100644 --- a/tfschema/tfschema.go +++ b/tfschema/tfschema.go @@ -2,6 +2,7 @@ package tfschema import ( "fmt" + "github.com/thoas/go-funk" "log" "strings" @@ -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) @@ -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 {