github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/steampipeconfig/parse/workspace_profile_parse_context.go (about)

     1  package parse
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/hcl/v2"
     6  	"github.com/turbot/steampipe/pkg/steampipeconfig/modconfig"
     7  	"github.com/zclconf/go-cty/cty"
     8  )
     9  
    10  type WorkspaceProfileParseContext struct {
    11  	ParseContext
    12  	workspaceProfiles map[string]*modconfig.WorkspaceProfile
    13  	valueMap          map[string]cty.Value
    14  }
    15  
    16  func NewWorkspaceProfileParseContext(rootEvalPath string) *WorkspaceProfileParseContext {
    17  	parseContext := NewParseContext(rootEvalPath)
    18  	// TODO uncomment once https://github.com/turbot/steampipe/issues/2640 is done
    19  	//parseContext.BlockTypes = []string{modconfig.BlockTypeWorkspaceProfile}
    20  	c := &WorkspaceProfileParseContext{
    21  		ParseContext:      parseContext,
    22  		workspaceProfiles: make(map[string]*modconfig.WorkspaceProfile),
    23  		valueMap:          make(map[string]cty.Value),
    24  	}
    25  
    26  	c.buildEvalContext()
    27  
    28  	return c
    29  }
    30  
    31  // AddResource stores this resource as a variable to be added to the eval context. It alse
    32  func (c *WorkspaceProfileParseContext) AddResource(workspaceProfile *modconfig.WorkspaceProfile) hcl.Diagnostics {
    33  	ctyVal, err := workspaceProfile.CtyValue()
    34  	if err != nil {
    35  		return hcl.Diagnostics{&hcl.Diagnostic{
    36  			Severity: hcl.DiagError,
    37  			Summary:  fmt.Sprintf("failed to convert workspaceProfile '%s' to its cty value", workspaceProfile.ProfileName),
    38  			Detail:   err.Error(),
    39  			Subject:  &workspaceProfile.DeclRange,
    40  		}}
    41  	}
    42  
    43  	c.workspaceProfiles[workspaceProfile.ProfileName] = workspaceProfile
    44  	c.valueMap[workspaceProfile.ProfileName] = ctyVal
    45  
    46  	// remove this resource from unparsed blocks
    47  	delete(c.UnresolvedBlocks, workspaceProfile.ProfileName)
    48  
    49  	c.buildEvalContext()
    50  
    51  	return nil
    52  }
    53  
    54  func (c *WorkspaceProfileParseContext) buildEvalContext() {
    55  	// rebuild the eval context
    56  	// build a map with a single key - workspace
    57  	vars := map[string]cty.Value{
    58  		"workspace": cty.ObjectVal(c.valueMap),
    59  	}
    60  	c.ParseContext.buildEvalContext(vars)
    61  
    62  }