Skip to content

Commit

Permalink
fix(deps): update module github.com/google/uuid to v1.3.1 (metal-tool…
Browse files Browse the repository at this point in the history
…box#233)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
renovate[bot] authored and Alva8756 committed Aug 23, 2023
1 parent 50b7634 commit 7808d57
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 1 deletion.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/gin-contrib/cors v1.4.0
github.com/gin-contrib/zap v0.1.0
github.com/gin-gonic/gin v1.9.1
github.com/google/uuid v1.3.0
github.com/google/uuid v1.3.1
github.com/gosimple/slug v1.13.1
github.com/jmoiron/sqlx v1.3.5
github.com/lib/pq v1.10.9
Expand All @@ -35,6 +35,7 @@ require (
)

require (
github.com/tealeg/xlsx v1.0.5
github.com/volatiletech/sqlboiler v3.7.1+incompatible
go.hollow.sh/toolbox v0.6.1
go.infratographer.com/x v0.3.7
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/wire v0.5.0 h1:I7ELFeVBr3yfPIcc8+MWvrjk+3VjbcSzoXm3JVa+jD8=
github.com/googleapis/enterprise-certificate-proxy v0.2.5 h1:UR4rDjcgpgEnqpIEvkiqTYKBCKLNmlge2eVjoZfySzM=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
Expand Down Expand Up @@ -644,6 +646,8 @@ github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/tealeg/xlsx v1.0.5 h1:+f8oFmvY8Gw1iUXzPk+kz+4GpbDZPK1FhPiQRd+ypgE=
github.com/tealeg/xlsx v1.0.5/go.mod h1:btRS8dz54TDnvKNosuAqxrM1QgN1udgk9O34bDCnORM=
github.com/thinkgos/zap v0.0.2-0.20210226022008-5b2cf0c4d297 h1:Ii/M4BSD1o5jPlCN8W6koAO4R/adwUzH0pLv+S/hhOs=
github.com/thinkgos/zap v0.0.2-0.20210226022008-5b2cf0c4d297/go.mod h1:vh38J1/dYcj37C+JppxjWqmxhW06zQYmFkz7sJaOeTw=
github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM=
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/v1/aoc_mac_address_bom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package serverservice

type AocMacAddressBom struct {
AocMacAddress string `json:"aoc_mac_address"`
SerialNum string `json:"serial_num"`
}
6 changes: 6 additions & 0 deletions pkg/api/v1/bmc_mac_address_bom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package serverservice

type BmcMacAddressBom struct {
BmcMacAddress string `json:"bmc_mac_address"`
SerialNum string `json:"serial_num"`
}
10 changes: 10 additions & 0 deletions pkg/api/v1/bom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package serverservice

type Bom struct {
SerialNum string `json:"serial_num"`
AocMacAddress string `json:"aoc_mac_address"`
BmcMacAddress string `json:"bmc_mac_address"`
NumDefipmi string `json:"num_defi_pmi"`
NumDefpwd string `json:"num_def_pwd"`
Metro string `json:"metro"`
}
103 changes: 103 additions & 0 deletions pkg/api/v1/router_bom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package serverservice

import (
"github.com/pkg/errors"
"github.com/tealeg/xlsx"
"golang.org/x/exp/maps"
)

const (
serialNumName string = "SERIALNUM"
subItemName string = "SUB-ITEM"
subSerialName string = "SUB-SERIAL"
AocMacAddrName string = "MAC-AOC-ADDRESS"
BmcMacAddrName string = "MAC-ADDRESS"
NumDefipmiName string = "NUM-DEFIPMI"
NumDefipwdName string = "NUM-DEFPWD"
)

type categoryColNum struct {
serialNumCol int
subItemCol int
subSerialCol int
}

func ParseXlsxFile(body []byte) ([]*Bom, []*AocMacAddressBom, []*BmcMacAddressBom, error) {
file, err := xlsx.OpenBinary(body)
if err != nil {
errors.New("failed to open the file")
}

var bomsMap map[string]*Bom = make(map[string]*Bom)
var aocMacBomMap map[string]*AocMacAddressBom = make(map[string]*AocMacAddressBom)
var bmcMacBomMap map[string]*BmcMacAddressBom = make(map[string]*BmcMacAddressBom)
for _, sheet := range file.Sheets {
var categoryCol *categoryColNum
for _, row := range sheet.Rows {
if categoryCol == nil {
categoryCol = &categoryColNum{}
for i, cell := range row.Cells {
switch cell.Value {
case serialNumName:
categoryCol.serialNumCol = i
case subItemName:
categoryCol.subItemCol = i
case subSerialName:
categoryCol.subSerialCol = i
}
}
continue
}
cells := row.Cells
serialNum := cells[categoryCol.serialNumCol].Value
if len(serialNum) == 0 {
return nil, nil, nil, errors.New("empty serial number: Invalid Argument")
}

_, ok := bomsMap[serialNum]
if !ok {
bomsMap[serialNum] = &Bom{SerialNum: serialNum}
}
bom := bomsMap[serialNum]
if cells[categoryCol.subItemCol].Value == AocMacAddrName {
if len(bom.AocMacAddress) > 0 {
bom.AocMacAddress += ","
}

aocMacAddress := cells[categoryCol.subSerialCol].Value
if len(aocMacAddress) == 0 {
return nil, nil, nil, errors.New("empty aocMacAddress: Invalid Argument")
}

bom.AocMacAddress += aocMacAddress

aocMacBomMap[aocMacAddress] = &AocMacAddressBom{}
macBom := aocMacBomMap[aocMacAddress]
macBom.AocMacAddress = aocMacAddress
macBom.SerialNum = serialNum
} else if cells[categoryCol.subItemCol].Value == BmcMacAddrName {
if len(bom.BmcMacAddress) > 0 {
bom.BmcMacAddress += ","
}

bmcMacAddress := cells[categoryCol.subSerialCol].Value
if len(bmcMacAddress) == 0 {
return nil, nil, nil, errors.New("empty bmcMacAddress: Invalid Argument")
}

bom.BmcMacAddress += bmcMacAddress
bmcMacBomMap[bmcMacAddress] = &BmcMacAddressBom{}
macBom := bmcMacBomMap[bmcMacAddress]
macBom.BmcMacAddress = bmcMacAddress
macBom.SerialNum = serialNum

} else if cells[categoryCol.subItemCol].Value == NumDefipmiName {
bom.NumDefipmi = cells[categoryCol.subSerialCol].Value
} else if cells[categoryCol.subItemCol].Value == NumDefipwdName {
bom.NumDefpwd = cells[categoryCol.subSerialCol].Value
}
}
}
return maps.Values(bomsMap), maps.Values(aocMacBomMap), maps.Values(bmcMacBomMap), nil

}
205 changes: 205 additions & 0 deletions pkg/api/v1/router_bom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package serverservice_test

import (
"bufio"
"fmt"
"io"
"os"
"strings"
"testing"

serverservice "go.hollow.sh/serverservice/pkg/api/v1"
)

func TestParseXlsxFile(t *testing.T) {
var testCases = []struct {
testName string
filePath string
expectedParseErr bool
expectedErrorMsg string
expectedBom []*serverservice.Bom
expectedAOCMac []*serverservice.AocMacAddressBom
expectedBMCMac []*serverservice.BmcMacAddressBom
}{
{
testName: "file missing serial number",
filePath: "./testdata/test_empty_serial.xlsx",
expectedParseErr: true,
expectedErrorMsg: "empty serial number: Invalid Argument",
expectedBom: nil,
expectedAOCMac: nil,
expectedBMCMac: nil,
},
{
testName: "file missing aocMacAddress ",
filePath: "./testdata/test_empty_aocMacAddress.xlsx",
expectedParseErr: true,
expectedErrorMsg: "empty aocMacAddress: Invalid Argument",
expectedBom: nil,
expectedAOCMac: nil,
expectedBMCMac: nil,
},
{
testName: "file missing bmcMacAddress",
filePath: "./testdata/test_empty_bmcMacAddress.xlsx",
expectedParseErr: true,
expectedErrorMsg: "empty bmcMacAddress: Invalid Argument",
expectedBom: nil,
expectedAOCMac: nil,
expectedBMCMac: nil,
},
{
testName: "valid file for single bom",
filePath: "./testdata/test_valid_bom.xlsx",
expectedParseErr: false,
expectedErrorMsg: "",
expectedBom: []*serverservice.Bom{
{
SerialNum: "test-serial-1",
AocMacAddress: "FakeAOC1,FakeAOC2",
BmcMacAddress: "FakeMac1,FakeMac2",
NumDefipmi: "FakeDEFI1",
NumDefpwd: "FakeDEFPWD1",
},
},
expectedAOCMac: []*serverservice.AocMacAddressBom{
{
AocMacAddress: "FakeAOC1",
SerialNum: "test-serial-1",
},
{
AocMacAddress: "FakeAOC2",
SerialNum: "test-serial-1",
},
},
expectedBMCMac: []*serverservice.BmcMacAddressBom{
{
BmcMacAddress: "FakeMac1",
SerialNum: "test-serial-1",
},
{
BmcMacAddress: "FakeMac2",
SerialNum: "test-serial-1",
},
},
},
// {
// testName: "valid file for multiple bom",
// filePath: "./testdata/test_valid_multiple_bom.xlsx",
// expectedParseErr: false,
// expectedErrorMsg: "",
// expectedBom: []*serverservice.Bom{
// {
// SerialNum: "test-serial-1",
// AocMacAddress: "FakeAOC1,FakeAOC2",
// BmcMacAddress: "FakeMac1,FakeMac2",
// NumDefipmi: "FakeDEFI1",
// NumDefpwd: "FakeDEFPWD1",
// },
// {
// SerialNum: "test-serial-2",
// AocMacAddress: "FakeAOC3,FakeAOC4",
// BmcMacAddress: "FakeMac3,FakeMac4",
// NumDefipmi: "FakeDEFI2",
// NumDefpwd: "FakeDEFPWD2",
// },
// },
// expectedAOCMac: []*serverservice.AocMacAddressBom{
// {
// AocMacAddress: "FakeAOC1",
// SerialNum: "test-serial-1",
// },
// {
// AocMacAddress: "FakeAOC2",
// SerialNum: "test-serial-1",
// },
// {
// AocMacAddress: "FakeAOC3",
// SerialNum: "test-serial-2",
// },
// {
// AocMacAddress: "FakeAOC4",
// SerialNum: "test-serial-2",
// },
// },
// expectedBMCMac: []*serverservice.BmcMacAddressBom{
// {
// BmcMacAddress: "FakeMac1",
// SerialNum: "test-serial-1",
// },
// {
// BmcMacAddress: "FakeMac2",
// SerialNum: "test-serial-1",
// },
// {
// BmcMacAddress: "FakeMac3",
// SerialNum: "test-serial-2",
// },
// {
// BmcMacAddress: "FakeMac4",
// SerialNum: "test-serial-2",
// },
// },
// },
}

for _, tt := range testCases {
t.Run(tt.testName, func(t *testing.T) {
file, err := os.Open(tt.filePath)
if err != nil {
t.Fatalf("err= %v\n", err)
}

stat, err := file.Stat()
if err != nil {
fmt.Println(err)
return
}

bs := make([]byte, stat.Size())
_, err = bufio.NewReader(file).Read(bs)
if err != nil && err != io.EOF {
fmt.Println(err)
return
}

bomsMap, aocMacBomMap, bmcMacBomMap, err := serverservice.ParseXlsxFile(bs)

if tt.expectedParseErr {
if !strings.Contains(err.Error(), tt.expectedErrorMsg) {
t.Fatalf("test %v failed, got %v, expect %v", tt.testName, err, tt.expectedErrorMsg)
}
if bomsMap != nil || aocMacBomMap != nil || bmcMacBomMap != nil {
t.Fatalf("test %v expect nil bomsMap, aocMacBomMap, bmcMacBomMap, got %v, %v, %v", tt.testName, bomsMap, aocMacBomMap, bmcMacBomMap)
}
}

if len(bomsMap) != len(tt.expectedBom) {
t.Fatalf("test %v failed to parse xlsx files, got %v, expect %v", tt.testName, bomsMap, tt.expectedBom)
}
for i := range bomsMap {
if *bomsMap[i] != *tt.expectedBom[i] {
t.Fatalf("test %v failed to parse xlsx files, got %v, expect %v", tt.testName, *bomsMap[i], *tt.expectedBom[i])
}
}
if len(aocMacBomMap) != len(tt.expectedAOCMac) {
t.Fatalf("test %v failed to parse xlsx files, got %v, expect %v", tt.testName, aocMacBomMap, tt.expectedAOCMac)
}

for i := range aocMacBomMap {
if *aocMacBomMap[i] != *tt.expectedAOCMac[i] {
t.Fatalf("test %v failed to parse xlsx files, got %v, expect %v", tt.testName, *aocMacBomMap[i], *tt.expectedAOCMac[i])
}
}
if len(bmcMacBomMap) != len(tt.expectedBMCMac) {
t.Fatalf("test %v failed to parse xlsx files, got %v, expect %v", tt.testName, bmcMacBomMap, tt.expectedBMCMac)
}
for i := range bmcMacBomMap {
if *bmcMacBomMap[i] != *tt.expectedBMCMac[i] {
t.Fatalf("test %v failed to parse xlsx files, got %v, expect %v", tt.testName, *bmcMacBomMap[i], *tt.expectedBMCMac[i])
}
}

})
}
}
Binary file added pkg/api/v1/testdata/test_empty_aocMacAddress.xlsx
Binary file not shown.
Binary file added pkg/api/v1/testdata/test_empty_bmcMacAddress.xlsx
Binary file not shown.
Binary file added pkg/api/v1/testdata/test_empty_serial.xlsx
Binary file not shown.
Binary file added pkg/api/v1/testdata/test_valid_bom.xlsx
Binary file not shown.

0 comments on commit 7808d57

Please sign in to comment.