Skip to content

Commit

Permalink
virtual benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
binaek committed Sep 21, 2023
1 parent 4184308 commit 9695bbe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
28 changes: 21 additions & 7 deletions pkg/control/controlexecute/execution_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/spf13/viper"
"github.com/turbot/go-kit/helpers"
"github.com/turbot/steampipe-plugin-sdk/v5/sperr"
"github.com/turbot/steampipe/pkg/connection_sync"
"github.com/turbot/steampipe/pkg/constants"
"github.com/turbot/steampipe/pkg/control/controlstatus"
Expand Down Expand Up @@ -40,6 +41,10 @@ type ExecutionTree struct {
}

func NewExecutionTree(ctx context.Context, workspace *workspace.Workspace, client db_common.Client, controlFilterWhereClause string, args ...string) (*ExecutionTree, error) {
if len(args) < 1 {
return nil, sperr.New("need at least one argument to create a check execution tree")
}

searchPath := client.GetRequiredSessionSearchPath()

// now populate the ExecutionTree
Expand All @@ -56,18 +61,27 @@ func NewExecutionTree(ctx context.Context, workspace *workspace.Workspace, clien
return nil, err
}

rootItems := []modconfig.ModTreeItem{}
for _, arg := range args {
// now identify the root item of the control list
rootItem, err := executionTree.getExecutionRootFromArg(arg)
var resolvedItem modconfig.ModTreeItem
if len(args) == 1 {
resolvedItem, err = executionTree.getExecutionRootFromArg(args[0])
if err != nil {
return nil, err
}
rootItems = append(rootItems, rootItem)
}
} else {
items := []modconfig.ModTreeItem{}
for _, arg := range args {
item, err := executionTree.getExecutionRootFromArg(arg)
if err != nil {
return nil, err
}
items = append(items, item)
}

// create a virtual benchmark with `items` as it's children
resolvedItem = modconfig.NewVirtualBenchmarkWithChildren(workspace.Mod, items).(modconfig.ModTreeItem)
}
// build tree of result groups, starting with a synthetic 'root' node
executionTree.Root = NewRootResultGroup(ctx, executionTree, rootItems...)
executionTree.Root = NewRootResultGroup(ctx, executionTree, resolvedItem)

// after tree has built, ControlCount will be set - create progress rendered
executionTree.Progress = controlstatus.NewControlProgress(len(executionTree.ControlRuns))
Expand Down
31 changes: 10 additions & 21 deletions pkg/control/controlexecute/result_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,7 @@ func NewGroupSummary() *GroupSummary {
}

// NewRootResultGroup creates a ResultGroup to act as the root node of a control execution tree
func NewRootResultGroup(ctx context.Context, executionTree *ExecutionTree, rootItems ...modconfig.ModTreeItem) *ResultGroup {
var title string
var rootGroupItem modconfig.ModTreeItem
if len(rootItems) == 1 {
// there's only one - use that title
title = rootItems[0].GetTitle()
rootGroupItem = rootItems[0]
}

func NewRootResultGroup(ctx context.Context, executionTree *ExecutionTree, rootItem modconfig.ModTreeItem) *ResultGroup {
root := &ResultGroup{
GroupId: RootResultGroupName,
Groups: []*ResultGroup{},
Expand All @@ -84,20 +76,17 @@ func NewRootResultGroup(ctx context.Context, executionTree *ExecutionTree, rootI
Severity: make(map[string]controlstatus.StatusSummary),
updateLock: new(sync.Mutex),
NodeType: modconfig.BlockTypeBenchmark,
Title: title,
GroupItem: rootGroupItem,
Title: rootItem.GetTitle(),
}

for _, rootItem := range rootItems {
if control, ok := rootItem.(*modconfig.Control); ok {
// if root item is a control, add control run
executionTree.AddControl(ctx, control, root)
} else {
// create a result group for this item
// if root item is a benchmark, create new result group with root as parent
itemGroup := NewResultGroup(ctx, executionTree, rootItem, root)
root.addResultGroup(itemGroup)
}
// if root item is a benchmark, create new result group with root as parent
if control, ok := rootItem.(*modconfig.Control); ok {
// if root item is a control, add control run
executionTree.AddControl(ctx, control, root)
} else {
// create a result group for this item
itemGroup := NewResultGroup(ctx, executionTree, rootItem, root)
root.addResultGroup(itemGroup)
}

return root
Expand Down
19 changes: 18 additions & 1 deletion pkg/steampipeconfig/modconfig/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package modconfig

import (
"fmt"
"github.com/zclconf/go-cty/cty"
"sort"
"strings"

"github.com/hashicorp/hcl/v2"
"github.com/turbot/go-kit/types"
typehelpers "github.com/turbot/go-kit/types"
"github.com/turbot/steampipe/pkg/utils"
"github.com/zclconf/go-cty/cty"
)

// Benchmark is a struct representing the Benchmark resource
Expand All @@ -32,6 +32,23 @@ type Benchmark struct {
Display *string `cty:"display" hcl:"display" json:"-"`
}

func NewVirtualBenchmarkWithChildren(mod *Mod, children []ModTreeItem) HclResource {
fullName := fmt.Sprintf("%s.%s.%s", mod.ShortName, "benchmark", "virtual")
benchmark := &Benchmark{
ModTreeItemImpl: ModTreeItemImpl{
HclResourceImpl: HclResourceImpl{
ShortName: "virtual",
FullName: fullName,
UnqualifiedName: fmt.Sprintf("%s.%s", "benchmark", "virtual"),
blockType: "benchmark",
},
Mod: mod,
},
}
benchmark.children = append(benchmark.children, children...)
return benchmark
}

func NewBenchmark(block *hcl.Block, mod *Mod, shortName string) HclResource {
fullName := fmt.Sprintf("%s.%s.%s", mod.ShortName, block.Type, shortName)
benchmark := &Benchmark{
Expand Down

0 comments on commit 9695bbe

Please sign in to comment.