예제 #1
0
// AddRecipientAction adds recipient to an alert at OpsGenie.
func AddRecipientAction(c *gcli.Context) {
	cli, err := NewAlertClient(c)
	if err != nil {
		os.Exit(1)
	}

	req := alerts.AddRecipientAlertRequest{}
	if val, success := getVal("id", c); success {
		req.ID = val
	}
	if val, success := getVal("alias", c); success {
		req.Alias = val
	}
	if val, success := getVal("recipient", c); success {
		req.Recipient = val
	}
	req.User = grabUsername(c)
	if val, success := getVal("source", c); success {
		req.Source = val
	}
	if val, success := getVal("note", c); success {
		req.Note = val
	}

	printVerboseMessage("Add recipient request prepared from flags, sending request to OpsGenie..")

	_, err = cli.AddRecipient(req)
	if err != nil {
		fmt.Printf("%s\n", err.Error())
		os.Exit(1)
	}
	printVerboseMessage("Recipient added successfully.")
}
예제 #2
0
// AddRecipient method adds recipient to an alert at OpsGenie.
func (cli *OpsGenieAlertClient) AddRecipient(req alerts.AddRecipientAlertRequest) (*alerts.AddRecipientAlertResponse, error) {
	req.APIKey = cli.apiKey
	resp, err := cli.sendRequest(cli.buildPostRequest(addRecipientAlertURL, req))

	if resp == nil {
		return nil, err
	}
	defer resp.Body.Close()

	var addRecipientAlertResp alerts.AddRecipientAlertResponse

	if err = resp.Body.FromJsonTo(&addRecipientAlertResp); err != nil {
		message := "Server response can not be parsed, " + err.Error()
		logging.Logger().Warn(message)
		return nil, errors.New(message)
	}
	return &addRecipientAlertResp, nil
}
예제 #3
0
func (cli *OpsGenieAlertClient) AddRecipient(req alerts.AddRecipientAlertRequest) (*alerts.AddRecipientAlertResponse, error) {
	req.ApiKey = cli.apiKey
	// validate the mandatory parameters: apiKey, alertId/alias, recipient
	if req.ApiKey == "" {
		return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
	}
	if req.Recipient == "" {
		return nil, errors.New("Recipient is a mandatory field and can not be empty")
	}
	if req.AlertId == "" && req.Alias == "" {
		return nil, errors.New("At least either Alert Id or Alias should be set in the request.")
	}
	if req.AlertId != "" && req.Alias != "" {
		return nil, errors.New("Either Alert Id or Alias should be set in the request not both.")
	}
	var resp *goreq.Response
	var err error
	for i := 0; i < cli.retries; i++ {
		resp, err = cli.buildRequest("POST", ADD_RECIPIENT_ALERT_URL, req).Do()
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}
	if err != nil {
		return nil, errors.New("Can not add recipient, unable to send 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 addRecipientAlertResp alerts.AddRecipientAlertResponse
	// check if the response can be unmarshalled
	if err = resp.Body.FromJsonTo(&addRecipientAlertResp); err != nil {
		return nil, errors.New("Server response can not be parsed.")
	}
	return &addRecipientAlertResp, nil
}