diff --git a/pkg/io/common.go b/pkg/io/common.go index 4e4f9e3..1a0eb69 100644 --- a/pkg/io/common.go +++ b/pkg/io/common.go @@ -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: diff --git a/pkg/io/commonACL.go b/pkg/io/commonACL.go index 4adfe77..865e325 100644 --- a/pkg/io/commonACL.go +++ b/pkg/io/commonACL.go @@ -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", diff --git a/pkg/io/commonSG.go b/pkg/io/commonSG.go index b286822..3a05a5e 100644 --- a/pkg/io/commonSG.go +++ b/pkg/io/commonSG.go @@ -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", diff --git a/pkg/io/csvWriter.go b/pkg/io/csvWriter.go index cb21f24..7a5edea 100644 --- a/pkg/io/csvWriter.go +++ b/pkg/io/csvWriter.go @@ -27,7 +27,7 @@ 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 { @@ -35,5 +35,5 @@ func (w *CSVWriter) WriteACL(collection *ir.ACLCollection, vpc string, _ bool) e if err != nil { return err } - return w.w.WriteAll(slices.Concat(ACLHeader(), aclTable)) + return w.w.WriteAll(slices.Concat(makeACLHeader(), aclTable)) } diff --git a/pkg/io/mdWriter.go b/pkg/io/mdWriter.go index 533f12f..461e259 100644 --- a/pkg/io/mdWriter.go +++ b/pkg/io/mdWriter.go @@ -15,9 +15,6 @@ import ( ) const ( - sgColsNum = 7 - aclColsNum = 10 - leftAlign = " :--- " separator = " | " ) @@ -36,7 +33,8 @@ 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 { @@ -44,23 +42,18 @@ func (w *MDWriter) WriteACL(collection *ir.ACLCollection, vpc string, _ bool) er 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 { diff --git a/pkg/io/tfio/acl.go b/pkg/io/tfio/acl.go index dffe4c5..1fc79e8 100644 --- a/pkg/io/tfio/acl.go +++ b/pkg/io/tfio/acl.go @@ -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))}, }, @@ -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())}, diff --git a/pkg/io/tfio/common.go b/pkg/io/tfio/common.go index 96611c6..2cfe005 100644 --- a/pkg/io/tfio/common.go +++ b/pkg/io/tfio/common.go @@ -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 diff --git a/pkg/io/tfio/sg.go b/pkg/io/tfio/sg.go index c008ef5..e269b68 100644 --- a/pkg/io/tfio/sg.go +++ b/pkg/io/tfio/sg.go @@ -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)}, }, @@ -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{