clustvirt/util/pcidb.go
Matthew Stobbs 8e62f7e271 merge migrate_to_libvirtxml
completed migration to libvirtxml
2024-03-23 22:05:06 -06:00

47 lines
937 B
Go

package util
import (
"git.staur.ca/stobbsm/clustvirt/lib/log"
"github.com/jaypipes/pcidb"
)
var (
pcidbInitDone = false
db *pcidb.PCIDB
)
const (
pcidbNOTFOUND string = `NOTFOUND`
pcidbNODB string = `NODBFOUND`
)
func initPCIDB() {
var err error
// Attempt to use local sources first, fallback to network if
// local sources aren't found
db, err = pcidb.New()
if err != nil {
log.Warn("util.initPCIDB").Err(err).Msg("fallback to downloading pcidb")
db, err = pcidb.New(pcidb.WithEnableNetworkFetch())
if err != nil {
log.Error("util.initPCIDB").Err(err).Msg("no more fallbacks available")
}
}
pcidbInitDone = true
}
func GetPCIClass(id string) string {
if !pcidbInitDone {
initPCIDB()
}
if pcidbInitDone && db == nil {
log.Warn("util.GetPCIClass").Msg("no pcidb to do lookup")
return pcidbNODB
}
if class, ok := db.Classes[id]; ok {
return class.Name
}
return pcidbNOTFOUND
}