diff --git a/base/notification/notification.go b/base/notification/notification.go index 7c2f98cdf..8cd7166ce 100644 --- a/base/notification/notification.go +++ b/base/notification/notification.go @@ -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{} @@ -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" { @@ -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, diff --git a/evaluator/evaluate.go b/evaluator/evaluate.go index c2905389d..7e83028b4 100644 --- a/evaluator/evaluate.go +++ b/evaluator/evaluate.go @@ -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(), diff --git a/evaluator/notifications.go b/evaluator/notifications.go index 288052950..397d40542 100644 --- a/evaluator/notifications.go +++ b/evaluator/notifications.go @@ -7,6 +7,7 @@ import ( "app/base/mqueue" ntf "app/base/notification" "app/base/utils" + "encoding/json" "time" "github.com/pkg/errors" @@ -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") } @@ -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") } @@ -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") diff --git a/evaluator/notifications_test.go b/evaluator/notifications_test.go index 23b063698..2173d06a6 100644 --- a/evaluator/notifications_test.go +++ b/evaluator/notifications_test.go @@ -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) @@ -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) @@ -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) + } +}