builtin/provisioners/*: new API

This commit is contained in:
Mitchell Hashimoto 2014-07-22 10:38:39 -07:00
parent 53ebc5cb51
commit 8720d2465e
4 changed files with 18 additions and 19 deletions

View File

@ -14,31 +14,31 @@ import (
type ResourceProvisioner struct{} type ResourceProvisioner struct{}
func (p *ResourceProvisioner) Apply(s *terraform.ResourceState, func (p *ResourceProvisioner) Apply(s *terraform.ResourceState,
c *terraform.ResourceConfig) (*terraform.ResourceState, error) { c *terraform.ResourceConfig) error {
// Ensure the connection type is SSH // Ensure the connection type is SSH
if err := helper.VerifySSH(s); err != nil { if err := helper.VerifySSH(s); err != nil {
return s, err return err
} }
// Get the SSH configuration // Get the SSH configuration
conf, err := helper.ParseSSHConfig(s) conf, err := helper.ParseSSHConfig(s)
if err != nil { if err != nil {
return s, err return err
} }
// Get the source and destination // Get the source and destination
sRaw := c.Config["source"] sRaw := c.Config["source"]
src, ok := sRaw.(string) src, ok := sRaw.(string)
if !ok { if !ok {
return s, fmt.Errorf("Unsupported 'source' type! Must be string.") return fmt.Errorf("Unsupported 'source' type! Must be string.")
} }
dRaw := c.Config["destination"] dRaw := c.Config["destination"]
dst, ok := dRaw.(string) dst, ok := dRaw.(string)
if !ok { if !ok {
return s, fmt.Errorf("Unsupported 'destination' type! Must be string.") return fmt.Errorf("Unsupported 'destination' type! Must be string.")
} }
return s, p.copyFiles(conf, src, dst) return p.copyFiles(conf, src, dst)
} }
func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string, es []error) { func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string, es []error) {

View File

@ -21,16 +21,16 @@ type ResourceProvisioner struct{}
func (p *ResourceProvisioner) Apply( func (p *ResourceProvisioner) Apply(
s *terraform.ResourceState, s *terraform.ResourceState,
c *terraform.ResourceConfig) (*terraform.ResourceState, error) { c *terraform.ResourceConfig) error {
// Get the command // Get the command
commandRaw, ok := c.Config["command"] commandRaw, ok := c.Config["command"]
if !ok { if !ok {
return s, fmt.Errorf("local-exec provisioner missing 'command'") return fmt.Errorf("local-exec provisioner missing 'command'")
} }
command, ok := commandRaw.(string) command, ok := commandRaw.(string)
if !ok { if !ok {
return s, fmt.Errorf("local-exec provisioner command must be a string") return fmt.Errorf("local-exec provisioner command must be a string")
} }
// Execute the command using a shell // Execute the command using a shell
@ -51,10 +51,10 @@ func (p *ResourceProvisioner) Apply(
// Run the command to completion // Run the command to completion
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return s, fmt.Errorf("Error running command '%s': %v. Output: %s", return fmt.Errorf("Error running command '%s': %v. Output: %s",
command, err, output.Bytes()) command, err, output.Bytes())
} }
return s, nil return nil
} }
func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, []error) { func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) ([]string, []error) {

View File

@ -21,8 +21,7 @@ func TestResourceProvider_Apply(t *testing.T) {
}) })
p := new(ResourceProvisioner) p := new(ResourceProvisioner)
_, err := p.Apply(nil, c) if err := p.Apply(nil, c); err != nil {
if err != nil {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }

View File

@ -23,22 +23,22 @@ const (
type ResourceProvisioner struct{} type ResourceProvisioner struct{}
func (p *ResourceProvisioner) Apply(s *terraform.ResourceState, func (p *ResourceProvisioner) Apply(s *terraform.ResourceState,
c *terraform.ResourceConfig) (*terraform.ResourceState, error) { c *terraform.ResourceConfig) error {
// Ensure the connection type is SSH // Ensure the connection type is SSH
if err := helper.VerifySSH(s); err != nil { if err := helper.VerifySSH(s); err != nil {
return s, err return err
} }
// Get the SSH configuration // Get the SSH configuration
conf, err := helper.ParseSSHConfig(s) conf, err := helper.ParseSSHConfig(s)
if err != nil { if err != nil {
return s, err return err
} }
// Collect the scripts // Collect the scripts
scripts, err := p.collectScripts(c) scripts, err := p.collectScripts(c)
if err != nil { if err != nil {
return s, err return err
} }
for _, s := range scripts { for _, s := range scripts {
defer s.Close() defer s.Close()
@ -46,9 +46,9 @@ func (p *ResourceProvisioner) Apply(s *terraform.ResourceState,
// Copy and execute each script // Copy and execute each script
if err := p.runScripts(conf, scripts); err != nil { if err := p.runScripts(conf, scripts); err != nil {
return s, err return err
} }
return s, nil return nil
} }
func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string, es []error) { func (p *ResourceProvisioner) Validate(c *terraform.ResourceConfig) (ws []string, es []error) {