mirror of
https://github.com/opentofu/opentofu.git
synced 2025-02-25 18:45:20 -06:00
terraform: Adding module lookups for state
This commit is contained in:
parent
ab7ae0516c
commit
a85d6fa6c3
@ -131,15 +131,17 @@ func (c *Context) Apply() (*State, error) {
|
|||||||
c.state.prune()
|
c.state.prune()
|
||||||
|
|
||||||
// If we have no errors, then calculate the outputs if we have any
|
// If we have no errors, then calculate the outputs if we have any
|
||||||
if err == nil && len(c.config.Outputs) > 0 && len(c.state.Resources) > 0 {
|
if err == nil && len(c.config.Outputs) > 0 {
|
||||||
c.state.Outputs = make(map[string]string)
|
outputs := make(map[string]string)
|
||||||
for _, o := range c.config.Outputs {
|
for _, o := range c.config.Outputs {
|
||||||
if err = c.computeVars(o.RawConfig); err != nil {
|
if err = c.computeVars(o.RawConfig); err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
outputs[o.Name] = o.RawConfig.Config()["value"].(string)
|
||||||
c.state.Outputs[o.Name] = o.RawConfig.Config()["value"].(string)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Assign the outputs to the root module
|
||||||
|
c.state.RootModule().Outputs = outputs
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.state, err
|
return c.state, err
|
||||||
|
@ -7,6 +7,9 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// rootModulePath is the path of the root module
|
||||||
|
var rootModulePath = []string{"root"}
|
||||||
|
|
||||||
// State keeps track of a snapshot state-of-the-world that Terraform
|
// State keeps track of a snapshot state-of-the-world that Terraform
|
||||||
// can use to keep track of what real world resources it is actually
|
// can use to keep track of what real world resources it is actually
|
||||||
// managing. This is the latest format as of Terraform 0.3
|
// managing. This is the latest format as of Terraform 0.3
|
||||||
@ -23,6 +26,23 @@ type State struct {
|
|||||||
Modules []*ModuleState `json:"modules"`
|
Modules []*ModuleState `json:"modules"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ModuleByPath is used to lookup the module state for the given path.
|
||||||
|
// This should be the prefered lookup mechanism as it allows for future
|
||||||
|
// lookup optimizations.
|
||||||
|
func (s *State) ModuleByPath(path []string) *ModuleState {
|
||||||
|
for _, mod := range s.Modules {
|
||||||
|
if reflect.Equal(mod.Path, path) {
|
||||||
|
return mod
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// RootModule returns the ModuleState for the root module
|
||||||
|
func (s *State) RootModule() *ModuleState {
|
||||||
|
return s.ModuleByPath(rootModulePath)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *State) deepcopy() *State {
|
func (s *State) deepcopy() *State {
|
||||||
n := &State{
|
n := &State{
|
||||||
Version: n.Version,
|
Version: n.Version,
|
||||||
|
Loading…
Reference in New Issue
Block a user