You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.7 KiB
75 lines
2.7 KiB
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/analogj/scrutiny/webapp/backend/pkg"
|
|
"github.com/analogj/scrutiny/webapp/backend/pkg/models"
|
|
"github.com/analogj/scrutiny/webapp/backend/pkg/models/collector"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// Device
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//insert device into DB (and update specified columns if device is already registered)
|
|
// update device fields that may change: (DeviceType, HostID)
|
|
func (sr *scrutinyRepository) RegisterDevice(ctx context.Context, dev models.Device) error {
|
|
if err := sr.gormClient.WithContext(ctx).Clauses(clause.OnConflict{
|
|
Columns: []clause.Column{{Name: "wwn"}},
|
|
DoUpdates: clause.AssignmentColumns([]string{"host_id", "device_name", "device_type"}),
|
|
}).Create(&dev).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// get a list of all devices (only device metadata, no SMART data)
|
|
func (sr *scrutinyRepository) GetDevices(ctx context.Context) ([]models.Device, error) {
|
|
//Get a list of all the active devices.
|
|
devices := []models.Device{}
|
|
if err := sr.gormClient.WithContext(ctx).Find(&devices).Error; err != nil {
|
|
return nil, fmt.Errorf("Could not get device summary from DB: %v", err)
|
|
}
|
|
return devices, nil
|
|
}
|
|
|
|
// update device (only metadata) from collector
|
|
func (sr *scrutinyRepository) UpdateDevice(ctx context.Context, wwn string, collectorSmartData collector.SmartInfo) (models.Device, error) {
|
|
var device models.Device
|
|
if err := sr.gormClient.WithContext(ctx).Where("wwn = ?", wwn).First(&device).Error; err != nil {
|
|
return device, fmt.Errorf("Could not get device from DB: %v", err)
|
|
}
|
|
|
|
//TODO catch GormClient err
|
|
err := device.UpdateFromCollectorSmartInfo(collectorSmartData)
|
|
if err != nil {
|
|
return device, err
|
|
}
|
|
return device, sr.gormClient.Model(&device).Updates(device).Error
|
|
}
|
|
|
|
//Update Device Status
|
|
func (sr *scrutinyRepository) UpdateDeviceStatus(ctx context.Context, wwn string, status pkg.DeviceStatus) (models.Device, error) {
|
|
var device models.Device
|
|
if err := sr.gormClient.WithContext(ctx).Where("wwn = ?", wwn).First(&device).Error; err != nil {
|
|
return device, fmt.Errorf("Could not get device from DB: %v", err)
|
|
}
|
|
|
|
device.DeviceStatus = pkg.Set(device.DeviceStatus, status)
|
|
return device, sr.gormClient.Model(&device).Updates(device).Error
|
|
}
|
|
|
|
func (sr *scrutinyRepository) GetDeviceDetails(ctx context.Context, wwn string) (models.Device, error) {
|
|
var device models.Device
|
|
|
|
fmt.Println("GetDeviceDetails from GORM")
|
|
|
|
if err := sr.gormClient.WithContext(ctx).Where("wwn = ?", wwn).First(&device).Error; err != nil {
|
|
return models.Device{}, err
|
|
}
|
|
|
|
return device, nil
|
|
}
|