Skip to content

Commit de8eec6

Browse files
RHINENG-26450: update InventoryID type from string to uuid
1 parent 743010c commit de8eec6

68 files changed

Lines changed: 502 additions & 450 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

base/database/query.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/google/uuid"
1314
"github.com/pkg/errors"
1415
)
1516

@@ -60,6 +61,13 @@ func parserForType(v reflect.Type) (AttrParser, error) {
6061
}, nil
6162
case reflect.Ptr:
6263
return parserForType(v.Elem())
64+
case reflect.Array:
65+
if v == reflect.TypeOf(uuid.UUID{}) {
66+
return func(s string) (i interface{}, err error) {
67+
return uuid.Parse(s)
68+
}, nil
69+
}
70+
fallthrough
6371
case reflect.Struct:
6472
if reflect.TypeOf(time.Time{}).AssignableTo(v) {
6573
return func(s string) (i interface{}, err error) {

base/database/testing.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ func CheckPackagesNamesInDB(t *testing.T, filter string, packageNames ...string)
159159
assert.Equal(t, int64(len(packageNames)), count)
160160
}
161161

162-
func CheckSystemJustEvaluated(t *testing.T, inventoryID string, nIAll, nIEnh, nIBug, nISec,
162+
func CheckSystemJustEvaluated(t *testing.T, inventoryID uuid.UUID, nIAll, nIEnh, nIBug, nISec,
163163
nAAll, nAEnh, nABug, nASec, nInstall, nInstallable, nApplicable int,
164164
thirdParty bool) {
165165
var inv models.SystemInventory
166-
assert.Nil(t, DB.Where("inventory_id = ?::uuid", inventoryID).First(&inv).Error)
166+
assert.Nil(t, DB.Where("inventory_id = ?", inventoryID).First(&inv).Error)
167167
var patch models.SystemPatch
168168
assert.Nil(t, DB.Where("rh_account_id = ? AND system_id = ?", inv.RhAccountID, inv.ID).First(&patch).Error)
169169
assert.NotNil(t, patch.LastEvaluation)
@@ -427,7 +427,7 @@ func GetPackageIDs(nevras ...string) []int64 {
427427
return ids
428428
}
429429

430-
func CreateTemplate(t *testing.T, account int, uuid string, inventoryIDs []string) {
430+
func CreateTemplate(t *testing.T, account int, uuid string, inventoryIDs []uuid.UUID) {
431431
template := &models.Template{
432432
TemplateBase: models.TemplateBase{
433433
RhAccountID: account, UUID: uuid, Name: uuid,
@@ -451,7 +451,7 @@ func CreateTemplate(t *testing.T, account int, uuid string, inventoryIDs []strin
451451
SELECT id
452452
FROM system_inventory
453453
WHERE rh_account_id = ?
454-
AND inventory_id = ?::uuid
454+
AND inventory_id = ?
455455
)`,
456456
template.ID, account, account, invID).Error
457457
assert.Nil(t, err)
@@ -477,21 +477,21 @@ func DeleteTemplate(t *testing.T, account int, templateUUID string) {
477477
assert.Nil(t, err)
478478
}
479479

480-
func CheckTemplateSystems(t *testing.T, account int, templateUUID string, inventoryIDs []string) {
481-
var dbInventoryIDs []string
480+
func CheckTemplateSystems(t *testing.T, account int, templateUUID string, inventoryIDs []uuid.UUID) {
481+
var foundInventoryIDs []uuid.UUID
482482
err := DB.Table("system_inventory si").Select("si.inventory_id as id").
483483
Joins("JOIN system_patch spatch ON si.id = spatch.system_id AND si.rh_account_id = spatch.rh_account_id").
484484
Joins("JOIN template tp ON tp.id = spatch.template_id AND tp.rh_account_id = spatch.rh_account_id").
485485
Where("si.rh_account_id = ? AND tp.uuid = ?::uuid", account, templateUUID).
486486
Order("id").
487-
Find(&dbInventoryIDs).Error
487+
Find(&foundInventoryIDs).Error
488488

489489
assert.Nil(t, err)
490490

491-
assert.Equal(t, len(inventoryIDs), len(dbInventoryIDs))
492-
if len(inventoryIDs) == len(dbInventoryIDs) {
491+
assert.Equal(t, len(inventoryIDs), len(foundInventoryIDs))
492+
if len(inventoryIDs) == len(foundInventoryIDs) {
493493
for index, inventoryID := range inventoryIDs {
494-
assert.Equal(t, inventoryID, dbInventoryIDs[index])
494+
assert.Equal(t, inventoryID, foundInventoryIDs[index])
495495
}
496496
}
497497
}

base/database/utils.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strings"
1212
"time"
1313

14+
"github.com/google/uuid"
1415
log "github.com/sirupsen/logrus"
1516
"gorm.io/driver/postgres"
1617
"gorm.io/gorm"
@@ -65,9 +66,9 @@ func PackageByName(tx *gorm.DB, pkgName string, joins ...join) *gorm.DB {
6566
return (joinsT)(joins).apply(tx)
6667
}
6768

68-
func SystemAdvisoriesByInventoryID(tx *gorm.DB, accountID int, groups map[string]string, inventoryID string,
69+
func SystemAdvisoriesByInventoryID(tx *gorm.DB, accountID int, groups map[string]string, inventoryID uuid.UUID,
6970
joins ...join) *gorm.DB {
70-
tx = SystemAdvisories(tx, accountID, groups).Where("si.inventory_id = ?::uuid", inventoryID)
71+
tx = SystemAdvisories(tx, accountID, groups).Where("si.inventory_id = ?", inventoryID)
7172
return (joinsT)(joins).apply(tx)
7273
}
7374

base/inventory_views/inventory_views.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"app/base/utils"
66
"time"
77

8+
"github.com/google/uuid"
89
"gorm.io/gorm"
910
)
1011

1112
type InventoryViewsHost struct {
1213
// Inventory ID (UUID) of the host
13-
ID string `json:"id"`
14+
ID uuid.UUID `json:"id"`
1415
Data InventoryViewsHostData `json:"data"`
1516
}
1617

base/inventory_views/inventory_views_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99
"time"
1010

11+
"github.com/google/uuid"
1112
"github.com/stretchr/testify/assert"
1213
"gorm.io/gorm"
1314
)
@@ -51,13 +52,13 @@ func TestMakeInventoryViewsEvent(t *testing.T) {
5152
assert.Equal(t, 2, len(event.Hosts))
5253

5354
assert.Equal(t, InventoryViewsHost{
54-
ID: "00000000-0000-0000-0000-000000000001",
55+
ID: uuid.MustParse("00000000-0000-0000-0000-000000000001"),
5556
Data: InventoryViewsHostData{2, 3, 3, 0, 2, 2, 1, 0, 0, 0, 0,
5657
utils.PtrString("temp1-1"), utils.PtrString("99900000-0000-0000-0000-000000000001")},
5758
}, event.Hosts[0])
5859

5960
assert.Equal(t, InventoryViewsHost{
60-
ID: "00000000-0000-0000-0000-000000000003",
61+
ID: uuid.MustParse("00000000-0000-0000-0000-000000000003"),
6162
Data: InventoryViewsHostData{0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
6263
utils.PtrString("temp2-1"), utils.PtrString("99900000-0000-0000-0000-000000000002")},
6364
}, event.Hosts[1])
@@ -98,7 +99,7 @@ func TestMakeInventoryViewsEventNoTemplate(t *testing.T) {
9899
assert.Equal(t, 1, len(event.Hosts))
99100

100101
host := event.Hosts[0]
101-
assert.Equal(t, "00000000-0000-0000-0000-000000000004", host.ID)
102+
assert.Equal(t, uuid.MustParse("00000000-0000-0000-0000-000000000004"), host.ID)
102103
// Template fields should be nil when template is not found
103104
assert.Nil(t, host.Data.TemplateName)
104105
assert.Nil(t, host.Data.TemplateUUID)

base/models/models.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ func (TemplateBase) TableName() string {
5454
}
5555

5656
type SystemInventory struct {
57-
ID int64 `gorm:"primaryKey"`
58-
InventoryID string `gorm:"unique"`
59-
RhAccountID int `gorm:"primaryKey"`
57+
ID int64 `gorm:"primaryKey"`
58+
InventoryID uuid.UUID `gorm:"unique"`
59+
RhAccountID int `gorm:"primaryKey"`
6060
VmaasJSON *string
6161
JSONChecksum *string
6262
LastUpdated *time.Time `gorm:"default:null"` // set by trigger system_inventory_set_last_updated
@@ -94,9 +94,9 @@ func (SystemInventory) TableName() string {
9494
return "system_inventory"
9595
}
9696

97-
func (s *SystemInventory) GetInventoryID() string {
97+
func (s *SystemInventory) GetInventoryID() uuid.UUID {
9898
if s == nil {
99-
return ""
99+
return uuid.Nil
100100
}
101101
return s.InventoryID
102102
}
@@ -133,9 +133,9 @@ type SystemPlatformV2 struct {
133133
Patch SystemPatch `gorm:"embedded"`
134134
}
135135

136-
func (v *SystemPlatformV2) GetInventoryID() string {
136+
func (v *SystemPlatformV2) GetInventoryID() uuid.UUID {
137137
if v == nil {
138-
return ""
138+
return uuid.Nil
139139
}
140140
return v.Inventory.GetInventoryID()
141141
}
@@ -200,7 +200,7 @@ type PackageUpdate struct {
200200
}
201201

202202
type DeletedSystem struct {
203-
InventoryID string
203+
InventoryID uuid.UUID
204204
WhenDeleted time.Time
205205
}
206206

base/mqueue/mqueue_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import (
77
"sync"
88
"testing"
99

10+
"github.com/google/uuid"
1011
"github.com/stretchr/testify/assert"
1112
)
1213

13-
const id = "99c0ffee-0000-0000-0000-0000c0ffee99"
14-
const someid = "99c0ffee-0000-0000-0000-0000000050de"
14+
var id = uuid.MustParse("99c0ffee-0000-0000-0000-0000c0ffee99")
15+
var someid = uuid.MustParse("99c0ffee-0000-0000-0000-0000000050de")
1516

16-
var msg = KafkaMessage{Value: []byte(`{"id": "` + id + `", "type": "delete"}`)}
17+
var msg = KafkaMessage{Value: []byte(`{"id": "` + id.String() + `", "type": "delete"}`)}
1718

1819
func TestParseEvents(t *testing.T) {
1920
reached := false

base/mqueue/payload_tracker_event.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77
"time"
88

99
"github.com/bytedance/sonic"
10+
"github.com/google/uuid"
1011
"github.com/pkg/errors"
1112
)
1213

1314
type PayloadTrackerEvent struct {
1415
Service string `json:"service"`
1516
OrgID *string `json:"org_id,omitempty"`
1617
RequestID *string `json:"request_id"`
17-
InventoryID string `json:"inventory_id"`
18+
InventoryID uuid.UUID `json:"inventory_id"`
1819
Status string `json:"status"`
1920
StatusMsg string `json:"status_msg,omitempty"`
2021
Date *types.Rfc3339TimestampWithZ `json:"date"`

base/mqueue/platform_event.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,26 @@ import (
66
"time"
77

88
"github.com/bytedance/sonic"
9+
"github.com/google/uuid"
910
"github.com/pkg/errors"
1011
)
1112

13+
// PlatformEvent ID is typed as uuid.UUID to match the inventory service contract:
14+
// https://github.com/RedHatInsights/insights-host-inventory/blob/master/swagger/host_events.spec.yaml
1215
type PlatformEvent struct {
13-
ID string `json:"id"`
16+
ID uuid.UUID `json:"id"`
1417
Type *string `json:"type"`
1518
Timestamp *types.Rfc3339Timestamp `json:"timestamp"`
1619
AccountID int `json:"account_id"`
1720
OrgID *string `json:"org_id,omitempty"`
1821
B64Identity *string `json:"b64_identity"`
1922
URL *string `json:"url"`
20-
SystemIDs []string `json:"system_ids,omitempty"`
23+
SystemIDs []uuid.UUID `json:"system_ids,omitempty"`
2124
RequestIDs []string `json:"request_ids,omitempty"`
2225
}
2326

2427
type EvalData struct {
25-
InventoryID string
28+
InventoryID uuid.UUID
2629
RhAccountID int
2730
RequestID string
2831
OrgID *string
@@ -31,7 +34,7 @@ type EvalData struct {
3134
type PlatformEvents []PlatformEvent
3235
type EvalDataSlice []EvalData
3336

34-
type accountInventories map[int][]string
37+
type accountInventories map[int][]uuid.UUID
3538
type accountRequests map[int][]string
3639
type orgIDs map[int]*string
3740

@@ -69,7 +72,7 @@ func writePlatformEvents(ctx context.Context, w Writer, events ...PlatformEvent)
6972
return w.WriteMessages(ctx, msgs...)
7073
}
7174

72-
func batchSize(grouped map[int][]string) int {
75+
func batchSize(grouped map[int][]uuid.UUID) int {
7376
// compute how many batches we will create
7477
var batches = 0
7578
for _, ev := range grouped {

base/mqueue/platform_event_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ import (
55
"testing"
66

77
"github.com/bytedance/sonic"
8+
"github.com/google/uuid"
89
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestWriteEventsOfInventoryAccounts(t *testing.T) {
12-
const (
13+
var (
1314
acc = 1
14-
inv2 = "00000000-0000-0000-0000-000000000002"
15-
inv3 = "00000000-0000-0000-0000-000000000003"
15+
inv2 = uuid.MustParse("00000000-0000-0000-0000-000000000002")
16+
inv3 = uuid.MustParse("00000000-0000-0000-0000-000000000003")
1617
)
1718

1819
var writer Writer = &MockKafkaWriter{}

0 commit comments

Comments
 (0)