provisioners/local-exec: switch to helper/schema
This commit is contained in:
parent
a1da59a73e
commit
c5b784c33f
|
@ -3,13 +3,10 @@ package main
|
||||||
import (
|
import (
|
||||||
"github.com/hashicorp/terraform/builtin/provisioners/local-exec"
|
"github.com/hashicorp/terraform/builtin/provisioners/local-exec"
|
||||||
"github.com/hashicorp/terraform/plugin"
|
"github.com/hashicorp/terraform/plugin"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
plugin.Serve(&plugin.ServeOpts{
|
plugin.Serve(&plugin.ServeOpts{
|
||||||
ProvisionerFunc: func() terraform.ResourceProvisioner {
|
ProvisionerFunc: localexec.Provisioner,
|
||||||
return new(localexec.ResourceProvisioner)
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
package localexec
|
package localexec
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/armon/circbuf"
|
"github.com/armon/circbuf"
|
||||||
"github.com/hashicorp/terraform/helper/config"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
"github.com/mitchellh/go-linereader"
|
"github.com/mitchellh/go-linereader"
|
||||||
)
|
)
|
||||||
|
@ -19,21 +20,26 @@ const (
|
||||||
maxBufSize = 8 * 1024
|
maxBufSize = 8 * 1024
|
||||||
)
|
)
|
||||||
|
|
||||||
type ResourceProvisioner struct{}
|
func Provisioner() terraform.ResourceProvisioner {
|
||||||
|
return &schema.Provisioner{
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
"command": &schema.Schema{
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
|
||||||
func (p *ResourceProvisioner) Apply(
|
ApplyFunc: applyFn,
|
||||||
o terraform.UIOutput,
|
|
||||||
s *terraform.InstanceState,
|
|
||||||
c *terraform.ResourceConfig) error {
|
|
||||||
|
|
||||||
// Get the command
|
|
||||||
commandRaw, ok := c.Config["command"]
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("local-exec provisioner missing 'command'")
|
|
||||||
}
|
}
|
||||||
command, ok := commandRaw.(string)
|
}
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("local-exec provisioner command must be a string")
|
func applyFn(ctx context.Context) error {
|
||||||
|
data := ctx.Value(schema.ProvConfigDataKey).(*schema.ResourceData)
|
||||||
|
o := ctx.Value(schema.ProvOutputKey).(terraform.UIOutput)
|
||||||
|
|
||||||
|
command := data.Get("command").(string)
|
||||||
|
if command == "" {
|
||||||
|
return fmt.Errorf("local-exec provisioner command must be a non-empty string")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute the command using a shell
|
// Execute the command using a shell
|
||||||
|
@ -49,7 +55,7 @@ func (p *ResourceProvisioner) Apply(
|
||||||
// Setup the reader that will read the lines from the command
|
// Setup the reader that will read the lines from the command
|
||||||
pr, pw := io.Pipe()
|
pr, pw := io.Pipe()
|
||||||
copyDoneCh := make(chan struct{})
|
copyDoneCh := make(chan struct{})
|
||||||
go p.copyOutput(o, pr, copyDoneCh)
|
go copyOutput(o, pr, copyDoneCh)
|
||||||
|
|
||||||
// Setup the command
|
// Setup the command
|
||||||
cmd := exec.Command(shell, flag, command)
|
cmd := exec.Command(shell, flag, command)
|
||||||
|
@ -78,15 +84,7 @@ func (p *ResourceProvisioner) Apply(
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, []error) {
|
func copyOutput(o terraform.UIOutput, r io.Reader, doneCh chan<- struct{}) {
|
||||||
validator := config.Validator{
|
|
||||||
Required: []string{"command"},
|
|
||||||
}
|
|
||||||
return validator.Validate(c)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *ResourceProvisioner) copyOutput(
|
|
||||||
o terraform.UIOutput, r io.Reader, doneCh chan<- struct{}) {
|
|
||||||
defer close(doneCh)
|
defer close(doneCh)
|
||||||
lr := linereader.New(r)
|
lr := linereader.New(r)
|
||||||
for line := range lr.Ch {
|
for line := range lr.Ch {
|
||||||
|
|
|
@ -10,10 +10,6 @@ import (
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestResourceProvisioner_impl(t *testing.T) {
|
|
||||||
var _ terraform.ResourceProvisioner = new(ResourceProvisioner)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestResourceProvider_Apply(t *testing.T) {
|
func TestResourceProvider_Apply(t *testing.T) {
|
||||||
defer os.Remove("test_out")
|
defer os.Remove("test_out")
|
||||||
c := testConfig(t, map[string]interface{}{
|
c := testConfig(t, map[string]interface{}{
|
||||||
|
@ -21,7 +17,7 @@ func TestResourceProvider_Apply(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
output := new(terraform.MockUIOutput)
|
output := new(terraform.MockUIOutput)
|
||||||
p := new(ResourceProvisioner)
|
p := Provisioner()
|
||||||
if err := p.Apply(output, nil, c); err != nil {
|
if err := p.Apply(output, nil, c); err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -43,7 +39,7 @@ func TestResourceProvider_Validate_good(t *testing.T) {
|
||||||
c := testConfig(t, map[string]interface{}{
|
c := testConfig(t, map[string]interface{}{
|
||||||
"command": "echo foo",
|
"command": "echo foo",
|
||||||
})
|
})
|
||||||
p := new(ResourceProvisioner)
|
p := Provisioner()
|
||||||
warn, errs := p.Validate(c)
|
warn, errs := p.Validate(c)
|
||||||
if len(warn) > 0 {
|
if len(warn) > 0 {
|
||||||
t.Fatalf("Warnings: %v", warn)
|
t.Fatalf("Warnings: %v", warn)
|
||||||
|
@ -55,7 +51,7 @@ func TestResourceProvider_Validate_good(t *testing.T) {
|
||||||
|
|
||||||
func TestResourceProvider_Validate_missing(t *testing.T) {
|
func TestResourceProvider_Validate_missing(t *testing.T) {
|
||||||
c := testConfig(t, map[string]interface{}{})
|
c := testConfig(t, map[string]interface{}{})
|
||||||
p := new(ResourceProvisioner)
|
p := Provisioner()
|
||||||
warn, errs := p.Validate(c)
|
warn, errs := p.Validate(c)
|
||||||
if len(warn) > 0 {
|
if len(warn) > 0 {
|
||||||
t.Fatalf("Warnings: %v", warn)
|
t.Fatalf("Warnings: %v", warn)
|
||||||
|
|
Loading…
Reference in New Issue