2014-05-28 15:56:43 -05:00
|
|
|
package terraform
|
|
|
|
|
2014-06-05 09:27:01 -05:00
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2014-05-28 15:56:43 -05:00
|
|
|
// State keeps track of a snapshot state-of-the-world that Terraform
|
|
|
|
// can use to keep track of what real world resources it is actually
|
|
|
|
// managing.
|
|
|
|
type State struct {
|
2014-06-05 09:27:01 -05:00
|
|
|
resources map[string]*ResourceState
|
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) init() {
|
|
|
|
s.once.Do(func() {
|
|
|
|
s.resources = make(map[string]*ResourceState)
|
|
|
|
})
|
2014-05-28 15:56:43 -05:00
|
|
|
}
|
2014-06-05 08:57:06 -05:00
|
|
|
|
|
|
|
// ResourceState holds the state of a resource that is used so that
|
|
|
|
// a provider can find and manage an existing resource as well as for
|
|
|
|
// storing attributes that are uesd to populate variables of child
|
|
|
|
// resources.
|
|
|
|
//
|
|
|
|
// Attributes has attributes about the created resource that are
|
|
|
|
// queryable in interpolation: "${type.id.attr}"
|
|
|
|
//
|
|
|
|
// Extra is just extra data that a provider can return that we store
|
|
|
|
// for later, but is not exposed in any way to the user.
|
|
|
|
type ResourceState struct {
|
|
|
|
ID string
|
|
|
|
Attributes map[string]string
|
|
|
|
Extra map[string]interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MergeDiff takes a ResourceDiff and merges the attributes into
|
|
|
|
// this resource state in order to generate a new state. This new
|
|
|
|
// state can be used to provide updated attribute lookups for
|
|
|
|
// variable interpolation.
|
2014-06-05 09:01:51 -05:00
|
|
|
//
|
|
|
|
// If the diff attribute requires computing the value, and hence
|
|
|
|
// won't be available until apply, the value is replaced with the
|
|
|
|
// computeID.
|
2014-06-05 08:57:06 -05:00
|
|
|
func (s *ResourceState) MergeDiff(
|
2014-06-05 09:01:51 -05:00
|
|
|
d map[string]*ResourceAttrDiff,
|
2014-06-05 09:27:01 -05:00
|
|
|
computedID string) *ResourceState {
|
2014-06-05 09:04:44 -05:00
|
|
|
var result ResourceState
|
|
|
|
if s != nil {
|
|
|
|
result = *s
|
|
|
|
}
|
|
|
|
|
2014-06-05 08:57:06 -05:00
|
|
|
result.Attributes = make(map[string]string)
|
2014-06-05 09:04:44 -05:00
|
|
|
if s != nil {
|
|
|
|
for k, v := range s.Attributes {
|
|
|
|
result.Attributes[k] = v
|
|
|
|
}
|
2014-06-05 08:57:06 -05:00
|
|
|
}
|
|
|
|
for k, diff := range d {
|
2014-06-05 09:01:51 -05:00
|
|
|
if diff.NewComputed {
|
|
|
|
result.Attributes[k] = computedID
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-06-05 08:57:06 -05:00
|
|
|
result.Attributes[k] = diff.New
|
|
|
|
}
|
|
|
|
|
2014-06-05 09:27:01 -05:00
|
|
|
return &result
|
2014-06-05 08:57:06 -05:00
|
|
|
}
|