-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FS-878 Add bom router handlers and client APIs to upload Boms and fet…
…ch Bom(by AocMacAddress) (#240) FS-878 Add bom related handler for 2 APIs(both server and client) in server service use transaction for bomsupload remove unused and sensitive code and file
- Loading branch information
Showing
8 changed files
with
577 additions
and
10 deletions.
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,78 @@ | ||
package serverservice | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/volatiletech/null/v8" | ||
|
||
"go.hollow.sh/serverservice/internal/models" | ||
) | ||
|
||
// Bom provides a struct to map the bom_info table. | ||
// Naming conversion is strange here just in order to make it consistent | ||
// with generated BomInfo. | ||
type Bom struct { | ||
SerialNum string `json:"serial_num"` // physical serial number listed outside of a server | ||
AocMacAddress string `json:"aoc_mac_address"` // Aoc is alternative name of the fiber channel card MAC address | ||
BmcMacAddress string `json:"bmc_mac_address"` | ||
NumDefiPmi string `json:"num_defi_pmi"` | ||
NumDefPWD string `json:"num_def_pwd"` // DefPWD is the IPMI Password in the portal | ||
Metro string `json:"metro"` | ||
} | ||
|
||
// AocMacAddressBom provides a struct to map the aoc_mac_address table. | ||
type AocMacAddressBom struct { | ||
AocMacAddress string `json:"aoc_mac_address"` | ||
SerialNum string `json:"serial_num"` | ||
} | ||
|
||
// toDBModel converts Bom to BomInfo. | ||
func (b *Bom) toDBModel() (*models.BomInfo, error) { | ||
if b.SerialNum == "" { | ||
return nil, errors.Errorf("the primary key serial-num can not be blank") | ||
} | ||
|
||
dbB := &models.BomInfo{ | ||
SerialNum: b.SerialNum, | ||
AocMacAddress: null.StringFrom(b.AocMacAddress), | ||
BMCMacAddress: null.StringFrom(b.BmcMacAddress), | ||
NumDefiPmi: null.StringFrom(b.NumDefiPmi), | ||
NumDefPWD: null.StringFrom(b.NumDefPWD), | ||
Metro: null.StringFrom(b.Metro), | ||
} | ||
|
||
return dbB, nil | ||
} | ||
|
||
// toDBModel converts BomInfo to Bom. | ||
func (b *Bom) fromDBModel(bomInfo *models.BomInfo) error { | ||
b.SerialNum = bomInfo.SerialNum | ||
b.AocMacAddress = bomInfo.AocMacAddress.String | ||
b.BmcMacAddress = bomInfo.BMCMacAddress.String | ||
b.NumDefiPmi = bomInfo.NumDefiPmi.String | ||
b.NumDefPWD = bomInfo.NumDefPWD.String | ||
b.Metro = bomInfo.Metro.String | ||
|
||
return nil | ||
} | ||
|
||
// toAocMacAddressDBModels converts Bom to one or multiple AocMacAddress. | ||
func (b *Bom) toAocMacAddressDBModels() ([]*models.AocMacAddress, error) { | ||
if b.AocMacAddress == "" { | ||
return nil, errors.Errorf("the primary key aoc-mac-address can not be blank") | ||
} | ||
|
||
dbAs := []*models.AocMacAddress{} | ||
|
||
AocMacAddrs := strings.Split(b.AocMacAddress, ",") | ||
for _, aocMacAddr := range AocMacAddrs { | ||
dbA := &models.AocMacAddress{ | ||
SerialNum: b.SerialNum, | ||
AocMacAddress: aocMacAddr, | ||
} | ||
dbAs = append(dbAs, dbA) | ||
} | ||
|
||
return dbAs, 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
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,81 @@ | ||
package serverservice | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/cockroachdb/cockroach-go/v2/crdb" | ||
"github.com/gin-gonic/gin" | ||
"github.com/volatiletech/sqlboiler/v4/boil" | ||
"github.com/volatiletech/sqlboiler/v4/queries/qm" | ||
|
||
"go.hollow.sh/serverservice/internal/models" | ||
) | ||
|
||
func (r *Router) bomsUpload(c *gin.Context) { | ||
var boms []Bom | ||
if err := c.ShouldBindJSON(&boms); err != nil { | ||
badRequestResponse(c, "invalid payload: []Bom{}", err) | ||
return | ||
} | ||
|
||
err := crdb.ExecuteTx(c.Request.Context(), r.DB.DB, nil, func(tx *sql.Tx) error { | ||
for _, bom := range boms { | ||
dbBomInfo, err := (bom).toDBModel() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := dbBomInfo.Insert(c.Request.Context(), r.DB, boil.Infer()); err != nil { | ||
return err | ||
} | ||
|
||
dbAocMacAddrsBoms, err := (bom).toAocMacAddressDBModels() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, dbAocMacAddrsBom := range dbAocMacAddrsBoms { | ||
if err := dbAocMacAddrsBom.Insert(c.Request.Context(), r.DB, boil.Infer()); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
dbErrorResponse(c, err) | ||
return | ||
} | ||
|
||
createdResponse(c, "") | ||
} | ||
|
||
func (r *Router) getBomFromAocMacAddress(c *gin.Context) { | ||
mods := []qm.QueryMod{ | ||
qm.Where("aoc_mac_address=?", c.Param("aoc_mac_address")), | ||
} | ||
|
||
aocMacAddr, err := models.AocMacAddresses(mods...).One(c.Request.Context(), r.DB) | ||
if err != nil { | ||
dbErrorResponse(c, err) | ||
return | ||
} | ||
|
||
mods = []qm.QueryMod{ | ||
qm.Where("serial_num=?", aocMacAddr.SerialNum), | ||
} | ||
|
||
bomInfo, err := models.BomInfos(mods...).One(c.Request.Context(), r.DB) | ||
if err != nil { | ||
dbErrorResponse(c, err) | ||
return | ||
} | ||
|
||
bom := Bom{} | ||
if err = bom.fromDBModel(bomInfo); err != nil { | ||
dbErrorResponse(c, err) | ||
return | ||
} | ||
|
||
itemResponse(c, bom) | ||
} |
Oops, something went wrong.