From 1e2b04416db78c897f5a1aa43f5c9fe71b5e4386 Mon Sep 17 00:00:00 2001 From: Patrik Segedy Date: Tue, 18 Jun 2024 13:10:43 +0200 Subject: [PATCH 1/3] RHINENG-10237: add joins param for common queries --- base/database/utils.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/base/database/utils.go b/base/database/utils.go index 8cf79a7bd..f2d5a6066 100644 --- a/base/database/utils.go +++ b/base/database/utils.go @@ -16,34 +16,49 @@ import ( "gorm.io/gorm" ) -func Systems(tx *gorm.DB, accountID int, groups map[string]string) *gorm.DB { +type join func(*gorm.DB) *gorm.DB +type joinsT []join + +func (j joinsT) apply(tx *gorm.DB) *gorm.DB { + for _, join := range j { + tx = join(tx) + } + return tx +} + +func Systems(tx *gorm.DB, accountID int, groups map[string]string, joins ...join) *gorm.DB { tx = tx.Table("system_platform sp").Where("sp.rh_account_id = ?", accountID) + tx = (joinsT)(joins).apply(tx) return InventoryHostsJoin(tx, groups) } -func SystemAdvisories(tx *gorm.DB, accountID int, groups map[string]string) *gorm.DB { - return Systems(tx, accountID, groups). +func SystemAdvisories(tx *gorm.DB, accountID int, groups map[string]string, joins ...join) *gorm.DB { + tx = Systems(tx, accountID, groups). Joins("JOIN system_advisories sa on sa.system_id = sp.id AND sa.rh_account_id = ?", accountID) + return (joinsT)(joins).apply(tx) } -func SystemPackagesShort(tx *gorm.DB, accountID int) *gorm.DB { - return tx.Table("system_package2 spkg"). +func SystemPackagesShort(tx *gorm.DB, accountID int, joins ...join) *gorm.DB { + tx = tx.Table("system_package2 spkg"). Where("spkg.rh_account_id = ?", accountID) + return (joinsT)(joins).apply(tx) } -func SystemPackages(tx *gorm.DB, accountID int, groups map[string]string) *gorm.DB { - return Systems(tx, accountID, groups). +func SystemPackages(tx *gorm.DB, accountID int, groups map[string]string, joins ...join) *gorm.DB { + tx = Systems(tx, accountID, groups). Joins("JOIN system_package2 spkg on spkg.system_id = sp.id AND spkg.rh_account_id = ?", accountID). Joins("JOIN package p on p.id = spkg.package_id"). Joins("JOIN package_name pn on pn.id = spkg.name_id") + return (joinsT)(joins).apply(tx) } -func Packages(tx *gorm.DB) *gorm.DB { - return tx.Table("package p"). +func Packages(tx *gorm.DB, joins ...join) *gorm.DB { + tx = tx.Table("package p"). Joins("JOIN package_name pn on p.name_id = pn.id"). Joins("JOIN strings descr ON p.description_hash = descr.id"). Joins("JOIN strings sum ON p.summary_hash = sum.id"). Joins("LEFT JOIN advisory_metadata am ON p.advisory_id = am.id") + return (joinsT)(joins).apply(tx) } func PackageByName(tx *gorm.DB, pkgName string) *gorm.DB { From 3cdaa0bcf6a7172adacdd0ef5cb4746ced62db98 Mon Sep 17 00:00:00 2001 From: Patrik Segedy Date: Tue, 18 Jun 2024 13:18:02 +0200 Subject: [PATCH 2/3] RHINENG-10237: show template name and id in systems endpoints --- base/database/utils.go | 6 ++++ docs/v3/openapi.json | 36 ++++++++++++++++++++++++ manager/controllers/advisory_systems.go | 4 +-- manager/controllers/common_attributes.go | 5 ++++ manager/controllers/package_systems.go | 4 +-- manager/controllers/system_detail.go | 3 +- manager/controllers/systems.go | 5 ++-- 7 files changed, 54 insertions(+), 9 deletions(-) diff --git a/base/database/utils.go b/base/database/utils.go index f2d5a6066..4c23b851e 100644 --- a/base/database/utils.go +++ b/base/database/utils.go @@ -250,3 +250,9 @@ func InventoryHostsJoin(tx *gorm.DB, groups map[string]string) *gorm.DB { } return tx.Where(db) } + +// LEFT JOIN templates to sp (system_platform) +func JoinTemplates(tx *gorm.DB) *gorm.DB { + return tx.Joins("LEFT JOIN baseline bl ON sp.baseline_id = bl.id AND sp.rh_account_id = bl.rh_account_id"). + Joins("LEFT JOIN template t ON sp.template_id = t.id AND sp.rh_account_id = t.rh_account_id") +} diff --git a/docs/v3/openapi.json b/docs/v3/openapi.json index 88a46c109..bec746932 100644 --- a/docs/v3/openapi.json +++ b/docs/v3/openapi.json @@ -7118,6 +7118,12 @@ "items": { "$ref": "#/components/schemas/controllers.SystemTag" } + }, + "template_id": { + "type": "integer" + }, + "template_name": { + "type": "string" } } }, @@ -7191,6 +7197,12 @@ "items": { "$ref": "#/components/schemas/controllers.SystemTag" } + }, + "template_id": { + "type": "integer" + }, + "template_name": { + "type": "string" } } }, @@ -7913,6 +7925,12 @@ "$ref": "#/components/schemas/controllers.SystemTag" } }, + "template_id": { + "type": "integer" + }, + "template_name": { + "type": "string" + }, "updatable": { "type": "boolean" }, @@ -8191,6 +8209,12 @@ "items": { "$ref": "#/components/schemas/controllers.SystemTag" } + }, + "template_id": { + "type": "integer" + }, + "template_name": { + "type": "string" } } }, @@ -8328,6 +8352,12 @@ "items": { "$ref": "#/components/schemas/controllers.SystemTag" } + }, + "template_id": { + "type": "integer" + }, + "template_name": { + "type": "string" } } }, @@ -8451,6 +8481,12 @@ "$ref": "#/components/schemas/controllers.SystemTag" } }, + "template_id": { + "type": "integer" + }, + "template_name": { + "type": "string" + }, "third_party": { "type": "boolean" } diff --git a/manager/controllers/advisory_systems.go b/manager/controllers/advisory_systems.go index eed715e06..23ec4b61a 100644 --- a/manager/controllers/advisory_systems.go +++ b/manager/controllers/advisory_systems.go @@ -43,6 +43,7 @@ type AdvisorySystemItemAttributes struct { SystemGroups BaselineIDAttr BaselineNameAttr + TemplateAttibutes SystemAdvisoryStatus SystemSatelliteManaged SystemBuiltPkgcache @@ -271,11 +272,10 @@ func AdvisorySystemsListIDsHandler(c *gin.Context) { func buildAdvisorySystemsQuery(db *gorm.DB, account int, groups map[string]string, advisoryName string) *gorm.DB { selectQuery := AdvisorySystemsSelect - query := database.SystemAdvisories(db, account, groups). + query := database.SystemAdvisories(db, account, groups, database.JoinTemplates). Select(selectQuery). Joins("JOIN advisory_metadata am ON am.id = sa.advisory_id"). Joins("LEFT JOIN status st ON sa.status_id = st.id"). - Joins("LEFT JOIN baseline bl ON sp.baseline_id = bl.id AND sp.rh_account_id = bl.rh_account_id"). Where("am.name = ?", advisoryName). Where("sp.stale = false") diff --git a/manager/controllers/common_attributes.go b/manager/controllers/common_attributes.go index 3dcc74c21..83a9b56d9 100644 --- a/manager/controllers/common_attributes.go +++ b/manager/controllers/common_attributes.go @@ -45,6 +45,11 @@ type BaselineIDAttr struct { BaselineID int64 `json:"baseline_id" csv:"baseline_id" query:"bl.id" gorm:"column:baseline_id"` } +type TemplateAttibutes struct { + TemplateName string `json:"template_name" csv:"template_name" query:"t.name" gorm:"column:template_name"` + TemplateID int64 `json:"template_id" csv:"template_id" query:"t.id" gorm:"column:template_id"` +} + type SystemDisplayName struct { DisplayName string `json:"display_name" csv:"display_name" query:"sp.display_name" gorm:"column:display_name"` } diff --git a/manager/controllers/package_systems.go b/manager/controllers/package_systems.go index 9598733ac..fbc61ee2f 100644 --- a/manager/controllers/package_systems.go +++ b/manager/controllers/package_systems.go @@ -31,6 +31,7 @@ type PackageSystemItem struct { Updatable bool `json:"updatable" csv:"updatable" query:"(spkg.installable_id IS NOT NULL)" gorm:"column:updatable"` SystemTags BaselineAttributes + TemplateAttibutes // helper to get AvailableEVRA (latest_evra) InstallableEVRA string `json:"-" csv:"-" query:"pi.evra" gorm:"column:installable_evra"` ApplicableEVRA string `json:"-" csv:"-" query:"pa.evra" gorm:"column:applicable_evra"` @@ -61,11 +62,10 @@ func packagesByNameQuery(db *gorm.DB, pkgName string) *gorm.DB { func packageSystemsQuery(db *gorm.DB, acc int, groups map[string]string, packageName string, packageIDs []int, ) *gorm.DB { - query := database.SystemPackages(db, acc, groups). + query := database.SystemPackages(db, acc, groups, database.JoinTemplates). Select(PackageSystemsSelect). Joins("LEFT JOIN package pi ON pi.id = spkg.installable_id"). Joins("LEFT JOIN package pa ON pa.id = spkg.applicable_id"). - Joins("LEFT JOIN baseline bl ON sp.baseline_id = bl.id AND sp.rh_account_id = bl.rh_account_id"). Where("sp.stale = false"). Where("pn.name = ?", packageName). Where("spkg.package_id in (?)", packageIDs) diff --git a/manager/controllers/system_detail.go b/manager/controllers/system_detail.go index 096b766d3..2bd267ddc 100644 --- a/manager/controllers/system_detail.go +++ b/manager/controllers/system_detail.go @@ -63,9 +63,8 @@ func SystemDetailHandler(c *gin.Context) { var systemDetail SystemDetailLookup db := middlewares.DBFromContext(c) - query := database.Systems(db, account, groups). + query := database.Systems(db, account, groups, database.JoinTemplates). Select(database.MustGetSelect(&systemDetail)). - Joins("LEFT JOIN baseline bl ON sp.baseline_id = bl.id AND sp.rh_account_id = bl.rh_account_id"). Where("sp.inventory_id = ?::uuid", inventoryID) err := query.Take(&systemDetail).Error diff --git a/manager/controllers/systems.go b/manager/controllers/systems.go index 9ee06e739..4f9ea20f7 100644 --- a/manager/controllers/systems.go +++ b/manager/controllers/systems.go @@ -93,6 +93,7 @@ type SystemItemAttributes struct { ApplicableRheaCount int `json:"applicable_rhea_count" csv:"applicable_rhea_count" query:"sp.applicable_advisory_enh_count_cache" gorm:"column:applicable_rhea_count"` ApplicableOtherCount int `json:"applicable_other_count" csv:"applicable_other_count" query:"(sp.applicable_advisory_count_cache - sp.installable_advisory_sec_count_cache - sp.installable_advisory_bug_count_cache - sp.installable_advisory_enh_count_cache)" gorm:"column:applicable_other_count"` BaselineIDAttr + TemplateAttibutes SystemGroups } @@ -345,7 +346,5 @@ func SystemsListIDsHandler(c *gin.Context) { } func querySystems(db *gorm.DB, account int, groups map[string]string) *gorm.DB { - q := database.Systems(db, account, groups). - Joins("LEFT JOIN baseline bl ON sp.baseline_id = bl.id AND sp.rh_account_id = bl.rh_account_id") - return q.Select(SystemsSelect) + return database.Systems(db, account, groups, database.JoinTemplates).Select(SystemsSelect) } From 93d4928f43321955e738ef3f5974ab546a398981 Mon Sep 17 00:00:00 2001 From: Patrik Segedy Date: Tue, 18 Jun 2024 13:46:27 +0200 Subject: [PATCH 3/3] RHINENG-10237: add templates to export tests --- manager/controllers/advisory_systems_export_test.go | 4 ++-- manager/controllers/package_systems_export_test.go | 7 ++++--- manager/controllers/systems_export_test.go | 4 ++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/manager/controllers/advisory_systems_export_test.go b/manager/controllers/advisory_systems_export_test.go index 2ffee2c56..7fe8fe90a 100644 --- a/manager/controllers/advisory_systems_export_test.go +++ b/manager/controllers/advisory_systems_export_test.go @@ -32,11 +32,11 @@ func TestAdvisorySystemsExportCSV(t *testing.T) { assert.Equal(t, 8, len(lines)) assert.Equal(t, "display_name,last_upload,stale,os,rhsm,stale_timestamp,stale_warning_timestamp,culled_timestamp,created,tags,"+ - "groups,baseline_id,baseline_name,status,satellite_managed,built_pkgcache,id", lines[0]) + "groups,baseline_id,baseline_name,template_name,template_id,status,satellite_managed,built_pkgcache,id", lines[0]) assert.Equal(t, "00000000-0000-0000-0000-000000000001,2020-09-22T16:00:00Z,false,RHEL 8.10,8.10,2018-08-26T16:00:00Z,"+ "2018-09-02T16:00:00Z,2018-09-09T16:00:00Z,2018-08-26T16:00:00Z,\"[{'key':'k1','namespace':'ns1','value':'val1'},"+ "{'key':'k2','namespace':'ns1','value':'val2'}]\",\"[{'id':'inventory-group-1','name':'group1'}]\","+ - "1,baseline_1-1,Installable,false,false,00000000-0000-0000-0000-000000000001", + "1,baseline_1-1,temp1-1,1,Installable,false,false,00000000-0000-0000-0000-000000000001", lines[1]) } diff --git a/manager/controllers/package_systems_export_test.go b/manager/controllers/package_systems_export_test.go index 9cea03ee8..c4297bacb 100644 --- a/manager/controllers/package_systems_export_test.go +++ b/manager/controllers/package_systems_export_test.go @@ -37,13 +37,14 @@ func TestPackageSystemsExportHandlerCSV(t *testing.T) { assert.Equal(t, 5, len(lines)) assert.Equal(t, "id,display_name,installed_evra,available_evra,updatable,tags,"+ - "baseline_name,baseline_uptodate,satellite_managed,baseline_id,os,rhsm,update_status,groups", lines[0]) + "baseline_name,baseline_uptodate,template_name,template_id,satellite_managed,baseline_id,os,rhsm,"+ + "update_status,groups", lines[0]) assert.Equal(t, "00000000-0000-0000-0000-000000000012,00000000-0000-0000-0000-000000000012,"+ "5.6.13-200.fc31.x86_64,5.10.13-200.fc31.x86_64,true,"+ - "\"[{'key':'k1','namespace':'ns1','value':'val1'}]\",,,false,0,RHEL 8.1,8.1,Installable,[]", + "\"[{'key':'k1','namespace':'ns1','value':'val1'}]\",,,,0,false,0,RHEL 8.1,8.1,Installable,[]", lines[1]) assert.Equal(t, "00000000-0000-0000-0000-000000000013,00000000-0000-0000-0000-000000000013,"+ - "5.6.13-200.fc31.x86_64,,false,\"[{'key':'k1','namespace':'ns1','value':'val1'}]\",,,"+ + "5.6.13-200.fc31.x86_64,,false,\"[{'key':'k1','namespace':'ns1','value':'val1'}]\",,,,0,"+ "false,0,RHEL 8.2,8.2,None,[]", lines[2]) } diff --git a/manager/controllers/systems_export_test.go b/manager/controllers/systems_export_test.go index 230b73fc9..068ad35ab 100644 --- a/manager/controllers/systems_export_test.go +++ b/manager/controllers/systems_export_test.go @@ -18,7 +18,7 @@ var SystemCsvHeader = "id,display_name,os,rhsm,tags,last_evaluation," + "satellite_managed,built_pkgcache,packages_installable,packages_applicable," + "installable_rhsa_count,installable_rhba_count,installable_rhea_count,installable_other_count," + "applicable_rhsa_count,applicable_rhba_count,applicable_rhea_count,applicable_other_count," + - "baseline_id,groups" + "baseline_id,template_name,template_id,groups" func makeRequest(t *testing.T, path string, contentType string) *httptest.ResponseRecorder { core.SetupTest(t) @@ -60,7 +60,7 @@ func TestSystemsExportCSV(t *testing.T) { "\"[{'key':'k1','namespace':'ns1','value':'val1'},{'key':'k2','namespace':'ns1','value':'val2'}]\","+ "2018-09-22T16:00:00Z,2,2,1,0,0,baseline_1-1,"+ "2020-09-22T16:00:00Z,2018-08-26T16:00:00Z,2018-09-02T16:00:00Z,2018-09-09T16:00:00Z,2018-08-26T16:00:00Z,"+ - "false,false,false,0,0,2,2,1,0,2,3,3,3,1,\"[{'id':'inventory-group-1','name':'group1'}]\"", + "false,false,false,0,0,2,2,1,0,2,3,3,3,1,temp1-1,1,\"[{'id':'inventory-group-1','name':'group1'}]\"", lines[1]) }