Skip to content

Commit

Permalink
Populate source table on startup
Browse files Browse the repository at this point in the history
Make the manual INSERT statement into the source table obsolete by
upserting data from the YAML configuration file.
  • Loading branch information
oxzi committed Nov 23, 2023
1 parent 0f45b14 commit 457028d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ It is required that you have created a new database and imported the [schema](sc
> **Note**
> At the moment **PostgreSQL** is the only database backend we support.
Additionally, it also requires you to manually insert items into the **source** table before starting the daemon.
```sql
INSERT INTO source (id, type, name) VALUES (1, 'icinga2', 'Icinga 2')
```

Then, you can launch the daemon with the following command.
```go
go run ./cmd/icinga-notifications-daemon --config config.yml
Expand Down
1 change: 1 addition & 0 deletions cmd/icinga-notifications-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func main() {
}
}

conf.UpsertSources(logs.GetChildLogger("source"), db)
channel.UpsertPlugins(conf.ChannelPluginDir, logs.GetChildLogger("channel"), db)

ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
Expand Down
1 change: 0 additions & 1 deletion icinga2.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ if (!globals.contains("IcingaNotificationsIcingaWebUrl")) {
const IcingaNotificationsIcingaWebUrl = "http://localhost/icingaweb2"
}
if (!globals.contains("IcingaNotificationsEventSourceId")) {
// INSERT INTO source (id, type, name) VALUES (1, 'icinga2', 'Icinga 2')
const IcingaNotificationsEventSourceId = 1
}
if (!globals.contains("IcingaNotificationsAuth")) {
Expand Down
31 changes: 31 additions & 0 deletions internal/daemon/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"github.com/creasty/defaults"
"github.com/goccy/go-yaml"
icingadbConfig "github.com/icinga/icingadb/pkg/config"
"github.com/icinga/icingadb/pkg/icingadb"
"github.com/icinga/icingadb/pkg/logging"
"go.uber.org/zap"
"os"
)

Expand Down Expand Up @@ -105,6 +108,34 @@ func (c *ConfigFile) Validate() error {
return nil
}

// UpsertSources updates the source SQL table to contain all defined ConfigFile.Sources.
func (c *ConfigFile) UpsertSources(logger *logging.Logger, db *icingadb.DB) {
// Source is a subset of ConfigSource to only contain the necessary fields for the DB transaction.
//
// Unfortunately, DB.BuildColumns does include all struct fields with a tag, not only those with the "db" tag. The
// contracts.Upserter interface only results in allowing duplicates on the listed fields, but still inserts all.
type Source struct {
Id int64 `db:"id"`
Type string `db:"type"`
Name string `db:"name"`
}

sources := make([]*Source, 0, len(c.Sources))
for _, source := range c.Sources {
sources = append(sources, &Source{
Id: source.Id,
Type: source.Type,
Name: source.Name,
})
}

stmt, _ := db.BuildUpsertStmt(&Source{})
_, err := db.NamedExec(stmt, sources)
if err != nil {
logger.Errorw("Failed to update sources", zap.Error(err))
}
}

// GetSource returns the ConfigSource for a requested ID.
func (c *ConfigFile) GetSource(id int64) (ConfigSource, error) {
for _, source := range c.Sources {
Expand Down

0 comments on commit 457028d

Please sign in to comment.