Skip to content

Commit

Permalink
Switched to use collection of clients rather restarting client each t…
Browse files Browse the repository at this point in the history
…ime tf providers
  • Loading branch information
juliosueiras committed Feb 17, 2020
1 parent 827f992 commit b503cab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
3 changes: 3 additions & 0 deletions tfstructs/clients.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package tfstructs

var Clients = make(map[string]*Client)
51 changes: 31 additions & 20 deletions tfstructs/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func NewProvisionerClient(name string, targetDir string) (*Client, error) {
if err != nil {
return nil, fmt.Errorf("Failed to initialize plugin: %s", err)
}

// create a new resource provisioner.
raw, err := rpcClient.Dispense(plugin.ProvisionerPluginName)
if err != nil {
Expand All @@ -101,26 +102,36 @@ func NewClient(providerName string, targetDir string) (*Client, error) {
if err != nil {
return nil, err
}

// initialize a plugin Client.
pluginClient := plugin.Client(*pluginMeta)
rpcClient, err := pluginClient.Client()
if err != nil {
return nil, fmt.Errorf("Failed to initialize plugin: %s", err)
}

// create a new resource provider.
raw, err := rpcClient.Dispense(plugin.ProviderPluginName)
if err != nil {
return nil, fmt.Errorf("Failed to dispense plugin: %s", err)
}

provider := raw.(*plugin.GRPCProvider)

return &Client{
provider: provider,
pluginClient: pluginClient,
}, nil

clientName := fmt.Sprintf("%s_%s", pluginMeta.Name, pluginMeta.Version)
// initialize a plugin Client.
var finalClient *Client
if Clients[clientName] == nil {
pluginClient := plugin.Client(*pluginMeta)
rpcClient, err := pluginClient.Client()
if err != nil {
return nil, fmt.Errorf("Failed to initialize plugin: %s", err)
}


// create a new resource provider.
raw, err := rpcClient.Dispense(plugin.ProviderPluginName)
if err != nil {
return nil, fmt.Errorf("Failed to dispense plugin: %s", err)
}

provider := raw.(*plugin.GRPCProvider)
finalClient = &Client{
provider: provider,
pluginClient: pluginClient,
}

Clients[clientName] = finalClient
} else {
finalClient = Clients[clientName]
}

return finalClient, nil
}

// findPlugin finds a plugin with the name specified in the arguments.
Expand Down

0 comments on commit b503cab

Please # to comment.