-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,108 additions
and
883 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,54 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/icinga/icinga-notifications/internal/recipient" | ||
"github.com/jmoiron/sqlx" | ||
"go.uber.org/zap" | ||
"slices" | ||
) | ||
|
||
func (r *RuntimeConfig) fetchContacts(ctx context.Context, tx *sqlx.Tx) error { | ||
var contactPtr *recipient.Contact | ||
stmt := r.db.BuildSelectStmt(contactPtr, contactPtr) | ||
r.logger.Debugf("Executing query %q", stmt) | ||
|
||
var contacts []*recipient.Contact | ||
if err := tx.SelectContext(ctx, &contacts, stmt); err != nil { | ||
r.logger.Errorln(err) | ||
return err | ||
} | ||
|
||
contactsByID := make(map[int64]*recipient.Contact) | ||
for _, c := range contacts { | ||
contactsByID[c.ID] = c | ||
|
||
r.logger.Debugw("loaded contact config", | ||
zap.Int64("id", c.ID), | ||
zap.String("name", c.FullName)) | ||
} | ||
|
||
if r.Contacts != nil { | ||
// mark no longer existing contacts for deletion | ||
for id := range r.Contacts { | ||
if _, ok := contactsByID[id]; !ok { | ||
contactsByID[id] = nil | ||
func (r *RuntimeConfig) applyPendingContacts() { | ||
incrementalApplyPending( | ||
r, | ||
&r.Contacts, &r.configChange.Contacts, | ||
nil, | ||
func(curElement, update *recipient.Contact) error { | ||
curElement.FullName = update.FullName | ||
curElement.Username = update.Username | ||
curElement.DefaultChannelID = update.DefaultChannelID | ||
return nil | ||
}, | ||
nil) | ||
|
||
incrementalApplyPending( | ||
r, | ||
&r.ContactAddresses, &r.configChange.ContactAddresses, | ||
func(newElement *recipient.Address) error { | ||
contact, ok := r.Contacts[newElement.ContactID] | ||
if !ok { | ||
return fmt.Errorf("contact address refers unknown contact %d", newElement.ContactID) | ||
} | ||
} | ||
} | ||
|
||
r.pending.Contacts = contactsByID | ||
|
||
return nil | ||
} | ||
|
||
func (r *RuntimeConfig) applyPendingContacts() { | ||
if r.Contacts == nil { | ||
r.Contacts = make(map[int64]*recipient.Contact) | ||
} | ||
contact.Addresses = append(contact.Addresses, newElement) | ||
return nil | ||
}, | ||
func(curElement, update *recipient.Address) error { | ||
if curElement.ContactID != update.ContactID { | ||
return reAddUpdateFnErr | ||
} | ||
|
||
for id, pendingContact := range r.pending.Contacts { | ||
if pendingContact == nil { | ||
delete(r.Contacts, id) | ||
} else if currentContact := r.Contacts[id]; currentContact != nil { | ||
currentContact.FullName = pendingContact.FullName | ||
currentContact.Username = pendingContact.Username | ||
currentContact.DefaultChannelID = pendingContact.DefaultChannelID | ||
} else { | ||
r.Contacts[id] = pendingContact | ||
} | ||
} | ||
curElement.Type = update.Type | ||
curElement.Address = update.Address | ||
return nil | ||
}, | ||
func(delElement *recipient.Address) error { | ||
contact, ok := r.Contacts[delElement.ContactID] | ||
if !ok { | ||
return fmt.Errorf("contact address refers unknown contact %d", delElement.ContactID) | ||
} | ||
|
||
r.pending.Contacts = nil | ||
contact.Addresses = slices.DeleteFunc(contact.Addresses, func(address *recipient.Address) bool { | ||
return address.ID == delElement.ID | ||
}) | ||
return nil | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.