Skip to content

Commit

Permalink
Merge pull request #58 from env0/chore-stop-over-instantiating-client
Browse files Browse the repository at this point in the history
Chore: Only create a new tfschema client for new providers
  • Loading branch information
RLRabinowitz authored Oct 7, 2020
2 parents a8a6a32 + 98b3085 commit 606fc94
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions tfschema/tfschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@ import (
"strings"
)

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

func IsTaggable(dir string, resource hclwrite.Block) bool {
var isTaggable bool
resourceType := terraform.GetResourceType(resource)

if providers.IsSupportedResource(resourceType) {
providerName, _ := detectProviderName(resourceType)

client, err := tfschema.NewClient(providerName, tfschema.Option{RootDir: dir})
errors.PanicOnError(err, nil)

client := getClient(providerName, dir)
typeSchema, err := client.GetResourceTypeSchema(resourceType)
if err != nil {
if strings.Contains(err.Error(), "Failed to find resource type") {
Expand Down Expand Up @@ -62,3 +61,16 @@ func detectProviderName(name string) (string, error) {
}
return s[0], nil
}

func getClient(providerName string, dir string) tfschema.Client {
client, exists := providerToClientMap[providerName]
if exists {
return client
} else {
newClient, err := tfschema.NewClient(providerName, tfschema.Option{RootDir: dir})
errors.PanicOnError(err, nil)

providerToClientMap[providerName] = newClient
return newClient
}
}

0 comments on commit 606fc94

Please sign in to comment.