Skip to content

Commit 0665732

Browse files
cristianciuteafryckbos
authored andcommitted
Fixed alert trigger update command issues (#12)
* fixed alert trigger update command issues
1 parent 5d087b9 commit 0665732

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

src/coscale/api/alert.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ type AlertTrigger struct {
5353
Metric int64
5454
Config string
5555
OnApp bool
56-
Group int64 // Optional
57-
Server int64 // Optional
56+
GroupID int64 // Optional
57+
ServerID int64 // Optional
5858
Source string
5959
Version int64
6060
}
@@ -198,18 +198,20 @@ func (api *Api) UpdateTrigger(typeID int64, trigger *AlertTrigger) (string, erro
198198
data := map[string][]string{
199199
"name": {trigger.Name},
200200
"description": {trigger.Description},
201-
"metric": {fmt.Sprintf("%d", trigger.Metric)},
202201
"config": {trigger.Config},
203202
"onApp": {fmt.Sprintf("%t", trigger.OnApp)},
204203
"source": {trigger.Source},
205204
"version": {fmt.Sprintf("%d", trigger.Version)},
206205
}
207206

208207
// Set the option values if they have value.
209-
if trigger.Server != 0 {
210-
data["server"] = []string{fmt.Sprintf("%d", trigger.Server)}
211-
} else if trigger.Group != 0 {
212-
data["group"] = []string{fmt.Sprintf("%d", trigger.Group)}
208+
if trigger.Metric != 0 {
209+
data["metric"] = []string{fmt.Sprintf("%d", trigger.Metric)}
210+
}
211+
if trigger.ServerID != 0 {
212+
data["server"] = []string{fmt.Sprintf("%d", trigger.ServerID)}
213+
} else if trigger.GroupID != 0 {
214+
data["group"] = []string{fmt.Sprintf("%d", trigger.GroupID)}
213215
}
214216
if trigger.AutoResolve != 0 {
215217
data["autoresolveSeconds"] = []string{fmt.Sprintf("%d", trigger.AutoResolve)}

src/coscale/api/common.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package api
22

33
import (
44
"fmt"
5-
"strings"
5+
"net/url"
66
)
77

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

5454
// GetObjectByName will return the object (json) specified by objectName and name
5555
func (api *Api) GetObjectByName(objectName string, name string) (string, error) {
56-
// In go %% is % escaped, we need to escape the name to work with string fmt.
57-
name = strings.Replace(name, "%", "%%", -1)
58-
name = strings.Replace(name, " ", "%20", -1)
56+
// URL Encoded.
57+
name = url.QueryEscape(name)
58+
5959
var result string
6060
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/?selectByName=%s", api.AppID, objectName, name), nil, true, &result); err != nil {
6161
return "", err
@@ -65,9 +65,9 @@ func (api *Api) GetObjectByName(objectName string, name string) (string, error)
6565

6666
// GetObejctRefByName will put in result a reference to the oject specified by objectName and name
6767
func (api *Api) GetObejctRefByName(objectName string, name string, result Object) error {
68-
// In go %% is % escaped, we need to escape the name to work with string fmt.
69-
name = strings.Replace(name, "%", "%%", -1)
70-
name = strings.Replace(name, " ", "%20", -1)
68+
// URL Encoded.
69+
name = url.QueryEscape(name)
70+
7171
objects := []*Object{&result}
7272
if err := api.makeCall("GET", fmt.Sprintf("/api/v1/app/%s/%ss/?selectByName=%s", api.AppID, objectName, name), nil, false, &objects); err != nil {
7373
return err
@@ -81,9 +81,9 @@ func (api *Api) GetObejctRefByName(objectName string, name string, result Object
8181

8282
// GetObejctRefByNameFromGroup will return the object specified by objectName from objectGroup that have a certain name
8383
func (api *Api) GetObejctRefByNameFromGroup(objectGroup, objectName string, groupID int64, name string, result Object) error {
84-
// In go %% is % escaped, we need to escape the name to work with string fmt.
85-
name = strings.Replace(name, "%", "%%", -1)
86-
name = strings.Replace(name, " ", "%20", -1)
84+
// URL Encoded.
85+
name = url.QueryEscape(name)
86+
8787
objects := []*Object{&result}
8888
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 {
8989
return err

src/coscale/command/alert.go

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -498,25 +498,25 @@ Optional:
498498
},
499499
{
500500
Name: "update",
501-
UsageLine: `alert trigger update (--name | --id) [--autoresolve --typename|--typeid --name --config --metric|--metricid --description --server|--serverid --servergroup|--servergroupid]`,
501+
UsageLine: `alert trigger update (--typeid --id|--typename --name) [--autoresolve --name --config --metric|--metricid --description --server|--serverid --servergroup|--servergroupid]`,
502502
Long: `
503503
Update a existing CoScale alert trigger.
504504
505505
The flags for update trigger action are:
506506
507-
Mandatory:
507+
Mandatory
508+
--typeid
509+
Specify the alert type id for the trigger.
510+
--id
511+
Unique identifier, if we want to update the name of the trigger, this become mandatory.
512+
or
513+
--typename
514+
Specify the name of the alert type for the trigger.
508515
--name
509516
Name for the trigger.
510517
Optional:
511-
--id
512-
Unique identifier, if we want to update the name of the trigger, this become mandatory.
513518
--autoresolve
514519
The amount of seconds to wait until the alert will be auto-resolved. [default: null]
515-
--typename
516-
specify the name of the alert type for triggers.
517-
or
518-
--typeid
519-
specify the alert type id for triggers.
520520
--config
521521
The trigger configuration which is formatted as follows:
522522
For metrics with DataType DOUBLE:
@@ -566,15 +566,20 @@ Optional:
566566
cmd.ParseArgs(args)
567567

568568
// Check if values were provided for mandatory flags.
569-
if name == DEFAULT_STRING_FLAG_VALUE {
569+
if id == -1 && name == DEFAULT_STRING_FLAG_VALUE {
570+
cmd.PrintUsage()
571+
os.Exit(EXIT_FLAG_ERROR)
572+
}
573+
574+
if typeID == -1 && typeName == DEFAULT_STRING_FLAG_VALUE {
570575
cmd.PrintUsage()
571576
os.Exit(EXIT_FLAG_ERROR)
572577
}
573578

574579
// Get the metric id
575580
var metricObj = &api.Metric{}
576581
var err error
577-
if metricID == -1 {
582+
if metricID == -1 && metric != DEFAULT_STRING_FLAG_VALUE {
578583
err = cmd.Capi.GetObejctRefByName("metric", metric, metricObj)
579584
if err != nil {
580585
cmd.PrintResult("", err)
@@ -605,9 +610,6 @@ Optional:
605610
serverGroupID = serverGroupObj.ID
606611
}
607612

608-
// if no error and no server or servergroup id was found then the trigger is for app.
609-
onApp = serverID == -1 && serverGroupID == -1
610-
611613
// get the alert type for the trigger
612614
var alertTypeObj = &api.AlertType{}
613615
if typeID == -1 {
@@ -652,13 +654,14 @@ Optional:
652654
alertTriggerObj.Config = config
653655
}
654656
if serverGroupID != -1 {
655-
alertTriggerObj.Group = serverGroupID
657+
alertTriggerObj.GroupID = serverGroupID
656658
}
657659
if serverID != -1 {
658-
alertTriggerObj.Server = serverID
660+
alertTriggerObj.ServerID = serverID
659661
}
660662

661-
onApp = serverGroupID == -1 && serverID == -1
663+
onApp = alertTriggerObj.GroupID == 0 && alertTriggerObj.ServerID == 0
664+
662665
if alertTriggerObj.OnApp != onApp {
663666
alertTriggerObj.OnApp = onApp
664667
}

0 commit comments

Comments
 (0)