github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/configs/configload/loader_load.go (about)

     1  package configload
     2  
     3  import (
     4  	"fmt"
     5  
     6  	version "github.com/hashicorp/go-version"
     7  	"github.com/hashicorp/hcl/v2"
     8  	"github.com/hashicorp/terraform/internal/configs"
     9  )
    10  
    11  // LoadConfig reads the Terraform module in the given directory and uses it as the
    12  // root module to build the static module tree that represents a configuration,
    13  // assuming that all required descendent modules have already been installed.
    14  //
    15  // If error diagnostics are returned, the returned configuration may be either
    16  // nil or incomplete. In the latter case, cautious static analysis is possible
    17  // in spite of the errors.
    18  //
    19  // LoadConfig performs the basic syntax and uniqueness validations that are
    20  // required to process the individual modules
    21  func (l *Loader) LoadConfig(rootDir string) (*configs.Config, hcl.Diagnostics) {
    22  	rootMod, diags := l.parser.LoadConfigDir(rootDir)
    23  	if rootMod == nil || diags.HasErrors() {
    24  		// Ensure we return any parsed modules here so that required_version
    25  		// constraints can be verified even when encountering errors.
    26  		cfg := &configs.Config{
    27  			Module: rootMod,
    28  		}
    29  
    30  		return cfg, diags
    31  	}
    32  
    33  	cfg, cDiags := configs.BuildConfig(rootMod, configs.ModuleWalkerFunc(l.moduleWalkerLoad))
    34  	diags = append(diags, cDiags...)
    35  
    36  	return cfg, diags
    37  }
    38  
    39  // moduleWalkerLoad is a configs.ModuleWalkerFunc for loading modules that
    40  // are presumed to have already been installed.
    41  func (l *Loader) moduleWalkerLoad(req *configs.ModuleRequest) (*configs.Module, *version.Version, hcl.Diagnostics) {
    42  	// Since we're just loading here, we expect that all referenced modules
    43  	// will be already installed and described in our manifest. However, we
    44  	// do verify that the manifest and the configuration are in agreement
    45  	// so that we can prompt the user to run "terraform init" if not.
    46  
    47  	key := l.modules.manifest.ModuleKey(req.Path)
    48  	record, exists := l.modules.manifest[key]
    49  
    50  	if !exists {
    51  		return nil, nil, hcl.Diagnostics{
    52  			{
    53  				Severity: hcl.DiagError,
    54  				Summary:  "Module not installed",
    55  				Detail:   "This module is not yet installed. Run \"terraform init\" to install all modules required by this configuration.",
    56  				Subject:  &req.CallRange,
    57  			},
    58  		}
    59  	}
    60  
    61  	var diags hcl.Diagnostics
    62  
    63  	// Check for inconsistencies between manifest and config.
    64  
    65  	// We ignore a nil SourceAddr here, which represents a failure during
    66  	// configuration parsing, and will be reported in a diagnostic elsewhere.
    67  	if req.SourceAddr != nil && req.SourceAddr.String() != record.SourceAddr {
    68  		diags = append(diags, &hcl.Diagnostic{
    69  			Severity: hcl.DiagError,
    70  			Summary:  "Module source has changed",
    71  			Detail:   "The source address was changed since this module was installed. Run \"terraform init\" to install all modules required by this configuration.",
    72  			Subject:  &req.SourceAddrRange,
    73  		})
    74  	}
    75  	if len(req.VersionConstraint.Required) > 0 && record.Version == nil {
    76  		diags = append(diags, &hcl.Diagnostic{
    77  			Severity: hcl.DiagError,
    78  			Summary:  "Module version requirements have changed",
    79  			Detail:   "The version requirements have changed since this module was installed and the installed version is no longer acceptable. Run \"terraform init\" to install all modules required by this configuration.",
    80  			Subject:  &req.SourceAddrRange,
    81  		})
    82  	}
    83  	if record.Version != nil && !req.VersionConstraint.Required.Check(record.Version) {
    84  		diags = append(diags, &hcl.Diagnostic{
    85  			Severity: hcl.DiagError,
    86  			Summary:  "Module version requirements have changed",
    87  			Detail: fmt.Sprintf(
    88  				"The version requirements have changed since this module was installed and the installed version (%s) is no longer acceptable. Run \"terraform init\" to install all modules required by this configuration.",
    89  				record.Version,
    90  			),
    91  			Subject: &req.SourceAddrRange,
    92  		})
    93  	}
    94  
    95  	mod, mDiags := l.parser.LoadConfigDir(record.Dir)
    96  	diags = append(diags, mDiags...)
    97  	if mod == nil {
    98  		// nil specifically indicates that the directory does not exist or
    99  		// cannot be read, so in this case we'll discard any generic diagnostics
   100  		// returned from LoadConfigDir and produce our own context-sensitive
   101  		// error message.
   102  		return nil, nil, hcl.Diagnostics{
   103  			{
   104  				Severity: hcl.DiagError,
   105  				Summary:  "Module not installed",
   106  				Detail:   fmt.Sprintf("This module's local cache directory %s could not be read. Run \"terraform init\" to install all modules required by this configuration.", record.Dir),
   107  				Subject:  &req.CallRange,
   108  			},
   109  		}
   110  	}
   111  
   112  	return mod, record.Version, diags
   113  }