2014-06-17 20:40:32 -05:00
|
|
|
package diff
|
|
|
|
|
|
|
|
import (
|
2014-07-01 18:06:06 -05:00
|
|
|
"strings"
|
|
|
|
|
2016-11-09 01:06:12 -06:00
|
|
|
"github.com/hashicorp/terraform/config"
|
2014-07-01 18:06:06 -05:00
|
|
|
"github.com/hashicorp/terraform/flatmap"
|
2014-06-17 20:40:32 -05:00
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2014-07-01 18:06:06 -05:00
|
|
|
// AttrType is an enum that tells the ResourceBuilder what type of attribute
|
|
|
|
// an attribute is, affecting the overall diff output.
|
|
|
|
//
|
|
|
|
// The valid values are:
|
|
|
|
//
|
|
|
|
// * AttrTypeCreate - This attribute can only be set or updated on create.
|
|
|
|
// This means that if this attribute is changed, it will require a new
|
|
|
|
// resource to be created if it is already created.
|
|
|
|
//
|
|
|
|
// * AttrTypeUpdate - This attribute can be set at create time or updated
|
|
|
|
// in-place. Changing this attribute does not require a new resource.
|
|
|
|
//
|
|
|
|
type AttrType byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
AttrTypeUnknown AttrType = iota
|
|
|
|
AttrTypeCreate
|
|
|
|
AttrTypeUpdate
|
|
|
|
)
|
|
|
|
|
2014-06-24 12:33:03 -05:00
|
|
|
// ResourceBuilder is a helper that knows about how a single resource
|
2014-06-17 20:43:10 -05:00
|
|
|
// changes and how those changes affect the diff.
|
2014-06-17 20:40:32 -05:00
|
|
|
type ResourceBuilder struct {
|
2014-07-01 18:06:06 -05:00
|
|
|
// Attrs are the mapping of attributes that can be set from the
|
|
|
|
// configuration, and the affect they have. See the documentation for
|
|
|
|
// AttrType for more info.
|
2014-07-11 13:36:44 -05:00
|
|
|
//
|
|
|
|
// Sometimes attributes in here are also computed. For example, an
|
|
|
|
// "availability_zone" might be optional, but will be chosen for you
|
|
|
|
// by AWS. In that case, specify it both here and in ComputedAttrs.
|
2014-08-07 02:19:56 -05:00
|
|
|
// This will make sure that the absence of the configuration won't
|
2014-07-11 13:36:44 -05:00
|
|
|
// cause a diff by setting it to the empty string.
|
2014-07-01 18:06:06 -05:00
|
|
|
Attrs map[string]AttrType
|
|
|
|
|
|
|
|
// ComputedAttrs are the attributes that are computed at
|
|
|
|
// resource creation time.
|
|
|
|
ComputedAttrs []string
|
2014-07-16 17:58:47 -05:00
|
|
|
|
2014-07-24 22:24:40 -05:00
|
|
|
// ComputedAttrsUpdate are the attributes that are computed
|
|
|
|
// at resource update time (this includes creation).
|
|
|
|
ComputedAttrsUpdate []string
|
|
|
|
|
2014-07-16 17:58:47 -05:00
|
|
|
// PreProcess is a mapping of exact keys that are sent through
|
|
|
|
// a pre-processor before comparing values. The original value will
|
|
|
|
// be put in the "NewExtra" field of the diff.
|
|
|
|
PreProcess map[string]PreProcessFunc
|
2014-06-17 20:40:32 -05:00
|
|
|
}
|
|
|
|
|
2014-07-16 17:58:47 -05:00
|
|
|
// PreProcessFunc is used with the PreProcess field in a ResourceBuilder
|
|
|
|
type PreProcessFunc func(string) string
|
|
|
|
|
2014-06-17 20:43:10 -05:00
|
|
|
// Diff returns the ResourceDiff for a resource given its state and
|
|
|
|
// configuration.
|
2014-06-17 20:40:32 -05:00
|
|
|
func (b *ResourceBuilder) Diff(
|
2014-09-16 18:56:14 -05:00
|
|
|
s *terraform.InstanceState,
|
2014-09-17 18:33:24 -05:00
|
|
|
c *terraform.ResourceConfig) (*terraform.InstanceDiff, error) {
|
2014-06-17 20:40:32 -05:00
|
|
|
attrs := make(map[string]*terraform.ResourceAttrDiff)
|
|
|
|
|
|
|
|
// We require a new resource if the ID is empty. Or, later, we set
|
|
|
|
// this to true if any configuration changed that triggers a new resource.
|
|
|
|
requiresNew := s.ID == ""
|
|
|
|
|
2014-07-01 18:06:06 -05:00
|
|
|
// Flatten the raw and processed configuration
|
|
|
|
flatRaw := flatmap.Flatten(c.Raw)
|
|
|
|
flatConfig := flatmap.Flatten(c.Config)
|
|
|
|
|
2014-07-09 11:47:21 -05:00
|
|
|
for ak, at := range b.Attrs {
|
|
|
|
// Keep track of all the keys we saw in the raw structure
|
|
|
|
// so that we can prune our attributes later.
|
|
|
|
seenKeys := make([]string, 0)
|
|
|
|
|
|
|
|
// Go through and find the added/changed keys in flatRaw
|
|
|
|
for k, v := range flatRaw {
|
|
|
|
// Find only the attributes that match our prefix
|
|
|
|
if !strings.HasPrefix(k, ak) {
|
|
|
|
continue
|
2014-07-01 18:06:06 -05:00
|
|
|
}
|
2014-06-20 14:12:53 -05:00
|
|
|
|
2014-07-09 11:47:21 -05:00
|
|
|
// Track that we saw this key
|
|
|
|
seenKeys = append(seenKeys, k)
|
|
|
|
|
2014-07-16 17:58:47 -05:00
|
|
|
// We keep track of this in case we have a pre-processor
|
|
|
|
// so that we can store the original value still.
|
|
|
|
originalV := v
|
|
|
|
|
2014-07-09 11:47:21 -05:00
|
|
|
// If this key is in the cleaned config, then use that value
|
|
|
|
// because it'll have its variables properly interpolated
|
2016-11-09 01:06:12 -06:00
|
|
|
if cleanV, ok := flatConfig[k]; ok && cleanV != config.UnknownVariableValue {
|
2014-07-09 11:47:21 -05:00
|
|
|
v = cleanV
|
2014-07-16 17:58:47 -05:00
|
|
|
originalV = v
|
|
|
|
|
|
|
|
// If we have a pre-processor for this, run it.
|
|
|
|
if pp, ok := b.PreProcess[k]; ok {
|
2014-07-16 18:40:54 -05:00
|
|
|
v = pp(v)
|
2014-07-16 17:58:47 -05:00
|
|
|
}
|
2014-07-09 11:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
oldV, ok := s.Attributes[k]
|
|
|
|
|
|
|
|
// If there is an old value and they're the same, no change
|
|
|
|
if ok && oldV == v {
|
|
|
|
continue
|
|
|
|
}
|
2014-06-17 20:40:32 -05:00
|
|
|
|
2014-07-09 11:47:21 -05:00
|
|
|
// Record the change
|
|
|
|
attrs[k] = &terraform.ResourceAttrDiff{
|
2014-07-16 17:58:47 -05:00
|
|
|
Old: oldV,
|
|
|
|
New: v,
|
|
|
|
NewExtra: originalV,
|
|
|
|
Type: terraform.DiffAttrInput,
|
2014-07-09 11:47:21 -05:00
|
|
|
}
|
2014-07-01 18:06:06 -05:00
|
|
|
|
2014-07-09 11:47:21 -05:00
|
|
|
// If this requires a new resource, record that and flag our
|
|
|
|
// boolean.
|
|
|
|
if at == AttrTypeCreate {
|
|
|
|
attrs[k].RequiresNew = true
|
|
|
|
requiresNew = true
|
|
|
|
}
|
2014-06-17 20:40:32 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 13:36:44 -05:00
|
|
|
// Find all the keys that are in our attributes right now that
|
|
|
|
// we also care about.
|
2014-07-09 11:47:21 -05:00
|
|
|
matchingKeys := make(map[string]struct{})
|
|
|
|
for k, _ := range s.Attributes {
|
|
|
|
// Find only the attributes that match our prefix
|
|
|
|
if !strings.HasPrefix(k, ak) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-07-11 13:36:44 -05:00
|
|
|
// If this key is computed, then we don't ever delete it
|
|
|
|
comp := false
|
|
|
|
for _, ck := range b.ComputedAttrs {
|
|
|
|
if ck == k {
|
|
|
|
comp = true
|
|
|
|
break
|
|
|
|
}
|
2014-07-16 17:21:01 -05:00
|
|
|
|
|
|
|
// If the key is prefixed with the computed key, don't
|
|
|
|
// mark it for delete, ever.
|
|
|
|
if strings.HasPrefix(k, ck+".") {
|
|
|
|
comp = true
|
|
|
|
break
|
|
|
|
}
|
2014-07-11 13:36:44 -05:00
|
|
|
}
|
|
|
|
if comp {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-07-09 11:47:21 -05:00
|
|
|
matchingKeys[k] = struct{}{}
|
2014-06-17 20:40:32 -05:00
|
|
|
}
|
|
|
|
|
2014-07-11 13:36:44 -05:00
|
|
|
// Delete the keys we saw in the configuration from the keys
|
|
|
|
// that are currently set.
|
2014-07-09 11:47:21 -05:00
|
|
|
for _, k := range seenKeys {
|
|
|
|
delete(matchingKeys, k)
|
|
|
|
}
|
|
|
|
for k, _ := range matchingKeys {
|
|
|
|
attrs[k] = &terraform.ResourceAttrDiff{
|
2014-07-09 11:51:36 -05:00
|
|
|
Old: s.Attributes[k],
|
|
|
|
NewRemoved: true,
|
|
|
|
Type: terraform.DiffAttrInput,
|
2014-07-09 11:47:21 -05:00
|
|
|
}
|
2014-06-17 20:40:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we require a new resource, then process all the attributes
|
|
|
|
// that will be changing due to the creation of the resource.
|
|
|
|
if requiresNew {
|
2014-07-01 18:06:06 -05:00
|
|
|
for _, k := range b.ComputedAttrs {
|
2014-07-11 13:46:21 -05:00
|
|
|
if _, ok := attrs[k]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-06-17 20:40:32 -05:00
|
|
|
old := s.Attributes[k]
|
|
|
|
attrs[k] = &terraform.ResourceAttrDiff{
|
|
|
|
Old: old,
|
|
|
|
NewComputed: true,
|
2014-06-23 14:49:30 -05:00
|
|
|
Type: terraform.DiffAttrOutput,
|
2014-06-17 20:40:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 22:24:40 -05:00
|
|
|
// If we're changing anything, then mark the updated
|
|
|
|
// attributes.
|
|
|
|
if len(attrs) > 0 {
|
|
|
|
for _, k := range b.ComputedAttrsUpdate {
|
|
|
|
if _, ok := attrs[k]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
old := s.Attributes[k]
|
|
|
|
attrs[k] = &terraform.ResourceAttrDiff{
|
|
|
|
Old: old,
|
|
|
|
NewComputed: true,
|
|
|
|
Type: terraform.DiffAttrOutput,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 20:40:32 -05:00
|
|
|
// Build our resulting diff if we had attributes change
|
2014-09-17 18:33:24 -05:00
|
|
|
var result *terraform.InstanceDiff
|
2014-06-17 20:40:32 -05:00
|
|
|
if len(attrs) > 0 {
|
2014-09-17 18:33:24 -05:00
|
|
|
result = &terraform.InstanceDiff{
|
2014-06-17 20:40:32 -05:00
|
|
|
Attributes: attrs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|