github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/dashboard/dashboardexecute/referenced_variables.go (about)

     1  package dashboardexecute
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/turbot/steampipe/pkg/dashboard/dashboardtypes"
     8  	"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
     9  	"github.com/turbot/steampipe/pkg/workspace"
    10  )
    11  
    12  // GetReferencedVariables builds map of variables values containing only those mod variables which are referenced
    13  // NOTE: we refer to variables in dependency mods in the format which is valid for an SPVARS filer, i.e.
    14  // <mod>.<var-name>
    15  // the VariableValues map will contain these variables with the name format <mod>.var.<var-name>,
    16  // so we must convert the name
    17  func GetReferencedVariables(root dashboardtypes.DashboardTreeRun, w *workspace.Workspace) map[string]string {
    18  	var referencedVariables = make(map[string]string)
    19  
    20  	addReferencedVars := func(refs []*modconfig.ResourceReference) {
    21  		for _, ref := range refs {
    22  			parts := strings.Split(ref.To, ".")
    23  			if len(parts) == 2 && parts[0] == "var" {
    24  				varName := parts[1]
    25  				varValueName := varName
    26  				// NOTE: if the ref is NOT for the workspace mod, then use the qualified variable name
    27  				// (e.g. aws_insights.var.v1)
    28  				if refMod := ref.GetMetadata().ModName; refMod != w.Mod.ShortName {
    29  					varValueName = fmt.Sprintf("%s.var.%s", refMod, varName)
    30  					varName = fmt.Sprintf("%s.%s", refMod, varName)
    31  				}
    32  				referencedVariables[varName] = w.VariableValues[varValueName]
    33  			}
    34  		}
    35  	}
    36  
    37  	switch r := root.(type) {
    38  	case *DashboardRun:
    39  		//nolint:errcheck // we don't care about errors here, since the callback does not return an error
    40  		r.dashboard.WalkResources(
    41  			func(resource modconfig.HclResource) (bool, error) {
    42  				if resourceWithMetadata, ok := resource.(modconfig.ResourceWithMetadata); ok {
    43  					addReferencedVars(resourceWithMetadata.GetReferences())
    44  				}
    45  				return true, nil
    46  			},
    47  		)
    48  	case *CheckRun:
    49  		switch n := r.resource.(type) {
    50  		case *modconfig.Benchmark:
    51  			//nolint:errcheck // we don't care about errors here, since the callback does not return an error
    52  			n.WalkResources(
    53  				func(resource modconfig.ModTreeItem) (bool, error) {
    54  					if resourceWithMetadata, ok := resource.(modconfig.ResourceWithMetadata); ok {
    55  						addReferencedVars(resourceWithMetadata.GetReferences())
    56  					}
    57  					return true, nil
    58  				},
    59  			)
    60  		case *modconfig.Control:
    61  			addReferencedVars(n.GetReferences())
    62  		}
    63  	}
    64  
    65  	return referencedVariables
    66  }