func (cli *OpsGenieAlertClient) List(req alerts.ListAlertsRequest) (*alerts.ListAlertsResponse, error) {
	req.ApiKey = cli.apiKey
	// validate the mandatory parameter: apiKey
	if req.ApiKey == "" {
		return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
	}
	v, _ := goquery.Values(req)
	var resp *goreq.Response
	var err error
	for i := 0; i < cli.retries; i++ {
		resp, err = cli.buildRequest("GET", LIST_ALERTS_URL+"?"+v.Encode(), nil).Do()
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}
	if err != nil {
		return nil, errors.New("Could not retrieve 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 listAlertsResp alerts.ListAlertsResponse
	// check if the response can be unmarshalled
	if err = resp.Body.FromJsonTo(&listAlertsResp); err != nil {
		return nil, errors.New("Server response can not be parsed.")
	}
	return &listAlertsResp, nil
}
func (cli *OpsGenieHeartbeatClient) Delete(req heartbeat.DeleteHeartbeatRequest) (*heartbeat.DeleteHeartbeatResponse, error) {
	req.ApiKey = cli.apiKey
	// validate mandatory fields: apiKey, name/id
	if req.ApiKey == "" {
		return nil, errors.New("Api Key is a mandatory field and can not be empty")
	}
	if req.Name == "" && req.Id == "" {
		return nil, errors.New("Either Name or Id field is mandatory and can not be empty")
	}
	if req.Name != "" && req.Id != "" {
		return nil, errors.New("Either Name or Id field should be supplied not both")
	}
	// send the request in a query string
	v, _ := goquery.Values(req)
	var resp *goreq.Response
	var err error
	for i := 0; i < cli.retries; i++ {
		resp, err = cli.buildRequest("DELETE", DELETE_HEARTBEAT_URL+"?"+v.Encode(), nil).Do()
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}

	// resp, err := goreq.Request{ Method: "DELETE", Uri: DELETE_HEARTBEAT_URL + "?" + v.Encode(), }.Do()
	if err != nil {
		return nil, errors.New("Can not delete the heart beat, unable to send the request")
	}
	// check for the returning http status, 4xx: client errors, 5xx: server errors
	statusCode := resp.StatusCode
	if statusCode >= 400 && statusCode < 500 {
		return nil, errors.New(fmt.Sprintf("Client error %d occured", statusCode))
	}
	if statusCode >= 500 {
		return nil, errors.New(fmt.Sprintf("Server error %d occured", statusCode))
	}
	// try to parse the returning JSON into the response
	var deleteHeartbeatResp heartbeat.DeleteHeartbeatResponse
	if err = resp.Body.FromJsonTo(&deleteHeartbeatResp); err != nil {
		return nil, errors.New("Server response can not be parsed")
	}
	// parsed successfuly with no errors
	return &deleteHeartbeatResp, nil
}
func (cli *OpsGenieAlertClient) ListRecipients(req alerts.ListAlertRecipientsRequest) (*alerts.ListAlertRecipientsResponse, error) {
	req.ApiKey = cli.apiKey
	// validate the mandatory parameters: apiKey, id/alias
	if req.ApiKey == "" {
		return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
	}
	if req.Id == "" && req.Alias == "" {
		return nil, errors.New("At least either Id or Alias should be set in the request.")
	}
	if req.Id != "" && req.Alias != "" {
		return nil, errors.New("Either Id or Alias should be set in the request not both.")
	}
	v, _ := goquery.Values(req)
	var resp *goreq.Response
	var err error
	for i := 0; i < cli.retries; i++ {
		resp, err = cli.buildRequest("GET", LIST_ALERT_RECIPIENTS_URL+"?"+v.Encode(), nil).Do()
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}
	if err != nil {
		return nil, errors.New("Can not list the recipient list, 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 listAlertRecipientsResp alerts.ListAlertRecipientsResponse
	// check if the response can be unmarshalled
	if err = resp.Body.FromJsonTo(&listAlertRecipientsResp); err != nil {
		return nil, errors.New("Server response can not be parsed.")
	}
	return &listAlertRecipientsResp, nil
}
func (cli *OpsGenieAlertClient) Get(req alerts.GetAlertRequest) (*alerts.GetAlertResponse, error) {
	req.ApiKey = cli.apiKey
	// validate the mandatory parameters: apiKey, id/alias/tinyId
	if req.ApiKey == "" {
		return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
	}
	if req.Id == "" && req.Alias == "" && req.TinyId == "" {
		return nil, errors.New("At least one of the parameters of id, alias and tiny id should be set.")
	}
	if (req.Id != "" && req.Alias != "") || (req.Id != "" && req.TinyId != "") || (req.Alias != "" && req.TinyId != "") {
		return nil, errors.New("Only one of the parameters of id, alias and tiny id should be set.")
	}
	v, _ := goquery.Values(req)
	var resp *goreq.Response
	var err error
	for i := 0; i < cli.retries; i++ {
		resp, err = cli.buildRequest("GET", GET_ALERT_URL+"?"+v.Encode(), nil).Do()
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}
	if err != nil {
		return nil, errors.New("Could not retrieve 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 getAlertResp alerts.GetAlertResponse
	// check if the response can be unmarshalled
	if err = resp.Body.FromJsonTo(&getAlertResp); err != nil {
		return nil, errors.New("Server response can not be parsed.")
	}
	return &getAlertResp, nil
}
func (cli *OpsGenieAlertClient) Delete(req alerts.DeleteAlertRequest) (*alerts.DeleteAlertResponse, error) {
	req.ApiKey = cli.apiKey
	// validate the mandatory parameters: apiKey, alertId/alias
	if req.ApiKey == "" {
		return nil, errors.New("ApiKey is a mandatory field and can not be empty.")
	}
	if req.AlertId == "" && req.Alias == "" {
		return nil, errors.New("Either Alert Id or Alias at least 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.")
	}
	v, _ := goquery.Values(req)
	var resp *goreq.Response
	var err error
	for i := 0; i < cli.retries; i++ {
		resp, err = cli.buildRequest("DELETE", DELETE_ALERT_URL+"?"+v.Encode(), nil).Do()
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}
	if err != nil {
		return nil, errors.New("Could not delete 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 deleteAlertResp alerts.DeleteAlertResponse
	// check if the response can be unmarshalled
	if err = resp.Body.FromJsonTo(&deleteAlertResp); err != nil {
		return nil, errors.New("Server response can not be parsed.")
	}
	return &deleteAlertResp, nil
}
Example #6
0
// addOptions adds the parameters in opt as URL query parameters to s.  opt
// must be a struct whose fields may contain "url" tags.
func addOptions(s string, opt interface{}) (*url.URL, error) {
	u, err := url.Parse(s)
	if err != nil {
		return nil, err
	}
	if opt == nil {
		return u, nil
	}

	v := reflect.ValueOf(opt)
	if v.Kind() == reflect.Ptr && v.IsNil() {
		// No query string to add
		return u, nil
	}

	qs, err := query.Values(opt)
	if err != nil {
		return nil, err
	}

	u.RawQuery = qs.Encode()
	return u, nil
}
func (cli *OpsGenieHeartbeatClient) List(req heartbeat.ListHeartbeatsRequest) (*heartbeat.ListHeartbeatsResponse, error) {
	req.ApiKey = cli.apiKey
	// validate the mandatory field: apiKey
	if req.ApiKey == "" {
		return nil, errors.New("Api Key is a mandatory field and can not be empty")
	}
	// send the request in a query string
	v, _ := goquery.Values(req)
	var resp *goreq.Response
	var err error
	for i := 0; i < cli.retries; i++ {
		resp, err = cli.buildRequest("GET", LIST_HEARTBEAT_URL+"?"+v.Encode(), nil).Do()
		if err == nil {
			break
		}
		time.Sleep(TIME_SLEEP_BETWEEN_REQUESTS)
	}
	if err != nil {
		return nil, errors.New("Can not retrieve the list of heart beats, unable to send the request")
	}
	// check for the returning http status, 4xx: client errors, 5xx: server errors
	statusCode := resp.StatusCode
	if statusCode >= 400 && statusCode < 500 {
		return nil, errors.New(fmt.Sprintf("Client error %d occured", statusCode))
	}
	if statusCode >= 500 {
		return nil, errors.New(fmt.Sprintf("Server error %d occured", statusCode))
	}
	// try to parse the returning JSON into the response
	var listHeartbeatsResp heartbeat.ListHeartbeatsResponse
	if err = resp.Body.FromJsonTo(&listHeartbeatsResp); err != nil {
		return nil, errors.New("Server response can not be parsed")
	}
	// parsed successfuly with no errors
	return &listHeartbeatsResp, nil
}