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-13828: add system tags to notification context #1531

Merged
merged 2 commits into from
Nov 12, 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
16 changes: 12 additions & 4 deletions base/notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const (
)

type Context struct {
InventoryID string `json:"inventory_id"`
DisplayName string `json:"display_name"`
HostURL string `json:"host_url"`
InventoryID string `json:"inventory_id"`
DisplayName string `json:"display_name"`
HostURL string `json:"host_url"`
Tags []SystemTag `json:"tags"`
}

type Metadata struct{}
Expand Down Expand Up @@ -73,7 +74,13 @@ type Advisory struct {
Synopsis string `json:"synopsis"`
}

func MakeNotification(system *models.SystemPlatform, event *mqueue.PlatformEvent,
type SystemTag struct {
Key string `json:"key,omitempty"`
Namespace string `json:"namespace,omitempty"`
Value string `json:"value,omitempty"`
}

func MakeNotification(system *models.SystemPlatform, systemTags []SystemTag, event *mqueue.PlatformEvent,
eventType string, events []Event) (*Notification, error) {
orgID := event.GetOrgID()
if orgID == "" || orgID == "null" {
Expand All @@ -91,6 +98,7 @@ func MakeNotification(system *models.SystemPlatform, event *mqueue.PlatformEvent
InventoryID: system.InventoryID,
DisplayName: system.DisplayName,
HostURL: event.GetURL(),
Tags: systemTags,
},
Events: events,
OrgID: orgID,
Expand Down
2 changes: 1 addition & 1 deletion evaluator/evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func evaluateAndStore(system *models.SystemPlatform,

// Send instant notification with new advisories
if enableInstantNotifications {
err = publishNewAdvisoriesNotification(tx, system, event, system.RhAccountID, systemAdvisoriesNew)
err = publishNewAdvisoriesNotification(tx, system, event, systemAdvisoriesNew)
if err != nil {
evaluationCnt.WithLabelValues("error-advisory-notification").Inc()
utils.LogError("orgID", event.GetOrgID(), "inventoryID", system.GetInventoryID(), "err", err.Error(),
Expand Down
37 changes: 33 additions & 4 deletions evaluator/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"app/base/mqueue"
ntf "app/base/notification"
"app/base/utils"
"encoding/json"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -63,13 +64,36 @@ func getUnnotifiedAdvisories(tx *gorm.DB, accountID int, newAdvs SystemAdvisoryM
return unAdvs, nil
}

func getSystemTags(tx *gorm.DB, system *models.SystemPlatform) ([]ntf.SystemTag, error) {
if system == nil {
return nil, nil
}

var tags []ntf.SystemTag
var tagsJSON string
err := tx.Table("system_platform sp").
Select("ih.tags").
Joins("JOIN inventory.hosts ih ON sp.inventory_id = ih.id").
Where("sp.rh_account_id = ?", system.RhAccountID).
Where("sp.id = ?", system.ID).
Scan(&tagsJSON).Error
if err != nil {
return nil, errors.Wrap(err, "system tags query failed")
}
if err = json.Unmarshal([]byte(tagsJSON), &tags); err != nil {
return nil, errors.Wrap(err, "system tags unmarshal failed")
}

return tags, nil
}

func publishNewAdvisoriesNotification(tx *gorm.DB, system *models.SystemPlatform, event *mqueue.PlatformEvent,
accountID int, newAdvisories SystemAdvisoryMap) error {
newAdvisories SystemAdvisoryMap) error {
if notificationsPublisher == nil {
return nil
}

advisories, err := getUnnotifiedAdvisories(tx, accountID, newAdvisories)
advisories, err := getUnnotifiedAdvisories(tx, system.RhAccountID, newAdvisories)
if err != nil {
return errors.Wrap(err, "getting unnotified advisories failed")
}
Expand All @@ -83,7 +107,12 @@ func publishNewAdvisoriesNotification(tx *gorm.DB, system *models.SystemPlatform
events = append(events, ntf.Event{Payload: advisory, Metadata: ntf.Metadata{}})
}

notif, err := ntf.MakeNotification(system, event, NewAdvisoryEvent, events)
tags, err := getSystemTags(tx, system)
if err != nil {
return errors.Wrap(err, "getting system tags failed")
}

notif, err := ntf.MakeNotification(system, tags, event, NewAdvisoryEvent, events)
if err != nil {
return errors.Wrap(err, "creating notification failed")
}
Expand All @@ -107,7 +136,7 @@ func publishNewAdvisoriesNotification(tx *gorm.DB, system *models.SystemPlatform
"notification sent successfully")

err = tx.Table("advisory_account_data").
Where("rh_account_id = ? AND advisory_id IN (?)", accountID, advisoryIDs).
Where("rh_account_id = ? AND advisory_id IN (?)", system.RhAccountID, advisoryIDs).
Update("notified", time.Now()).Error
if err != nil {
return errors.Wrap(err, "updating notified column failed")
Expand Down
25 changes: 24 additions & 1 deletion evaluator/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func TestAdvisoriesNotificationMessage(t *testing.T) {
InventoryID: inventoryID,
DisplayName: displayName,
}
tags := []ntf.SystemTag{{Key: "key", Namespace: "namespace", Value: "value"}}

orgID := "1234567"
url := fmt.Sprintf("www.console.redhat.com/insights/inventory/%s", inventoryID)
Expand All @@ -109,12 +110,13 @@ func TestAdvisoriesNotificationMessage(t *testing.T) {
URL: &url,
}

notification, err := ntf.MakeNotification(system, event, NewAdvisoryEvent, events)
notification, err := ntf.MakeNotification(system, tags, event, NewAdvisoryEvent, events)
assert.Nil(t, err)
assert.Equal(t, orgID, notification.OrgID)
assert.Equal(t, url, notification.Context.HostURL)
assert.Equal(t, inventoryID, notification.Context.InventoryID)
assert.Equal(t, displayName, notification.Context.DisplayName)
assert.Equal(t, tags, notification.Context.Tags)

msg, err := mqueue.MessageFromJSON(inventoryID, notification)
assert.Nil(t, err)
Expand All @@ -124,3 +126,24 @@ func TestAdvisoriesNotificationMessage(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, notificationJSON, msg.Value)
}

func TestGetSystemTags(t *testing.T) {
utils.SkipWithoutDB(t)
core.SetupTestEnvironment()
configure()

system := models.SystemPlatform{
ID: 1,
RhAccountID: 1,
InventoryID: "00000000-0000-0000-0000-000000000001",
DisplayName: "display name",
}
tags, err := getSystemTags(database.DB, &system)
expected := []ntf.SystemTag{
{Key: "k1", Value: "val1", Namespace: "ns1"},
{Key: "k2", Value: "val2", Namespace: "ns1"},
}
if assert.NoError(t, err) {
assert.Equal(t, expected, tags)
}
}
Loading