Skip to content

Commit 353b6b7

Browse files
committed
RHINENG-21443: store host attributes
1 parent 65090d9 commit 353b6b7

2 files changed

Lines changed: 57 additions & 20 deletions

File tree

base/utils/openapi.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ func EmptyToNil(s *string) *string {
3333
}
3434
return s
3535
}
36+
37+
// MarshalNilToJSONB returns JSON bytes suitable for a NOT NULL JSONB column.
38+
func MarshalNilToJSONB(data []byte) []byte {
39+
if len(data) == 0 {
40+
return []byte("[]")
41+
}
42+
return data
43+
}

listener/upload.go

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,12 @@ import (
1919
"net/http"
2020
"net/url"
2121
"regexp"
22+
"slices"
2223
"strings"
2324
"time"
2425

26+
"github.com/lib/pq"
27+
2528
stdErrors "errors"
2629

2730
"github.com/bytedance/sonic"
@@ -89,6 +92,8 @@ type Host struct {
8992
Reporter string `json:"reporter,omitempty"`
9093
SystemProfile inventory.SystemProfile `json:"system_profile,omitempty"`
9194
PerReporterStaleness map[string]inventory.ReporterStaleness `json:"per_reporter_staleness,omitempty"`
95+
Groups []inventory.Group `json:"groups,omitempty"`
96+
Tags json.RawMessage `json:"tags,omitempty"`
9297
ParsedYumUpdates *YumUpdates `json:"-"`
9398
}
9499

@@ -383,7 +388,7 @@ func updateSystemPlatform(tx *gorm.DB, accountID int, host *Host,
383388
colsToUpdate = append(colsToUpdate, "yum_updates", "yum_checksum")
384389
}
385390

386-
if err := storeOrUpdateSysPlatform(tx, &systemPlatform, colsToUpdate); err != nil {
391+
if err := storeOrUpdateSysPlatform(tx, &systemPlatform, host, colsToUpdate); err != nil {
387392
return nil, errors.Wrap(err, "Unable to save or update system in database")
388393
}
389394

@@ -405,7 +410,13 @@ func updateSystemPlatform(tx *gorm.DB, accountID int, host *Host,
405410
return &systemPlatform, nil
406411
}
407412

408-
func storeOrUpdateSysPlatform(tx *gorm.DB, system *models.SystemPlatform, colsToUpdate []string) error {
413+
// nolint: funlen
414+
func storeOrUpdateSysPlatform(
415+
tx *gorm.DB,
416+
system *models.SystemPlatform,
417+
host *Host,
418+
colsToUpdate []string,
419+
) error {
409420
if err := tx.Where("rh_account_id = ? AND inventory_id = ?", system.RhAccountID, system.InventoryID).
410421
Select("id").Find(system).Error; err != nil {
411422
utils.LogWarn("err", err, "couldn't find system for update")
@@ -419,25 +430,43 @@ func storeOrUpdateSysPlatform(tx *gorm.DB, system *models.SystemPlatform, colsTo
419430
},
420431
})
421432

433+
workspaces := make([]string, len(host.Groups))
434+
for i, group := range host.Groups {
435+
workspaces[i] = group.ID
436+
}
437+
slices.Sort(workspaces)
438+
422439
inventoryRecord := models.SystemInventory{
423-
ID: system.ID,
424-
InventoryID: system.InventoryID,
425-
RhAccountID: system.RhAccountID,
426-
DisplayName: system.DisplayName,
427-
LastUpload: system.LastUpload,
428-
SatelliteManaged: system.SatelliteManaged,
429-
BuiltPkgcache: system.BuiltPkgcache,
430-
Arch: system.Arch,
431-
Bootc: system.Bootc,
432-
VmaasJSON: system.VmaasJSON,
433-
JSONChecksum: system.JSONChecksum,
434-
ReporterID: system.ReporterID,
435-
YumUpdates: system.YumUpdates,
436-
YumChecksum: system.YumChecksum,
437-
StaleTimestamp: system.StaleTimestamp,
438-
StaleWarningTimestamp: system.StaleWarningTimestamp,
439-
CulledTimestamp: system.CulledTimestamp,
440-
Tags: []byte("[]"),
440+
ID: system.ID,
441+
InventoryID: system.InventoryID,
442+
RhAccountID: system.RhAccountID,
443+
VmaasJSON: system.VmaasJSON,
444+
JSONChecksum: system.JSONChecksum,
445+
LastUpload: system.LastUpload,
446+
DisplayName: system.DisplayName,
447+
ReporterID: system.ReporterID,
448+
YumUpdates: system.YumUpdates,
449+
YumChecksum: system.YumChecksum,
450+
SatelliteManaged: system.SatelliteManaged,
451+
BuiltPkgcache: system.BuiltPkgcache,
452+
Arch: system.Arch,
453+
Bootc: system.Bootc,
454+
Tags: utils.MarshalNilToJSONB(host.Tags),
455+
Workspaces: pq.StringArray(workspaces),
456+
StaleTimestamp: system.StaleTimestamp,
457+
StaleWarningTimestamp: system.StaleWarningTimestamp,
458+
CulledTimestamp: system.CulledTimestamp,
459+
OSName: utils.EmptyToNil(&host.SystemProfile.OperatingSystem.Name),
460+
OSMajor: &host.SystemProfile.OperatingSystem.Major,
461+
OSMinor: &host.SystemProfile.OperatingSystem.Minor,
462+
RhsmVersion: utils.EmptyToNil(&host.SystemProfile.Rhsm.Version),
463+
SubscriptionManagerID: utils.EmptyToNil(&host.SystemProfile.ConsumerID),
464+
SapWorkload: host.SystemProfile.Workloads.Sap.SapSystem,
465+
SapWorkloadSIDs: pq.StringArray(host.SystemProfile.Workloads.Sap.Sids),
466+
AnsibleWorkload: host.SystemProfile.Workloads.Ansible.ControllerVersion != "",
467+
AnsibleWorkloadControllerVersion: utils.EmptyToNil(&host.SystemProfile.Workloads.Ansible.ControllerVersion),
468+
MssqlWorkload: host.SystemProfile.Workloads.Mssql.Version != "",
469+
MssqlWorkloadVersion: utils.EmptyToNil(&host.SystemProfile.Workloads.Mssql.Version),
441470
}
442471

443472
err := database.OnConflictUpdateMulti(txi, []string{"rh_account_id", "inventory_id"}, colsToUpdate...).

0 commit comments

Comments
 (0)