Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thom/add device groups to device list #86

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module github.com/redhatinsights/edge-api

require (
github.com/aws/aws-sdk-go v1.43.37
github.com/aws/aws-sdk-go v1.43.38
github.com/bxcodec/faker/v3 v3.8.0
github.com/cavaliercoder/grab v2.0.0+incompatible
github.com/confluentinc/confluent-kafka-go v1.8.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:W
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/aws/aws-sdk-go v1.38.51/go.mod h1:hcU610XS61/+aQV88ixoOzUoG7v3b31pl2zKMmprdro=
github.com/aws/aws-sdk-go v1.43.37 h1:kyZ7UjaPZaCik+asF33UFOOYSwr9liDRr/UM/vuw8yY=
github.com/aws/aws-sdk-go v1.43.37/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go v1.43.38 h1:TDRjsUIsx2aeSuKkyzbwgltIRTbIKH6YCZbZ27JYhPk=
github.com/aws/aws-sdk-go v1.43.38/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down
23 changes: 13 additions & 10 deletions pkg/models/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type DeviceDetails struct {
Device EdgeDevice `json:"Device,omitempty"`
Image *ImageInfo `json:"ImageInfo"`
UpdateTransactions *[]UpdateTransaction `json:"UpdateTransactions,omitempty"`
DevicesGroups *[]DeviceGroup `json:"DevicesGroups,omitempty"`
}

// DeviceDetailsList is the list of devices with details from Inventory and Edge API
Expand Down Expand Up @@ -46,6 +47,7 @@ type DeviceView struct {
LastSeen string `json:"LastSeen"`
UpdateAvailable bool `json:"UpdateAvailable"`
Status string `json:"Status"`
ImageSetID uint `json:"ImageSetID"`
}

// Device is a record of Edge Devices referenced by their UUID as per the
Expand All @@ -57,14 +59,15 @@ type DeviceView struct {
// THEEDGE-1921 created 2 temporary indexes to address production issue
type Device struct {
Model
UUID string `gorm:"index" json:"UUID"`
AvailableHash string `json:"AvailableHash,omitempty"`
RHCClientID string `json:"RHCClientID"`
Connected bool `gorm:"default:true" json:"Connected"`
Name string `json:"Name"`
LastSeen EdgeAPITime `json:"LastSeen"`
CurrentHash string `json:"CurrentHash,omitempty"`
Account string `gorm:"index" json:"Account"`
ImageID uint `json:"ImageID"`
UpdateAvailable bool `json:"UpdateAvailable"`
UUID string `gorm:"index" json:"UUID"`
AvailableHash string `json:"AvailableHash,omitempty"`
RHCClientID string `json:"RHCClientID"`
Connected bool `gorm:"default:true" json:"Connected"`
Name string `json:"Name"`
LastSeen EdgeAPITime `json:"LastSeen"`
CurrentHash string `json:"CurrentHash,omitempty"`
Account string `gorm:"index" json:"Account"`
ImageID uint `json:"ImageID"`
UpdateAvailable bool `json:"UpdateAvailable"`
DevicesGroups []DeviceGroup `json:"DevicesGroups" gorm:"many2many:device_groups_devices;"`
}
28 changes: 16 additions & 12 deletions pkg/routes/updates.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ func updateFromHTTP(w http.ResponseWriter, r *http.Request) (*[]models.UpdateTra
w.WriteHeader(err.GetStatus())
return nil, err
}
//validate if commit is valid before continue process
commit, err := services.CommitService.GetCommitByID(devicesUpdate.CommitID)
services.Log.WithField("commit", commit.ID).Debug("Commit retrieved from this update")

if err != nil {
services.Log.WithFields(log.Fields{
"error": err.Error(),
"commitID": devicesUpdate.CommitID,
}).Error("No commit found for Commit ID")
err := errors.NewNotFound(err.Error())
err.SetTitle(fmt.Sprintf("No commit found for CommitID %d", devicesUpdate.CommitID))
w.WriteHeader(err.GetStatus())
return nil, err
}

// TODO: Implement update by tag - Add validation per tag
if devicesUpdate.DevicesUUID == nil {
err := errors.NewBadRequest("DeviceUUID required.")
Expand Down Expand Up @@ -237,8 +252,7 @@ func updateFromHTTP(w http.ResponseWriter, r *http.Request) (*[]models.UpdateTra
}

// Get the models.Commit from the Commit ID passed in via JSON
update.Commit, err = services.CommitService.GetCommitByID(devicesUpdate.CommitID)
services.Log.WithField("commit", update.Commit).Debug("Commit retrieved from this update")
update.Commit = commit

notify, errNotify := services.UpdateService.SendDeviceNotification(&update)
if errNotify != nil {
Expand All @@ -247,16 +261,6 @@ func updateFromHTTP(w http.ResponseWriter, r *http.Request) (*[]models.UpdateTra

}
update.DispatchRecords = []models.DispatchRecord{}
if err != nil {
services.Log.WithFields(log.Fields{
"error": err.Error(),
"commitID": devicesUpdate.CommitID,
}).Error("No commit found for Commit ID")
err := errors.NewInternalServerError()
err.SetTitle(fmt.Sprintf("No commit found for CommitID %d", devicesUpdate.CommitID))
w.WriteHeader(err.GetStatus())
return nil, err
}

// Removing commit dependency to avoid overwriting the repo
var repo *models.Repo
Expand Down
33 changes: 26 additions & 7 deletions pkg/services/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func (s *DeviceService) GetDeviceByID(deviceID uint) (*models.Device, error) {
s.log.WithField("error", result.Error.Error()).Error("Error finding device")
return nil, new(DeviceNotFoundError)
}
//Load from device DB the groups info and add to the new struct
db.DB.Model(&device).Association("DevicesGroups").Find(&device.DevicesGroups)
return &device, nil
}

Expand All @@ -116,6 +118,8 @@ func (s *DeviceService) GetDeviceByUUID(deviceUUID string) (*models.Device, erro
s.log.WithField("error", result.Error.Error()).Error("Error finding device")
return nil, new(DeviceNotFoundError)
}
//Load from device DB the groups info and add to the new struct
db.DB.Model(&device).Association("DevicesGroups").Find(&device.DevicesGroups)
return &device, nil
}

Expand Down Expand Up @@ -159,6 +163,8 @@ func (s *DeviceService) GetDeviceDetails(device inventory.Device) (*models.Devic
},
Image: imageInfo,
UpdateTransactions: updates,
//Given we are concat the info from inventory with our db, we need to add this field to the struct to be able to see the result
DevicesGroups: &databaseDevice.DevicesGroups,
}
return details, nil
}
Expand Down Expand Up @@ -373,8 +379,10 @@ func (s *DeviceService) GetDevices(params *inventory.Params) (*models.DeviceDeta
if res := db.DB.Where("account = ? AND uuid IN ?", account, devicesUUIDs).Find(&storedDevices); res.Error != nil {
return nil, res.Error
}

mapDevicesUUIDToID := make(map[string]uint, len(devicesUUIDs))
for _, device := range storedDevices {

mapDevicesUUIDToID[device.UUID] = device.ID
}

Expand All @@ -384,13 +392,20 @@ func (s *DeviceService) GetDevices(params *inventory.Params) (*models.DeviceDeta
if !ok {
dbDeviceID = 0
}

//Load from device DB the groups info and add to the new struct
var storeDevice models.Device
db.DB.Where("id=?", dbDeviceID).First(&storeDevice)
db.DB.Model(&storeDevice).Association("DevicesGroups").Find(&storeDevice.DevicesGroups)

dd := models.DeviceDetails{}
dd.Device = models.EdgeDevice{
Device: &models.Device{
Model: models.Model{ID: dbDeviceID},
UUID: device.ID,
RHCClientID: device.Ostree.RHCClientID,
Account: device.Account,
Model: models.Model{ID: dbDeviceID},
UUID: device.ID,
RHCClientID: device.Ostree.RHCClientID,
Account: device.Account,
DevicesGroups: storeDevice.DevicesGroups,
},
Account: device.Account,
DeviceName: device.DisplayName,
Expand Down Expand Up @@ -590,8 +605,9 @@ func (s *DeviceService) GetDevicesView(limit int, offset int, tx *gorm.DB) (*mod
}

type neededImageInfo struct {
Name string
Status string
Name string
Status string
ImageSetID uint
}
// create a map of unique image id's. We dont want to look of a given image id more than once.
setOfImages := make(map[uint]*neededImageInfo)
Expand All @@ -613,7 +629,7 @@ func (s *DeviceService) GetDevicesView(limit int, offset int, tx *gorm.DB) (*mod
}

for _, image := range images {
setOfImages[image.ID] = &neededImageInfo{Name: image.Name, Status: image.Status}
setOfImages[image.ID] = &neededImageInfo{Name: image.Name, Status: image.Status, ImageSetID: *image.ImageSetID}
}

// build the return object
Expand All @@ -622,9 +638,11 @@ func (s *DeviceService) GetDevicesView(limit int, offset int, tx *gorm.DB) (*mod
for _, device := range storedDevices {
var imageName string
var imageStatus string
var imageSetID uint
if _, ok := setOfImages[device.ImageID]; ok {
imageName = setOfImages[device.ImageID].Name
imageStatus = setOfImages[device.ImageID].Status
imageSetID = setOfImages[device.ImageID].ImageSetID
}
currentDeviceView := models.DeviceView{
DeviceID: device.ID,
Expand All @@ -635,6 +653,7 @@ func (s *DeviceService) GetDevicesView(limit int, offset int, tx *gorm.DB) (*mod
LastSeen: device.LastSeen.Time.String(),
UpdateAvailable: device.UpdateAvailable,
Status: imageStatus,
ImageSetID: imageSetID,
}
returnDevices = append(returnDevices, currentDeviceView)
}
Expand Down