forked from metal-toolbox/hollow-serverservice
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(deps): update module github.com/google/uuid to v1.3.1 (metal-tool…
…box#233) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
- Loading branch information
1 parent
50b7634
commit 7808d57
Showing
11 changed files
with
336 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.