Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
olasaadi99 committed Jan 18, 2024
1 parent 437fd40 commit fa252ff
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 32 deletions.
19 changes: 2 additions & 17 deletions cmd/analyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"os"

"github.com/np-guard/vpc-network-config-analyzer/pkg/common"
"github.com/np-guard/vpc-network-config-analyzer/pkg/ibmvpc"
"github.com/np-guard/vpc-network-config-analyzer/pkg/version"
"github.com/np-guard/vpc-network-config-analyzer/pkg/vpcmodel"
Expand Down Expand Up @@ -56,26 +55,12 @@ func analysisTypeToUseCase(inArgs *InArgs) vpcmodel.OutputUseCase {
}

func analysisVPCConfigs(c1, c2 map[string]*vpcmodel.VPCConfig, inArgs *InArgs, outFile string) (string, error) {
var explanation *vpcmodel.Explanation
var err error
if *inArgs.AnalysisType == explainMode {
// in explain analysis-type we have only one vpc
_, vpcConfig := common.AnyMapEntry(c1)
connQuery := vpcmodel.TranslateCDtoConnectionSet(*inArgs.EProtocol, *inArgs.ESrcMinPort,
*inArgs.ESrcMaxPort, *inArgs.EDstMinPort, *inArgs.EDstMaxPort)
explanation, err = vpcConfig.ExplainConnectivity(*inArgs.ESrc, *inArgs.EDst, connQuery)
if err != nil {
return "", err
}
} else {
explanation = nil
}

og, err := vpcmodel.NewOutputGenerator(c1, c2,
*inArgs.Grouping,
analysisTypeToUseCase(inArgs),
false,
explanation)
vpcmodel.NewExplanationArgs(*inArgs.ESrc, *inArgs.EDst, *inArgs.EProtocol,
*inArgs.ESrcMinPort, *inArgs.ESrcMaxPort, *inArgs.EDstMinPort, *inArgs.EDstMaxPort))
if err != nil {
return "", err
}
Expand Down
18 changes: 7 additions & 11 deletions pkg/ibmvpc/analysis_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/stretchr/testify/require"

"github.com/np-guard/vpc-network-config-analyzer/pkg/common"
"github.com/np-guard/vpc-network-config-analyzer/pkg/vpcmodel"
)

Expand Down Expand Up @@ -527,20 +526,17 @@ func (tt *vpcGeneralTest) runTest(t *testing.T) {
tt.inputConfig2nd = ""
}

var explanation *vpcmodel.Explanation
var explanationArgs *vpcmodel.ExplanationArgs
if explainUseCase {
// in explain analysis-type we have only one vpc
_, vpcConfig := common.AnyMapEntry(vpcConfigs)
connQuery := vpcmodel.TranslateCDtoConnectionSet(tt.EProtocol, tt.ESrcMinPort,
tt.ESrcMaxPort, tt.EDstMinPort, tt.EDstMaxPort)
explanation, _ = vpcConfig.ExplainConnectivity(tt.ESrc, tt.EDst, connQuery)
vpcmodel.NewExplanationArgs(tt.ESrc, tt.EDst, tt.EProtocol,
tt.ESrcMinPort, tt.ESrcMaxPort, tt.EDstMinPort, tt.EDstMaxPort)
} else {
explanation = nil
explanationArgs = nil
}

// generate actual output for all use cases specified for this test
for _, uc := range tt.useCases {
err := runTestPerUseCase(t, tt, vpcConfigs, vpcConfigs2nd, uc, tt.mode, explanation)
err := runTestPerUseCase(t, tt, vpcConfigs, vpcConfigs2nd, uc, tt.mode, explanationArgs)
require.Equal(t, tt.errPerUseCase[uc], err, "comparing actual err to expected err")
}
for uc, outFile := range tt.actualOutput {
Expand Down Expand Up @@ -614,11 +610,11 @@ func runTestPerUseCase(t *testing.T,
c1, c2 map[string]*vpcmodel.VPCConfig,
uc vpcmodel.OutputUseCase,
mode testMode,
explanation *vpcmodel.Explanation) error {
explanationArgs *vpcmodel.ExplanationArgs) error {
if err := initTestFileNames(tt, uc, "", true); err != nil {
return err
}
og, err := vpcmodel.NewOutputGenerator(c1, c2, tt.grouping, uc, tt.format == vpcmodel.ARCHDRAWIO, explanation)
og, err := vpcmodel.NewOutputGenerator(c1, c2, tt.grouping, uc, tt.format == vpcmodel.ARCHDRAWIO, explanationArgs)
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/vpcmodel/nodesExplainability.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ type rulesSingleSrcDst struct {

type explainStruct []*rulesSingleSrcDst

type ExplanationArgs struct {
src string
dst string
protocol string
srcMinPort int64
srcMaxPort int64
dstMinPort int64
dstMaxPort int64
}

func NewExplanationArgs(src, dst, protocol string, srcMinPort, srcMaxPort, dstMinPort, dstMaxPort int64) *ExplanationArgs {
return &ExplanationArgs{src: src, dst: dst, protocol: protocol,
srcMinPort: srcMinPort, srcMaxPort: srcMaxPort, dstMinPort: dstMinPort, dstMaxPort: dstMaxPort}
}

type Explanation struct {
c *VPCConfig
connQuery *common.ConnectionSet
Expand Down
16 changes: 12 additions & 4 deletions pkg/vpcmodel/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,18 @@ type OutputGenerator struct {
nodesConn map[string]*VPCConnectivity
subnetsConn map[string]*VPCsubnetConnectivity
cfgsDiff *diffBetweenCfgs
explainStruct *Explanation
explanation *Explanation
}

func NewOutputGenerator(c1, c2 map[string]*VPCConfig, grouping bool, uc OutputUseCase,
archOnly bool, explainStruct *Explanation) (*OutputGenerator, error) {
archOnly bool, explanationArgs *ExplanationArgs) (*OutputGenerator, error) {
res := &OutputGenerator{
config1: c1,
config2: c2,
outputGrouping: grouping,
useCase: uc,
nodesConn: map[string]*VPCConnectivity{},
subnetsConn: map[string]*VPCsubnetConnectivity{},
explainStruct: explainStruct,
}
if !archOnly {
for i := range c1 {
Expand Down Expand Up @@ -97,6 +96,15 @@ func NewOutputGenerator(c1, c2 map[string]*VPCConfig, grouping bool, uc OutputUs
}
res.cfgsDiff = configsDiff
}
if uc == Explain {
connQuery := TranslateCDtoConnectionSet(explanationArgs.protocol, explanationArgs.srcMinPort,
explanationArgs.srcMaxPort, explanationArgs.dstMinPort, explanationArgs.dstMaxPort)
explanation, err := c1[i].ExplainConnectivity(explanationArgs.src, explanationArgs.dst, connQuery)
if err != nil {
return nil, err
}
res.explanation = explanation
}
}
}
return res, nil
Expand Down Expand Up @@ -128,7 +136,7 @@ func (o *OutputGenerator) Generate(f OutFormat, outFile string) (string, error)
return "", errors.New("unsupported output format")
}
return formatter.WriteOutput(o.config1, o.config2, o.nodesConn, o.subnetsConn, o.cfgsDiff,
outFile, o.outputGrouping, o.useCase, o.explainStruct)
outFile, o.outputGrouping, o.useCase, o.explanation)
}

// SingleVpcOutputFormatter is an interface for a formatter that can handle only one vpc
Expand Down

0 comments on commit fa252ff

Please sign in to comment.