terraform/terraform/eval_refresh.go

56 lines
1.0 KiB
Go
Raw Normal View History

2015-02-11 17:48:45 +01:00
package terraform
2015-02-12 23:46:22 +01:00
import (
2015-07-22 13:51:14 +02:00
"fmt"
2015-02-12 23:46:22 +01:00
"log"
)
2015-02-11 17:48:45 +01:00
// EvalRefresh is an EvalNode implementation that does a refresh for
// a resource.
type EvalRefresh struct {
2015-02-14 07:58:41 +01:00
Provider *ResourceProvider
2015-02-12 23:46:22 +01:00
State **InstanceState
2015-02-11 17:48:45 +01:00
Info *InstanceInfo
2015-02-12 23:46:22 +01:00
Output **InstanceState
2015-02-11 17:48:45 +01:00
}
// TODO: test
2015-02-14 07:58:41 +01:00
func (n *EvalRefresh) Eval(ctx EvalContext) (interface{}, error) {
provider := *n.Provider
2015-02-12 23:46:22 +01:00
state := *n.State
2015-02-11 17:48:45 +01:00
2015-02-11 17:52:19 +01:00
// If we have no state, we don't do any refreshing
if state == nil {
2015-02-12 23:46:22 +01:00
log.Printf("[DEBUG] refresh: %s: no state, not refreshing", n.Info.Id)
2015-02-11 17:52:19 +01:00
return nil, nil
}
2015-02-11 22:43:07 +01:00
// Call pre-refresh hook
err := ctx.Hook(func(h Hook) (HookAction, error) {
return h.PreRefresh(n.Info, state)
})
if err != nil {
return nil, err
}
// Refresh!
state, err = provider.Refresh(n.Info, state)
if err != nil {
2015-07-22 13:51:14 +02:00
return nil, fmt.Errorf("%s: %s", n.Info.Id, err.Error())
2015-02-11 22:43:07 +01:00
}
// Call post-refresh hook
err = ctx.Hook(func(h Hook) (HookAction, error) {
return h.PostRefresh(n.Info, state)
})
if err != nil {
return nil, err
}
2015-02-12 23:46:22 +01:00
if n.Output != nil {
*n.Output = state
}
2015-02-11 17:48:45 +01:00
2015-02-14 07:58:41 +01:00
return nil, nil
2015-02-11 17:48:45 +01:00
}