github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/plans/planfile/writer.go (about) 1 package planfile 2 3 import ( 4 "archive/zip" 5 "fmt" 6 "os" 7 "time" 8 9 "github.com/hashicorp/terraform-plugin-sdk/internal/configs/configload" 10 "github.com/hashicorp/terraform-plugin-sdk/internal/plans" 11 "github.com/hashicorp/terraform-plugin-sdk/internal/states/statefile" 12 ) 13 14 // Create creates a new plan file with the given filename, overwriting any 15 // file that might already exist there. 16 // 17 // A plan file contains both a snapshot of the configuration and of the latest 18 // state file in addition to the plan itself, so that Terraform can detect 19 // if the world has changed since the plan was created and thus refuse to 20 // apply it. 21 func Create(filename string, configSnap *configload.Snapshot, stateFile *statefile.File, plan *plans.Plan) error { 22 f, err := os.Create(filename) 23 if err != nil { 24 return err 25 } 26 defer f.Close() 27 28 zw := zip.NewWriter(f) 29 defer zw.Close() 30 31 // tfplan file 32 { 33 w, err := zw.CreateHeader(&zip.FileHeader{ 34 Name: tfplanFilename, 35 Method: zip.Deflate, 36 Modified: time.Now(), 37 }) 38 if err != nil { 39 return fmt.Errorf("failed to create tfplan file: %s", err) 40 } 41 err = writeTfplan(plan, w) 42 if err != nil { 43 return fmt.Errorf("failed to write plan: %s", err) 44 } 45 } 46 47 // tfstate file 48 { 49 w, err := zw.CreateHeader(&zip.FileHeader{ 50 Name: tfstateFilename, 51 Method: zip.Deflate, 52 Modified: time.Now(), 53 }) 54 if err != nil { 55 return fmt.Errorf("failed to create embedded tfstate file: %s", err) 56 } 57 err = statefile.Write(stateFile, w) 58 if err != nil { 59 return fmt.Errorf("failed to write state snapshot: %s", err) 60 } 61 } 62 63 // tfconfig directory 64 { 65 err := writeConfigSnapshot(configSnap, zw) 66 if err != nil { 67 return fmt.Errorf("failed to write config snapshot: %s", err) 68 } 69 } 70 71 return nil 72 }