diff options
Diffstat (limited to 'vendor/github.com/hashicorp/terraform/internal/earlyconfig/config.go')
-rw-r--r-- | vendor/github.com/hashicorp/terraform/internal/earlyconfig/config.go | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/terraform/internal/earlyconfig/config.go b/vendor/github.com/hashicorp/terraform/internal/earlyconfig/config.go new file mode 100644 index 0000000..a9b8f98 --- /dev/null +++ b/vendor/github.com/hashicorp/terraform/internal/earlyconfig/config.go | |||
@@ -0,0 +1,123 @@ | |||
1 | package earlyconfig | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | "sort" | ||
6 | |||
7 | version "github.com/hashicorp/go-version" | ||
8 | "github.com/hashicorp/terraform-config-inspect/tfconfig" | ||
9 | "github.com/hashicorp/terraform/addrs" | ||
10 | "github.com/hashicorp/terraform/moduledeps" | ||
11 | "github.com/hashicorp/terraform/plugin/discovery" | ||
12 | "github.com/hashicorp/terraform/tfdiags" | ||
13 | ) | ||
14 | |||
15 | // A Config is a node in the tree of modules within a configuration. | ||
16 | // | ||
17 | // The module tree is constructed by following ModuleCall instances recursively | ||
18 | // through the root module transitively into descendent modules. | ||
19 | type Config struct { | ||
20 | // RootModule points to the Config for the root module within the same | ||
21 | // module tree as this module. If this module _is_ the root module then | ||
22 | // this is self-referential. | ||
23 | Root *Config | ||
24 | |||
25 | // ParentModule points to the Config for the module that directly calls | ||
26 | // this module. If this is the root module then this field is nil. | ||
27 | Parent *Config | ||
28 | |||
29 | // Path is a sequence of module logical names that traverse from the root | ||
30 | // module to this config. Path is empty for the root module. | ||
31 | // | ||
32 | // This should only be used to display paths to the end-user in rare cases | ||
33 | // where we are talking about the static module tree, before module calls | ||
34 | // have been resolved. In most cases, an addrs.ModuleInstance describing | ||
35 | // a node in the dynamic module tree is better, since it will then include | ||
36 | // any keys resulting from evaluating "count" and "for_each" arguments. | ||
37 | Path addrs.Module | ||
38 | |||
39 | // ChildModules points to the Config for each of the direct child modules | ||
40 | // called from this module. The keys in this map match the keys in | ||
41 | // Module.ModuleCalls. | ||
42 | Children map[string]*Config | ||
43 | |||
44 | // Module points to the object describing the configuration for the | ||
45 | // various elements (variables, resources, etc) defined by this module. | ||
46 | Module *tfconfig.Module | ||
47 | |||
48 | // CallPos is the source position for the header of the module block that | ||
49 | // requested this module. | ||
50 | // | ||
51 | // This field is meaningless for the root module, where its contents are undefined. | ||
52 | CallPos tfconfig.SourcePos | ||
53 | |||
54 | // SourceAddr is the source address that the referenced module was requested | ||
55 | // from, as specified in configuration. | ||
56 | // | ||
57 | // This field is meaningless for the root module, where its contents are undefined. | ||
58 | SourceAddr string | ||
59 | |||
60 | // Version is the specific version that was selected for this module, | ||
61 | // based on version constraints given in configuration. | ||
62 | // | ||
63 | // This field is nil if the module was loaded from a non-registry source, | ||
64 | // since versions are not supported for other sources. | ||
65 | // | ||
66 | // This field is meaningless for the root module, where it will always | ||
67 | // be nil. | ||
68 | Version *version.Version | ||
69 | } | ||
70 | |||
71 | // ProviderDependencies returns the provider dependencies for the recieving | ||
72 | // config, including all of its descendent modules. | ||
73 | func (c *Config) ProviderDependencies() (*moduledeps.Module, tfdiags.Diagnostics) { | ||
74 | var diags tfdiags.Diagnostics | ||
75 | |||
76 | var name string | ||
77 | if len(c.Path) > 0 { | ||
78 | name = c.Path[len(c.Path)-1] | ||
79 | } | ||
80 | |||
81 | ret := &moduledeps.Module{ | ||
82 | Name: name, | ||
83 | } | ||
84 | |||
85 | providers := make(moduledeps.Providers) | ||
86 | for name, reqs := range c.Module.RequiredProviders { | ||
87 | inst := moduledeps.ProviderInstance(name) | ||
88 | var constraints version.Constraints | ||
89 | for _, reqStr := range reqs { | ||
90 | if reqStr != "" { | ||
91 | constraint, err := version.NewConstraint(reqStr) | ||
92 | if err != nil { | ||
93 | diags = diags.Append(wrapDiagnostic(tfconfig.Diagnostic{ | ||
94 | Severity: tfconfig.DiagError, | ||
95 | Summary: "Invalid provider version constraint", | ||
96 | Detail: fmt.Sprintf("Invalid version constraint %q for provider %s.", reqStr, name), | ||
97 | })) | ||
98 | continue | ||
99 | } | ||
100 | constraints = append(constraints, constraint...) | ||
101 | } | ||
102 | } | ||
103 | providers[inst] = moduledeps.ProviderDependency{ | ||
104 | Constraints: discovery.NewConstraints(constraints), | ||
105 | Reason: moduledeps.ProviderDependencyExplicit, | ||
106 | } | ||
107 | } | ||
108 | ret.Providers = providers | ||
109 | |||
110 | childNames := make([]string, 0, len(c.Children)) | ||
111 | for name := range c.Children { | ||
112 | childNames = append(childNames, name) | ||
113 | } | ||
114 | sort.Strings(childNames) | ||
115 | |||
116 | for _, name := range childNames { | ||
117 | child, childDiags := c.Children[name].ProviderDependencies() | ||
118 | ret.Children = append(ret.Children, child) | ||
119 | diags = diags.Append(childDiags) | ||
120 | } | ||
121 | |||
122 | return ret, diags | ||
123 | } | ||