Skip to content

Commit

Permalink
Fixed alert trigger update command issues (#12)
Browse files Browse the repository at this point in the history
* fixed alert trigger update command issues
  • Loading branch information
cristianciutea authored and fryckbos committed Jun 7, 2018
1 parent 5d087b9 commit 0665732
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 34 deletions.
16 changes: 9 additions & 7 deletions src/coscale/api/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ type AlertTrigger struct {
Metric int64
Config string
OnApp bool
Group int64 // Optional
Server int64 // Optional
GroupID int64 // Optional
ServerID int64 // Optional
Source string
Version int64
}
Expand Down Expand Up @@ -198,18 +198,20 @@ func (api *Api) UpdateTrigger(typeID int64, trigger *AlertTrigger) (string, erro
data := map[string][]string{
"name": {trigger.Name},
"description": {trigger.Description},
"metric": {fmt.Sprintf("%d", trigger.Metric)},
"config": {trigger.Config},
"onApp": {fmt.Sprintf("%t", trigger.OnApp)},
"source": {trigger.Source},
"version": {fmt.Sprintf("%d", trigger.Version)},
}

// Set the option values if they have value.
if trigger.Server != 0 {
data["server"] = []string{fmt.Sprintf("%d", trigger.Server)}
} else if trigger.Group != 0 {
data["group"] = []string{fmt.Sprintf("%d", trigger.Group)}
if trigger.Metric != 0 {
data["metric"] = []string{fmt.Sprintf("%d", trigger.Metric)}
}
if trigger.ServerID != 0 {
data["server"] = []string{fmt.Sprintf("%d", trigger.ServerID)}
} else if trigger.GroupID != 0 {
data["group"] = []string{fmt.Sprintf("%d", trigger.GroupID)}
}
if trigger.AutoResolve != 0 {
data["autoresolveSeconds"] = []string{fmt.Sprintf("%d", trigger.AutoResolve)}
Expand Down
20 changes: 10 additions & 10 deletions src/coscale/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package api

import (
"fmt"
"strings"
"net/url"
)

//some of the api calls will be common for different objects
Expand Down Expand Up @@ -53,9 +53,9 @@ func (api *Api) GetObjectRefFromGroup(objectGroup, objectName string, groupID, o

// GetObjectByName will return the object (json) specified by objectName and name
func (api *Api) GetObjectByName(objectName string, name string) (string, error) {
// In go %% is % escaped, we need to escape the name to work with string fmt.
name = strings.Replace(name, "%", "%%", -1)
name = strings.Replace(name, " ", "%20", -1)
// URL Encoded.
name = url.QueryEscape(name)

var result string
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/?selectByName=%s", api.AppID, objectName, name), nil, true, &result); err != nil {
return "", err
Expand All @@ -65,9 +65,9 @@ func (api *Api) GetObjectByName(objectName string, name string) (string, error)

// GetObejctRefByName will put in result a reference to the oject specified by objectName and name
func (api *Api) GetObejctRefByName(objectName string, name string, result Object) error {
// In go %% is % escaped, we need to escape the name to work with string fmt.
name = strings.Replace(name, "%", "%%", -1)
name = strings.Replace(name, " ", "%20", -1)
// URL Encoded.
name = url.QueryEscape(name)

objects := []*Object{&result}
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/?selectByName=%s", api.AppID, objectName, name), nil, false, &objects); err != nil {
return err
Expand All @@ -81,9 +81,9 @@ func (api *Api) GetObejctRefByName(objectName string, name string, result Object

// GetObejctRefByNameFromGroup will return the object specified by objectName from objectGroup that have a certain name
func (api *Api) GetObejctRefByNameFromGroup(objectGroup, objectName string, groupID int64, name string, result Object) error {
// In go %% is % escaped, we need to escape the name to work with string fmt.
name = strings.Replace(name, "%", "%%", -1)
name = strings.Replace(name, " ", "%20", -1)
// URL Encoded.
name = url.QueryEscape(name)

objects := []*Object{&result}
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/%d/%ss/?selectByName=%s", api.AppID, objectGroup, groupID, objectName, name), nil, false, &objects); err != nil {
return err
Expand Down
37 changes: 20 additions & 17 deletions src/coscale/command/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,25 +498,25 @@ Optional:
},
{
Name: "update",
UsageLine: `alert trigger update (--name | --id) [--autoresolve --typename|--typeid --name --config --metric|--metricid --description --server|--serverid --servergroup|--servergroupid]`,
UsageLine: `alert trigger update (--typeid --id|--typename --name) [--autoresolve --name --config --metric|--metricid --description --server|--serverid --servergroup|--servergroupid]`,
Long: `
Update a existing CoScale alert trigger.
The flags for update trigger action are:
Mandatory:
Mandatory
--typeid
Specify the alert type id for the trigger.
--id
Unique identifier, if we want to update the name of the trigger, this become mandatory.
or
--typename
Specify the name of the alert type for the trigger.
--name
Name for the trigger.
Optional:
--id
Unique identifier, if we want to update the name of the trigger, this become mandatory.
--autoresolve
The amount of seconds to wait until the alert will be auto-resolved. [default: null]
--typename
specify the name of the alert type for triggers.
or
--typeid
specify the alert type id for triggers.
--config
The trigger configuration which is formatted as follows:
For metrics with DataType DOUBLE:
Expand Down Expand Up @@ -566,15 +566,20 @@ Optional:
cmd.ParseArgs(args)

// Check if values were provided for mandatory flags.
if name == DEFAULT_STRING_FLAG_VALUE {
if id == -1 && name == DEFAULT_STRING_FLAG_VALUE {
cmd.PrintUsage()
os.Exit(EXIT_FLAG_ERROR)
}

if typeID == -1 && typeName == DEFAULT_STRING_FLAG_VALUE {
cmd.PrintUsage()
os.Exit(EXIT_FLAG_ERROR)
}

// Get the metric id
var metricObj = &api.Metric{}
var err error
if metricID == -1 {
if metricID == -1 && metric != DEFAULT_STRING_FLAG_VALUE {
err = cmd.Capi.GetObejctRefByName("metric", metric, metricObj)
if err != nil {
cmd.PrintResult("", err)
Expand Down Expand Up @@ -605,9 +610,6 @@ Optional:
serverGroupID = serverGroupObj.ID
}

// if no error and no server or servergroup id was found then the trigger is for app.
onApp = serverID == -1 && serverGroupID == -1

// get the alert type for the trigger
var alertTypeObj = &api.AlertType{}
if typeID == -1 {
Expand Down Expand Up @@ -652,13 +654,14 @@ Optional:
alertTriggerObj.Config = config
}
if serverGroupID != -1 {
alertTriggerObj.Group = serverGroupID
alertTriggerObj.GroupID = serverGroupID
}
if serverID != -1 {
alertTriggerObj.Server = serverID
alertTriggerObj.ServerID = serverID
}

onApp = serverGroupID == -1 && serverID == -1
onApp = alertTriggerObj.GroupID == 0 && alertTriggerObj.ServerID == 0

if alertTriggerObj.OnApp != onApp {
alertTriggerObj.OnApp = onApp
}
Expand Down

0 comments on commit 0665732

Please sign in to comment.