fleet/server/datastore/mysql/scheduled_queries.go
Zachary Wasserman 0b7747bef0
Fix pack and query UI issues in Fleet 2.0 (#1829)
Replaces (and appropriately refactors) a number of endpoints that were removed long ago when we decided to kill the UI with the fleetctl release. We turned out not to do this, and now need to restore these missing endpoints.

This is not a straight up replacement of the existing code because of refactoring to the DB schemas that was also done in the migration.

Most of the replaced code was removed in #1670 and #1686.

Fixes #1811, fixes #1810
2018-06-15 10:13:11 -04:00

148 lines
3.6 KiB
Go

package mysql
import (
"database/sql"
"github.com/kolide/fleet/server/kolide"
"github.com/pkg/errors"
)
func (d *Datastore) ListScheduledQueriesInPack(id uint, opts kolide.ListOptions) ([]*kolide.ScheduledQuery, error) {
query := `
SELECT
sq.id,
sq.pack_id,
COALESCE(sq.name, q.name) AS name,
sq.query_name,
COALESCE(sq.description, '') AS description,
sq.interval,
sq.snapshot,
sq.removed,
COALESCE(sq.platform, '') AS platform,
sq.version,
sq.shard,
q.query,
q.id AS query_id
FROM scheduled_queries sq
JOIN queries q
ON sq.query_name = q.name
WHERE sq.pack_id = ?
AND NOT sq.deleted
`
query = appendListOptionsToSQL(query, opts)
results := []*kolide.ScheduledQuery{}
if err := d.db.Select(&results, query, id); err != nil {
return nil, errors.Wrap(err, "listing scheduled queries")
}
return results, nil
}
func (d *Datastore) NewScheduledQuery(sq *kolide.ScheduledQuery, opts ...kolide.OptionalArg) (*kolide.ScheduledQuery, error) {
db := d.getTransaction(opts)
// This query looks up the query name using the ID (for backwards
// compatibility with the UI)
query := `
INSERT INTO scheduled_queries (
query_name,
pack_id,
snapshot,
removed,
` + "`interval`" + `,
platform,
version,
shard
)
SELECT name, ?, ?, ?, ?, ?, ?, ?
FROM queries
WHERE id = ?
`
result, err := db.Exec(query, sq.PackID, sq.Snapshot, sq.Removed, sq.Interval, sq.Platform, sq.Version, sq.Shard, sq.QueryID)
if err != nil {
return nil, errors.Wrap(err, "inserting scheduled query")
}
id, _ := result.LastInsertId()
sq.ID = uint(id)
query = `SELECT query, name FROM queries WHERE id = ? LIMIT 1`
metadata := []struct {
Query string
Name string
}{}
err = db.Select(&metadata, query, sq.QueryID)
if err != nil && err == sql.ErrNoRows {
return nil, notFound("Query").WithID(sq.QueryID)
} else if err != nil {
return nil, errors.Wrap(err, "select query by ID")
}
if len(metadata) != 1 {
return nil, errors.Wrap(err, "wrong number of results returned from database")
}
sq.Query = metadata[0].Query
sq.Name = metadata[0].Name
return sq, nil
}
func (d *Datastore) SaveScheduledQuery(sq *kolide.ScheduledQuery) (*kolide.ScheduledQuery, error) {
query := `
UPDATE scheduled_queries
SET pack_id = ?, query_id = ?, ` + "`interval`" + ` = ?, snapshot = ?, removed = ?, platform = ?, version = ?, shard = ?
WHERE id = ? AND NOT deleted
`
result, err := d.db.Exec(query, sq.PackID, sq.QueryID, sq.Interval, sq.Snapshot, sq.Removed, sq.Platform, sq.Version, sq.Shard, sq.ID)
if err != nil {
return nil, errors.Wrap(err, "saving a scheduled query")
}
rows, err := result.RowsAffected()
if err != nil {
return nil, errors.Wrap(err, "rows affected saving a scheduled query")
}
if rows == 0 {
return nil, notFound("ScheduledQueries").WithID(sq.ID)
}
return sq, nil
}
func (d *Datastore) DeleteScheduledQuery(id uint) error {
return d.deleteEntity("scheduled_queries", id)
}
func (d *Datastore) ScheduledQuery(id uint) (*kolide.ScheduledQuery, error) {
query := `
SELECT
sq.id,
sq.created_at,
sq.updated_at,
sq.pack_id,
sq.interval,
sq.snapshot,
sq.removed,
sq.platform,
sq.version,
sq.shard,
sq.query_name,
COALESCE(sq.description, '') AS description,
q.query,
q.name,
q.id AS query_id
FROM scheduled_queries sq
JOIN queries q
ON sq.query_name = q.name
WHERE sq.id = ?
AND NOT sq.deleted
`
sq := &kolide.ScheduledQuery{}
if err := d.db.Get(sq, query, id); err != nil {
return nil, errors.Wrap(err, "selecting a scheduled query")
}
return sq, nil
}