forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
35 lines (29 loc) · 880 Bytes
/
rpc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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})
case terraform.ResourceProvisioner:
name = fmt.Sprintf("Terraform%d", nextId)
err = server.RegisterName(name, &ResourceProvisionerServer{Provisioner: t})
default:
return "", errors.New("Unknown type to register for RPC server.")
}
nextId += 1
return
}