Ui hook
This commit is contained in:
parent
501f926eba
commit
01319e1dc9
|
@ -0,0 +1,20 @@
|
||||||
|
package command
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/terraform"
|
||||||
|
"github.com/mitchellh/cli"
|
||||||
|
)
|
||||||
|
|
||||||
|
type UiHook struct {
|
||||||
|
terraform.NilHook
|
||||||
|
|
||||||
|
Ui cli.Ui
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *UiHook) PreRefresh(
|
||||||
|
id string, s *terraform.ResourceState) (terraform.HookAction, error) {
|
||||||
|
h.Ui.Output(fmt.Sprintf("Refreshing state for %s (ID: %s)", id, s.ID))
|
||||||
|
return terraform.HookActionContinue, nil
|
||||||
|
}
|
11
commands.go
11
commands.go
|
@ -10,11 +10,14 @@ import (
|
||||||
// Commands is the mapping of all the available Terraform commands.
|
// Commands is the mapping of all the available Terraform commands.
|
||||||
var Commands map[string]cli.CommandFactory
|
var Commands map[string]cli.CommandFactory
|
||||||
|
|
||||||
|
// Ui is the cli.Ui used for communicating to the outside world.
|
||||||
|
var Ui cli.Ui
|
||||||
|
|
||||||
const ErrorPrefix = "e:"
|
const ErrorPrefix = "e:"
|
||||||
const OutputPrefix = "o:"
|
const OutputPrefix = "o:"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
ui := &cli.PrefixedUi{
|
Ui = &cli.PrefixedUi{
|
||||||
AskPrefix: OutputPrefix,
|
AskPrefix: OutputPrefix,
|
||||||
OutputPrefix: OutputPrefix,
|
OutputPrefix: OutputPrefix,
|
||||||
InfoPrefix: OutputPrefix,
|
InfoPrefix: OutputPrefix,
|
||||||
|
@ -26,14 +29,14 @@ func init() {
|
||||||
"apply": func() (cli.Command, error) {
|
"apply": func() (cli.Command, error) {
|
||||||
return &command.ApplyCommand{
|
return &command.ApplyCommand{
|
||||||
TFConfig: &TFConfig,
|
TFConfig: &TFConfig,
|
||||||
Ui: ui,
|
Ui: Ui,
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
"plan": func() (cli.Command, error) {
|
"plan": func() (cli.Command, error) {
|
||||||
return &command.PlanCommand{
|
return &command.PlanCommand{
|
||||||
TFConfig: &TFConfig,
|
TFConfig: &TFConfig,
|
||||||
Ui: ui,
|
Ui: Ui,
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
|
|
||||||
|
@ -42,7 +45,7 @@ func init() {
|
||||||
Revision: GitCommit,
|
Revision: GitCommit,
|
||||||
Version: Version,
|
Version: Version,
|
||||||
VersionPrerelease: VersionPrerelease,
|
VersionPrerelease: VersionPrerelease,
|
||||||
Ui: ui,
|
Ui: Ui,
|
||||||
}, nil
|
}, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
3
main.go
3
main.go
|
@ -7,7 +7,9 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/command"
|
||||||
"github.com/hashicorp/terraform/plugin"
|
"github.com/hashicorp/terraform/plugin"
|
||||||
|
"github.com/hashicorp/terraform/terraform"
|
||||||
"github.com/mitchellh/cli"
|
"github.com/mitchellh/cli"
|
||||||
"github.com/mitchellh/panicwrap"
|
"github.com/mitchellh/panicwrap"
|
||||||
"github.com/mitchellh/prefixedio"
|
"github.com/mitchellh/prefixedio"
|
||||||
|
@ -84,6 +86,7 @@ func wrappedMain() int {
|
||||||
defer plugin.CleanupClients()
|
defer plugin.CleanupClients()
|
||||||
|
|
||||||
// Initialize the TFConfig settings for the commands...
|
// Initialize the TFConfig settings for the commands...
|
||||||
|
TFConfig.Hooks = []terraform.Hook{&command.UiHook{Ui: Ui}}
|
||||||
TFConfig.Providers = config.ProviderFactories()
|
TFConfig.Providers = config.ProviderFactories()
|
||||||
|
|
||||||
// Get the command line args. We shortcut "--version" and "-v" to
|
// Get the command line args. We shortcut "--version" and "-v" to
|
||||||
|
|
|
@ -22,10 +22,10 @@ const (
|
||||||
// nothing. Then, override only the functions you want to implement.
|
// nothing. Then, override only the functions you want to implement.
|
||||||
type Hook interface {
|
type Hook interface {
|
||||||
// PreRefresh is called before a resource is refreshed.
|
// PreRefresh is called before a resource is refreshed.
|
||||||
PreRefresh(*ResourceState) (HookAction, error)
|
PreRefresh(string, *ResourceState) (HookAction, error)
|
||||||
|
|
||||||
// PostRefresh is called after a resource is refreshed.
|
// PostRefresh is called after a resource is refreshed.
|
||||||
PostRefresh(*ResourceState) (HookAction, error)
|
PostRefresh(string, *ResourceState) (HookAction, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NilHook is a Hook implementation that does nothing. It exists only to
|
// NilHook is a Hook implementation that does nothing. It exists only to
|
||||||
|
@ -33,10 +33,10 @@ type Hook interface {
|
||||||
// and only implement the functions you are interested in.
|
// and only implement the functions you are interested in.
|
||||||
type NilHook struct{}
|
type NilHook struct{}
|
||||||
|
|
||||||
func (*NilHook) PreRefresh(*ResourceState) (HookAction, error) {
|
func (*NilHook) PreRefresh(string, *ResourceState) (HookAction, error) {
|
||||||
return HookActionContinue, nil
|
return HookActionContinue, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (*NilHook) PostRefresh(*ResourceState) (HookAction, error) {
|
func (*NilHook) PostRefresh(string, *ResourceState) (HookAction, error) {
|
||||||
return HookActionContinue, nil
|
return HookActionContinue, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,24 +4,28 @@ package terraform
|
||||||
// It records all of its function calls.
|
// It records all of its function calls.
|
||||||
type MockHook struct {
|
type MockHook struct {
|
||||||
PostRefreshCalled bool
|
PostRefreshCalled bool
|
||||||
|
PostRefreshId string
|
||||||
PostRefreshState *ResourceState
|
PostRefreshState *ResourceState
|
||||||
PostRefreshReturn HookAction
|
PostRefreshReturn HookAction
|
||||||
PostRefreshError error
|
PostRefreshError error
|
||||||
|
|
||||||
PreRefreshCalled bool
|
PreRefreshCalled bool
|
||||||
|
PreRefreshId string
|
||||||
PreRefreshState *ResourceState
|
PreRefreshState *ResourceState
|
||||||
PreRefreshReturn HookAction
|
PreRefreshReturn HookAction
|
||||||
PreRefreshError error
|
PreRefreshError error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *MockHook) PreRefresh(s *ResourceState) (HookAction, error) {
|
func (h *MockHook) PreRefresh(n string, s *ResourceState) (HookAction, error) {
|
||||||
h.PreRefreshCalled = true
|
h.PreRefreshCalled = true
|
||||||
|
h.PreRefreshId = n
|
||||||
h.PreRefreshState = s
|
h.PreRefreshState = s
|
||||||
return h.PreRefreshReturn, h.PreRefreshError
|
return h.PreRefreshReturn, h.PreRefreshError
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *MockHook) PostRefresh(s *ResourceState) (HookAction, error) {
|
func (h *MockHook) PostRefresh(n string, s *ResourceState) (HookAction, error) {
|
||||||
h.PostRefreshCalled = true
|
h.PostRefreshCalled = true
|
||||||
|
h.PostRefreshId = n
|
||||||
h.PostRefreshState = s
|
h.PostRefreshState = s
|
||||||
return h.PostRefreshReturn, h.PostRefreshError
|
return h.PostRefreshReturn, h.PostRefreshError
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,7 +133,7 @@ func (t *Terraform) refreshWalkFn(result *State) depgraph.WalkFunc {
|
||||||
cb := func(r *Resource) (map[string]string, error) {
|
cb := func(r *Resource) (map[string]string, error) {
|
||||||
for _, h := range t.hooks {
|
for _, h := range t.hooks {
|
||||||
// TODO: return value
|
// TODO: return value
|
||||||
h.PreRefresh(r.State)
|
h.PreRefresh(r.Id, r.State)
|
||||||
}
|
}
|
||||||
|
|
||||||
rs, err := r.Provider.Refresh(r.State)
|
rs, err := r.Provider.Refresh(r.State)
|
||||||
|
@ -153,7 +153,7 @@ func (t *Terraform) refreshWalkFn(result *State) depgraph.WalkFunc {
|
||||||
|
|
||||||
for _, h := range t.hooks {
|
for _, h := range t.hooks {
|
||||||
// TODO: return value
|
// TODO: return value
|
||||||
h.PostRefresh(rs)
|
h.PostRefresh(r.Id, rs)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|
Loading…
Reference in New Issue