rpc: Make a Register function to make working with easier
This commit is contained in:
parent
f8dc3286d9
commit
5ef20cf3a2
|
@ -8,11 +8,12 @@ import (
|
||||||
|
|
||||||
type ResourceProvider struct {
|
type ResourceProvider struct {
|
||||||
Client *rpc.Client
|
Client *rpc.Client
|
||||||
|
Name string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ResourceProvider) Configure(c map[string]interface{}) ([]string, error) {
|
func (p *ResourceProvider) Configure(c map[string]interface{}) ([]string, error) {
|
||||||
var resp ResourceProviderConfigureResponse
|
var resp ResourceProviderConfigureResponse
|
||||||
err := p.Client.Call("ResourceProvider.Configure", c, &resp)
|
err := p.Client.Call(p.Name+".Configure", c, &resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,11 +10,11 @@ import (
|
||||||
func TestResourceProvider_configure(t *testing.T) {
|
func TestResourceProvider_configure(t *testing.T) {
|
||||||
p := new(terraform.MockResourceProvider)
|
p := new(terraform.MockResourceProvider)
|
||||||
client, server := testClientServer(t)
|
client, server := testClientServer(t)
|
||||||
server.RegisterName("ResourceProvider", &ResourceProviderServer{
|
name, err := Register(server, p)
|
||||||
Provider: p,
|
if err != nil {
|
||||||
})
|
t.Fatalf("err: %s", err)
|
||||||
|
}
|
||||||
provider := &ResourceProvider{Client: client}
|
provider := &ResourceProvider{Client: client, Name: name}
|
||||||
|
|
||||||
// Configure
|
// Configure
|
||||||
config := map[string]interface{}{"foo": "bar"}
|
config := map[string]interface{}{"foo": "bar"}
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
package rpc
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/rpc"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/terraform"
|
||||||
|
)
|
||||||
|
|
||||||
|
// nextId is the next ID to use for names registered.
|
||||||
|
var nextId uint32 = 0
|
||||||
|
var nextLock sync.Mutex
|
||||||
|
|
||||||
|
// Register registers a Terraform thing with the RPC server and returns
|
||||||
|
// the name it is registered under.
|
||||||
|
func Register(server *rpc.Server, thing interface{}) (name string, err error) {
|
||||||
|
nextLock.Lock()
|
||||||
|
defer nextLock.Unlock()
|
||||||
|
|
||||||
|
switch t := thing.(type) {
|
||||||
|
case terraform.ResourceProvider:
|
||||||
|
name = fmt.Sprintf("Terraform%d", nextId)
|
||||||
|
err = server.RegisterName(name, &ResourceProviderServer{Provider: t})
|
||||||
|
default:
|
||||||
|
return "", errors.New("Unknown type to register for RPC server.")
|
||||||
|
}
|
||||||
|
|
||||||
|
nextId += 1
|
||||||
|
return
|
||||||
|
}
|
Loading…
Reference in New Issue