Skip to content

Commit

Permalink
Merge branch 'heartbeat-api'
Browse files Browse the repository at this point in the history
  • Loading branch information
Omer Ozkan committed Apr 13, 2017
2 parents 9a469f5 + 2b9c954 commit a57909b
Show file tree
Hide file tree
Showing 22 changed files with 873 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class ScriptProxyAdminTest {
GetHeartbeatResponse response = new GetHeartbeatResponse();
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
response.setHeartbeat(new Heartbeat(name: "source1", lastHeartbeat: date, expired: false))
response.setHeartbeat(new Heartbeat(name: "source1", lastPingTime: date, expired: false))
opsGenieClient.setGetHeartbeatResponse(response);

def params = [name: "source1"]
Expand All @@ -270,8 +270,8 @@ class ScriptProxyAdminTest {
assertEquals("customer1", request.getApiKey())
}
assertEquals(response.getHeartbeat().getName(), responseMap[TestConstants.API.NAME])
assertEquals(sdf.format(response.getHeartbeat().getLastHeartbeat()), sdf.format(date))
assertEquals(response.getHeartbeat().getLastHeartbeat().getTime(), (date.getTime()))
assertEquals(sdf.format(response.getHeartbeat().getLastPingTime()), sdf.format(date))
assertEquals(response.getHeartbeat().getLastPingTime().getTime(), (date.getTime()))
assertEquals(response.getHeartbeat().isExpired(), responseMap[TestConstants.API.EXPIRED])
}

Expand All @@ -291,7 +291,7 @@ class ScriptProxyAdminTest {
ListHeartbeatsResponse response = new ListHeartbeatsResponse();
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
response.setHeartbeats([new Heartbeat(name: "source1", lastHeartbeat: date, expired: false)])
response.setHeartbeats([new Heartbeat(name: "source1", lastPingTime: date, expired: false)])
opsGenieClient.setListHeartbeatsResponse(response);

def params = [:]
Expand All @@ -313,8 +313,8 @@ class ScriptProxyAdminTest {
}

assertEquals(response.getHeartbeats()[0].getName(), responseMap[0][TestConstants.API.NAME])
assertEquals(sdf.format(response.getHeartbeats()[0].getLastHeartbeat()), sdf.format(date))
assertEquals(response.getHeartbeats()[0].getLastHeartbeat().getTime(), (date.getTime()))
assertEquals(sdf.format(response.getHeartbeats()[0].getLastPingTime()), sdf.format(date))
assertEquals(response.getHeartbeats()[0].getLastPingTime().getTime(), (date.getTime()))
assertEquals(response.getHeartbeats()[0].isExpired(), responseMap[0][TestConstants.API.EXPIRED])
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
package com.ifountain.opsgenie.client;

import com.ifountain.opsgenie.client.model.customer.*;
import com.ifountain.opsgenie.client.model.customer.AddHeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.AddHeartbeatResponse;
import com.ifountain.opsgenie.client.model.customer.CopyNotificationRulesRequest;
import com.ifountain.opsgenie.client.model.customer.CopyNotificationRulesResponse;
import com.ifountain.opsgenie.client.model.customer.DeleteHeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.DeleteHeartbeatResponse;
import com.ifountain.opsgenie.client.model.customer.EnableHeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.EnableHeartbeatResponse;
import com.ifountain.opsgenie.client.model.customer.GetHeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.GetHeartbeatResponse;
import com.ifountain.opsgenie.client.model.customer.HeartbeatPingRequest;
import com.ifountain.opsgenie.client.model.customer.HeartbeatPingResponse;
import com.ifountain.opsgenie.client.model.customer.HeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.HeartbeatResponse;
import com.ifountain.opsgenie.client.model.customer.ListHeartbeatsRequest;
import com.ifountain.opsgenie.client.model.customer.ListHeartbeatsResponse;
import com.ifountain.opsgenie.client.model.customer.UpdateHeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.UpdateHeartbeatResponse;

import java.io.IOException;
import java.text.ParseException;
Expand Down Expand Up @@ -91,6 +108,12 @@ public interface IOpsGenieClient {
*/
IIntegrationOpsGenieClient integration();

/**
* @deprecated Use {@link IOpsGenieClient#pingHeartbeat(HeartbeatPingRequest)}
*/
@Deprecated
HeartbeatResponse heartbeat(HeartbeatRequest heartbeatRequest) throws OpsGenieClientException, IOException, ParseException;

/**
* Sends heartbeat messages to OpsGenie. If heartbeat monitoring is enabled and OpsGenie does not get a heartbeat message within 10 minutes,
* OpsGenie creates an alert to notify the specified people.
Expand All @@ -100,7 +123,7 @@ public interface IOpsGenieClient {
* @see com.ifountain.opsgenie.client.model.customer.HeartbeatRequest
* @see com.ifountain.opsgenie.client.model.customer.HeartbeatResponse
*/
public HeartbeatResponse heartbeat(HeartbeatRequest heartbeatRequest) throws OpsGenieClientException, IOException, ParseException;
HeartbeatPingResponse pingHeartbeat(HeartbeatPingRequest heartbeatRequest) throws ParseException, OpsGenieClientException, IOException;

/**
* Deletes heartbeat monitor on OpsGenie. If heartbeat monitor is deleted, OpsGenie will not create alert for expired heartbeat.
Expand Down Expand Up @@ -157,11 +180,13 @@ public interface IOpsGenieClient {
/**
* List all heartbeat monitor details on OpsGenie.
*
* @deprecated Deprecated for removal
* @param listHeartbeatsRequest Object to construct request parameters.
* @return Object containing OpsGenie response information.
* @see com.ifountain.opsgenie.client.model.customer.ListHeartbeatsRequest
* @see com.ifountain.opsgenie.client.model.customer.ListHeartbeatsResponse
*/
@Deprecated
public ListHeartbeatsResponse listHeartbeats(ListHeartbeatsRequest listHeartbeatsRequest) throws OpsGenieClientException, IOException, ParseException;

/**
Expand Down
150 changes: 120 additions & 30 deletions sdk/src/main/java/com/ifountain/opsgenie/client/OpsGenieClient.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
package com.ifountain.opsgenie.client;

import com.ifountain.opsgenie.client.http.OpsGenieHttpClient;
import com.ifountain.opsgenie.client.model.customer.*;
import com.ifountain.opsgenie.client.model.BaseResponse;
import com.ifountain.opsgenie.client.model.beans.Heartbeat;
import com.ifountain.opsgenie.client.model.customer.AddHeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.AddHeartbeatResponse;
import com.ifountain.opsgenie.client.model.customer.CopyNotificationRulesRequest;
import com.ifountain.opsgenie.client.model.customer.CopyNotificationRulesResponse;
import com.ifountain.opsgenie.client.model.customer.DeleteHeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.DeleteHeartbeatResponse;
import com.ifountain.opsgenie.client.model.customer.EnableHeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.EnableHeartbeatResponse;
import com.ifountain.opsgenie.client.model.customer.GetHeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.GetHeartbeatResponse;
import com.ifountain.opsgenie.client.model.customer.HeartbeatPingRequest;
import com.ifountain.opsgenie.client.model.customer.HeartbeatPingResponse;
import com.ifountain.opsgenie.client.model.customer.HeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.HeartbeatResponse;
import com.ifountain.opsgenie.client.model.customer.ListHeartbeatsRequest;
import com.ifountain.opsgenie.client.model.customer.ListHeartbeatsResponse;
import com.ifountain.opsgenie.client.model.customer.UpdateHeartbeatRequest;
import com.ifountain.opsgenie.client.model.customer.UpdateHeartbeatResponse;
import com.ifountain.opsgenie.client.rest.RestApiClient;
import com.ifountain.opsgenie.client.rest.response.RestSuccessResult;
import com.ifountain.opsgenie.client.util.ClientConfiguration;

import java.io.IOException;
import java.text.ParseException;

/**
* Provides the client for accessing the OpsGenie web service.
* <p></p>
* <p><code>OpsGenieClient</code> class provides the implementation APIs for OpsGenie operations like creating, closing and getting alerts,
* adding comments, attaching files, etc. All service calls made using this client are blocking, and will not return until the service call completes.</p>
* <p></p>
* <p><strong>Creating an Alerts</strong></p>
* <p>Construct a <code>CreateAlertRequest</code> object with preferred options and call <code>createAlert</code> method on client.</p>
* <p>
* Provides the client for accessing the OpsGenie web service. <p></p>
* <p><code>OpsGenieClient</code> class provides the implementation APIs for OpsGenie operations
* like creating, closing and getting alerts, adding comments, attaching files, etc. All service
* calls made using this client are blocking, and will not return until the service call
* completes.</p> <p></p> <p><strong>Creating an Alerts</strong></p> <p>Construct a
* <code>CreateAlertRequest</code> object with preferred options and call <code>createAlert</code>
* method on client.</p> <p>
* <blockquote><pre>
* OpsGenieClient client = new OpsGenieClient();
* CreateAlertRequest request = new CreateAlertRequest();
Expand All @@ -30,11 +50,8 @@
* CreateAlertResponse response = client.createAlert(request);
* String alertId = response.getAlertId();
* </pre></blockquote>
* <p>
* <p><strong>Adding Notes</strong></p>
* <p>Construct a <code>AddNoteRequest</code> object with preferred options and call <code>addNote</code> method on client.</p>
* <p>
* <p>
* <p> <p><strong>Adding Notes</strong></p> <p>Construct a <code>AddNoteRequest</code> object with
* preferred options and call <code>addNote</code> method on client.</p> <p> <p>
* <blockquote><pre>
* OpsGenieClient client = new OpsGenieClient();
* AddNoteRequest request = new AddNoteRequest();
Expand All @@ -45,10 +62,8 @@
* AddNoteResponse response = client.addNote(request);
* assert response.isSuccess();
* </pre></blockquote>
* <p>
* <p>
* <p><strong>Attaching Files</strong></p>
* <p>Construct a <code>FileAttachRequest</code> object with preferred options and call <code>attach</code> method on client.</p>
* <p> <p> <p><strong>Attaching Files</strong></p> <p>Construct a <code>FileAttachRequest</code>
* object with preferred options and call <code>attach</code> method on client.</p>
* <p><blockquote><pre>
* OpsGenieClient client = new OpsGenieClient();
* FileAttachRequest request = new FileAttachRequest();
Expand Down Expand Up @@ -84,7 +99,13 @@ public class OpsGenieClient implements IOpsGenieClient {
private StreamOpsgenieHttpClient streamOpsgenieHttpClient;

/**
* Constructs a new client to invoke service methods on OpsGenie using the specified client configuration options.
* New Rest Api Client helper
*/
private RestApiClient restApiClient;

/**
* Constructs a new client to invoke service methods on OpsGenie using the specified client
* configuration options.
*/
public OpsGenieClient(ClientConfiguration config) {
this(new OpsGenieHttpClient(config));
Expand All @@ -105,6 +126,8 @@ public OpsGenieClient(OpsGenieHttpClient httpClient) {
this.jsonHttpClient = new JsonOpsgenieHttpClient(httpClient);
this.streamOpsgenieHttpClient = new StreamOpsgenieHttpClient(httpClient);
this.jsonHttpClient.setApiKey(getApiKey());
this.restApiClient = new RestApiClient(httpClient);
this.restApiClient.setApiKey(getApiKey());
innerUserOpsGenieClient = new InnerUserOpsGenieClient(this.jsonHttpClient);
innerGroupOpsGenieClient = new InnerGroupOpsGenieClient(this.jsonHttpClient);
innerTeamOpsGenieClient = new InnerTeamOpsGenieClient(this.jsonHttpClient);
Expand All @@ -116,6 +139,7 @@ public OpsGenieClient(OpsGenieHttpClient httpClient) {
innerContactOpsGenieClient = new InnerContactOpsGenieClient(this.jsonHttpClient);
innerNotificationRuleOpsGenieClient = new InnerNotificationRuleOpsGenieClient(this.jsonHttpClient);
innerAccountOpsGenieClient = new InnerAccountOpsGenieClient(this.jsonHttpClient);

}

/**
Expand All @@ -126,8 +150,7 @@ public IUserOpsGenieClient user() {
}

/**
* @see com.ifountain.opsgenie.client.IOpsGenieClient#group()
* groups are deprecated
* @see com.ifountain.opsgenie.client.IOpsGenieClient#group() groups are deprecated
*/
@Deprecated
public IGroupOpsGenieClient group() {
Expand Down Expand Up @@ -185,58 +208,122 @@ public INotificationRuleOpsGenieClient notificationRule() {
return innerNotificationRuleOpsGenieClient;
}

//TODO will be deprecated

/**
* @deprecated Use {@link OpsGenieClient#pingHeartbeat(HeartbeatPingRequest)}
* @see IOpsGenieClient#heartbeat(com.ifountain.opsgenie.client.model.customer.HeartbeatRequest)
*/
@Override
@Deprecated
public HeartbeatResponse heartbeat(HeartbeatRequest heartbeatRequest) throws OpsGenieClientException, IOException, ParseException {
return (HeartbeatResponse) jsonHttpClient.doPostRequest(heartbeatRequest);
}

@Override
public HeartbeatPingResponse pingHeartbeat(HeartbeatPingRequest pingRequest) throws OpsGenieClientException, IOException, ParseException {
RestSuccessResult<Object> result = restApiClient.post(pingRequest.getEndPoint())
.apiKey(pingRequest.getApiKey())
.getResponse(Object.class);

HeartbeatPingResponse response = new HeartbeatPingResponse();
populateMetaData(response, result);
return response;
}

private void populateMetaData(BaseResponse response, RestSuccessResult<?> result) {
response.setSuccess(true);

if(result.getTook() != null ) {
response.setTook(result.getTook().intValue());
}

response.setJson(result.getRawData());
}

/**
* @see IOpsGenieClient#deleteHeartbeat(com.ifountain.opsgenie.client.model.customer.DeleteHeartbeatRequest)
*/
@Override
public DeleteHeartbeatResponse deleteHeartbeat(DeleteHeartbeatRequest deleteHeartbeatRequest) throws OpsGenieClientException, IOException, ParseException {
return (DeleteHeartbeatResponse) jsonHttpClient.doDeleteRequest(deleteHeartbeatRequest);
RestSuccessResult<Object> result = restApiClient.delete(deleteHeartbeatRequest.getEndPoint())
.apiKey(deleteHeartbeatRequest.getApiKey())
.getResponse(Object.class);


DeleteHeartbeatResponse response = new DeleteHeartbeatResponse();
populateMetaData(response, result);

return response;
}

/**
* @see IOpsGenieClient#addHeartbeat(com.ifountain.opsgenie.client.model.customer.AddHeartbeatRequest)
*/
@Override
public AddHeartbeatResponse addHeartbeat(AddHeartbeatRequest addHeartbeatRequest) throws OpsGenieClientException, IOException, ParseException {
return (AddHeartbeatResponse) jsonHttpClient.doPostRequest(addHeartbeatRequest);
RestSuccessResult<Heartbeat> result = restApiClient.post(addHeartbeatRequest.getEndPoint())
.apiKey(addHeartbeatRequest.getApiKey())
.json(addHeartbeatRequest)
.getResponse(Heartbeat.class);

AddHeartbeatResponse response = new AddHeartbeatResponse();
populateMetaData(response, result);
response.setName(result.getData().getName());
return response;
}

/**
* @see IOpsGenieClient#updateHeartbeat(com.ifountain.opsgenie.client.model.customer.UpdateHeartbeatRequest)
*/
@Override
public UpdateHeartbeatResponse updateHeartbeat(UpdateHeartbeatRequest updateHeartbeatRequest) throws OpsGenieClientException, IOException, ParseException {
return (UpdateHeartbeatResponse) jsonHttpClient.doPostRequest(updateHeartbeatRequest);
RestSuccessResult<Heartbeat> result = restApiClient.patch(updateHeartbeatRequest.getEndPoint())
.json(updateHeartbeatRequest)
.apiKey(updateHeartbeatRequest.getApiKey())
.getResponse(Heartbeat.class);

UpdateHeartbeatResponse response = new UpdateHeartbeatResponse();
populateMetaData(response, result);
response.setName(result.getData().getName());

return response;
}

/**
* @see IOpsGenieClient#enableHeartbeat(com.ifountain.opsgenie.client.model.customer.EnableHeartbeatRequest)
*/
@Override
public EnableHeartbeatResponse enableHeartbeat(EnableHeartbeatRequest enableHeartbeatRequest) throws OpsGenieClientException, IOException, ParseException {
return (EnableHeartbeatResponse) jsonHttpClient.doPostRequest(enableHeartbeatRequest);
RestSuccessResult<Object> result = restApiClient.post(enableHeartbeatRequest.getEndPoint())
.apiKey(enableHeartbeatRequest.getApiKey())
.getResponse(Object.class);
EnableHeartbeatResponse response = new EnableHeartbeatResponse();
populateMetaData(response, result);
return response;
}

/**
* @see IOpsGenieClient#getHeartbeat(com.ifountain.opsgenie.client.model.customer.GetHeartbeatRequest)
*/
@Override
public GetHeartbeatResponse getHeartbeat(GetHeartbeatRequest getHeartbeatRequest) throws OpsGenieClientException, IOException, ParseException {
return (GetHeartbeatResponse) jsonHttpClient.doGetRequest(getHeartbeatRequest);
public GetHeartbeatResponse getHeartbeat(GetHeartbeatRequest request) throws OpsGenieClientException, IOException, ParseException {
RestSuccessResult<Heartbeat> result = restApiClient.get(request.getEndPoint())
.apiKey(request.getApiKey())
.getResponse(Heartbeat.class);

GetHeartbeatResponse response = new GetHeartbeatResponse();
populateMetaData(response, result);
response.setHeartbeat(result.getData());
return response;
}

/**
* @deprecated Deprecated for removal
* @see IOpsGenieClient#listHeartbeats(com.ifountain.opsgenie.client.model.customer.ListHeartbeatsRequest)
*/
@Override
@Deprecated
public ListHeartbeatsResponse listHeartbeats(ListHeartbeatsRequest listHeartbeatsRequest) throws OpsGenieClientException, IOException, ParseException {
return (ListHeartbeatsResponse) jsonHttpClient.doGetRequest(listHeartbeatsRequest);
}
Expand All @@ -250,14 +337,16 @@ public CopyNotificationRulesResponse copyNotificationRules(CopyNotificationRules
}

/**
* Set root endpoint uri that the client uses to send http requests. Default is https://api.opsgenie.com. Mostly used for testing.
* Set root endpoint uri that the client uses to send http requests. Default is
* https://api.opsgenie.com. Mostly used for testing.
*
* @param rootUri Uri to set.
*/
@Override
public void setRootUri(String rootUri) {
this.jsonHttpClient.setRootUri(rootUri);
this.streamOpsgenieHttpClient.setRootUri(rootUri);
this.restApiClient.setRootUrl(rootUri);
}

/**
Expand All @@ -270,13 +359,14 @@ public String getApiKey() {

/**
* Sets the customer key used for authenticating API requests.
*
* @param apiKey
*/
public void setApiKey(String apiKey) {
if (this.jsonHttpClient != null) {
this.jsonHttpClient.setApiKey(apiKey);
}
if (this.restApiClient != null) {
this.restApiClient.setApiKey(apiKey);
}
}

/**
Expand Down
Loading

0 comments on commit a57909b

Please sign in to comment.