Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deferred actions: don't plan partial resources during refresh and destroy #36310

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 153 additions & 0 deletions internal/stacks/stackruntime/apply_destroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,6 +1186,159 @@ func TestApplyDestroy(t *testing.T) {
},
},
},
"destroy-partial-state": {
path: "destroy-partial-state",
state: stackstate.NewStateBuilder().
AddComponentInstance(stackstate.NewComponentInstanceBuilder(mustAbsComponentInstance("component.parent")).
AddDependent(mustAbsComponent("component.child"))).
AddResourceInstance(stackstate.NewResourceInstanceBuilder().
SetAddr(mustAbsResourceInstanceObject("component.parent.testing_resource.primary")).
SetProviderAddr(mustDefaultRootProvider("testing")).
SetResourceInstanceObjectSrc(states.ResourceInstanceObjectSrc{
AttrsJSON: mustMarshalJSONAttrs(map[string]interface{}{
"id": "primary",
}),
Status: states.ObjectReady,
})).
AddResourceInstance(stackstate.NewResourceInstanceBuilder().
SetAddr(mustAbsResourceInstanceObject("component.parent.testing_resource.secondary")).
SetProviderAddr(mustDefaultRootProvider("testing")).
SetResourceInstanceObjectSrc(states.ResourceInstanceObjectSrc{
AttrsJSON: mustMarshalJSONAttrs(map[string]interface{}{
"id": "secondary",
"value": "primary",
}),
Status: states.ObjectReady,
})).
Build(),
store: stacks_testing_provider.NewResourceStoreBuilder().
AddResource("primary", cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("primary"),
"value": cty.NullVal(cty.String),
})).
AddResource("secondary", cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("secondary"),
"value": cty.StringVal("primary"),
})).
Build(),
cycles: []TestCycle{
{
planMode: plans.DestroyMode,
wantPlannedChanges: []stackplan.PlannedChange{
&stackplan.PlannedChangeApplyable{
Applyable: true,
},
&stackplan.PlannedChangeComponentInstance{
Addr: mustAbsComponentInstance("component.child"),
Action: plans.Delete,
Mode: plans.DestroyMode,
PlanApplyable: true,
PlanComplete: true,
RequiredComponents: collections.NewSet(mustAbsComponent("component.parent")),
PlannedOutputValues: make(map[string]cty.Value),
PlanTimestamp: fakePlanTimestamp,
},
&stackplan.PlannedChangeComponentInstance{
Addr: mustAbsComponentInstance("component.parent"),
Action: plans.Delete,
Mode: plans.DestroyMode,
PlanApplyable: true,
PlanComplete: true,
PlannedInputValues: make(map[string]plans.DynamicValue),
PlannedOutputValues: map[string]cty.Value{
"deleted_id": cty.DynamicVal,
},
PlannedCheckResults: &states.CheckResults{},
PlanTimestamp: fakePlanTimestamp,
},
&stackplan.PlannedChangeResourceInstancePlanned{
ResourceInstanceObjectAddr: mustAbsResourceInstanceObject("component.parent.testing_resource.primary"),
ChangeSrc: &plans.ResourceInstanceChangeSrc{
Addr: mustAbsResourceInstance("testing_resource.primary"),
PrevRunAddr: mustAbsResourceInstance("testing_resource.primary"),
ProviderAddr: mustDefaultRootProvider("testing"),
ChangeSrc: plans.ChangeSrc{
Action: plans.Delete,
Before: mustPlanDynamicValue(cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("primary"),
"value": cty.NullVal(cty.String),
})),
After: mustPlanDynamicValue(cty.NullVal(cty.Object(map[string]cty.Type{
"id": cty.String,
"value": cty.String,
}))),
},
},
PriorStateSrc: &states.ResourceInstanceObjectSrc{
AttrsJSON: mustMarshalJSONAttrs(map[string]interface{}{
"id": "primary",
"value": nil,
}),
Status: states.ObjectReady,
Dependencies: make([]addrs.ConfigResource, 0),
},
ProviderConfigAddr: mustDefaultRootProvider("testing"),
Schema: stacks_testing_provider.TestingResourceSchema,
},
&stackplan.PlannedChangeResourceInstancePlanned{
ResourceInstanceObjectAddr: mustAbsResourceInstanceObject("component.parent.testing_resource.secondary"),
ChangeSrc: &plans.ResourceInstanceChangeSrc{
Addr: mustAbsResourceInstance("testing_resource.secondary"),
PrevRunAddr: mustAbsResourceInstance("testing_resource.secondary"),
ProviderAddr: mustDefaultRootProvider("testing"),
ChangeSrc: plans.ChangeSrc{
Action: plans.Delete,
Before: mustPlanDynamicValue(cty.ObjectVal(map[string]cty.Value{
"id": cty.StringVal("secondary"),
"value": cty.StringVal("primary"),
})),
After: mustPlanDynamicValue(cty.NullVal(cty.Object(map[string]cty.Type{
"id": cty.String,
"value": cty.String,
}))),
},
},
PriorStateSrc: &states.ResourceInstanceObjectSrc{
AttrsJSON: mustMarshalJSONAttrs(map[string]interface{}{
"id": "secondary",
"value": "primary",
}),
Status: states.ObjectReady,
Dependencies: []addrs.ConfigResource{
mustAbsResourceInstance("testing_resource.primary").ConfigResource(),
},
},
ProviderConfigAddr: mustDefaultRootProvider("testing"),
Schema: stacks_testing_provider.TestingResourceSchema,
},
&stackplan.PlannedChangeHeader{
TerraformVersion: version.SemVer,
},
&stackplan.PlannedChangePlannedTimestamp{
PlannedTimestamp: fakePlanTimestamp,
},
},
wantAppliedChanges: []stackstate.AppliedChange{
&stackstate.AppliedChangeComponentInstanceRemoved{
ComponentAddr: mustAbsComponent("component.child"),
ComponentInstanceAddr: mustAbsComponentInstance("component.child"),
},
&stackstate.AppliedChangeComponentInstanceRemoved{
ComponentAddr: mustAbsComponent("component.parent"),
ComponentInstanceAddr: mustAbsComponentInstance("component.parent"),
},
&stackstate.AppliedChangeResourceInstanceObject{
ResourceInstanceObjectAddr: mustAbsResourceInstanceObject("component.parent.testing_resource.primary"),
ProviderConfigAddr: mustDefaultRootProvider("testing"),
},
&stackstate.AppliedChangeResourceInstanceObject{
ResourceInstanceObjectAddr: mustAbsResourceInstanceObject("component.parent.testing_resource.secondary"),
ProviderConfigAddr: mustDefaultRootProvider("testing"),
},
},
},
},
},
}
for name, tc := range tcs {
t.Run(name, func(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import (
// instantiated by the configuration, it is loaded dynamically by a relevant
// component or removed block. It represents the refresh action of a given
// instance within state.
//
// This is only ever called during a destroy operation, and is used to refresh
// the state of the component before it is destroyed. If this changes, then
// the PreDestroyRefresh option should be removed from the plan options.
type RefreshInstance struct {
component *ComponentInstance

Expand Down Expand Up @@ -70,6 +74,12 @@ func (r *RefreshInstance) Plan(ctx context.Context) (*plans.Plan, tfdiags.Diagno
return nil, diags
}

// For now, the refresh option is only used to separate the refresh
// from the apply during a destroy operation. So, we want to use that
// option here to ensure that the refresh is done in a way that is
// compatible with the destroy operation.
opts.PreDestroyRefresh = true

plan, moreDiags := PlanComponentInstance(ctx, r.component.main, r.component.PlanPrevState(ctx), opts, r.component)
return plan, diags.Append(moreDiags)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
terraform {
required_providers {
testing = {
source = "hashicorp/testing"
version = "0.1.0"
}
}
}

variable "key" {
type = string
}

resource "testing_resource" "primary" {
for_each = {
(var.key) = "primary"
}
id = each.value
}


Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
required_providers {
testing = {
source = "hashicorp/testing"
version = "0.1.0"
}
}

provider "testing" "default" {}


component "parent" {
source = "./parent"

providers = {
testing = provider.testing.default
}
}

component "child" {
source = "./child"

providers = {
testing = provider.testing.default
}

inputs = {
key = component.parent.deleted_id
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
terraform {
required_providers {
testing = {
source = "hashicorp/testing"
version = "0.1.0"
}
}
}

resource "testing_resource" "deleted" {}

resource "testing_resource" "primary" {}

resource "testing_resource" "secondary" {
value = testing_resource.primary.id
}

resource "testing_resource" "depends_on_deleted" {
for_each = {
(testing_resource.deleted.id) = "tertiary"
}
id = each.value
}

output "deleted_id" {
value = testing_resource.deleted.id
}
Loading
Loading