コード例 #1
0
func main() {
	cli := new(ogcli.OpsGenieClient)
	cli.SetAPIKey(constants.APIKey)

	alertCli, cliErr := cli.Alert()

	if cliErr != nil {
		panic(cliErr)
	}

	// create the alert
	req := alerts.CreateAlertRequest{Message: samples.RandStringWithPrefix("Test", 8)}
	response, alertErr := alertCli.Create(req)

	if alertErr != nil {
		panic(alertErr)
	}

	fmt.Printf("message: %s\n", response.Message)
	fmt.Printf("alert id: %s\n", response.AlertID)
	fmt.Printf("status: %s\n", response.Status)
	fmt.Printf("code: %d\n", response.Code)

	addnotereq := alerts.AddNoteAlertRequest{}
	// add alert ten notes
	for i := 0; i < 10; i++ {
		addnotereq.ID = response.AlertID
		addnotereq.Note = samples.RandString(45)
		addnoteresp, alertErr := alertCli.AddNote(addnotereq)
		if alertErr != nil {
			panic(alertErr)
		}
		fmt.Printf("[Add note] %s %d\n", addnoteresp.Status, addnoteresp.Code)
	}
	listNotesReq := alerts.ListAlertNotesRequest{IF: response.AlertID}
	listNotesResponse, alertErr := alertCli.ListNotes(listNotesReq)
	if alertErr != nil {
		panic(alertErr)
	}

	alertNotes := listNotesResponse.Notes

	fmt.Printf("Last key: %s\n", listNotesResponse.LastKey)
	fmt.Printf("Notes:\n")
	fmt.Printf("------\n")

	for _, note := range alertNotes {
		fmt.Printf("Note: %s\n", note.Note)
		fmt.Printf("Owner: %s\n", note.Owner)
		fmt.Printf("Created at: %d\n", note.CreatedAt)
		fmt.Printf("-------------------------\n")
	}
}
コード例 #2
0
ファイル: client_at_test.go プロジェクト: ryane/consul-alerts
func TestAddNoteAlert(t *testing.T) {
	addnotereq := alerts.AddNoteAlertRequest{}
	// add alert ten notes
	for i := 0; i < 10; i++ {
		addnotereq.AlertId = alertId
		addnotereq.Note = fmt.Sprintf("Alert note # %d", i)
		addnoteresp, alertErr := cli.AddNote(addnotereq)
		if alertErr != nil {
			t.Errorf(alertErr.Error())
		}
		if addnoteresp.Code >= 400 {
			t.Errorf(fmt.Sprintf("Add alert note failed with response code: %d", addnoteresp.Code))
		}
	}
	t.Log("[OK] notes added to alert")
}
コード例 #3
0
// AddNoteAction adds a note to an alert at OpsGenie.
func AddNoteAction(c *gcli.Context) {
	cli, err := NewAlertClient(c)
	if err != nil {
		os.Exit(1)
	}

	req := alerts.AddNoteAlertRequest{}

	if val, success := getVal("id", c); success {
		req.ID = val
	}
	if val, success := getVal("alias", c); success {
		req.Alias = 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 note request prepared from flags, sending request to OpsGenie..")

	_, err = cli.AddNote(req)
	if err != nil {
		fmt.Printf("%s\n", err.Error())
		os.Exit(1)
	}
	printVerboseMessage("Note added successfully.")
}
コード例 #4
0
// AddNote method adds a note to an alert at OpsGenie.
func (cli *OpsGenieAlertClient) AddNote(req alerts.AddNoteAlertRequest) (*alerts.AddNoteAlertResponse, error) {
	req.APIKey = cli.apiKey
	resp, err := cli.sendRequest(cli.buildPostRequest(addNoteAlertURL, req))

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

	var addNoteAlertResp alerts.AddNoteAlertResponse

	if err = resp.Body.FromJsonTo(&addNoteAlertResp); err != nil {
		message := "Server response can not be parsed, " + err.Error()
		logging.Logger().Warn(message)
		return nil, errors.New(message)
	}
	return &addNoteAlertResp, nil
}
コード例 #5
0
func (cli *OpsGenieAlertClient) AddNote(req alerts.AddNoteAlertRequest) (*alerts.AddNoteAlertResponse, error) {
	req.ApiKey = cli.apiKey
	// validate the mandatory parameters: apiKey, alertId/alias, note
	if req.ApiKey == "" {
		return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
	}
	if req.Note == "" {
		return nil, errors.New("Note 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_NOTE_ALERT_URL, req).Do()
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}
	if err != nil {
		return nil, errors.New("Can not add note, 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 addNoteAlertResp alerts.AddNoteAlertResponse
	// check if the response can be unmarshalled
	if err = resp.Body.FromJsonTo(&addNoteAlertResp); err != nil {
		return nil, errors.New("Server response can not be parsed.")
	}
	return &addNoteAlertResp, nil
}