Skip to content

Commit 28ba98c

Browse files
committed
RHINENG-21444: update workspace field to use struct
1 parent bb1a275 commit 28ba98c

4 files changed

Lines changed: 42 additions & 15 deletions

File tree

base/inventory/inventory.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package inventory
22

33
import (
44
"app/base/types"
5+
"database/sql/driver"
6+
"encoding/json"
7+
"errors"
58

69
"github.com/google/uuid"
710
)
@@ -83,6 +86,35 @@ type Group struct {
8386
Name string `json:"name"`
8487
}
8588

89+
// Groups is a slice of Group that implements driver.Valuer and sql.Scanner
90+
// for storing/loading as JSONB in the database (e.g. system_inventory.workspaces).
91+
type Groups []Group
92+
93+
// Value implements driver.Valuer for GORM: marshal to JSON for DB write.
94+
func (g *Groups) Value() (driver.Value, error) {
95+
if g == nil {
96+
return nil, nil
97+
}
98+
return json.Marshal(g)
99+
}
100+
101+
// Scan implements sql.Scanner for GORM: unmarshal from JSON on DB read.
102+
func (g *Groups) Scan(value interface{}) error {
103+
if value == nil {
104+
*g = nil
105+
return nil
106+
}
107+
b, ok := value.([]byte)
108+
if !ok {
109+
return errors.New("inventory.Groups: type assertion to []byte failed")
110+
}
111+
if len(b) == 0 {
112+
*g = nil
113+
return nil
114+
}
115+
return json.Unmarshal(b, g)
116+
}
117+
86118
type Workloads struct {
87119
Sap SapWorkload `json:"sap,omitempty"`
88120
Ansible AnsibleWorkload `json:"ansible,omitempty"`

base/models/models.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package models
22

33
import (
4+
"app/base/inventory"
45
"time"
56

67
"github.com/google/uuid"
@@ -118,9 +119,9 @@ type SystemInventory struct {
118119
BuiltPkgcache bool `gorm:"column:built_pkgcache"`
119120
Arch *string
120121
Bootc bool
121-
Tags []byte `gorm:"column:tags"`
122-
Created time.Time // set by trigger system_platform_insert_trigger
123-
Workspaces []byte `gorm:"column:workspaces"`
122+
Tags []byte `gorm:"column:tags"`
123+
Created time.Time // set by trigger system_platform_insert_trigger
124+
Workspaces *inventory.Groups `gorm:"column:workspaces"`
124125
StaleTimestamp *time.Time
125126
StaleWarningTimestamp *time.Time
126127
CulledTimestamp *time.Time

listener/upload.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,7 @@ func storeOrUpdateSysPlatform(
428428
},
429429
})
430430

431-
workspaces, err := json.Marshal(host.Groups)
432-
if err != nil {
433-
return errors.Wrap(err, "marshalling groups")
434-
}
435-
431+
hostWorkspaces := inventory.Groups(host.Groups)
436432
inventoryRecord := models.SystemInventory{
437433
ID: system.ID,
438434
InventoryID: system.InventoryID,
@@ -449,7 +445,7 @@ func storeOrUpdateSysPlatform(
449445
Arch: system.Arch,
450446
Bootc: system.Bootc,
451447
Tags: utils.MarshalNilToJSONB(host.Tags),
452-
Workspaces: utils.MarshalNilToJSONB(workspaces),
448+
Workspaces: &hostWorkspaces,
453449
StaleTimestamp: system.StaleTimestamp,
454450
StaleWarningTimestamp: system.StaleWarningTimestamp,
455451
CulledTimestamp: system.CulledTimestamp,
@@ -466,7 +462,7 @@ func storeOrUpdateSysPlatform(
466462
MssqlWorkloadVersion: utils.EmptyToNil(&host.SystemProfile.Workloads.Mssql.Version),
467463
}
468464

469-
err = database.OnConflictUpdateMulti(txi, []string{"rh_account_id", "inventory_id"}, colsToUpdate...).
465+
err := database.OnConflictUpdateMulti(txi, []string{"rh_account_id", "inventory_id"}, colsToUpdate...).
470466
Create(&inventoryRecord).Error
471467
if err != nil {
472468
return base.WrapFatalDBError(err, "unable to insert to system_inventory")

listener/upload_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"app/base/utils"
1212
"app/base/vmaas"
1313
"context"
14-
"encoding/json"
1514
"errors"
1615
"fmt"
1716
"net/http"
@@ -22,6 +21,7 @@ import (
2221
"github.com/lib/pq"
2322
log "github.com/sirupsen/logrus"
2423
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/require"
2525
)
2626

2727
var accountID = int(1)
@@ -395,10 +395,8 @@ func TestStoreOrUpdateSysPlatform(t *testing.T) {
395395
assert.Contains(t, string(inventoryAfterInsert.Tags), `"key": "env"`)
396396
assert.Contains(t, string(inventoryAfterInsert.Tags), `"value": "prod"`)
397397

398-
var expectedWorkspaces []inventory.Group
399-
err = json.Unmarshal(inventoryAfterInsert.Workspaces, &expectedWorkspaces)
400-
assert.Nil(t, err)
401-
assert.Equal(t, hostEvent.Host.Groups, expectedWorkspaces)
398+
require.NotNil(t, inventoryAfterInsert.Workspaces)
399+
assert.Equal(t, hostEvent.Host.Groups, []inventory.Group(*inventoryAfterInsert.Workspaces))
402400

403401
assert.Equal(t, hostEvent.Host.SystemProfile.OperatingSystem.Name, *inventoryAfterInsert.OSName)
404402
assert.Equal(t, hostEvent.Host.SystemProfile.OperatingSystem.Major, *inventoryAfterInsert.OSMajor)

0 commit comments

Comments
 (0)