statping/integrators/integrations.go

176 lines
4.7 KiB
Go
Raw Normal View History

2020-01-03 06:10:25 +00:00
// Statping
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statping
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2020-03-04 10:29:00 +00:00
package integrators
2020-01-03 06:10:25 +00:00
import (
2020-02-01 03:53:00 +00:00
"encoding/json"
2020-01-03 06:10:25 +00:00
"errors"
2020-02-01 03:53:00 +00:00
"fmt"
2020-02-22 23:52:05 +00:00
"github.com/hunterlong/statping/database"
2020-03-04 10:29:00 +00:00
"github.com/hunterlong/statping/types/integrations"
2020-01-03 06:10:25 +00:00
"github.com/hunterlong/statping/utils"
)
var (
2020-03-04 10:29:00 +00:00
Integrations []integrations.Integrator
2020-01-03 06:10:25 +00:00
log = utils.Log.WithField("type", "integration")
2020-02-22 23:52:05 +00:00
db database.Database
2020-01-03 06:10:25 +00:00
)
2020-02-01 03:53:00 +00:00
//func init() {
// Integrations = append(Integrations,
// CsvIntegrator,
// DockerIntegrator,
// TraefikIntegrator,
// )
//}
2020-03-04 10:29:00 +00:00
func init() {
AddIntegrations(
CsvIntegrator,
TraefikIntegrator,
DockerIntegrator,
)
}
2020-02-01 03:53:00 +00:00
// integrationsDb returns the 'integrations' database column
2020-02-22 23:52:05 +00:00
func integrationsDb() database.Database {
2020-03-04 10:29:00 +00:00
return db.Model(&integrations.Integration{})
2020-02-01 03:53:00 +00:00
}
// SetDB is called by core to inject the database for a integrator to use
2020-02-22 23:52:05 +00:00
func SetDB(d database.Database) {
2020-02-01 03:53:00 +00:00
db = d
2020-01-03 06:10:25 +00:00
}
2020-03-04 10:29:00 +00:00
func Value(intg integrations.Integrator, fieldName string) interface{} {
2020-01-03 06:10:25 +00:00
for _, v := range intg.Get().Fields {
if fieldName == v.Name {
return v.Value
}
}
return nil
}
2020-03-04 10:29:00 +00:00
func Update(integrator *integrations.Integration) error {
2020-02-01 03:53:00 +00:00
fields := FieldsToJson(integrator)
fmt.Println(fields)
2020-03-04 10:29:00 +00:00
set := db.Model(&integrations.Integration{}).Where("name = ?", integrator.Name)
2020-02-01 03:53:00 +00:00
set.Set("enabled", integrator.Enabled)
set.Set("fields", fields)
return set.Error()
}
2020-03-04 10:29:00 +00:00
func FieldsToJson(integrator *integrations.Integration) string {
2020-02-01 03:53:00 +00:00
jsonData := make(map[string]interface{})
for _, v := range integrator.Fields {
jsonData[v.Name] = v.Value
}
data, _ := json.Marshal(jsonData)
return string(data)
}
2020-03-04 10:29:00 +00:00
func JsonToFields(intg integrations.Integrator, input string) []*integrations.IntegrationField {
2020-02-01 03:53:00 +00:00
integrator := intg.Get()
var jsonData map[string]interface{}
json.Unmarshal([]byte(input), &jsonData)
for _, v := range integrator.Fields {
v.Value = jsonData[v.Name]
}
return integrator.Fields
}
2020-03-04 10:29:00 +00:00
func SetFields(intg integrations.Integrator, data map[string][]string) (*integrations.Integration, error) {
2020-01-03 06:10:25 +00:00
i := intg.Get()
for _, v := range i.Fields {
if data[v.Name] != nil {
v.Value = data[v.Name][0]
}
}
return i, nil
}
2020-03-04 10:29:00 +00:00
func Find(name string) (integrations.Integrator, error) {
2020-01-03 06:10:25 +00:00
for _, i := range Integrations {
obj := i.Get()
if obj.ShortName == name {
return i, nil
}
}
return nil, errors.New(name + " not found")
}
2020-02-01 03:53:00 +00:00
// db will return the notifier database column/record
2020-03-04 10:29:00 +00:00
func integratorDb(n *integrations.Integration) database.Database {
return db.Model(&integrations.Integration{}).Where("name = ?", n.Name).Find(n)
2020-02-01 03:53:00 +00:00
}
// isInDatabase returns true if the integration has already been installed
2020-03-04 10:29:00 +00:00
func isInDatabase(i integrations.Integrator) bool {
2020-02-01 03:53:00 +00:00
inDb := integratorDb(i.Get()).RecordNotFound()
return !inDb
}
// SelectIntegration returns the Notification struct from the database
2020-03-04 10:29:00 +00:00
func SelectIntegration(i integrations.Integrator) (*integrations.Integration, error) {
2020-02-01 03:53:00 +00:00
integration := i.Get()
2020-03-04 10:29:00 +00:00
err := db.Model(&integrations.Integration{}).Where("name = ?", integration.Name).Scan(&integration)
2020-02-01 03:53:00 +00:00
return integration, err.Error()
}
// AddIntegrations accept a Integrator interface to be added into the array
2020-03-04 10:29:00 +00:00
func AddIntegrations(inte ...integrations.Integrator) error {
for _, i := range inte {
if utils.IsType(i, new(integrations.Integrator)) {
2020-02-01 03:53:00 +00:00
Integrations = append(Integrations, i)
err := install(i)
if err != nil {
return err
}
} else {
return errors.New("notifier does not have the required methods")
}
}
return nil
}
// install will check the database for the notification, if its not inserted it will insert a new record for it
2020-03-04 10:29:00 +00:00
func install(i integrations.Integrator) error {
2020-02-01 03:53:00 +00:00
inDb := isInDatabase(i)
log.WithField("installed", inDb).
WithFields(utils.ToFields(i)).
Debugln(fmt.Sprintf("Checking if integrator '%v' is installed: %v", i.Get().Name, inDb))
if !inDb {
_, err := insertDatabase(i)
if err != nil {
log.Errorln(err)
return err
}
}
return nil
}
// insertDatabase will create a new record into the database for the integrator
2020-03-04 10:29:00 +00:00
func insertDatabase(i integrations.Integrator) (string, error) {
2020-02-01 03:53:00 +00:00
integrator := i.Get()
query := db.Create(integrator)
if query.Error() != nil {
return "", query.Error()
}
return integrator.Name, query.Error()
}