Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DashboardLeafNodeImpl and NodeAndEdgeProviderImpl #586

Open
wants to merge 7 commits into
base: tp
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add NodeAndEdgeProviderImpl
  • Loading branch information
kaidaguerre committed Nov 29, 2024
commit b9d8a41936512b3e41f5cb92c1271d9e4a6d3142
79 changes: 3 additions & 76 deletions internal/resources/dashboard_flow.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package resources

import (
"fmt"
"github.com/turbot/pipe-fittings/modconfig"

"github.com/hashicorp/hcl/v2"
Expand All @@ -14,27 +13,19 @@ import (
// DashboardFlow is a struct representing a leaf dashboard node
type DashboardFlow struct {
modconfig.ResourceWithMetadataImpl
QueryProviderImpl
WithProviderImpl
DashboardLeafNodeImpl
// NOTE: we must have cty tag on at least one property otherwise gohcl.DecodeExpression panics
NodeAndEdgeProviderImpl `cty:"node_and_edge_provider"`

// required to allow partial decoding
Remain hcl.Body `hcl:",remain" json:"-"`

Nodes DashboardNodeList `cty:"node_list" json:"-"`
Edges DashboardEdgeList `cty:"edge_list" json:"-"`
NodeNames []string `json:"nodes" snapshot:"nodes"`
EdgeNames []string `json:"edges" snapshot:"edges"`

Categories map[string]*DashboardCategory `cty:"categories" json:"categories" snapshot:"categories"`

Base *DashboardFlow `hcl:"base" json:"-"`
}

func NewDashboardFlow(block *hcl.Block, mod *modconfig.Mod, shortName string) modconfig.HclResource {
f := &DashboardFlow{
Categories: make(map[string]*DashboardCategory),
QueryProviderImpl: NewQueryProviderImpl(block, mod, shortName),
NodeAndEdgeProviderImpl: NewNodeAndEdgeProviderImpl(block, mod, shortName),
}
f.SetAnonymous(block)
return f
Expand Down Expand Up @@ -105,70 +96,6 @@ func (*DashboardFlow) ValidateQuery() hcl.Diagnostics {
return nil
}

// GetEdges implements NodeAndEdgeProvider
func (f *DashboardFlow) GetEdges() DashboardEdgeList {
return f.Edges
}

// GetNodes implements NodeAndEdgeProvider
func (f *DashboardFlow) GetNodes() DashboardNodeList {
return f.Nodes
}

// SetEdges implements NodeAndEdgeProvider
func (f *DashboardFlow) SetEdges(edges DashboardEdgeList) {
f.Edges = edges
}

// SetNodes implements NodeAndEdgeProvider
func (f *DashboardFlow) SetNodes(nodes DashboardNodeList) {
f.Nodes = nodes
}

// AddCategory implements NodeAndEdgeProvider
func (f *DashboardFlow) AddCategory(category *DashboardCategory) hcl.Diagnostics {
categoryName := category.ShortName
if _, ok := f.Categories[categoryName]; ok {
return hcl.Diagnostics{&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("%s has duplicate category %s", f.Name(), categoryName),
Subject: category.GetDeclRange(),
}}
}
f.Categories[categoryName] = category
return nil
}

// AddChild implements NodeAndEdgeProvider
func (f *DashboardFlow) AddChild(child modconfig.HclResource) hcl.Diagnostics {
var diags hcl.Diagnostics
switch c := child.(type) {
case *DashboardNode:
f.Nodes = append(f.Nodes, c)
case *DashboardEdge:
f.Edges = append(f.Edges, c)
default:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("DashboardFlow does not support children of type %s", child.GetBlockType()),
Subject: f.GetDeclRange(),
})
return diags
}
// set ourselves as parent
err := child.(modconfig.ModTreeItem).AddParent(f)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "failed to add parent to ModTreeItem",
Detail: err.Error(),
Subject: child.GetDeclRange(),
})
}

return diags
}

// CtyValue implements CtyValueProvider
func (f *DashboardFlow) CtyValue() (cty.Value, error) {
return cty_helpers.GetCtyValue(f)
Expand Down
79 changes: 4 additions & 75 deletions internal/resources/dashboard_graph.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package resources

import (
"fmt"
"github.com/turbot/pipe-fittings/modconfig"

"github.com/hashicorp/hcl/v2"
Expand All @@ -14,28 +13,21 @@ import (
// DashboardGraph is a struct representing a leaf dashboard node
type DashboardGraph struct {
modconfig.ResourceWithMetadataImpl
QueryProviderImpl
WithProviderImpl
DashboardLeafNodeImpl
// NOTE: we must have cty tag on at least one property otherwise gohcl.DecodeExpression panics
NodeAndEdgeProviderImpl `cty:"node_and_edge_provider"`

// required to allow partial decoding
Remain hcl.Body `hcl:",remain" json:"-"`

Nodes DashboardNodeList `cty:"node_list" json:"nodes,omitempty"`
Edges DashboardEdgeList `cty:"edge_list" json:"edges,omitempty"`
NodeNames []string `snapshot:"nodes"`
EdgeNames []string `snapshot:"edges"`

Categories map[string]*DashboardCategory `cty:"categories" json:"categories,omitempty" snapshot:"categories"`
Direction *string `cty:"direction" hcl:"direction" json:"direction,omitempty" snapshot:"direction"`
Direction *string `cty:"direction" hcl:"direction" json:"direction,omitempty" snapshot:"direction"`

Base *DashboardGraph `hcl:"base" json:"-"`
}

func NewDashboardGraph(block *hcl.Block, mod *modconfig.Mod, shortName string) modconfig.HclResource {
g := &DashboardGraph{
Categories: make(map[string]*DashboardCategory),
QueryProviderImpl: NewQueryProviderImpl(block, mod, shortName),
NodeAndEdgeProviderImpl: NewNodeAndEdgeProviderImpl(block, mod, shortName),
}
g.SetAnonymous(block)
return g
Expand Down Expand Up @@ -105,69 +97,6 @@ func (g *DashboardGraph) Diff(other *DashboardGraph) *modconfig.ModTreeItemDiffs
return res
}

// GetEdges implements NodeAndEdgeProvider
func (g *DashboardGraph) GetEdges() DashboardEdgeList {
return g.Edges
}

// GetNodes implements NodeAndEdgeProvider
func (g *DashboardGraph) GetNodes() DashboardNodeList {
return g.Nodes
}

// SetEdges implements NodeAndEdgeProvider
func (g *DashboardGraph) SetEdges(edges DashboardEdgeList) {
g.Edges = edges
}

// SetNodes implements NodeAndEdgeProvider
func (g *DashboardGraph) SetNodes(nodes DashboardNodeList) {
g.Nodes = nodes
}

// AddCategory implements NodeAndEdgeProvider
func (g *DashboardGraph) AddCategory(category *DashboardCategory) hcl.Diagnostics {
categoryName := category.ShortName
if _, ok := g.Categories[categoryName]; ok {
return hcl.Diagnostics{&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("%s has duplicate category %s", g.Name(), categoryName),
Subject: category.GetDeclRange(),
}}
}
g.Categories[categoryName] = category
return nil
}

// AddChild implements NodeAndEdgeProvider
func (g *DashboardGraph) AddChild(child modconfig.HclResource) hcl.Diagnostics {
var diags hcl.Diagnostics
switch c := child.(type) {
case *DashboardNode:
g.Nodes = append(g.Nodes, c)
case *DashboardEdge:
g.Edges = append(g.Edges, c)
default:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("DashboardGraph does not support children of type %s", child.GetBlockType()),
Subject: g.GetDeclRange(),
})
return diags
}
// set ourselves as parent
err := child.(modconfig.ModTreeItem).AddParent(g)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "failed to add parent to ModTreeItem",
Detail: err.Error(),
Subject: child.GetDeclRange(),
})
}
return diags
}

// CtyValue implements CtyValueProvider
func (g *DashboardGraph) CtyValue() (cty.Value, error) {
return cty_helpers.GetCtyValue(g)
Expand Down
78 changes: 3 additions & 75 deletions internal/resources/dashboard_hierarchy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package resources

import (
"fmt"
"github.com/turbot/pipe-fittings/modconfig"

"github.com/hashicorp/hcl/v2"
Expand All @@ -14,26 +13,19 @@ import (
// DashboardHierarchy is a struct representing a leaf dashboard node
type DashboardHierarchy struct {
modconfig.ResourceWithMetadataImpl
QueryProviderImpl
WithProviderImpl
DashboardLeafNodeImpl
// NOTE: we must have cty tag on at least one property otherwise gohcl.DecodeExpression panics
NodeAndEdgeProviderImpl `cty:"node_and_edge_provider"`

// required to allow partial decoding
Remain hcl.Body `hcl:",remain" json:"-"`

Nodes DashboardNodeList `cty:"node_list" json:"nodes,omitempty"`
Edges DashboardEdgeList `cty:"edge_list" json:"edges,omitempty"`
NodeNames []string `snapshot:"nodes"`
EdgeNames []string `snapshot:"edges"`
Categories map[string]*DashboardCategory `cty:"categories" json:"categories,omitempty" snapshot:"categories"`

Base *DashboardHierarchy `hcl:"base" json:"-"`
}

func NewDashboardHierarchy(block *hcl.Block, mod *modconfig.Mod, shortName string) modconfig.HclResource {
h := &DashboardHierarchy{
Categories: make(map[string]*DashboardCategory),
QueryProviderImpl: NewQueryProviderImpl(block, mod, shortName),
NodeAndEdgeProviderImpl: NewNodeAndEdgeProviderImpl(block, mod, shortName),
}
h.SetAnonymous(block)
return h
Expand Down Expand Up @@ -102,70 +94,6 @@ func (h *DashboardHierarchy) GetDocumentation() string {
return ""
}

// GetEdges implements NodeAndEdgeProvider
func (h *DashboardHierarchy) GetEdges() DashboardEdgeList {
return h.Edges
}

// GetNodes implements NodeAndEdgeProvider
func (h *DashboardHierarchy) GetNodes() DashboardNodeList {
return h.Nodes
}

// SetEdges implements NodeAndEdgeProvider
func (h *DashboardHierarchy) SetEdges(edges DashboardEdgeList) {
h.Edges = edges
}

// SetNodes implements NodeAndEdgeProvider
func (h *DashboardHierarchy) SetNodes(nodes DashboardNodeList) {
h.Nodes = nodes
}

// AddCategory implements NodeAndEdgeProvider
func (h *DashboardHierarchy) AddCategory(category *DashboardCategory) hcl.Diagnostics {
categoryName := category.ShortName
if _, ok := h.Categories[categoryName]; ok {
return hcl.Diagnostics{&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("%s has duplicate category %s", h.Name(), categoryName),
Subject: category.GetDeclRange(),
}}
}
h.Categories[categoryName] = category
return nil
}

// AddChild implements NodeAndEdgeProvider
func (h *DashboardHierarchy) AddChild(child modconfig.HclResource) hcl.Diagnostics {
var diags hcl.Diagnostics
switch c := child.(type) {
case *DashboardNode:
h.Nodes = append(h.Nodes, c)
case *DashboardEdge:
h.Edges = append(h.Edges, c)
default:
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: fmt.Sprintf("DashboardHierarchy does not support children of type %s", child.GetBlockType()),
Subject: h.GetDeclRange(),
})
return diags
}
// set ourselves as parent
err := child.(modconfig.ModTreeItem).AddParent(h)
if err != nil {
diags = append(diags, &hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "failed to add parent to ModTreeItem",
Detail: err.Error(),
Subject: child.GetDeclRange(),
})
}

return diags
}

// CtyValue implements CtyValueProvider
func (h *DashboardHierarchy) CtyValue() (cty.Value, error) {
return cty_helpers.GetCtyValue(h)
Expand Down
Loading
Loading