Skip to content

Commit 2fb9ad2

Browse files
committed
RHINENG-22325: use only necessary subset of Template attributes for Inventory Views
1 parent 6ea8bc2 commit 2fb9ad2

4 files changed

Lines changed: 26 additions & 15 deletions

File tree

base/database/testing.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,12 @@ func DeleteSystem(t *testing.T, inventoryID string) {
400400

401401
func CreateTemplate(t *testing.T, account int, uuid string, inventoryIDs []string) {
402402
template := &models.Template{
403-
RhAccountID: account, UUID: uuid, Name: uuid, EnvironmentID: strings.ReplaceAll(uuid, "-", ""),
404-
Arch: "x86_64", Version: "8",
403+
TemplateBase: models.TemplateBase{
404+
RhAccountID: account, UUID: uuid, Name: uuid,
405+
},
406+
EnvironmentID: strings.ReplaceAll(uuid, "-", ""),
407+
Arch: "x86_64",
408+
Version: "8",
405409
}
406410

407411
tx := DB.Begin()

base/inventory_views/inventory_views.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ type InventoryViewsEvent struct {
3535
Hosts []InventoryViewsHost `json:"hosts"`
3636
}
3737

38-
func MakeInventoryViewsHosts(systems []*models.SystemPlatform, templates map[int64]models.Template) (
38+
func MakeInventoryViewsHosts(systems []*models.SystemPlatform, templates map[int64]models.TemplateBase) (
3939
[]InventoryViewsHost, error) {
4040
hosts := make([]InventoryViewsHost, len(systems))
4141
for i, system := range systems {
@@ -69,7 +69,7 @@ func MakeInventoryViewsHosts(systems []*models.SystemPlatform, templates map[int
6969
return hosts, nil
7070
}
7171

72-
func FindSystemsTemplates(tx *gorm.DB, systems []*models.SystemPlatform) (map[int64]models.Template, error) {
72+
func FindSystemsTemplates(tx *gorm.DB, systems []*models.SystemPlatform) (map[int64]models.TemplateBase, error) {
7373
templateIDs := make([]int64, 0, len(systems))
7474
for _, system := range systems {
7575
if system.TemplateID == nil {
@@ -78,14 +78,15 @@ func FindSystemsTemplates(tx *gorm.DB, systems []*models.SystemPlatform) (map[in
7878
templateIDs = append(templateIDs, *system.TemplateID)
7979
}
8080

81-
templates := make([]models.Template, 0, len(templateIDs))
82-
q := tx.Model(&models.Template{}).Where("id IN (?)", templateIDs).Select("id, name, uuid")
81+
templates := make([]models.TemplateBase, 0, len(templateIDs))
82+
q := tx.Model(&models.TemplateBase{}).
83+
Where("rh_account_id = ? AND id IN (?)", systems[0].RhAccountID, templateIDs)
8384
err := q.Find(&templates).Error
8485
if err != nil {
8586
return nil, err
8687
}
8788

88-
templatesMap := make(map[int64]models.Template, len(templates))
89+
templatesMap := make(map[int64]models.TemplateBase, len(templates))
8990
for _, t := range templates {
9091
templatesMap[t.ID] = t
9192
}

base/models/models.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,16 @@ func (Reporter) TableName() string {
2525
return "reporter"
2626
}
2727

28+
type TemplateBase struct {
29+
ID int64 `gorm:"primaryKey"`
30+
RhAccountID int `gorm:"primaryKey"`
31+
UUID string
32+
Name string
33+
}
34+
2835
type Template struct {
29-
ID int64 `gorm:"primaryKey"`
30-
RhAccountID int `gorm:"primaryKey"`
31-
UUID string
36+
TemplateBase
3237
EnvironmentID string
33-
Name string
3438
Arch string
3539
Version string
3640
// Config pgtype.JSONB // currently unused
@@ -40,7 +44,7 @@ type Template struct {
4044
LastEdited *time.Time
4145
}
4246

43-
func (Template) TableName() string {
47+
func (TemplateBase) TableName() string {
4448
return "template"
4549
}
4650

listener/templates.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,12 @@ func TemplateUpdate(template mqueue.TemplateResponse) error {
118118
}
119119

120120
row := models.Template{
121-
RhAccountID: accountID,
122-
UUID: template.UUID,
121+
TemplateBase: models.TemplateBase{
122+
RhAccountID: accountID,
123+
UUID: template.UUID,
124+
Name: template.Name,
125+
},
123126
EnvironmentID: template.EnvironmentID,
124-
Name: template.Name,
125127
Arch: template.Arch,
126128
Version: template.Version,
127129
//Config: nil,

0 commit comments

Comments
 (0)