-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea10f55
commit 6861226
Showing
46 changed files
with
6,542 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
package resources | ||
|
||
import ( | ||
"fmt" | ||
"github.com/turbot/pipe-fittings/modconfig" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
"github.com/turbot/go-kit/types" | ||
typehelpers "github.com/turbot/go-kit/types" | ||
"github.com/turbot/pipe-fittings/cty_helpers" | ||
"github.com/turbot/pipe-fittings/printers" | ||
"github.com/turbot/pipe-fittings/utils" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
// Benchmark is a struct representing the Benchmark resource | ||
type Benchmark struct { | ||
modconfig.ResourceWithMetadataImpl | ||
modconfig.ModTreeItemImpl | ||
|
||
// required to allow partial decoding | ||
Remain hcl.Body `hcl:",remain" json:"-"` | ||
|
||
// child names as NamedItem structs - used to allow setting children via the 'children' property | ||
ChildNames modconfig.NamedItemList `cty:"child_names" json:"-"` | ||
// used for introspection tables | ||
ChildNameStrings []string `cty:"child_name_strings" json:"children,omitempty"` | ||
|
||
// dashboard specific properties | ||
Base *Benchmark `hcl:"base" json:"-"` | ||
Width *int `cty:"width" hcl:"width" json:"width,omitempty"` | ||
Type *string `cty:"type" hcl:"type" json:"type,omitempty"` | ||
Display *string `cty:"display" hcl:"display" json:"display,omitempty"` | ||
} | ||
|
||
func NewRootBenchmarkWithChildren(mod *modconfig.Mod, children []modconfig.ModTreeItem) modconfig.HclResource { | ||
fullName := fmt.Sprintf("%s.%s.%s", mod.ShortName, "benchmark", "root") | ||
benchmark := &Benchmark{ | ||
ModTreeItemImpl: modconfig.ModTreeItemImpl{ | ||
HclResourceImpl: modconfig.HclResourceImpl{ | ||
ShortName: "root", | ||
FullName: fullName, | ||
UnqualifiedName: fmt.Sprintf("%s.%s", "benchmark", "root"), | ||
BlockType: "benchmark", | ||
}, | ||
Mod: mod, | ||
}, | ||
} | ||
|
||
benchmark.AddChild(children...) | ||
return benchmark | ||
} | ||
|
||
func NewBenchmark(block *hcl.Block, mod *modconfig.Mod, shortName string) modconfig.HclResource { | ||
benchmark := &Benchmark{ | ||
ModTreeItemImpl: modconfig.NewModTreeItemImpl(block, mod, shortName), | ||
} | ||
benchmark.SetAnonymous(block) | ||
return benchmark | ||
} | ||
|
||
func (b *Benchmark) Equals(other *Benchmark) bool { | ||
if other == nil { | ||
return false | ||
} | ||
|
||
return !b.Diff(other).HasChanges() | ||
} | ||
|
||
// OnDecoded implements HclResource | ||
func (b *Benchmark) OnDecoded(block *hcl.Block, _ modconfig.ResourceMapsProvider) hcl.Diagnostics { | ||
b.SetBaseProperties() | ||
return nil | ||
} | ||
|
||
func (b *Benchmark) String() string { | ||
// build list of children's names | ||
var children []string | ||
for _, child := range b.GetChildren() { | ||
children = append(children, child.Name()) | ||
} | ||
// build list of parents names | ||
var parents []string | ||
for _, p := range b.GetParents() { | ||
parents = append(parents, p.Name()) | ||
} | ||
sort.Strings(children) | ||
return fmt.Sprintf(` | ||
----- | ||
Name: %s | ||
Title: %s | ||
Description: %s | ||
Parent: %s | ||
Children: | ||
%s | ||
`, | ||
b.FullName, | ||
types.SafeString(b.Title), | ||
types.SafeString(b.Description), | ||
strings.Join(parents, "\n "), | ||
strings.Join(children, "\n ")) | ||
} | ||
|
||
// GetChildControls return a flat list of controls underneath the benchmark in the tree | ||
func (b *Benchmark) GetChildControls() []*Control { | ||
var res []*Control | ||
for _, child := range b.GetChildren() { | ||
if control, ok := child.(*Control); ok { | ||
res = append(res, control) | ||
} else if benchmark, ok := child.(*Benchmark); ok { | ||
res = append(res, benchmark.GetChildControls()...) | ||
} | ||
} | ||
return res | ||
} | ||
|
||
// GetWidth implements DashboardLeafNode | ||
func (b *Benchmark) GetWidth() int { | ||
if b.Width == nil { | ||
return 0 | ||
} | ||
return *b.Width | ||
} | ||
|
||
// GetDisplay implements DashboardLeafNode | ||
func (b *Benchmark) GetDisplay() string { | ||
return typehelpers.SafeString(b.Display) | ||
} | ||
|
||
// GetType implements DashboardLeafNode | ||
func (b *Benchmark) GetType() string { | ||
return typehelpers.SafeString(b.Type) | ||
} | ||
|
||
// GetUnqualifiedName implements DashboardLeafNode, ModTreeItem | ||
func (b *Benchmark) GetUnqualifiedName() string { | ||
return b.UnqualifiedName | ||
} | ||
|
||
func (b *Benchmark) Diff(other *Benchmark) *modconfig.ModTreeItemDiffs { | ||
res := &modconfig.ModTreeItemDiffs{ | ||
Item: b, | ||
Name: b.Name(), | ||
} | ||
|
||
if !utils.SafeStringsEqual(b.Description, other.Description) { | ||
res.AddPropertyDiff("Description") | ||
} | ||
if !utils.SafeStringsEqual(b.Documentation, other.Documentation) { | ||
res.AddPropertyDiff("Documentation") | ||
} | ||
if !utils.SafeStringsEqual(b.Title, other.Title) { | ||
res.AddPropertyDiff("Title") | ||
} | ||
if len(b.Tags) != len(other.Tags) { | ||
res.AddPropertyDiff("Tags") | ||
} else { | ||
for k, v := range b.Tags { | ||
if otherVal := other.Tags[k]; v != otherVal { | ||
res.AddPropertyDiff("Tags") | ||
} | ||
} | ||
} | ||
|
||
if !utils.SafeStringsEqual(b.Type, other.Type) { | ||
res.AddPropertyDiff("Type") | ||
} | ||
|
||
if len(b.ChildNameStrings) != len(other.ChildNameStrings) { | ||
res.AddPropertyDiff("Childen") | ||
} else { | ||
myChildNames := b.ChildNameStrings | ||
sort.Strings(myChildNames) | ||
otherChildNames := other.ChildNameStrings | ||
sort.Strings(otherChildNames) | ||
if strings.Join(myChildNames, ",") != strings.Join(otherChildNames, ",") { | ||
res.AddPropertyDiff("Childen") | ||
} | ||
} | ||
|
||
res.Merge(dashboardLeafNodeDiff(b, other)) | ||
return res | ||
} | ||
|
||
func (b *Benchmark) WalkResources(resourceFunc func(resource modconfig.ModTreeItem) (bool, error)) error { | ||
for _, child := range b.GetChildren() { | ||
continueWalking, err := resourceFunc(child) | ||
if err != nil { | ||
return err | ||
} | ||
if !continueWalking { | ||
break | ||
} | ||
|
||
if childContainer, ok := child.(*Benchmark); ok { | ||
if err := childContainer.WalkResources(resourceFunc); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// CtyValue implements CtyValueProvider | ||
func (b *Benchmark) CtyValue() (cty.Value, error) { | ||
return cty_helpers.GetCtyValue(b) | ||
} | ||
|
||
func (b *Benchmark) SetBaseProperties() { | ||
if b.Base == nil { | ||
return | ||
} | ||
// copy base into the HclResourceImpl 'base' property so it is accessible to all nested structs | ||
b.HclResourceImpl.SetBase(b.Base) | ||
// call into parent nested struct SetBaseProperties | ||
b.ModTreeItemImpl.SetBaseProperties() | ||
|
||
if b.Width == nil { | ||
b.Width = b.Base.Width | ||
} | ||
|
||
if b.Display == nil { | ||
b.Display = b.Base.Display | ||
} | ||
|
||
if len(b.GetChildren()) == 0 { | ||
b.Children = b.Base.Children | ||
b.ChildNameStrings = b.Base.ChildNameStrings | ||
b.ChildNames = b.Base.ChildNames | ||
} | ||
} | ||
|
||
// GetShowData implements printers.Showable | ||
func (b *Benchmark) GetShowData() *printers.RowData { | ||
res := printers.NewRowData( | ||
printers.FieldValue{Name: "Children", Value: b.ChildNameStrings}, | ||
) | ||
res.Merge(b.ModTreeItemImpl.GetShowData()) | ||
return res | ||
} |
Oops, something went wrong.