Skip to content

Commit a66be5d

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 a66be5d

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

provisioner/terraform/provision.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,11 @@ 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+
// #nosec
243+
stateData, err := exec.CommandContext(stream.Context(), t.binaryPath, "state", "pull").Output()
243244
if err != nil {
244245
return xerrors.Errorf("show state: %w", err)
245246
}
246-
stateData, err := json.Marshal(state)
247-
if err != nil {
248-
return xerrors.Errorf("marshal state: %w", err)
249-
}
250247
return stream.Send(&proto.Provision_Response{
251248
Type: &proto.Provision_Response_Complete{
252249
Complete: &proto.Provision_Complete{
@@ -364,9 +361,15 @@ func parseTerraformPlan(ctx context.Context, terraform *tfexec.Terraform, planfi
364361
}
365362

366363
func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform) (*proto.Provision_Response, error) {
367-
state, err := terraform.Show(ctx)
364+
// #nosec
365+
stateRaw, err := exec.CommandContext(ctx, terraform.ExecPath(), "state", "pull").Output()
366+
if err != nil {
367+
return nil, xerrors.Errorf("show terraform state: %w", err)
368+
}
369+
var state tfjson.State
370+
err = json.Unmarshal(stateRaw, &state)
368371
if err != nil {
369-
return nil, xerrors.Errorf("show state file: %w", err)
372+
return nil, xerrors.Errorf("unmarshal state: %w", err)
370373
}
371374
resources := make([]*proto.Resource, 0)
372375
if state.Values != nil {
@@ -501,15 +504,10 @@ func parseTerraformApply(ctx context.Context, terraform *tfexec.Terraform) (*pro
501504
}
502505
}
503506

504-
statefileContent, err := json.Marshal(state)
505-
if err != nil {
506-
return nil, xerrors.Errorf("marshal state: %w", err)
507-
}
508-
509507
return &proto.Provision_Response{
510508
Type: &proto.Provision_Response_Complete{
511509
Complete: &proto.Provision_Complete{
512-
State: statefileContent,
510+
State: stateRaw,
513511
Resources: resources,
514512
},
515513
},

0 commit comments

Comments
 (0)