Merge pull request #452 from influxdata/fix-kapacitor-update

Fix #442 ; update tickscript to be disabled during script update followed by enable
pull/455/head
Chris Goller 2016-11-10 13:13:03 -06:00 committed by GitHub
commit 2764db6cb1
2 changed files with 50 additions and 3 deletions

View File

@ -3,7 +3,6 @@ package kapacitor
import (
"context"
"fmt"
"path"
"github.com/influxdata/chronograf"
client "github.com/influxdata/kapacitor/client/v1"
@ -36,6 +35,11 @@ func (c *Client) Href(ID string) string {
return fmt.Sprintf("/kapacitor/v1/tasks/%s", ID)
}
// HrefOutput returns the link to a kapacitor task httpOut Node given an id
func (c *Client) HrefOutput(ID string) string {
return fmt.Sprintf("/kapacitor/v1/tasks/%s/%s", ID, HTTPEndpoint)
}
// Create builds and POSTs a tickscript to kapacitor
func (c *Client) Create(ctx context.Context, rule chronograf.AlertRule) (*Task, error) {
kapa, err := c.kapaClient(ctx)
@ -68,7 +72,7 @@ func (c *Client) Create(ctx context.Context, rule chronograf.AlertRule) (*Task,
return &Task{
ID: kapaID,
Href: task.Link.Href,
HrefOutput: path.Join(task.Link.Href, HTTPEndpoint),
HrefOutput: c.HrefOutput(kapaID),
TICKScript: script,
}, nil
}
@ -82,6 +86,39 @@ func (c *Client) Delete(ctx context.Context, href string) error {
return kapa.DeleteTask(client.Link{Href: href})
}
func (c *Client) updateStatus(ctx context.Context, href string, status client.TaskStatus) (*Task, error) {
kapa, err := c.kapaClient(ctx)
if err != nil {
return nil, err
}
opts := client.UpdateTaskOptions{
Status: status,
}
task, err := kapa.UpdateTask(client.Link{Href: href}, opts)
if err != nil {
return nil, err
}
return &Task{
ID: task.ID,
Href: task.Link.Href,
HrefOutput: c.HrefOutput(task.ID),
TICKScript: chronograf.TICKScript(task.TICKscript),
}, nil
}
// Disable changes the tickscript status to disabled for a given href.
func (c *Client) Disable(ctx context.Context, href string) (*Task, error) {
return c.updateStatus(ctx, href, client.Disabled)
}
// Enable changes the tickscript status to disabled for a given href.
func (c *Client) Enable(ctx context.Context, href string) (*Task, error) {
return c.updateStatus(ctx, href, client.Enabled)
}
// Update changes the tickscript of a given id.
func (c *Client) Update(ctx context.Context, href string, rule chronograf.AlertRule) (*Task, error) {
kapa, err := c.kapaClient(ctx)
@ -94,9 +131,10 @@ func (c *Client) Update(ctx context.Context, href string, rule chronograf.AlertR
return nil, err
}
// We need to disable the kapacitor task followed by enabling it during update.
opts := client.UpdateTaskOptions{
TICKscript: string(script),
Status: client.Enabled,
Status: client.Disabled,
Type: toTask(rule.Query),
DBRPs: []client.DBRP{
{
@ -111,9 +149,15 @@ func (c *Client) Update(ctx context.Context, href string, rule chronograf.AlertR
return nil, err
}
// Now enable the task.
if _, err := c.Enable(ctx, href); err != nil {
return nil, err
}
return &Task{
ID: task.ID,
Href: task.Link.Href,
HrefOutput: c.HrefOutput(task.ID),
TICKScript: script,
}, nil
}

View File

@ -412,6 +412,7 @@ func (h *Service) KapacitorRulesPut(w http.ResponseWriter, r *http.Request) {
Links: alertLinks{
Self: fmt.Sprintf("/chronograf/v1/sources/%d/kapacitors/%d/rules/%s", srv.SrcID, srv.ID, req.ID),
Kapacitor: fmt.Sprintf("/chronograf/v1/sources/%d/kapacitors/%d/proxy?path=%s", srv.SrcID, srv.ID, url.QueryEscape(task.Href)),
Output: fmt.Sprintf("/chronograf/v1/sources/%d/kapacitors/%d/proxy?path=%s", srv.SrcID, srv.ID, url.QueryEscape(task.HrefOutput)),
},
TICKScript: string(task.TICKScript),
}
@ -462,6 +463,7 @@ func (h *Service) KapacitorRulesGet(w http.ResponseWriter, r *http.Request) {
Links: alertLinks{
Self: fmt.Sprintf("/chronograf/v1/sources/%d/kapacitors/%d/rules/%s", srv.SrcID, srv.ID, rule.ID),
Kapacitor: fmt.Sprintf("/chronograf/v1/sources/%d/kapacitors/%d/proxy?path=%s", srv.SrcID, srv.ID, url.QueryEscape(c.Href(rule.ID))),
Output: fmt.Sprintf("/chronograf/v1/sources/%d/kapacitors/%d/proxy?path=%s", srv.SrcID, srv.ID, url.QueryEscape(c.HrefOutput(rule.ID))),
},
TICKScript: string(tickscript),
}
@ -519,6 +521,7 @@ func (h *Service) KapacitorRulesID(w http.ResponseWriter, r *http.Request) {
Links: alertLinks{
Self: fmt.Sprintf("/chronograf/v1/sources/%d/kapacitors/%d/rules/%s", srv.SrcID, srv.ID, rule.ID),
Kapacitor: fmt.Sprintf("/chronograf/v1/sources/%d/kapacitors/%d/proxy?path=%s", srv.SrcID, srv.ID, url.QueryEscape(c.Href(rule.ID))),
Output: fmt.Sprintf("/chronograf/v1/sources/%d/kapacitors/%d/proxy?path=%s", srv.SrcID, srv.ID, url.QueryEscape(c.HrefOutput(rule.ID))),
},
TICKScript: string(tickscript),
}