Skip to content

Commit 45c609b

Browse files
committed
fix: Use "terraform state pull" instead of "terraform show"
Although the terraform-exec docs don't indicate this, the result of "terraform show" isn't actually the state... it's a trimmed version of the state that excludes resource identifiers, essentially removing all state that did exist. Tests will be written to ensure Terraform state reconciliation can occur. This will happen in another PR, as dogfood is currently broken because of this.
1 parent dacc025 commit 45c609b

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

provisioner/terraform/provision.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,10 @@ func (t *terraform) Provision(stream proto.DRPCProvisioner_ProvisionStream) erro
239239
errorMessage := err.Error()
240240
// Terraform can fail and apply and still need to store it's state.
241241
// In this case, we return Complete with an explicit error message.
242-
state, err := terraform.Show(stream.Context())
242+
stateData, err := exec.CommandContext(stream.Context(), t.binaryPath, "state", "pull").Output()
243243
if err != nil {
244244
return xerrors.Errorf("show state: %w", err)
245245
}
246-
stateData, err := json.Marshal(state)
247-
if err != nil {
248-
return xerrors.Errorf("marshal state: %w", err)
249-
}
250246
return stream.Send(&proto.Provision_Response{
251247
Type: &proto.Provision_Response_Complete{
252248
Complete: &proto.Provision_Complete{
@@ -364,9 +360,14 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
364360
}
365361

366362
func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform) (*proto.Provision_Response, error) {
367-
state, err := terraform.Show(ctx)
363+
stateRaw, err := exec.CommandContext(ctx, terraform.ExecPath(), "state", "pull").Output()
364+
if err != nil {
365+
return nil, xerrors.Errorf("show terraform state: %w", err)
366+
}
367+
var state tfjson.State
368+
err = json.Unmarshal(stateRaw, &state)
368369
if err != nil {
369-
return nil, xerrors.Errorf("show state file: %w", err)
370+
return nil, xerrors.Errorf("unmarshal state: %w", err)
370371
}
371372
resources := make([]*proto.Resource, 0)
372373
if state.Values != nil {
@@ -501,15 +502,10 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform) (*pro
501502
}
502503
}
503504

504-
statefileContent, err := json.Marshal(state)
505-
if err != nil {
506-
return nil, xerrors.Errorf("marshal state: %w", err)
507-
}
508-
509505
return &proto.Provision_Response{
510506
Type: &proto.Provision_Response_Complete{
511507
Complete: &proto.Provision_Complete{
512-
State: statefileContent,
508+
State: stateRaw,
513509
Resources: resources,
514510
},
515511
},

0 commit comments

Comments
 (0)