func testReadPlan(t *testing.T, path string) *terraform.Plan { f, err := os.Open(path) if err != nil { t.Fatalf("err: %s", err) } defer f.Close() p, err := terraform.ReadPlan(f) if err != nil { t.Fatalf("err: %s", err) } return p }
func (s *Server) newContext( rConf json.RawMessage, rDestroy bool, rPlan []byte, rState json.RawMessage, rParallelism int32, hooks []terraform.Hook) (*terraform.Context, error) { conf, err := config.LoadJSON(rConf) if err != nil { return nil, err } mod := module.NewTree("", conf) err = mod.Load(nil, module.GetModeNone) if err != nil { return nil, fmt.Errorf("Error loading module: %s", err) } ctxOpts := &terraform.ContextOpts{ Destroy: rDestroy, Hooks: hooks, Module: mod, Parallelism: int(rParallelism), Providers: s.providers, Provisioners: s.provisioners, } if rState != nil { b := bytes.NewBuffer(rState) state, err := terraform.ReadState(b) if err != nil { return nil, fmt.Errorf("Error reading state: %s", err) } ctxOpts.State = state } if rPlan != nil { b := bytes.NewBuffer(rPlan) plan, err := terraform.ReadPlan(b) if err != nil { return nil, fmt.Errorf("Error reading plan: %s", err) } return plan.Context(ctxOpts), nil } return terraform.NewContext(ctxOpts), nil }
func TestPlan_outPath(t *testing.T) { tf, err := ioutil.TempFile("", "tf") if err != nil { t.Fatalf("err: %s", err) } outPath := tf.Name() os.Remove(tf.Name()) p := testProvider() ui := new(cli.MockUi) c := &PlanCommand{ Meta: Meta{ ContextOpts: testCtxConfig(p), Ui: ui, }, } p.DiffReturn = &terraform.InstanceDiff{ Destroy: true, } args := []string{ "-out", outPath, testFixturePath("plan"), } if code := c.Run(args); code != 0 { t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) } f, err := os.Open(outPath) if err != nil { t.Fatalf("err: %s", err) } defer f.Close() if _, err := terraform.ReadPlan(f); err != nil { t.Fatalf("err: %s", err) } }
func (c *ShowCommand) Run(args []string) int { var moduleDepth int args = c.Meta.process(args, false) cmdFlags := flag.NewFlagSet("show", flag.ContinueOnError) c.addModuleDepthFlag(cmdFlags, &moduleDepth) cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } if err := cmdFlags.Parse(args); err != nil { return 1 } args = cmdFlags.Args() if len(args) > 1 { c.Ui.Error( "The show command expects at most one argument with the path\n" + "to a Terraform state or plan file.\n") cmdFlags.Usage() return 1 } var planErr, stateErr error var path string var plan *terraform.Plan var state *terraform.State if len(args) > 0 { path = args[0] f, err := os.Open(path) if err != nil { c.Ui.Error(fmt.Sprintf("Error loading file: %s", err)) return 1 } defer f.Close() plan, err = terraform.ReadPlan(f) if err != nil { if _, err := f.Seek(0, 0); err != nil { c.Ui.Error(fmt.Sprintf("Error reading file: %s", err)) return 1 } plan = nil planErr = err } if plan == nil { state, err = terraform.ReadState(f) if err != nil { stateErr = err } } } else { stateOpts := c.StateOpts() stateOpts.RemoteCacheOnly = true result, err := State(stateOpts) if err != nil { c.Ui.Error(fmt.Sprintf("Error reading state: %s", err)) return 1 } state = result.State.State() if state == nil { c.Ui.Output("No state.") return 0 } } if plan == nil && state == nil { c.Ui.Error(fmt.Sprintf( "Terraform couldn't read the given file as a state or plan file.\n"+ "The errors while attempting to read the file as each format are\n"+ "shown below.\n\n"+ "State read error: %s\n\nPlan read error: %s", stateErr, planErr)) return 1 } if plan != nil { c.Ui.Output(FormatPlan(&FormatPlanOpts{ Plan: plan, Color: c.Colorize(), ModuleDepth: moduleDepth, })) return 0 } c.Ui.Output(FormatState(&FormatStateOpts{ State: state, Color: c.Colorize(), ModuleDepth: moduleDepth, })) return 0 }
// Context returns a Terraform Context taking into account the context // options used to initialize this meta configuration. func (m *Meta) Context(copts contextOpts) (*terraform.Context, bool, error) { opts := m.contextOpts() // First try to just read the plan directly from the path given. f, err := os.Open(copts.Path) if err == nil { plan, err := terraform.ReadPlan(f) f.Close() if err == nil { // Setup our state state, statePath, err := StateFromPlan(m.statePath, plan) if err != nil { return nil, false, fmt.Errorf("Error loading plan: %s", err) } // Set our state m.state = state m.stateOutPath = statePath if len(m.variables) > 0 { return nil, false, fmt.Errorf( "You can't set variables with the '-var' or '-var-file' flag\n" + "when you're applying a plan file. The variables used when\n" + "the plan was created will be used. If you wish to use different\n" + "variable values, create a new plan file.") } return plan.Context(opts), true, nil } } // Load the statePath if not given if copts.StatePath != "" { m.statePath = copts.StatePath } // Tell the context if we're in a destroy plan / apply opts.Destroy = copts.Destroy // Store the loaded state state, err := m.State() if err != nil { return nil, false, err } // Load the root module mod, err := module.NewTreeModule("", copts.Path) if err != nil { return nil, false, fmt.Errorf("Error loading config: %s", err) } err = mod.Load(m.moduleStorage(m.DataDir()), copts.GetMode) if err != nil { return nil, false, fmt.Errorf("Error downloading modules: %s", err) } opts.Module = mod opts.Parallelism = copts.Parallelism opts.State = state.State() ctx := terraform.NewContext(opts) return ctx, false, nil }