Merge pull request #26387 from hashicorp/jbardin/ignore-changes-2

Fix handling of ignore_changes
This commit is contained in:
James Bardin 2020-09-29 15:35:29 -04:00 committed by GitHub
commit a78d75ccfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 117 additions and 54 deletions

View File

@ -4626,18 +4626,14 @@ func TestContext2Plan_ignoreChanges(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if res.Action != plans.Update {
t.Fatalf("resource %s should be updated, got %s", ric.Addr, res.Action)
}
if ric.Addr.String() != "aws_instance.foo" {
t.Fatalf("unexpected resource: %s", ric.Addr)
}
checkVals(t, objectVal(t, schema, map[string]cty.Value{
"id": cty.StringVal("bar"),
"ami": cty.StringVal("ami-abcd1234"),
"type": cty.StringVal("aws_instance"),
"id": cty.StringVal("bar"),
"ami": cty.StringVal("ami-abcd1234"),
}), ric.After)
}

View File

@ -1712,3 +1712,47 @@ output "out" {
}
}
}
func TestContext2Validate_invalidIgnoreChanges(t *testing.T) {
// validate module and output depends_on
m := testModuleInline(t, map[string]string{
"main.tf": `
resource "test_instance" "a" {
lifecycle {
ignore_changes = [foo]
}
}
`,
})
p := testProvider("test")
p.GetSchemaReturn = &ProviderSchema{
ResourceTypes: map[string]*configschema.Block{
"test_instance": {
Attributes: map[string]*configschema.Attribute{
"id": {Type: cty.String, Computed: true},
"foo": {Type: cty.String, Computed: true, Optional: true},
},
},
},
}
ctx := testContext2(t, &ContextOpts{
Config: m,
Providers: map[addrs.Provider]providers.Factory{
addrs.NewDefaultProvider("test"): testProviderFuncFixed(p),
},
})
diags := ctx.Validate()
if !diags.HasErrors() {
t.Fatal("succeeded; want errors")
}
for _, d := range diags {
des := d.Description().Summary
if !strings.Contains(des, "Cannot ignore") {
t.Fatalf(`expected "Invalid depends_on reference", got %q`, des)
}
}
}

View File

@ -117,7 +117,6 @@ type EvalDiff struct {
CreateBeforeDestroy bool
OutputChange **plans.ResourceInstanceChange
OutputValue *cty.Value
OutputState **states.ResourceInstanceObject
Stub bool
@ -153,23 +152,12 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
}
forEach, _ := evaluateForEachExpression(n.Config.ForEach, ctx)
keyData := EvalDataForInstanceKey(n.Addr.Key, forEach)
configVal, _, configDiags := ctx.EvaluateBlock(config.Config, schema, nil, keyData)
origConfigVal, _, configDiags := ctx.EvaluateBlock(config.Config, schema, nil, keyData)
diags = diags.Append(configDiags)
if configDiags.HasErrors() {
return nil, diags.Err()
}
// Create an unmarked version of our config val, defaulting
// to the configVal so we don't do the work of unmarking unless
// necessary
unmarkedConfigVal := configVal
var unmarkedPaths []cty.PathValueMarks
if configVal.ContainsMarked() {
// store the marked values so we can re-mark them later after
// we've sent things over the wire.
unmarkedConfigVal, unmarkedPaths = configVal.UnmarkDeepWithPaths()
}
metaConfigVal := cty.NullVal(cty.DynamicPseudoType)
if n.ProviderMetas != nil {
if m, ok := n.ProviderMetas[n.ProviderAddr.Provider]; ok && m != nil {
@ -213,6 +201,28 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
priorVal = cty.NullVal(schema.ImpliedType())
}
// ignore_changes is meant to only apply to the configuration, so it must
// be applied before we generate a plan. This ensures the config used for
// the proposed value, the proposed value itself, and the config presented
// to the provider in the PlanResourceChange request all agree on the
// starting values.
configValIgnored, ignoreChangeDiags := n.processIgnoreChanges(priorVal, origConfigVal)
diags = diags.Append(ignoreChangeDiags)
if ignoreChangeDiags.HasErrors() {
return nil, diags.Err()
}
// Create an unmarked version of our config val, defaulting
// to the configVal so we don't do the work of unmarking unless
// necessary
unmarkedConfigVal := configValIgnored
var unmarkedPaths []cty.PathValueMarks
if configValIgnored.ContainsMarked() {
// store the marked values so we can re-mark them later after
// we've sent things over the wire.
unmarkedConfigVal, unmarkedPaths = configValIgnored.UnmarkDeepWithPaths()
}
unmarkedPriorVal := priorVal
if priorVal.ContainsMarked() {
// store the marked values so we can re-mark them later after
@ -246,17 +256,6 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
return nil, validateResp.Diagnostics.InConfigBody(config.Config).Err()
}
// The provider gets an opportunity to customize the proposed new value,
// which in turn produces the _planned_ new value. But before
// we send back this information, we need to process ignore_changes
// so that CustomizeDiff will not act on them
var ignoreChangeDiags tfdiags.Diagnostics
proposedNewVal, ignoreChangeDiags = n.processIgnoreChanges(unmarkedPriorVal, proposedNewVal)
diags = diags.Append(ignoreChangeDiags)
if ignoreChangeDiags.HasErrors() {
return nil, diags.Err()
}
resp := provider.PlanResourceChange(providers.PlanResourceChangeRequest{
TypeName: n.Addr.Resource.Type,
Config: unmarkedConfigVal,
@ -303,7 +302,7 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
return nil, diags.Err()
}
if errs := objchange.AssertPlanValid(schema, priorVal, configVal, plannedNewVal); len(errs) > 0 {
if errs := objchange.AssertPlanValid(schema, priorVal, configValIgnored, plannedNewVal); len(errs) > 0 {
if resp.LegacyTypeSystem {
// The shimming of the old type system in the legacy SDK is not precise
// enough to pass this consistency check, so we'll give it a pass here,
@ -334,16 +333,6 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
}
}
// TODO: We should be able to remove this repeat of processing ignored changes
// after the plan, which helps providers relying on old behavior "just work"
// in the next major version, such that we can be stricter about ignore_changes
// values
plannedNewVal, ignoreChangeDiags = n.processIgnoreChanges(priorVal, plannedNewVal)
diags = diags.Append(ignoreChangeDiags)
if ignoreChangeDiags.HasErrors() {
return nil, diags.Err()
}
// The provider produces a list of paths to attributes whose changes mean
// that we must replace rather than update an existing remote object.
// However, we only need to do that if the identified attributes _have_
@ -444,6 +433,13 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
// able to predict new values for any of these computed attributes.
nullPriorVal := cty.NullVal(schema.ImpliedType())
// Since there is no prior state to compare after replacement, we need
// a new unmarked config from our original with no ignored values.
unmarkedConfigVal := origConfigVal
if origConfigVal.ContainsMarked() {
unmarkedConfigVal, _ = origConfigVal.UnmarkDeep()
}
// create a new proposed value from the null state and the config
proposedNewVal = objchange.ProposedNewObject(schema, nullPriorVal, unmarkedConfigVal)
@ -541,10 +537,6 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
}
}
if n.OutputValue != nil {
*n.OutputValue = configVal
}
// Update the state if we care
if n.OutputState != nil {
*n.OutputState = &states.ResourceInstanceObject{
@ -563,32 +555,32 @@ func (n *EvalDiff) Eval(ctx EvalContext) (interface{}, error) {
return nil, nil
}
func (n *EvalDiff) processIgnoreChanges(prior, proposed cty.Value) (cty.Value, tfdiags.Diagnostics) {
func (n *EvalDiff) processIgnoreChanges(prior, config cty.Value) (cty.Value, tfdiags.Diagnostics) {
// ignore_changes only applies when an object already exists, since we
// can't ignore changes to a thing we've not created yet.
if prior.IsNull() {
return proposed, nil
return config, nil
}
ignoreChanges := n.Config.Managed.IgnoreChanges
ignoreAll := n.Config.Managed.IgnoreAllChanges
if len(ignoreChanges) == 0 && !ignoreAll {
return proposed, nil
return config, nil
}
if ignoreAll {
return prior, nil
}
if prior.IsNull() || proposed.IsNull() {
if prior.IsNull() || config.IsNull() {
// Ignore changes doesn't apply when we're creating for the first time.
// Proposed should never be null here, but if it is then we'll just let it be.
return proposed, nil
return config, nil
}
return processIgnoreChangesIndividual(prior, proposed, ignoreChanges)
return processIgnoreChangesIndividual(prior, config, ignoreChanges)
}
func processIgnoreChangesIndividual(prior, proposed cty.Value, ignoreChanges []hcl.Traversal) (cty.Value, tfdiags.Diagnostics) {
func processIgnoreChangesIndividual(prior, config cty.Value, ignoreChanges []hcl.Traversal) (cty.Value, tfdiags.Diagnostics) {
// When we walk below we will be using cty.Path values for comparison, so
// we'll convert our traversals here so we can compare more easily.
ignoreChangesPath := make([]cty.Path, len(ignoreChanges))
@ -616,7 +608,7 @@ func processIgnoreChangesIndividual(prior, proposed cty.Value, ignoreChanges []h
}
var diags tfdiags.Diagnostics
ret, _ := cty.Transform(proposed, func(path cty.Path, v cty.Value) (cty.Value, error) {
ret, _ := cty.Transform(config, func(path cty.Path, v cty.Value) (cty.Value, error) {
// First we must see if this is a path that's being ignored at all.
// We're looking for an exact match here because this walk will visit
// leaf values first and then their containers, and we want to do

View File

@ -410,8 +410,39 @@ func (n *EvalValidateResource) Validate(ctx EvalContext) error {
if cfg.Managed != nil { // can be nil only in tests with poorly-configured mocks
for _, traversal := range cfg.Managed.IgnoreChanges {
// This will error out if the traversal contains an invalid
// index step. That is OK if we want users to be able to ignore
// a key that is no longer specified in the config.
moreDiags := schema.StaticValidateTraversal(traversal)
diags = diags.Append(moreDiags)
if diags.HasErrors() {
continue
}
// first check to see if this assigned in the config
v, _ := traversal.TraverseRel(configVal)
if !v.IsNull() {
// it's assigned, so we can also assume it's not computed-only
continue
}
// We can't ignore changes that don't exist in the configuration.
// We're not checking specifically if the traversal resolves to
// a computed-only value, but we can hint to the user that it
// might also be the case.
sourceRange := traversal.SourceRange()
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Cannot ignore argument not set in the configuration",
Detail: fmt.Sprintf("The ignore_changes argument is not set in the configuration.\n" +
"The ignore_changes mechanism only applies to changes " +
"within the configuration, and must be used with " +
"arguments set in the configuration and not computed by " +
"the provider.",
),
Subject: &sourceRange,
})
return diags.Err()
}
}

View File

@ -308,7 +308,7 @@ func (p *MockProvider) PlanResourceChange(r providers.PlanResourceChangeRequest)
Type: r.TypeName,
}
priorState := NewInstanceStateShimmedFromValue(r.PriorState, 0)
cfg := NewResourceConfigShimmed(r.Config, schema)
cfg := NewResourceConfigShimmed(r.ProposedNewState, schema)
legacyDiff, err := p.DiffFn(info, priorState, cfg)