kubeform.dev/terraform-backend-sdk@v0.0.0-20220310143633-45f07fe731c5/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  	"kubeform.dev/terraform-backend-sdk/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  		return nil, diags
    25  	}
    26  
    27  	cfg, cDiags := configs.BuildConfig(rootMod, configs.ModuleWalkerFunc(l.moduleWalkerLoad))
    28  	diags = append(diags, cDiags...)
    29  
    30  	return cfg, diags
    31  }
    32  
    33  // moduleWalkerLoad is a configs.ModuleWalkerFunc for loading modules that
    34  // are presumed to have already been installed.
    35  func (l *Loader) moduleWalkerLoad(req *configs.ModuleRequest) (*configs.Module, *version.Version, hcl.Diagnostics) {
    36  	// Since we're just loading here, we expect that all referenced modules
    37  	// will be already installed and described in our manifest. However, we
    38  	// do verify that the manifest and the configuration are in agreement
    39  	// so that we can prompt the user to run "terraform init" if not.
    40  
    41  	key := l.modules.manifest.ModuleKey(req.Path)
    42  	record, exists := l.modules.manifest[key]
    43  
    44  	if !exists {
    45  		return nil, nil, hcl.Diagnostics{
    46  			{
    47  				Severity: hcl.DiagError,
    48  				Summary:  "Module not installed",
    49  				Detail:   "This module is not yet installed. Run \"terraform init\" to install all modules required by this configuration.",
    50  				Subject:  &req.CallRange,
    51  			},
    52  		}
    53  	}
    54  
    55  	var diags hcl.Diagnostics
    56  
    57  	// Check for inconsistencies between manifest and config
    58  	if req.SourceAddr.String() != record.SourceAddr {
    59  		diags = append(diags, &hcl.Diagnostic{
    60  			Severity: hcl.DiagError,
    61  			Summary:  "Module source has changed",
    62  			Detail:   "The source address was changed since this module was installed. Run \"terraform init\" to install all modules required by this configuration.",
    63  			Subject:  &req.SourceAddrRange,
    64  		})
    65  	}
    66  	if len(req.VersionConstraint.Required) > 0 && record.Version == nil {
    67  		diags = append(diags, &hcl.Diagnostic{
    68  			Severity: hcl.DiagError,
    69  			Summary:  "Module version requirements have changed",
    70  			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.",
    71  			Subject:  &req.SourceAddrRange,
    72  		})
    73  	}
    74  	if record.Version != nil && !req.VersionConstraint.Required.Check(record.Version) {
    75  		diags = append(diags, &hcl.Diagnostic{
    76  			Severity: hcl.DiagError,
    77  			Summary:  "Module version requirements have changed",
    78  			Detail: fmt.Sprintf(
    79  				"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.",
    80  				record.Version,
    81  			),
    82  			Subject: &req.SourceAddrRange,
    83  		})
    84  	}
    85  
    86  	mod, mDiags := l.parser.LoadConfigDir(record.Dir)
    87  	diags = append(diags, mDiags...)
    88  	if mod == nil {
    89  		// nil specifically indicates that the directory does not exist or
    90  		// cannot be read, so in this case we'll discard any generic diagnostics
    91  		// returned from LoadConfigDir and produce our own context-sensitive
    92  		// error message.
    93  		return nil, nil, hcl.Diagnostics{
    94  			{
    95  				Severity: hcl.DiagError,
    96  				Summary:  "Module not installed",
    97  				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),
    98  				Subject:  &req.CallRange,
    99  			},
   100  		}
   101  	}
   102  
   103  	return mod, record.Version, diags
   104  }