Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RHINENG-10237: show template name and id in systems endpoints #1438

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions base/database/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -235,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")
}
36 changes: 36 additions & 0 deletions docs/v3/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -7118,6 +7118,12 @@
"items": {
"$ref": "#/components/schemas/controllers.SystemTag"
}
},
"template_id": {
"type": "integer"
},
"template_name": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -7191,6 +7197,12 @@
"items": {
"$ref": "#/components/schemas/controllers.SystemTag"
}
},
"template_id": {
"type": "integer"
},
"template_name": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -7913,6 +7925,12 @@
"$ref": "#/components/schemas/controllers.SystemTag"
}
},
"template_id": {
"type": "integer"
},
"template_name": {
"type": "string"
},
"updatable": {
"type": "boolean"
},
Expand Down Expand Up @@ -8191,6 +8209,12 @@
"items": {
"$ref": "#/components/schemas/controllers.SystemTag"
}
},
"template_id": {
"type": "integer"
},
"template_name": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -8328,6 +8352,12 @@
"items": {
"$ref": "#/components/schemas/controllers.SystemTag"
}
},
"template_id": {
"type": "integer"
},
"template_name": {
"type": "string"
}
}
},
Expand Down Expand Up @@ -8451,6 +8481,12 @@
"$ref": "#/components/schemas/controllers.SystemTag"
}
},
"template_id": {
"type": "integer"
},
"template_name": {
"type": "string"
},
"third_party": {
"type": "boolean"
}
Expand Down
4 changes: 2 additions & 2 deletions manager/controllers/advisory_systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type AdvisorySystemItemAttributes struct {
SystemGroups
BaselineIDAttr
BaselineNameAttr
TemplateAttibutes
SystemAdvisoryStatus
SystemSatelliteManaged
SystemBuiltPkgcache
Expand Down Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions manager/controllers/advisory_systems_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
5 changes: 5 additions & 0 deletions manager/controllers/common_attributes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
4 changes: 2 additions & 2 deletions manager/controllers/package_systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions manager/controllers/package_systems_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

Expand Down
3 changes: 1 addition & 2 deletions manager/controllers/system_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions manager/controllers/systems.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions manager/controllers/systems_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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])
}

Expand Down
Loading