github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/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  	// The ProtocolVersion is the version that must match between TF core
    18  	// and TF plugins. This should be bumped whenever a change happens in
    19  	// one or the other that makes it so that they can't safely communicate.
    20  	// This could be adding a new interface value, it could be how
    21  	// helper/schema computes diffs, etc.
    22  	ProtocolVersion: 4,
    23  
    24  	// The magic cookie values should NEVER be changed.
    25  	MagicCookieKey:   "TF_PLUGIN_MAGIC_COOKIE",
    26  	MagicCookieValue: "d602bf8f470bc67ca7faa0386276bbdd4330efaf76d1a219cb4d6991ca9872b2",
    27  }
    28  
    29  type ProviderFunc func() terraform.ResourceProvider
    30  type ProvisionerFunc func() terraform.ResourceProvisioner
    31  
    32  // ServeOpts are the configurations to serve a plugin.
    33  type ServeOpts struct {
    34  	ProviderFunc    ProviderFunc
    35  	ProvisionerFunc ProvisionerFunc
    36  }
    37  
    38  // Serve serves a plugin. This function never returns and should be the final
    39  // function called in the main function of the plugin.
    40  func Serve(opts *ServeOpts) {
    41  	plugin.Serve(&plugin.ServeConfig{
    42  		HandshakeConfig: Handshake,
    43  		Plugins:         pluginMap(opts),
    44  	})
    45  }
    46  
    47  // pluginMap returns the map[string]plugin.Plugin to use for configuring a plugin
    48  // server or client.
    49  func pluginMap(opts *ServeOpts) map[string]plugin.Plugin {
    50  	return map[string]plugin.Plugin{
    51  		"provider":    &ResourceProviderPlugin{F: opts.ProviderFunc},
    52  		"provisioner": &ResourceProvisionerPlugin{F: opts.ProvisionerFunc},
    53  	}
    54  }