Update models/service method for saving queries (#553)

- Add saved state to query (to differentiate queries explicitly saved from
  those just run as distributed queries)
- Remove unique constraint on query name

Closes #390
This commit is contained in:
Zachary Wasserman 2016-12-01 13:21:27 -08:00 committed by GitHub
parent 9e6a8eae56
commit b901c4c0d3
4 changed files with 21 additions and 18 deletions

View file

@ -18,6 +18,7 @@ func Up_20161118212649(tx *sql.Tx) error {
"`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP," +
"`deleted_at` timestamp NULL DEFAULT NULL," +
"`deleted` tinyint(1) NOT NULL DEFAULT FALSE," +
"`saved` tinyint(1) NOT NULL DEFAULT FALSE," +
"`name` varchar(255) NOT NULL," +
"`description` varchar(255) DEFAULT NULL," +
"`query` varchar(255) NOT NULL," +
@ -26,8 +27,7 @@ func Up_20161118212649(tx *sql.Tx) error {
"`differential` tinyint(1) NOT NULL DEFAULT FALSE," +
"`platform` varchar(255) DEFAULT NULL," +
"`version` varchar(255) DEFAULT NULL," +
"PRIMARY KEY (`id`)," +
"UNIQUE KEY `idx_query_unique_name` (`name`)" +
"PRIMARY KEY (`id`)" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;",
)
return err

View file

@ -24,20 +24,21 @@ type QueryService interface {
}
type QueryPayload struct {
Name *string
Description *string
Query *string
Interval *uint
Snapshot *bool
Differential *bool
Platform *string
Version *string
Name *string `json:"name"`
Description *string `json:"description"`
Query *string `json:"query"`
Interval *uint `json:"interval"`
Snapshot *bool `json:"snapshot"`
Differential *bool `json:"differential"`
Platform *string `json:"platform"`
Version *string `json:"version"`
}
type Query struct {
UpdateCreateTimestamps
DeleteFields
ID uint `json:"id"`
Saved bool `json:"saved"`
Name string `json:"name"`
Description string `json:"description"`
Query string `json:"query"`

View file

@ -7,6 +7,7 @@ import (
"github.com/kolide/kolide-ose/server/contexts/viewer"
"github.com/kolide/kolide-ose/server/kolide"
"github.com/kolide/kolide-ose/server/websocket"
"github.com/pkg/errors"
"golang.org/x/net/context"
)
@ -16,12 +17,13 @@ func (svc service) NewDistributedQueryCampaign(ctx context.Context, queryString
return nil, errNoContext
}
query, err := svc.NewQuery(ctx, kolide.QueryPayload{
Name: &queryString,
Query: &queryString,
query, err := svc.ds.NewQuery(&kolide.Query{
Name: fmt.Sprintf("distributed_%s_%d", vc.Username(), time.Now().Unix()),
Query: queryString,
Saved: false,
})
if err != nil {
return nil, err
return nil, errors.Wrap(err, "new query")
}
campaign, err := svc.ds.NewDistributedQueryCampaign(&kolide.DistributedQueryCampaign{
@ -30,7 +32,7 @@ func (svc service) NewDistributedQueryCampaign(ctx context.Context, queryString
UserID: vc.UserID(),
})
if err != nil {
return nil, err
return nil, errors.Wrap(err, "new campaign")
}
// Add host targets
@ -41,7 +43,7 @@ func (svc service) NewDistributedQueryCampaign(ctx context.Context, queryString
TargetID: hid,
})
if err != nil {
return nil, err
return nil, errors.Wrap(err, "adding host target")
}
}
@ -53,7 +55,7 @@ func (svc service) NewDistributedQueryCampaign(ctx context.Context, queryString
TargetID: lid,
})
if err != nil {
return nil, err
return nil, errors.Wrap(err, "adding label target")
}
}

View file

@ -14,7 +14,7 @@ func (svc service) GetQuery(ctx context.Context, id uint) (*kolide.Query, error)
}
func (svc service) NewQuery(ctx context.Context, p kolide.QueryPayload) (*kolide.Query, error) {
query := &kolide.Query{}
query := &kolide.Query{Saved: true}
if p.Name != nil {
query.Name = *p.Name