Skip to content

Commit ae4207f

Browse files
committed
include warnings on using deprecated modules w/ mocked data
1 parent 661bfb7 commit ae4207f

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

internal/initwd/module_install.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import (
88
"errors"
99
"fmt"
1010
"log"
11+
"math/rand"
1112
"os"
1213
"path"
1314
"path/filepath"
1415
"strings"
16+
"time"
1517

1618
"github.com/apparentlymart/go-versions/versions"
1719
version "github.com/hashicorp/go-version"
@@ -58,6 +60,23 @@ func NewModuleInstaller(modsDir string, loader *configload.Loader, reg *registry
5860
}
5961
}
6062

63+
// mdTODO: remove this later, only for iteration while the api hasn't been updated.
64+
func injectMockDeprecations(modules *response.ModuleVersions) {
65+
//jsonBytes, _ := json.MarshalIndent(modules, "", " ")
66+
//log.Printf("[DEBUG] submodule!!!: %s ", string(jsonBytes))
67+
//log.Printf("[DEBUG] __________________________________________________")
68+
for _, module := range modules.Modules {
69+
for _, version := range module.Versions {
70+
// Inject a mock deprecation into each version
71+
version.Deprecation = response.Deprecation{
72+
Deprecated: true,
73+
Message: "Mock deprecation message for: ",
74+
ExternalLink: "https://example.com/mock-deprecation",
75+
}
76+
}
77+
}
78+
}
79+
6180
// InstallModules analyses the root module in the given directory and installs
6281
// all of its direct and transitive dependencies into the given modules
6382
// directory, which must already exist.
@@ -253,6 +272,26 @@ func (i *ModuleInstaller) moduleInstallWalker(ctx context.Context, manifest mods
253272
}
254273

255274
log.Printf("[TRACE] ModuleInstaller: Module installer: %s %s already installed in %s", key, record.Version, record.Dir)
275+
276+
// Checking for module deprecations in the case no new module versions need installation
277+
if addr, isRegistryModule := req.SourceAddr.(addrs.ModuleSourceRegistry); isRegistryModule {
278+
regClient := i.reg
279+
280+
regsrcAddr := regsrc.ModuleFromRegistryPackageAddr(addr.Package)
281+
resp, err := regClient.ModuleVersions(ctx, regsrcAddr)
282+
283+
// mdTODO: remove this later on
284+
injectMockDeprecations(resp)
285+
286+
if err != nil {
287+
log.Printf("[DEBUG] Deprecation for %s could not be checked: call to registry failed", addr.Package.Namespace)
288+
289+
} else {
290+
modDeprecations := collectModuleDeprecationWarnings(resp.Modules, record.Version, req.CallRange.Ptr())
291+
diags = diags.Extend(modDeprecations)
292+
}
293+
}
294+
256295
return mod, record.Version, diags
257296
}
258297
}
@@ -434,6 +473,8 @@ func (i *ModuleInstaller) installRegistryModule(ctx context.Context, req *config
434473
var err error
435474
log.Printf("[DEBUG] %s listing available versions of %s at %s", key, addr, hostname)
436475
resp, err = reg.ModuleVersions(ctx, regsrcAddr)
476+
// mdTODO: remove this
477+
injectMockDeprecations(resp)
437478
if err != nil {
438479
if registry.IsModuleNotFound(err) {
439480
diags = diags.Append(&hcl.Diagnostic{
@@ -570,6 +611,9 @@ func (i *ModuleInstaller) installRegistryModule(ctx context.Context, req *config
570611
return nil, nil, diags
571612
}
572613

614+
modDeprecations := collectModuleDeprecationWarnings(resp.Modules, latestMatch, req.CallRange.Ptr())
615+
diags = diags.Extend(modDeprecations)
616+
573617
// Report up to the caller that we're about to start downloading.
574618
hooks.Download(key, packageAddr.String(), latestMatch)
575619

@@ -921,6 +965,37 @@ func maybeImproveLocalInstallError(req *configs.ModuleRequest, diags hcl.Diagnos
921965
return diags
922966
}
923967

968+
// mdTODO: not actually needed, just used to Separate out warnings where the source is the same
969+
func randomString(length int, charset string) string {
970+
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
971+
b := make([]byte, length)
972+
for i := range b {
973+
b[i] = charset[seededRand.Intn(len(charset))]
974+
}
975+
return string(b)
976+
}
977+
978+
func collectModuleDeprecationWarnings(moduleVersions []*response.ModuleProviderVersions, targetVersion *version.Version, subject *hcl.Range) hcl.Diagnostics {
979+
var moduleDeprecationDiags hcl.Diagnostics
980+
for _, module := range moduleVersions {
981+
for _, moduleVersion := range module.Versions {
982+
v, _ := version.NewVersion(moduleVersion.Version)
983+
if targetVersion.Equal(v) {
984+
if moduleVersion.Deprecation.Deprecated {
985+
moduleDeprecationDiags = moduleDeprecationDiags.Append(&hcl.Diagnostic{
986+
Severity: hcl.DiagWarning,
987+
Summary: moduleVersion.Deprecation.Message + module.Source + randomString(10, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"), // mdTODO: fix this up when mocking is not needed
988+
Detail: moduleVersion.Deprecation.ExternalLink,
989+
Subject: subject,
990+
})
991+
}
992+
return moduleDeprecationDiags
993+
}
994+
}
995+
}
996+
return moduleDeprecationDiags
997+
}
998+
924999
func splitAddrSubdir(addr addrs.ModuleSource) (string, string) {
9251000
switch addr := addr.(type) {
9261001
case addrs.ModuleSourceRegistry:

0 commit comments

Comments
 (0)