// Create method creates an alert at OpsGenie. func (cli *OpsGenieAlertClient) Create(req alerts.CreateAlertRequest) (*alerts.CreateAlertResponse, error) { req.APIKey = cli.apiKey resp, err := cli.sendRequest(cli.buildPostRequest(createAlertURL, req)) if resp == nil { return nil, err } defer resp.Body.Close() var createAlertResp alerts.CreateAlertResponse if err = resp.Body.FromJsonTo(&createAlertResp); err != nil { message := "Server response can not be parsed, " + err.Error() logging.Logger().Warn(message) return nil, errors.New(message) } return &createAlertResp, nil }
func (cli *OpsGenieAlertClient) Create(req alerts.CreateAlertRequest) (*alerts.CreateAlertResponse, error) { req.ApiKey = cli.apiKey // validate the mandatory parameters: apiKey, message if req.ApiKey == "" { return nil, errors.New("ApiKey is a mandatory field and can not be empty.") } if req.Message == "" { return nil, errors.New("Message is a mandatory field and can not be empty.") } // send the request var resp *goreq.Response var err error for i := 0; i < cli.retries; i++ { resp, err = cli.buildRequest("POST", CREATE_ALERT_URL, req).Do() if err == nil { break } // sleep for a second time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS) } if err != nil { return nil, errors.New("Could not create the alert: a problem occured while sending the request.") } // check the returning HTTP status code httpStatusCode := resp.StatusCode if httpStatusCode >= 400 && httpStatusCode < 500 { return nil, errors.New(fmt.Sprintf("Client error %d returned.", httpStatusCode)) } if httpStatusCode >= 500 { return nil, errors.New(fmt.Sprintf("Server error %d returned.", httpStatusCode)) } var createAlertResp alerts.CreateAlertResponse // check if the response can be unmarshalled if err = resp.Body.FromJsonTo(&createAlertResp); err != nil { return nil, errors.New("Server response can not be parsed") } return &createAlertResp, nil }
// CreateAlertAction creates an alert at OpsGenie. func CreateAlertAction(c *gcli.Context) { cli, err := NewAlertClient(c) if err != nil { os.Exit(1) } req := alerts.CreateAlertRequest{} if val, success := getVal("message", c); success { req.Message = val } if val, success := getVal("teams", c); success { req.Teams = strings.Split(val, ",") } if val, success := getVal("recipients", c); success { req.Recipients = strings.Split(val, ",") } if val, success := getVal("alias", c); success { req.Alias = val } if val, success := getVal("actions", c); success { req.Actions = strings.Split(val, ",") } if val, success := getVal("source", c); success { req.Source = val } if val, success := getVal("tags", c); success { req.Tags = strings.Split(val, ",") } if val, success := getVal("description", c); success { req.Description = val } if val, success := getVal("entity", c); success { req.Entity = val } req.User = grabUsername(c) if val, success := getVal("note", c); success { req.Note = val } if c.IsSet("D") { req.Details = extractDetailsFromCommand(c) } printVerboseMessage("Create alert request prepared from flags, sending request to OpsGenie..") resp, err := cli.Create(req) if err != nil { fmt.Printf("%s\n", err.Error()) os.Exit(1) } printVerboseMessage("Alert created successfully.") fmt.Printf("alertId=%s\n", resp.AlertID) }