@@ -8,10 +8,12 @@ import (
8
8
"errors"
9
9
"fmt"
10
10
"log"
11
+ "math/rand"
11
12
"os"
12
13
"path"
13
14
"path/filepath"
14
15
"strings"
16
+ "time"
15
17
16
18
"github.com/apparentlymart/go-versions/versions"
17
19
version "github.com/hashicorp/go-version"
@@ -58,6 +60,23 @@ func NewModuleInstaller(modsDir string, loader *configload.Loader, reg *registry
58
60
}
59
61
}
60
62
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
+
61
80
// InstallModules analyses the root module in the given directory and installs
62
81
// all of its direct and transitive dependencies into the given modules
63
82
// directory, which must already exist.
@@ -253,6 +272,26 @@ func (i *ModuleInstaller) moduleInstallWalker(ctx context.Context, manifest mods
253
272
}
254
273
255
274
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
+
256
295
return mod , record .Version , diags
257
296
}
258
297
}
@@ -434,6 +473,8 @@ func (i *ModuleInstaller) installRegistryModule(ctx context.Context, req *config
434
473
var err error
435
474
log .Printf ("[DEBUG] %s listing available versions of %s at %s" , key , addr , hostname )
436
475
resp , err = reg .ModuleVersions (ctx , regsrcAddr )
476
+ // mdTODO: remove this
477
+ injectMockDeprecations (resp )
437
478
if err != nil {
438
479
if registry .IsModuleNotFound (err ) {
439
480
diags = diags .Append (& hcl.Diagnostic {
@@ -570,6 +611,9 @@ func (i *ModuleInstaller) installRegistryModule(ctx context.Context, req *config
570
611
return nil , nil , diags
571
612
}
572
613
614
+ modDeprecations := collectModuleDeprecationWarnings (resp .Modules , latestMatch , req .CallRange .Ptr ())
615
+ diags = diags .Extend (modDeprecations )
616
+
573
617
// Report up to the caller that we're about to start downloading.
574
618
hooks .Download (key , packageAddr .String (), latestMatch )
575
619
@@ -921,6 +965,37 @@ func maybeImproveLocalInstallError(req *configs.ModuleRequest, diags hcl.Diagnos
921
965
return diags
922
966
}
923
967
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
+
924
999
func splitAddrSubdir (addr addrs.ModuleSource ) (string , string ) {
925
1000
switch addr := addr .(type ) {
926
1001
case addrs.ModuleSourceRegistry :
0 commit comments