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

Create drawio in ibmvpc #165

Merged
merged 26 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions pkg/drawio/iconTreeNode.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func NewNITreeNode(parent SquareTreeNodeInterface, sg *SGTreeNode, name string)
ni := NITreeNode{abstractIconTreeNode: newAbstractIconTreeNode(parent, name)}
parent.addIconTreeNode(&ni)
if sg != nil {
ni.setSG(sg)
ni.SetSG(sg)
}
return &ni
}
func (tn *NITreeNode) setSG(sg *SGTreeNode) {
func (tn *NITreeNode) SetSG(sg *SGTreeNode) {
sg.addIconTreeNode(tn)
tn.sg = sg
}
Expand Down
81 changes: 81 additions & 0 deletions pkg/ibmvpc/ibmDrawioGenerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ibmvpc

import (
"github.com/np-guard/vpc-network-config-analyzer/pkg/drawio"
"github.com/np-guard/vpc-network-config-analyzer/pkg/vpcmodel"
)

// implementations of the GenerateDrawioTreeNode() for resource defined in ibmvpc:
func (v *VPC) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) drawio.TreeNodeInterface {
return drawio.NewVpcTreeNode(gen.Cloud(), v.Name())
}

func (z *Zone) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) drawio.TreeNodeInterface {
return drawio.NewZoneTreeNode(gen.TreeNode(z.VPC()).(*drawio.VpcTreeNode), z.name)
}

func (s *Subnet) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) drawio.TreeNodeInterface {
// todo - how to handle this error:
zone, _ := s.Zone()
zoneTn := gen.TreeNode(zone).(*drawio.ZoneTreeNode)
return drawio.NewSubnetTreeNode(zoneTn, s.Name(), s.cidr, "")
}

func (sgl *SecurityGroupLayer) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) drawio.TreeNodeInterface {
tn := drawio.NewSGTreeNode(gen.TreeNode(sgl.VPC()).(*drawio.VpcTreeNode), sgl.Name())
for _, sg := range sgl.sgList {
for _, ni := range sg.members {
gen.TreeNode(ni).(*drawio.NITreeNode).SetSG(tn)
}
}
return tn
}

func (nl *NaclLayer) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) drawio.TreeNodeInterface {
for _, acl := range nl.naclList {
for _, sn := range acl.subnets {
gen.TreeNode(sn).(*drawio.SubnetTreeNode).SetACL(acl.Name())
}
}
return nil
}

func (ni *NetworkInterface) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) drawio.TreeNodeInterface {
return drawio.NewNITreeNode(
gen.TreeNode(ni.subnet).(drawio.SquareTreeNodeInterface),
nil, ni.Name())
}

func (n *IKSNode) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) drawio.TreeNodeInterface {
// todo - what is this? should we handle it? can we ignore it?
return nil
adisos marked this conversation as resolved.
Show resolved Hide resolved
}

func (v *Vsi) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) drawio.TreeNodeInterface {
if len(v.Nodes()) == 0 {
return nil
}
vsiNIs := []drawio.TreeNodeInterface{}
for _, ni := range v.Nodes() {
vsiNIs = append(vsiNIs, gen.TreeNode(ni))
}
// todo - how to handle this error:
zone, _ := v.Zone()
zoneTn := gen.TreeNode(zone).(*drawio.ZoneTreeNode)
drawio.GroupNIsWithVSI(zoneTn, v.Name(), vsiNIs)
return nil
}

func (pgw *PublicGateway) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) drawio.TreeNodeInterface {
// todo - how to handle this error:
zone, _ := pgw.Zone()
zoneTn := gen.TreeNode(zone).(*drawio.ZoneTreeNode)
return drawio.NewGatewayTreeNode(zoneTn, pgw.Name())
}

func (fip *FloatingIP) GenerateDrawioTreeNode(gen *vpcmodel.DrawioGenerator) drawio.TreeNodeInterface {
// todo - what if r.Src() is not at size of one?
nitn := gen.TreeNode(fip.Src()[0]).(*drawio.NITreeNode)
nitn.SetFIP(fip.Name())
return nitn
}
1 change: 1 addition & 0 deletions pkg/ibmvpc/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ func NewCloudConfig(rc *ResourcesContainer) (*vpcmodel.CloudConfig, error) {
FilterResources: []vpcmodel.FilterTrafficResource{},
RoutingResources: []vpcmodel.RoutingResource{},
NameToResource: map[string]vpcmodel.VPCResourceIntf{},
CloudName: "IBM Cloud",
}

var err error
Expand Down
1 change: 1 addition & 0 deletions pkg/vpcmodel/abstractVPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type VPCResourceIntf interface {
// TODO: remove Details and DetailsMap from this interface
Details() []string
DetailsMap() []map[string]string
DrawioResourceIntf
adisos marked this conversation as resolved.
Show resolved Hide resolved
}

// VPCResource implements VPCResourceIntf
Expand Down
1 change: 1 addition & 0 deletions pkg/vpcmodel/cloudConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type CloudConfig struct {
FilterResources []FilterTrafficResource
RoutingResources []RoutingResource
NameToResource map[string]VPCResourceIntf
CloudName string
}

// TODO: consider add this mapping to CloudConfig
Expand Down
56 changes: 56 additions & 0 deletions pkg/vpcmodel/drawioGenerator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package vpcmodel

import (
"github.com/np-guard/vpc-network-config-analyzer/pkg/drawio"
)

// DrawioResourceIntf is the interface of all the resources that are converted to a drawio treeNodes
type DrawioResourceIntf interface {
GenerateDrawioTreeNode(gen *DrawioGenerator) drawio.TreeNodeInterface
}

// DrawioGenerator is the struct that generate the drawio tree.
// its main interface is:
// 1. TreeNode() - generate and returns the drawio tree node of a resource
// 2. the constructor - generate the treeNodes that does not represent a specific resource
// (the constructor creates the publicNetwork tree node, and the Cloud TreeNode)
adisos marked this conversation as resolved.
Show resolved Hide resolved
// the rest of the interface i getters:
// Network(), PublicNetwork(), Cloud()
// returns the tree nodes which are created at the constructor
// please notice:
// creating the cloud treeNode is vendor specific (IBM, aws...).
// currently, the input that distinguish between the vendors is the cloudName, which is provided to NewDrawioGenerator() as parameter.
// we might later give as parameters more information to create the cloud, or create the cloud at the specific pkg.
type DrawioGenerator struct {
network *drawio.NetworkTreeNode
publicNetwork *drawio.PublicNetworkTreeNode
cloud *drawio.CloudTreeNode
treeNodes map[DrawioResourceIntf]drawio.TreeNodeInterface
}

func NewDrawioGenerator(cloudName string) *DrawioGenerator {
// creates the top of the tree node - treeNodes that does not represent a specific resource.
gen := &DrawioGenerator{}
gen.network = drawio.NewNetworkTreeNode()
gen.publicNetwork = drawio.NewPublicNetworkTreeNode(gen.network)
gen.cloud = drawio.NewCloudTreeNode(gen.network, cloudName)
gen.treeNodes = map[DrawioResourceIntf]drawio.TreeNodeInterface{}
return gen
}
func (gen *DrawioGenerator) Network() *drawio.NetworkTreeNode { return gen.network }
func (gen *DrawioGenerator) PublicNetwork() *drawio.PublicNetworkTreeNode { return gen.publicNetwork }
func (gen *DrawioGenerator) Cloud() *drawio.CloudTreeNode { return gen.cloud }

func (gen *DrawioGenerator) TreeNode(res DrawioResourceIntf) drawio.TreeNodeInterface {
if gen.treeNodes[res] == nil {
gen.treeNodes[res] = res.GenerateDrawioTreeNode(gen)
}
return gen.treeNodes[res]
}

// ////////////////////////////////////////////////////////////////////////////////////////////////////////////
// implementations of the GenerateDrawioTreeNode() for resource defined in vpcmodel:
// (currently only ExternalNetwork, will add the grouping resource later)
func (exn *ExternalNetwork) GenerateDrawioTreeNode(gen *DrawioGenerator) drawio.TreeNodeInterface {
return drawio.NewInternetTreeNode(gen.PublicNetwork(), exn.CidrStr)
}
Loading