Example #1
0
func (c *walkContext) computeModuleVariable(
	v *config.ModuleVariable) (string, error) {
	// Build the path to our child
	path := make([]string, len(c.Path), len(c.Path)+1)
	copy(path, c.Path)
	path = append(path, v.Name)

	// Grab some locks
	c.Context.sl.RLock()
	defer c.Context.sl.RUnlock()

	// Get that module from our state
	mod := c.Context.state.ModuleByPath(path)
	if mod == nil {
		return "", fmt.Errorf(
			"Module '%s' not found for variable '%s'",
			strings.Join(path[1:], "."),
			v.FullKey())
	}

	value, ok := mod.Outputs[v.Field]
	if !ok {
		return "", fmt.Errorf(
			"Output field '%s' not found for variable '%s'",
			v.Field,
			v.FullKey())
	}

	return value, nil
}
Example #2
0
func (i *Interpolater) valueModuleVar(
	scope *InterpolationScope,
	n string,
	v *config.ModuleVariable,
	result map[string]ast.Variable) error {

	// Build the path to the child module we want
	path := make([]string, len(scope.Path), len(scope.Path)+1)
	copy(path, scope.Path)
	path = append(path, v.Name)

	// Grab the lock so that if other interpolations are running or
	// state is being modified, we'll be safe.
	i.StateLock.RLock()
	defer i.StateLock.RUnlock()

	// Get the module where we're looking for the value
	mod := i.State.ModuleByPath(path)
	if mod == nil {
		// If the module doesn't exist, then we can return an empty string.
		// This happens usually only in Refresh() when we haven't populated
		// a state. During validation, we semantically verify that all
		// modules reference other modules, and graph ordering should
		// ensure that the module is in the state, so if we reach this
		// point otherwise it really is a panic.
		result[n] = unknownVariable()

		// During apply this is always an error
		if i.Operation == walkApply {
			return fmt.Errorf(
				"Couldn't find module %q for var: %s",
				v.Name, v.FullKey())
		}
	} else {
		// Get the value from the outputs
		if outputState, ok := mod.Outputs[v.Field]; ok {
			output, err := hil.InterfaceToVariable(outputState.Value)
			if err != nil {
				return err
			}
			result[n] = output
		} else {
			// Same reasons as the comment above.
			result[n] = unknownVariable()

			// During apply this is always an error
			if i.Operation == walkApply {
				return fmt.Errorf(
					"Couldn't find output %q for module var: %s",
					v.Field, v.FullKey())
			}
		}
	}

	return nil
}