Skip to content

Commit

Permalink
Merge branch 'optimization_locals' into read_nacls
Browse files Browse the repository at this point in the history
  • Loading branch information
YairSlobodin1 committed Dec 18, 2024
2 parents 643b938 + d3b9853 commit b65c15e
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 24 deletions.
2 changes: 1 addition & 1 deletion pkg/io/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func direction(d ir.Direction) string {
func printProtocolName(protocol netp.Protocol) string {
switch p := protocol.(type) {
case netp.ICMP:
return "ICMP"
return string(netp.ProtocolStringICMP)
case netp.TCPUDP:
return strings.ToUpper(string(p.ProtocolString()))
case netp.AnyProtocol:
Expand Down
2 changes: 1 addition & 1 deletion pkg/io/commonACL.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func WriteACL(collection *ir.ACLCollection, vpc string) ([][]string, error) {
return res, nil
}

func ACLHeader() [][]string {
func makeACLHeader() [][]string {
return [][]string{{
"Acl",
"Subnet",
Expand Down
2 changes: 1 addition & 1 deletion pkg/io/commonSG.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func WriteSG(collection *ir.SGCollection, vpc string) ([][]string, error) {
return res, nil
}

func SGHeader() [][]string {
func makeSGHeader() [][]string {
return [][]string{{
"SG",
"Direction",
Expand Down
4 changes: 2 additions & 2 deletions pkg/io/csvWriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func (w *CSVWriter) WriteSG(collection *ir.SGCollection, vpc string, _ bool) err
if err != nil {
return err
}
return w.w.WriteAll(slices.Concat(SGHeader(), sgTable))
return w.w.WriteAll(slices.Concat(makeSGHeader(), sgTable))
}

func (w *CSVWriter) WriteACL(collection *ir.ACLCollection, vpc string, _ bool) error {
aclTable, err := WriteACL(collection, vpc)
if err != nil {
return err
}
return w.w.WriteAll(slices.Concat(ACLHeader(), aclTable))
return w.w.WriteAll(slices.Concat(makeACLHeader(), aclTable))
}
21 changes: 7 additions & 14 deletions pkg/io/mdWriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (
)

const (
sgColsNum = 7
aclColsNum = 10

leftAlign = " :--- "
separator = " | "
)
Expand All @@ -36,31 +33,27 @@ func (w *MDWriter) WriteSG(collection *ir.SGCollection, vpc string, _ bool) erro
if err != nil {
return err
}
return w.writeAll(slices.Concat(SGHeader(), addAligns(sgColsNum), sgTable))
sgHeader := makeSGHeader()
return w.writeAll(slices.Concat(sgHeader, addAligns(len(sgHeader[0])), sgTable))
}

func (w *MDWriter) WriteACL(collection *ir.ACLCollection, vpc string, _ bool) error {
aclTable, err := WriteACL(collection, vpc)
if err != nil {
return err
}
return w.writeAll(slices.Concat(ACLHeader(), addAligns(aclColsNum), aclTable))
aclHeader := makeACLHeader()
return w.writeAll(slices.Concat(aclHeader, addAligns(len(aclHeader[0])), aclTable))
}

func (w *MDWriter) writeAll(rows [][]string) error {
for _, row := range rows {
if _, err := w.w.WriteString(separator); err != nil {
return err
}
if _, err := w.w.WriteString(strings.Join(row, separator)); err != nil {
return err
}
if _, err := w.w.WriteString(separator + "\n"); err != nil {
finalString := separator + strings.Join(row, separator) + separator + "\n"
if _, err := w.w.WriteString(finalString); err != nil {
return err
}
}
w.w.Flush()
return nil
return w.w.Flush()
}

func addAligns(n int) [][]string {
Expand Down
6 changes: 3 additions & 3 deletions pkg/io/tfio/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ func singleACL(t *ir.ACL, comment string) (tf.Block, error) {
}
return tf.Block{
Comment: comment,
Name: "resource",
Name: resourceConst,
Labels: []string{quote("ibm_is_network_acl"), quote(aclName)},
Arguments: []tf.Argument{
{Name: "name", Value: quote(aclName)},
{Name: nameConst, Value: quote(aclName)},
{Name: "resource_group", Value: "local.acl_synth_resource_group_id"},
{Name: "vpc", Value: fmt.Sprintf("local.acl_synth_%s_id", ir.VpcFromScopedResource(t.Subnet))},
},
Expand All @@ -85,7 +85,7 @@ func aclRule(rule *ir.ACLRule, name string) (tf.Block, error) {
return tf.Block{}, err
}
arguments := []tf.Argument{
{Name: "name", Value: quote(name)}, //nolint:revive // obvious false positive
{Name: nameConst, Value: quote(name)},
{Name: "action", Value: quote(action(rule.Action))},
{Name: "direction", Value: quote(direction(rule.Direction))},
{Name: "source", Value: quote(rule.Source.String())},
Expand Down
5 changes: 5 additions & 0 deletions pkg/io/tfio/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
"github.com/np-guard/vpc-network-config-synthesis/pkg/ir"
)

const (
resourceConst = "resource"
nameConst = "name"
)

// Writer implements ir.Writer
type Writer struct {
w *bufio.Writer
Expand Down
4 changes: 2 additions & 2 deletions pkg/io/tfio/sg.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func sg(sG *ir.SG, vpcName string) (tf.Block, error) {
Labels: []string{quote("ibm_is_security_group"), quote(sgName)},
Comment: comment,
Arguments: []tf.Argument{
{Name: "name", Value: quote("sg-" + sgName)},
{Name: nameConst, Value: quote("sg-" + sgName)},
{Name: "resource_group", Value: "local.sg_synth_resource_group_id"},
{Name: "vpc", Value: fmt.Sprintf("local.sg_synth_%s_id", vpcName)},
},
Expand All @@ -96,7 +96,7 @@ func sgRule(rule *ir.SGRule, sgName ir.SGName, i int) (tf.Block, error) {
}

return tf.Block{
Name: "resource", //nolint:revive // obvious false positive
Name: resourceConst,
Labels: []string{quote("ibm_is_security_group_rule"), ir.ChangeScoping(quote(ruleName))},
Comment: comment,
Arguments: []tf.Argument{
Expand Down

0 comments on commit b65c15e

Please sign in to comment.