fleet/server/datastore/mysql/decorators.go
John Murphy 693600ba2b Decorator support (#1430)
* Added migrations

* Added handler for decorators

* Added logging and metrics for decorators

* WIP decorators

* Wip added decorator service

* Added service implementation

* Added mock decorator

* Added modify decorator

* Added testing

* Addressed code review issues raised by @zwass

* Added logging for missing type per @zwass
2017-03-28 16:45:18 -05:00

92 lines
1.9 KiB
Go

package mysql
import (
"database/sql"
"github.com/pkg/errors"
"github.com/kolide/kolide/server/kolide"
)
func (ds *Datastore) SaveDecorator(dec *kolide.Decorator) error {
sqlStatement :=
"UPDATE decorators SET " +
"`query` = ?, " +
"`type` = ?, " +
"`interval` = ? " +
"WHERE id = ?"
_, err := ds.db.Exec(
sqlStatement,
dec.Query,
dec.Type,
dec.Interval,
dec.ID,
)
if err != nil {
return errors.Wrap(err, "saving decorator")
}
return nil
}
func (ds *Datastore) NewDecorator(decorator *kolide.Decorator) (*kolide.Decorator, error) {
sqlStatement :=
"INSERT INTO decorators (" +
"`query`," +
"`type`," +
"`interval` ) " +
"VALUES (?, ?, ?)"
result, err := ds.db.Exec(sqlStatement, decorator.Query, decorator.Type, decorator.Interval)
if err != nil {
return nil, errors.Wrap(err, "creating decorator")
}
id, _ := result.LastInsertId()
decorator.ID = uint(id)
return decorator, nil
}
func (ds *Datastore) DeleteDecorator(id uint) error {
sqlStatement := `
DELETE FROM decorators
WHERE id = ?
`
res, err := ds.db.Exec(sqlStatement, id)
if err != nil {
return errors.Wrap(err, "deleting decorator")
}
deleted, _ := res.RowsAffected()
if deleted < 1 {
return notFound("Decorator").WithID(id)
}
return nil
}
func (ds *Datastore) Decorator(id uint) (*kolide.Decorator, error) {
sqlStatement := `
SELECT *
FROM decorators
WHERE id = ?
`
var result kolide.Decorator
err := ds.db.Get(&result, sqlStatement, id)
if err != nil {
if err == sql.ErrNoRows {
return nil, notFound("Decorator").WithID(id)
}
return nil, errors.Wrap(err, "retrieving decorator")
}
return &result, nil
}
func (ds *Datastore) ListDecorators() ([]*kolide.Decorator, error) {
sqlStatement := `
SELECT *
FROM decorators
`
var results []*kolide.Decorator
err := ds.db.Select(&results, sqlStatement)
if err != nil {
return nil, errors.Wrap(err, "listing decorators")
}
return results, nil
}