diff --git a/src/coscale/api/alert.go b/src/coscale/api/alert.go index 856cfdb..57a5b01 100644 --- a/src/coscale/api/alert.go +++ b/src/coscale/api/alert.go @@ -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 } @@ -198,7 +198,6 @@ 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}, @@ -206,10 +205,13 @@ func (api *Api) UpdateTrigger(typeID int64, trigger *AlertTrigger) (string, erro } // 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)} diff --git a/src/coscale/api/common.go b/src/coscale/api/common.go index bc4dc41..09be4ff 100644 --- a/src/coscale/api/common.go +++ b/src/coscale/api/common.go @@ -2,7 +2,7 @@ package api import ( "fmt" - "strings" + "net/url" ) //some of the api calls will be common for different objects @@ -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 @@ -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 @@ -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 diff --git a/src/coscale/command/alert.go b/src/coscale/command/alert.go index b0fb087..10f475f 100644 --- a/src/coscale/command/alert.go +++ b/src/coscale/command/alert.go @@ -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: @@ -566,7 +566,12 @@ 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) } @@ -574,7 +579,7 @@ Optional: // 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) @@ -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 { @@ -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 }