2016-01-25 03:10:52 +01:00
|
|
|
package plugin
|
2014-05-29 00:07:47 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/rpc"
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
"github.com/hashicorp/go-plugin"
|
2014-05-29 00:07:47 +02:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
// ResourceProviderPlugin is the plugin.Plugin implementation.
|
|
|
|
type ResourceProviderPlugin struct {
|
|
|
|
F func() terraform.ResourceProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProviderPlugin) Server(b *plugin.MuxBroker) (interface{}, error) {
|
|
|
|
return &ResourceProviderServer{Broker: b, Provider: p.F()}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ResourceProviderPlugin) Client(
|
|
|
|
b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
|
|
|
|
return &ResourceProvider{Broker: b, Client: c}, nil
|
|
|
|
}
|
|
|
|
|
2014-05-29 06:27:10 +02:00
|
|
|
// ResourceProvider is an implementation of terraform.ResourceProvider
|
|
|
|
// that communicates over RPC.
|
2014-05-29 00:07:47 +02:00
|
|
|
type ResourceProvider struct {
|
2016-01-25 03:10:52 +01:00
|
|
|
Broker *plugin.MuxBroker
|
2014-05-29 00:07:47 +02:00
|
|
|
Client *rpc.Client
|
|
|
|
}
|
|
|
|
|
2014-09-29 09:23:17 +02:00
|
|
|
func (p *ResourceProvider) Input(
|
|
|
|
input terraform.UIInput,
|
|
|
|
c *terraform.ResourceConfig) (*terraform.ResourceConfig, error) {
|
|
|
|
id := p.Broker.NextId()
|
2016-01-25 03:10:52 +01:00
|
|
|
go p.Broker.AcceptAndServe(id, &UIInputServer{
|
2014-09-29 09:23:17 +02:00
|
|
|
UIInput: input,
|
|
|
|
})
|
|
|
|
|
|
|
|
var resp ResourceProviderInputResponse
|
|
|
|
args := ResourceProviderInputArgs{
|
|
|
|
InputId: id,
|
|
|
|
Config: c,
|
|
|
|
}
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
err := p.Client.Call("Plugin.Input", &args, &resp)
|
2014-09-29 09:23:17 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Config, nil
|
|
|
|
}
|
|
|
|
|
2014-06-13 07:37:46 +02:00
|
|
|
func (p *ResourceProvider) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
|
|
|
var resp ResourceProviderValidateResponse
|
2014-06-13 08:22:28 +02:00
|
|
|
args := ResourceProviderValidateArgs{
|
|
|
|
Config: c,
|
|
|
|
}
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
err := p.Client.Call("Plugin.Validate", &args, &resp)
|
2014-06-13 07:37:46 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
if len(resp.Errors) > 0 {
|
|
|
|
errs = make([]error, len(resp.Errors))
|
|
|
|
for i, err := range resp.Errors {
|
|
|
|
errs[i] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Warnings, errs
|
|
|
|
}
|
|
|
|
|
2014-07-03 20:51:38 +02:00
|
|
|
func (p *ResourceProvider) ValidateResource(
|
|
|
|
t string, c *terraform.ResourceConfig) ([]string, []error) {
|
|
|
|
var resp ResourceProviderValidateResourceResponse
|
|
|
|
args := ResourceProviderValidateResourceArgs{
|
|
|
|
Config: c,
|
|
|
|
Type: t,
|
|
|
|
}
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
err := p.Client.Call("Plugin.ValidateResource", &args, &resp)
|
2014-07-03 20:51:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, []error{err}
|
|
|
|
}
|
|
|
|
|
|
|
|
var errs []error
|
|
|
|
if len(resp.Errors) > 0 {
|
|
|
|
errs = make([]error, len(resp.Errors))
|
|
|
|
for i, err := range resp.Errors {
|
|
|
|
errs[i] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Warnings, errs
|
|
|
|
}
|
|
|
|
|
2014-06-13 03:06:26 +02:00
|
|
|
func (p *ResourceProvider) Configure(c *terraform.ResourceConfig) error {
|
2014-05-29 00:07:47 +02:00
|
|
|
var resp ResourceProviderConfigureResponse
|
2016-01-25 03:10:52 +01:00
|
|
|
err := p.Client.Call("Plugin.Configure", c, &resp)
|
2014-06-06 09:48:32 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-06-19 01:49:34 +02:00
|
|
|
func (p *ResourceProvider) Apply(
|
2014-09-17 01:48:33 +02:00
|
|
|
info *terraform.InstanceInfo,
|
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
d *terraform.InstanceDiff) (*terraform.InstanceState, error) {
|
2014-06-19 01:49:34 +02:00
|
|
|
var resp ResourceProviderApplyResponse
|
|
|
|
args := &ResourceProviderApplyArgs{
|
2014-09-17 01:48:33 +02:00
|
|
|
Info: info,
|
2014-06-19 01:49:34 +02:00
|
|
|
State: s,
|
|
|
|
Diff: d,
|
|
|
|
}
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
err := p.Client.Call("Plugin.Apply", args, &resp)
|
2014-06-19 01:49:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.State, err
|
|
|
|
}
|
|
|
|
|
2014-06-06 09:48:32 +02:00
|
|
|
func (p *ResourceProvider) Diff(
|
2014-09-17 01:48:33 +02:00
|
|
|
info *terraform.InstanceInfo,
|
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
|
2014-06-06 09:48:32 +02:00
|
|
|
var resp ResourceProviderDiffResponse
|
|
|
|
args := &ResourceProviderDiffArgs{
|
2014-09-17 01:48:33 +02:00
|
|
|
Info: info,
|
2014-06-06 09:48:32 +02:00
|
|
|
State: s,
|
|
|
|
Config: c,
|
|
|
|
}
|
2016-01-25 03:10:52 +01:00
|
|
|
err := p.Client.Call("Plugin.Diff", args, &resp)
|
2014-05-29 00:07:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-29 00:31:09 +02:00
|
|
|
if resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
}
|
2014-05-29 00:07:47 +02:00
|
|
|
|
2014-06-06 09:48:32 +02:00
|
|
|
return resp.Diff, err
|
2014-05-29 00:07:47 +02:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:51:07 +02:00
|
|
|
func (p *ResourceProvider) Refresh(
|
2014-09-17 01:48:33 +02:00
|
|
|
info *terraform.InstanceInfo,
|
|
|
|
s *terraform.InstanceState) (*terraform.InstanceState, error) {
|
2014-06-20 20:51:07 +02:00
|
|
|
var resp ResourceProviderRefreshResponse
|
2014-09-17 01:48:33 +02:00
|
|
|
args := &ResourceProviderRefreshArgs{
|
|
|
|
Info: info,
|
|
|
|
State: s,
|
|
|
|
}
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
err := p.Client.Call("Plugin.Refresh", args, &resp)
|
2014-06-20 20:51:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.State, err
|
|
|
|
}
|
|
|
|
|
2016-05-04 19:58:49 +02:00
|
|
|
func (p *ResourceProvider) ImportState(
|
2016-05-04 21:43:10 +02:00
|
|
|
info *terraform.InstanceInfo,
|
|
|
|
id string) ([]*terraform.InstanceState, error) {
|
2016-05-04 19:58:49 +02:00
|
|
|
var resp ResourceProviderImportStateResponse
|
|
|
|
args := &ResourceProviderImportStateArgs{
|
|
|
|
Info: info,
|
2016-05-04 21:43:10 +02:00
|
|
|
Id: id,
|
2016-05-04 19:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err := p.Client.Call("Plugin.ImportState", args, &resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.State, err
|
|
|
|
}
|
|
|
|
|
2014-06-03 23:26:31 +02:00
|
|
|
func (p *ResourceProvider) Resources() []terraform.ResourceType {
|
|
|
|
var result []terraform.ResourceType
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
err := p.Client.Call("Plugin.Resources", new(interface{}), &result)
|
2014-06-03 23:26:31 +02:00
|
|
|
if err != nil {
|
|
|
|
// TODO: panic, log, what?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2015-06-19 21:52:50 +02:00
|
|
|
func (p *ResourceProvider) Close() error {
|
|
|
|
return p.Client.Close()
|
|
|
|
}
|
|
|
|
|
2014-05-29 06:27:10 +02:00
|
|
|
// ResourceProviderServer is a net/rpc compatible structure for serving
|
|
|
|
// a ResourceProvider. This should not be used directly.
|
2014-05-29 00:07:47 +02:00
|
|
|
type ResourceProviderServer struct {
|
2016-01-25 03:10:52 +01:00
|
|
|
Broker *plugin.MuxBroker
|
2014-05-29 00:07:47 +02:00
|
|
|
Provider terraform.ResourceProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceProviderConfigureResponse struct {
|
2016-01-25 03:10:52 +01:00
|
|
|
Error *plugin.BasicError
|
2014-06-06 09:48:32 +02:00
|
|
|
}
|
|
|
|
|
2014-09-29 09:23:17 +02:00
|
|
|
type ResourceProviderInputArgs struct {
|
|
|
|
InputId uint32
|
|
|
|
Config *terraform.ResourceConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceProviderInputResponse struct {
|
|
|
|
Config *terraform.ResourceConfig
|
2016-01-25 03:10:52 +01:00
|
|
|
Error *plugin.BasicError
|
2014-09-29 09:23:17 +02:00
|
|
|
}
|
|
|
|
|
2014-06-19 01:49:34 +02:00
|
|
|
type ResourceProviderApplyArgs struct {
|
2014-09-18 01:33:24 +02:00
|
|
|
Info *terraform.InstanceInfo
|
2014-09-17 01:48:33 +02:00
|
|
|
State *terraform.InstanceState
|
2014-09-18 01:33:24 +02:00
|
|
|
Diff *terraform.InstanceDiff
|
2014-06-19 01:49:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceProviderApplyResponse struct {
|
2014-09-17 01:48:33 +02:00
|
|
|
State *terraform.InstanceState
|
2016-01-25 03:10:52 +01:00
|
|
|
Error *plugin.BasicError
|
2014-06-19 01:49:34 +02:00
|
|
|
}
|
|
|
|
|
2014-06-06 09:48:32 +02:00
|
|
|
type ResourceProviderDiffArgs struct {
|
2014-09-18 01:33:24 +02:00
|
|
|
Info *terraform.InstanceInfo
|
2014-09-17 01:48:33 +02:00
|
|
|
State *terraform.InstanceState
|
2014-06-13 03:06:26 +02:00
|
|
|
Config *terraform.ResourceConfig
|
2014-06-06 09:48:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceProviderDiffResponse struct {
|
2014-09-18 01:33:24 +02:00
|
|
|
Diff *terraform.InstanceDiff
|
2016-01-25 03:10:52 +01:00
|
|
|
Error *plugin.BasicError
|
2014-05-29 00:07:47 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 01:48:33 +02:00
|
|
|
type ResourceProviderRefreshArgs struct {
|
2014-09-18 01:33:24 +02:00
|
|
|
Info *terraform.InstanceInfo
|
|
|
|
State *terraform.InstanceState
|
2014-09-17 01:48:33 +02:00
|
|
|
}
|
|
|
|
|
2014-06-20 20:51:07 +02:00
|
|
|
type ResourceProviderRefreshResponse struct {
|
2014-09-17 01:48:33 +02:00
|
|
|
State *terraform.InstanceState
|
2016-01-25 03:10:52 +01:00
|
|
|
Error *plugin.BasicError
|
2014-06-20 20:51:07 +02:00
|
|
|
}
|
|
|
|
|
2016-05-04 19:58:49 +02:00
|
|
|
type ResourceProviderImportStateArgs struct {
|
|
|
|
Info *terraform.InstanceInfo
|
2016-05-04 21:43:10 +02:00
|
|
|
Id string
|
2016-05-04 19:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceProviderImportStateResponse struct {
|
|
|
|
State []*terraform.InstanceState
|
|
|
|
Error *plugin.BasicError
|
|
|
|
}
|
|
|
|
|
2014-06-13 08:22:28 +02:00
|
|
|
type ResourceProviderValidateArgs struct {
|
|
|
|
Config *terraform.ResourceConfig
|
|
|
|
}
|
|
|
|
|
2014-06-13 07:37:46 +02:00
|
|
|
type ResourceProviderValidateResponse struct {
|
|
|
|
Warnings []string
|
2016-01-25 03:10:52 +01:00
|
|
|
Errors []*plugin.BasicError
|
2014-06-13 07:37:46 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 20:51:38 +02:00
|
|
|
type ResourceProviderValidateResourceArgs struct {
|
|
|
|
Config *terraform.ResourceConfig
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceProviderValidateResourceResponse struct {
|
|
|
|
Warnings []string
|
2016-01-25 03:10:52 +01:00
|
|
|
Errors []*plugin.BasicError
|
2014-07-03 20:51:38 +02:00
|
|
|
}
|
|
|
|
|
2014-09-29 09:23:17 +02:00
|
|
|
func (s *ResourceProviderServer) Input(
|
|
|
|
args *ResourceProviderInputArgs,
|
|
|
|
reply *ResourceProviderInputResponse) error {
|
|
|
|
conn, err := s.Broker.Dial(args.InputId)
|
|
|
|
if err != nil {
|
|
|
|
*reply = ResourceProviderInputResponse{
|
2016-01-25 03:10:52 +01:00
|
|
|
Error: plugin.NewBasicError(err),
|
2014-09-29 09:23:17 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
client := rpc.NewClient(conn)
|
|
|
|
defer client.Close()
|
|
|
|
|
2016-01-25 03:10:52 +01:00
|
|
|
input := &UIInput{Client: client}
|
2014-09-29 09:23:17 +02:00
|
|
|
|
|
|
|
config, err := s.Provider.Input(input, args.Config)
|
|
|
|
*reply = ResourceProviderInputResponse{
|
|
|
|
Config: config,
|
2016-01-25 03:10:52 +01:00
|
|
|
Error: plugin.NewBasicError(err),
|
2014-09-29 09:23:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-13 07:37:46 +02:00
|
|
|
func (s *ResourceProviderServer) Validate(
|
2014-06-13 08:22:28 +02:00
|
|
|
args *ResourceProviderValidateArgs,
|
2014-06-13 07:37:46 +02:00
|
|
|
reply *ResourceProviderValidateResponse) error {
|
2014-06-13 08:22:28 +02:00
|
|
|
warns, errs := s.Provider.Validate(args.Config)
|
2016-01-25 03:10:52 +01:00
|
|
|
berrs := make([]*plugin.BasicError, len(errs))
|
2014-06-13 07:37:46 +02:00
|
|
|
for i, err := range errs {
|
2016-01-25 03:10:52 +01:00
|
|
|
berrs[i] = plugin.NewBasicError(err)
|
2014-06-13 07:37:46 +02:00
|
|
|
}
|
|
|
|
*reply = ResourceProviderValidateResponse{
|
|
|
|
Warnings: warns,
|
|
|
|
Errors: berrs,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-07-03 20:51:38 +02:00
|
|
|
func (s *ResourceProviderServer) ValidateResource(
|
|
|
|
args *ResourceProviderValidateResourceArgs,
|
|
|
|
reply *ResourceProviderValidateResourceResponse) error {
|
|
|
|
warns, errs := s.Provider.ValidateResource(args.Type, args.Config)
|
2016-01-25 03:10:52 +01:00
|
|
|
berrs := make([]*plugin.BasicError, len(errs))
|
2014-07-03 20:51:38 +02:00
|
|
|
for i, err := range errs {
|
2016-01-25 03:10:52 +01:00
|
|
|
berrs[i] = plugin.NewBasicError(err)
|
2014-07-03 20:51:38 +02:00
|
|
|
}
|
|
|
|
*reply = ResourceProviderValidateResourceResponse{
|
|
|
|
Warnings: warns,
|
|
|
|
Errors: berrs,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-29 00:07:47 +02:00
|
|
|
func (s *ResourceProviderServer) Configure(
|
2014-06-13 03:06:26 +02:00
|
|
|
config *terraform.ResourceConfig,
|
2014-05-29 00:07:47 +02:00
|
|
|
reply *ResourceProviderConfigureResponse) error {
|
2014-06-06 09:48:32 +02:00
|
|
|
err := s.Provider.Configure(config)
|
2014-05-29 00:07:47 +02:00
|
|
|
*reply = ResourceProviderConfigureResponse{
|
2016-01-25 03:10:52 +01:00
|
|
|
Error: plugin.NewBasicError(err),
|
2014-06-06 09:48:32 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-19 01:49:34 +02:00
|
|
|
func (s *ResourceProviderServer) Apply(
|
|
|
|
args *ResourceProviderApplyArgs,
|
|
|
|
result *ResourceProviderApplyResponse) error {
|
2014-09-17 01:48:33 +02:00
|
|
|
state, err := s.Provider.Apply(args.Info, args.State, args.Diff)
|
2014-06-19 01:49:34 +02:00
|
|
|
*result = ResourceProviderApplyResponse{
|
|
|
|
State: state,
|
2016-01-25 03:10:52 +01:00
|
|
|
Error: plugin.NewBasicError(err),
|
2014-06-19 01:49:34 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-06 09:48:32 +02:00
|
|
|
func (s *ResourceProviderServer) Diff(
|
|
|
|
args *ResourceProviderDiffArgs,
|
|
|
|
result *ResourceProviderDiffResponse) error {
|
2014-09-17 01:48:33 +02:00
|
|
|
diff, err := s.Provider.Diff(args.Info, args.State, args.Config)
|
2014-06-06 09:48:32 +02:00
|
|
|
*result = ResourceProviderDiffResponse{
|
|
|
|
Diff: diff,
|
2016-01-25 03:10:52 +01:00
|
|
|
Error: plugin.NewBasicError(err),
|
2014-05-29 00:07:47 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-06-03 23:26:31 +02:00
|
|
|
|
2014-06-20 20:51:07 +02:00
|
|
|
func (s *ResourceProviderServer) Refresh(
|
2014-09-17 01:48:33 +02:00
|
|
|
args *ResourceProviderRefreshArgs,
|
2014-06-20 20:51:07 +02:00
|
|
|
result *ResourceProviderRefreshResponse) error {
|
2014-09-17 01:48:33 +02:00
|
|
|
newState, err := s.Provider.Refresh(args.Info, args.State)
|
2014-06-20 20:51:07 +02:00
|
|
|
*result = ResourceProviderRefreshResponse{
|
|
|
|
State: newState,
|
2016-01-25 03:10:52 +01:00
|
|
|
Error: plugin.NewBasicError(err),
|
2014-06-20 20:51:07 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-04 19:58:49 +02:00
|
|
|
func (s *ResourceProviderServer) ImportState(
|
|
|
|
args *ResourceProviderImportStateArgs,
|
|
|
|
result *ResourceProviderImportStateResponse) error {
|
2016-05-04 21:43:10 +02:00
|
|
|
states, err := s.Provider.ImportState(args.Info, args.Id)
|
2016-05-04 19:58:49 +02:00
|
|
|
*result = ResourceProviderImportStateResponse{
|
|
|
|
State: states,
|
|
|
|
Error: plugin.NewBasicError(err),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-03 23:26:31 +02:00
|
|
|
func (s *ResourceProviderServer) Resources(
|
|
|
|
nothing interface{},
|
|
|
|
result *[]terraform.ResourceType) error {
|
|
|
|
*result = s.Provider.Resources()
|
|
|
|
return nil
|
|
|
|
}
|