github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/plugin/serve.go (about)

     1  package plugin
     2  
     3  import (
     4  	"github.com/hashicorp/go-plugin"
     5  	"github.com/hashicorp/terraform/terraform"
     6  )
     7  
     8  // The constants below are the names of the plugins that can be dispensed
     9  // from the plugin server.
    10  const (
    11  	ProviderPluginName    = "provider"
    12  	ProvisionerPluginName = "provisioner"
    13  )
    14  
    15  // Handshake is the HandshakeConfig used to configure clients and servers.
    16  var Handshake = plugin.HandshakeConfig{
    17  	ProtocolVersion:  1,
    18  	MagicCookieKey:   "TF_PLUGIN_MAGIC_COOKIE",
    19  	MagicCookieValue: "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d6991ca9872b2",
    20  }
    21  
    22  type ProviderFunc func() terraform.ResourceProvider
    23  type ProvisionerFunc func() terraform.ResourceProvisioner
    24  
    25  // ServeOpts are the configurations to serve a plugin.
    26  type ServeOpts struct {
    27  	ProviderFunc    ProviderFunc
    28  	ProvisionerFunc ProvisionerFunc
    29  }
    30  
    31  // Serve serves a plugin. This function never returns and should be the final
    32  // function called in the main function of the plugin.
    33  func Serve(opts *ServeOpts) {
    34  	plugin.Serve(&plugin.ServeConfig{
    35  		HandshakeConfig: Handshake,
    36  		Plugins:         pluginMap(opts),
    37  	})
    38  }
    39  
    40  // pluginMap returns the map[string]plugin.Plugin to use for configuring a plugin
    41  // server or client.
    42  func pluginMap(opts *ServeOpts) map[string]plugin.Plugin {
    43  	return map[string]plugin.Plugin{
    44  		"provider":    &ResourceProviderPlugin{F: opts.ProviderFunc},
    45  		"provisioner": &ResourceProvisionerPlugin{F: opts.ProvisionerFunc},
    46  	}
    47  }