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

subnet-grouping2 #245

Merged
merged 27 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
120b132
init
haim-kermany Nov 29, 2023
0d800b2
go 20
haim-kermany Nov 29, 2023
3b05954
go 20
haim-kermany Nov 29, 2023
e75c3c0
not 1.2
haim-kermany Nov 29, 2023
a120861
Merge branch 'main' into subnet-grouping2
haim-kermany Dec 7, 2023
4d6a3a2
ShowOnSubnetMode for tgw
haim-kermany Dec 7, 2023
55a6259
use go-version-file
haim-kermany Dec 7, 2023
f056052
CR from Ziv
haim-kermany Dec 7, 2023
6d2840c
removing subnetMode flag
haim-kermany Dec 7, 2023
0fd51e2
does not work
haim-kermany Dec 7, 2023
85e53ff
another try
haim-kermany Dec 7, 2023
4290fdf
change subnetmode Location
haim-kermany Dec 7, 2023
d911f9b
test
haim-kermany Dec 7, 2023
ee96ef4
lint
haim-kermany Dec 7, 2023
d609bb7
code review
haim-kermany Dec 7, 2023
7d9baac
documenting
haim-kermany Dec 7, 2023
95daa44
using maps from golang 21
haim-kermany Dec 10, 2023
16d6066
Merge branch 'main' into subnet-grouping2
haim-kermany Dec 12, 2023
ae50b61
code review
haim-kermany Dec 12, 2023
c47a06e
not use pointer to map
haim-kermany Dec 12, 2023
f85c7c8
handle pointers to string
haim-kermany Dec 12, 2023
e878c09
adding tests to main
haim-kermany Dec 18, 2023
2a0a03b
Merge branch 'main' into subnet-grouping2
haim-kermany Dec 18, 2023
9fa3846
Merge branch 'main' into subnet-grouping2
haim-kermany Dec 20, 2023
3db9e2b
Merge branch 'main' into subnet-grouping2
haim-kermany Dec 21, 2023
bf29ee0
Merge branch 'main' into subnet-grouping2
haim-kermany Dec 27, 2023
a1d301c
Merge branch 'main' into subnet-grouping2
haim-kermany Jan 1, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/go-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe
with:
go-version: 1.18
go-version-file: ./go.mod

- name: Build
env:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jobs:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe
with:
go-version: 1.18
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11
go-version-file: ./go.mod
- uses: golangci/golangci-lint-action@3a919529898de77ec3da873e3063ca4b10e7f5cc
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/np-guard/vpc-network-config-analyzer

go 1.18
go 1.21

require (
github.com/IBM/vpc-go-sdk v0.44.0
Expand Down
50 changes: 50 additions & 0 deletions pkg/common/genericSet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package common

import (
"sort"
"strconv"
"strings"
)

// //////////////////////////////////////////////////////////////////////////////////////////////

// a genericSet is a generic implementation of a set.
// the main functionality of genericSet is asKey() - conversion to a string.
// asKey() is needed for using genericSet as a key of a map
// //////////////////////////////////////////////////////////////////////////////////////////////

type GenericSet[T comparable] map[T]bool
type SetAsKey string

var interfaceIndex map[interface{}]int = map[interface{}]int{}

func (s *GenericSet[T]) AsKey() SetAsKey {
zivnevo marked this conversation as resolved.
Show resolved Hide resolved
ss := []string{}
for i := range *s {
if _, ok := interfaceIndex[i]; !ok {
interfaceIndex[i] = len(interfaceIndex)
}
ss = append(ss, strconv.Itoa(interfaceIndex[i]))
}
sort.Strings(ss)
return SetAsKey(strings.Join(ss, ","))
}
zivnevo marked this conversation as resolved.
Show resolved Hide resolved

func (s *GenericSet[T]) AsList() []T {
keys := make([]T, len(*s))
i := 0
for k := range *s {
keys[i] = k
i++
}
return keys
}

func (s *GenericSet[T]) IsIntersect(s2 *GenericSet[T]) bool {
for i := range *s {
if (*s2)[i] {
return true
}
}
return false
}
20 changes: 11 additions & 9 deletions pkg/drawio/abstractTreeNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ const (
)

type abstractTreeNode struct {
id uint
x int
y int
name string
width int
height int
parent TreeNodeInterface
location *Location
id uint
x int
y int
name string
width int
height int
parent TreeNodeInterface
location *Location
doNotShowInDrawio bool
}

func (tn *abstractTreeNode) Label() string { return tn.name }
Expand Down Expand Up @@ -51,7 +52,8 @@ func (tn *abstractTreeNode) DrawioParent() TreeNodeInterface { return tn.parent

func (tn *abstractTreeNode) setLocation(location *Location) { tn.location = location }
func (tn *abstractTreeNode) setParent(p TreeNodeInterface) { tn.parent = p }
func (tn *abstractTreeNode) NotShownInDrawio() bool { return false }
func (tn *abstractTreeNode) NotShownInDrawio() bool { return tn.doNotShowInDrawio }
func (tn *abstractTreeNode) SetNotShownInDrawio() { tn.doNotShowInDrawio = true }

var idCounter uint = minID

Expand Down
13 changes: 9 additions & 4 deletions pkg/drawio/createMapFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ func (data *drawioData) ElementComment(tn TreeNodeInterface) string {
// (the last in the file will be on top in the canvas)
// 1. we put the lines at the top so they will overlap the icons
// 2. we put the icons above the squares so we can mouse over it for tooltips
// 3. we put the sgs in the bottom. if a sg is above a square, it will block the the tooltip of the children of the square.
// 3. we put the sgs and the gs in the bottom.
// (if a sg ot a gs is above a square, it will block the the tooltip of the children of the square.)
func orderNodesForDrawio(nodes []TreeNodeInterface) []TreeNodeInterface {
var sg, sq, ln, ic, orderedNodes []TreeNodeInterface
var sg, sq, ln, ic, gs, orderedNodes []TreeNodeInterface
for _, tn := range nodes {
switch {
case reflect.TypeOf(tn).Elem() == reflect.TypeOf(PartialSGTreeNode{}):
sg = append(sg, tn)
case tn.IsSquare() && tn.(SquareTreeNodeInterface).IsGroupingSquare(),
tn.IsSquare() && tn.(SquareTreeNodeInterface).IsGroupSubnetsSquare():
gs = append(gs, tn)
case tn.IsSquare():
sq = append(sq, tn)
case tn.IsIcon():
Expand All @@ -57,15 +61,16 @@ func orderNodesForDrawio(nodes []TreeNodeInterface) []TreeNodeInterface {
ln = append(ln, tn)
}
}
orderedNodes = append(orderedNodes, gs...)
orderedNodes = append(orderedNodes, sg...)
orderedNodes = append(orderedNodes, sq...)
orderedNodes = append(orderedNodes, ic...)
orderedNodes = append(orderedNodes, ln...)
return orderedNodes
}

func CreateDrawioConnectivityMapFile(network SquareTreeNodeInterface, outputFile string) error {
newLayout(network).layout()
func CreateDrawioConnectivityMapFile(network SquareTreeNodeInterface, outputFile string, subnetMode bool) error {
newLayout(network, subnetMode).layout()

return writeDrawioFile(NewDrawioData(network), outputFile)
}
Expand Down
69 changes: 64 additions & 5 deletions pkg/drawio/drawio_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,37 @@ import (

func TestWithParsing(t *testing.T) {
n := createNetwork()
err := CreateDrawioConnectivityMapFile(n, "fake.drawio")
err := CreateDrawioConnectivityMapFile(n, "fake.drawio", false)
if err != nil {
fmt.Println("Error when calling CreateDrawioConnectivityMapFile():", err)
}
n = createNetwork2()
err = CreateDrawioConnectivityMapFile(n, "fake2.drawio")
err = CreateDrawioConnectivityMapFile(n, "fake2.drawio", false)
if err != nil {
fmt.Println("Error when calling CreateDrawioConnectivityMapFile():", err)
}
n = createNetworkGrouping()
err = CreateDrawioConnectivityMapFile(n, "grouping.drawio")
err = CreateDrawioConnectivityMapFile(n, "grouping.drawio", false)
if err != nil {
fmt.Println("Error when calling CreateDrawioConnectivityMapFile():", err)
}
n = createNetworkSubnetGrouping()
err = CreateDrawioConnectivityMapFile(n, "subnetGrouping.drawio", true)
if err != nil {
fmt.Println("Error when calling CreateDrawioConnectivityMapFile():", err)
}

n2 := NewNetworkTreeNode()
NewCloudTreeNode(n2, "empty Cloud")
NewPublicNetworkTreeNode(n2)
NewCloudTreeNode(n2, "empty cloud2")
err = CreateDrawioConnectivityMapFile(n2, "fake3.drawio")
err = CreateDrawioConnectivityMapFile(n2, "fake3.drawio", false)
if err != nil {
fmt.Println("Error when calling CreateDrawioConnectivityMapFile():", err)
}

n = createNetworkAllTypes()
err = CreateDrawioConnectivityMapFile(n, "all.drawio")
err = CreateDrawioConnectivityMapFile(n, "all.drawio", false)
if err != nil {
fmt.Println("Error when calling CreateDrawioConnectivityMapFile():", err)
}
Expand Down Expand Up @@ -278,6 +284,59 @@ func createNetworkAllTypes() SquareTreeNodeInterface {
return network
}

func createZone(zones *[][]SquareTreeNodeInterface, vpc *VpcTreeNode, size int, name string) {
zone := NewZoneTreeNode(vpc, name)
subnets := make([]SquareTreeNodeInterface, size)
*zones = append(*zones, subnets)
for i := 0; i < size; i++ {
sname := fmt.Sprint(name, i)
subnets[i] = NewSubnetTreeNode(zone, sname, "", "")
}
}
func createGroup(zones *[][]SquareTreeNodeInterface, vpc *VpcTreeNode, i1, i2, j1, j2 int) SquareTreeNodeInterface {
gr := []SquareTreeNodeInterface{}
for i := i1; i <= i2; i++ {
for j := j1; j <= j2; j++ {
gr = append(gr, (*zones)[i][j])
}
}
g := GroupedSubnetsSquare(vpc, gr)
g.(*GroupSubnetsSquareTreeNode).name = fmt.Sprintf("%d-%d,%d,%d", i1, i2, j1, j2)
return g
}

func createNetworkSubnetGrouping() SquareTreeNodeInterface {
network := NewNetworkTreeNode()
zones := &[][]SquareTreeNodeInterface{}
cloud1 := NewCloudTreeNode(network, "IBM Cloud")
publicNetwork := NewPublicNetworkTreeNode(network)
vpc1 := NewVpcTreeNode(cloud1, "vpc1")
for i := 0; i < 10; i++ {
createZone(zones, vpc1, 8, fmt.Sprintf("z%d", i))
}
groups := []SquareTreeNodeInterface{
createGroup(zones, vpc1, 0, 0, 0, 1),
createGroup(zones, vpc1, 1, 1, 0, 1),
createGroup(zones, vpc1, 0, 2, 0, 6),
createGroup(zones, vpc1, 0, 2, 4, 6),
createGroup(zones, vpc1, 3, 3, 1, 2),
createGroup(zones, vpc1, 2, 3, 1, 2),
createGroup(zones, vpc1, 0, 4, 0, 3),
createGroup(zones, vpc1, 0, 5, 0, 3),

createGroup(zones, vpc1, 6, 7, 0, 1),
createGroup(zones, vpc1, 6, 6, 2, 3),
createGroup(zones, vpc1, 7, 8, 1, 2),
}
NewConnectivityLineTreeNode(network, groups[0], groups[len(groups)-1], true, "gconn")

for _, gr := range groups {
i1 := NewInternetTreeNode(publicNetwork, "I "+gr.Label())
NewConnectivityLineTreeNode(network, gr, i1, true, "gconn "+gr.Label())
}
return network
}

func createNetworkGrouping() SquareTreeNodeInterface {
network := NewNetworkTreeNode()
publicNetwork := NewPublicNetworkTreeNode(network)
Expand Down
Loading