github.com/subuk/terraform@v0.6.14-0.20160317140351-de1567c2e732/rpc/rpc.go (about)

     1  package rpc
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"net/rpc"
     7  	"sync"
     8  
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  // nextId is the next ID to use for names registered.
    13  var nextId uint32 = 0
    14  var nextLock sync.Mutex
    15  
    16  // Register registers a Terraform thing with the RPC server and returns
    17  // the name it is registered under.
    18  func Register(server *rpc.Server, thing interface{}) (name string, err error) {
    19  	nextLock.Lock()
    20  	defer nextLock.Unlock()
    21  
    22  	switch t := thing.(type) {
    23  	case terraform.ResourceProvider:
    24  		name = fmt.Sprintf("Terraform%d", nextId)
    25  		err = server.RegisterName(name, &ResourceProviderServer{Provider: t})
    26  	case terraform.ResourceProvisioner:
    27  		name = fmt.Sprintf("Terraform%d", nextId)
    28  		err = server.RegisterName(name, &ResourceProvisionerServer{Provisioner: t})
    29  	default:
    30  		return "", errors.New("Unknown type to register for RPC server.")
    31  	}
    32  
    33  	nextId += 1
    34  	return
    35  }