terraform/command/meta_new.go

180 lines
4.6 KiB
Go
Raw Normal View History

2017-01-19 05:50:45 +01:00
package command
import (
"fmt"
"log"
2017-01-19 05:50:45 +01:00
"os"
"path/filepath"
2017-01-19 05:50:45 +01:00
"strconv"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/terraform"
"github.com/hashicorp/terraform/tfdiags"
2017-01-19 05:50:45 +01:00
)
// NOTE: Temporary file until this branch is cleaned up.
// Input returns whether or not input asking is enabled.
func (m *Meta) Input() bool {
if test || !m.input {
return false
}
if envVar := os.Getenv(InputModeEnvVar); envVar != "" {
if v, err := strconv.ParseBool(envVar); err == nil && !v {
return false
}
}
return true
}
// Module loads the module tree for the given root path using the legacy
// configuration loader.
2017-01-19 05:50:45 +01:00
//
// It expects the modules to already be downloaded. This will never
// download any modules.
//
// The configuration is validated before returning, so the returned diagnostics
// may contain warnings and/or errors. If the diagnostics contains only
// warnings, the caller may treat the returned module.Tree as valid after
// presenting the warnings to the user.
func (m *Meta) Module(path string) (*module.Tree, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
2017-01-19 05:50:45 +01:00
mod, err := module.NewTreeModule("", path)
if err != nil {
// Check for the error where we have no config files
if errwrap.ContainsType(err, new(config.ErrNoConfigsFound)) {
return nil, nil
}
diags = diags.Append(err)
return nil, diags
2017-01-19 05:50:45 +01:00
}
err = mod.Load(m.moduleStorage(m.DataDir(), module.GetModeNone))
2017-01-19 05:50:45 +01:00
if err != nil {
diags = diags.Append(errwrap.Wrapf("Error loading modules: {{err}}", err))
return nil, diags
2017-01-19 05:50:45 +01:00
}
diags = diags.Append(mod.Validate())
return mod, diags
2017-01-19 05:50:45 +01:00
}
// Config loads the root config for the path specified, using the legacy
// configuration loader.
//
// Path may be a directory or file. The absence of configuration is not an
// error and returns a nil Config.
func (m *Meta) Config(path string) (*config.Config, error) {
// If no explicit path was given then it is okay for there to be
// no backend configuration found.
emptyOk := path == ""
// If we had no path set, it is an error. We can't initialize unset
if path == "" {
path = "."
}
// Expand the path
if !filepath.IsAbs(path) {
var err error
path, err = filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf(
"Error expanding path to backend config %q: %s", path, err)
}
}
log.Printf("[DEBUG] command: loading backend config file: %s", path)
// We first need to determine if we're loading a file or a directory.
fi, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) && emptyOk {
log.Printf(
"[INFO] command: backend config not found, returning nil: %s",
path)
return nil, nil
}
return nil, err
}
var f func(string) (*config.Config, error) = config.LoadFile
if fi.IsDir() {
f = config.LoadDir
}
// Load the configuration
c, err := f(path)
if err != nil {
// Check for the error where we have no config files and return nil
// as the configuration type.
if errwrap.ContainsType(err, new(config.ErrNoConfigsFound)) {
log.Printf(
"[INFO] command: backend config not found, returning nil: %s",
path)
return nil, nil
}
return nil, err
}
return c, nil
}
2017-01-19 05:50:45 +01:00
// Plan returns the plan for the given path.
//
// This only has an effect if the path itself looks like a plan.
// If error is nil and the plan is nil, then the path didn't look like
// a plan.
//
// Error will be non-nil if path looks like a plan and loading the plan
// failed.
func (m *Meta) Plan(path string) (*terraform.Plan, error) {
// Open the path no matter if its a directory or file
f, err := os.Open(path)
defer f.Close()
if err != nil {
return nil, fmt.Errorf(
"Failed to load Terraform configuration or plan: %s", err)
}
// Stat it so we can check if its a directory
fi, err := f.Stat()
if err != nil {
return nil, fmt.Errorf(
"Failed to load Terraform configuration or plan: %s", err)
}
// If this path is a directory, then it can't be a plan. Not an error.
if fi.IsDir() {
return nil, nil
}
// Read the plan
p, err := terraform.ReadPlan(f)
if err != nil {
return nil, err
}
// We do a validation here that seems odd but if any plan is given,
// we must not have set any extra variables. The plan itself contains
// the variables and those aren't overwritten.
terraform: ugly huge change to weave in new HCL2-oriented types Due to how deeply the configuration types go into Terraform Core, there isn't a great way to switch out to HCL2 gradually. As a consequence, this huge commit gets us from the old state to a _compilable_ new state, but does not yet attempt to fix any tests and has a number of known missing parts and bugs. We will continue to iterate on this in forthcoming commits, heading back towards passing tests and making Terraform fully-functional again. The three main goals here are: - Use the configuration models from the "configs" package instead of the older models in the "config" package, which is now deprecated and preserved only to help us write our migration tool. - Do expression inspection and evaluation using the functionality of the new "lang" package, instead of the Interpolator type and related functionality in the main "terraform" package. - Represent addresses of various objects using types in the addrs package, rather than hand-constructed strings. This is not critical to support the above, but was a big help during the implementation of these other points since it made it much more explicit what kind of address is expected in each context. Since our new packages are built to accommodate some future planned features that are not yet implemented (e.g. the "for_each" argument on resources, "count"/"for_each" on modules), and since there's still a fair amount of functionality still using old-style APIs, there is a moderate amount of shimming here to connect new assumptions with old, hopefully in a way that makes it easier to find and eliminate these shims later. I apologize in advance to the person who inevitably just found this huge commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
if len(m.variableArgs.AllItems()) > 0 {
2017-01-19 05:50:45 +01:00
return nil, 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 p, nil
}