Java OpsGenie SDK to create, close, ack ... an Alert error, but List and delete is fine.
Asked by DT_Pham:

I follow the page <https://docs.opsgenie.com/docs/opsgenie-java-api> to download the java SDK and use the sample code in this document page to create, close, delete, get current all alerts. I successfully can get all the alerts ( alerts created via Default API from the web page) and delete an alert by using OpsGenie Java SDK. However, when I try to create, close, and ack an alert via the java SDK, I am getting a same error. below is error from "ack" an alert. **I am not sure what the real problem here in my test code**. Of course I have all the libraries (+ third_party) from the SDK on my classpath in order to have List and Delete working, so I don't think I miss any libraries. My ant build already merge all the SDK libraries into my test jar file and here is for reference:

\<zipgroupfileset dir="../opsgenie-java-sdk/third_party" includes="\*\*.jar" /\>  
\<zipgroupfileset dir="../opsgenie-java-sdk" includes="\*\*.jar" /\>

Here is my error capture:![opsgenie-ack-issue.PNG](https://community.atlassian.com/t5/image/serverpage/image-id/78372i8D8177F4FD4B32D1/image-size/large?v=v2&px=999 "opsgenie-ack-issue.PNG")

Here is my Test code for reference:

package com.networks.tol.alert;  

import java.util.ArrayList;  
import java.util.List;  

import com.ifountain.opsgenie.client.swagger.api.AlertApi;  
import com.ifountain.opsgenie.client.swagger.model.AcknowledgeAlertRequest;  
import com.ifountain.opsgenie.client.swagger.model.BaseAlert;  
import com.ifountain.opsgenie.client.swagger.model.CloseAlertRequest;  
import com.ifountain.opsgenie.client.swagger.model.CreateAlertRequest;  
import com.ifountain.opsgenie.client.swagger.model.CreateAlertRequest.PriorityEnum;  
import com.ifountain.opsgenie.client.swagger.model.DeleteAlertRequest;  
import com.ifountain.opsgenie.client.swagger.model.ListAlertsRequest;  
import com.ifountain.opsgenie.client.swagger.model.ListAlertsResponse;  
import com.ifountain.opsgenie.client.swagger.model.Recipient;  
import com.ifountain.opsgenie.client.swagger.model.Recipient.TypeEnum;  
import com.ifountain.opsgenie.client.swagger.model.SuccessResponse;  
import com.ifountain.opsgenie.client.swagger.model.TeamRecipient;  
import com.ifountain.opsgenie.client.swagger.model.UserRecipient;  
import com.ifountain.opsgenie.client.OpsGenieClient;  
import com.ifountain.opsgenie.client.swagger.ApiException;  

public class OpsGenieAlertMgr  
{  
String defaultApiKey = "I do have a valid key here";  
String action;  
String apiKey;  
String message = null;  
String alias = null;  
String desc = null;  
String recipient = null;  
String visibleRecipient = null;  
String actions = null;  
String tags = null;  
String entity = null;  
String priority = null;  
String user = null;  
String note = null;  
String id = null;  
String tinyId = null;  
String\[\] params;  
public OpsGenieAlertMgr(String\[\] params)  
{  

this.params = params;  
////////////////process args here  
int optindex = 0;  
int count = 0;  
final int MaxLoop = 100;  
// look for action and api key only;  
while (optindex \< params.length \&\& count \< MaxLoop) {  
count++;  
int remaining = params.length - optindex;  
if (params\[optindex\].equals("-action")) {  
if (remaining \> 1) {  
action = params\[optindex + 1\];  
System.out.println("action=" + action);  
}  
optindex += 2;  
} else if (params\[optindex\].equals("-key")) {  
if (remaining \> 1) {  
apiKey = params\[optindex + 1\];  
System.out.println("apiKey=" + apiKey);  
}  
optindex += 2;  
}  
else if (params\[optindex\].equals("-m")) {  
if (remaining \> 1) {  
message = params\[optindex + 1\];  
System.out.println("message=" + message);  
}  
optindex += 2;  
} else if (params\[optindex\].equals("-d")) {  
if (remaining \> 1) {  
desc = params\[optindex + 1\];  
System.out.println("desc=" + desc);  
}  
optindex += 2;  
} else if (params\[optindex\].equals("-r")) {  
if (remaining \> 1) {  
recipient = params\[optindex + 1\];  
System.out.println("recipient=" + recipient);  
}  
optindex += 2;  
} else if (params\[optindex\].equals("-vr")) {  
if (remaining \> 1) {  
visibleRecipient = params\[optindex + 1\];  
System.out.println("visibleRecipient=" + visibleRecipient);  
}  
optindex += 2;  
} else if (params\[optindex\].equals("-a")) {  
if (remaining \> 1) {  
actions = params\[optindex + 1\];  
System.out.println("actions=" + actions);  
}  
optindex += 2;  
} else if (params\[optindex\].equals("-t")) {  
if (remaining \> 1) {  
tags = params\[optindex + 1\];  
System.out.println("tags=" + tags);  
}  
optindex += 2;  
}  
else if (params\[optindex\].equals("-e")) {  
if (remaining \> 1) {  
entity = params\[optindex + 1\];  
System.out.println("entity=" + entity);  
}  
optindex += 2;  
}  
else if (params\[optindex\].equals("-p")) {  
if (remaining \> 1) {  
priority = params\[optindex + 1\];  
System.out.println("priority=" + priority);  
}  
optindex += 2;  
}  
else if (params\[optindex\].equals("-u")) {  
if (remaining \> 1) {  
user = params\[optindex + 1\];  
System.out.println("user=" + user);  
}  
optindex += 2;  
}  
else if (params\[optindex\].equals("-n")) {  
if (remaining \> 1) {  
note = params\[optindex + 1\];  
System.out.println("note=" + note);  
}  
optindex += 2;  
}  
else if (params\[optindex\].equals("-id")) {  
if (remaining \> 1) {  
id = params\[optindex + 1\];  
System.out.println("id=" + id);  
}  
optindex += 2;  
}  
else if (params\[optindex\].equals("-tid")) {  
if (remaining \> 1) {  
tinyId = params\[optindex + 1\];  
System.out.println("tinyId=" + tinyId);  
}  
optindex += 2;  
}  
else  
{  
optindex += 1;  
}  
}  

///////////////  
}  

private String getKey()  
{  
if( apiKey == null \|\| apiKey.length() == 0)  
return this.defaultApiKey;  
else  
return apiKey;  
}  

public SuccessResponse createAlert(String\[\] params) throws IllegalArgumentException  
{  
// look for create alert data  

<br />

<br />

if(message == null)  
throw new IllegalArgumentException("alert message is empty");  
if(recipient == null)  
recipient = "OpsG Administrator";  
if(alias == null)  
alias = "No Alias1";  
if(desc==null)  
desc = "No description";  
if(visibleRecipient==null)  
visibleRecipient = recipient;  
if(actions==null)  
actions = "No actions";  
if(tags==null)  
tags = "No tags";  
if(entity==null)  
entity = "No entity";  
if(user==null)  
user = "dt.pham@parsons.com";  
if(note==null)  
note = "Alert Create";  
AlertApi client = new OpsGenieClient().alertV2();  
client.getApiClient().setApiKey(getKey());  

CreateAlertRequest request = new CreateAlertRequest();  

request.setMessage(message);  
request.setAlias(alias + "-" + System.currentTimeMillis());  
request.setDescription(desc);  
request.setTeams(toTeamRecipients(recipient));  
request.setVisibleTo(toVisibleRecipients(visibleRecipient));  
request.setActions(getStringList(actions));  
request.setTags(getStringList(tags));  
request.setEntity(entity);  
request.setPriority(getPriority(priority));  
request.setUser(user);  
request.setNote(note);  
SuccessResponse response = null;  
try {  
response = client.createAlert(request);  
printResp(response);  
return response;  
} catch (ApiException e) {  
e.printStackTrace();  
}  
return response;  
}  

private void printResp(SuccessResponse response) {  
Float took = response.getTook();  
String requestId = response.getRequestId();  
String respMessage = response.getResult();  
System.out.println("Response: action '" + action + "' took " + took + ". reqId=" +requestId + ". resp=" + respMessage);  
}  
private PriorityEnum getPriority(String priority) {  
if("1".equals(priority))  
return CreateAlertRequest.PriorityEnum.P1;  
else if("2".equals(priority))  
return CreateAlertRequest.PriorityEnum.P2;  
else if("3".equals(priority))  
return CreateAlertRequest.PriorityEnum.P3;  
else if("4".equals(priority))  
return CreateAlertRequest.PriorityEnum.P4;  
else if("5".equals(priority))  
return CreateAlertRequest.PriorityEnum.P5;  
else  
return CreateAlertRequest.PriorityEnum.P1;  
}  

private List\<String\> getStringList(String values) {  
List\<String\> reps= new ArrayList\<String\>();  
String \[\] rcps = values.split(",");  
for(String rcp: rcps)  
{  
reps.add(rcp);  
}  
return reps;  
}  

private List\<Recipient\> toVisibleRecipients(String visibleRecipient) {  
List\<Recipient\> reps= new ArrayList\<Recipient\>();  
// valid user hard code for testing  
Recipient tp = new UserRecipient();  
tp.setId("a35e7ff7-421a-48c9-b949-2443247dc57b-1578527320761");  
tp.setType(TypeEnum.USER);  
reps.add(tp);  

<br />

return reps;  
}  

private List\<TeamRecipient\> toTeamRecipients(String recipient) {  
List\<TeamRecipient\> reps= new ArrayList\<TeamRecipient\>();  

TeamRecipient tp = new TeamRecipient();  
// valid team hard code for testing  
tp.setId("605bd3fb-ceef-4f26-9071-75fca61e14db");  
tp.setType(TypeEnum.TEAM);  
reps.add(tp);  

<br />

return reps;  
}  

public SuccessResponse deleteAlert(String\[\] params) throws IllegalArgumentException  
{  
AlertApi client = new OpsGenieClient().alertV2();  
client.getApiClient().setApiKey(getKey());  
DeleteAlertRequest.IdentifierTypeEnum idType = DeleteAlertRequest.IdentifierTypeEnum.ID;  
String theId = null;  
if(id != null)  
{  
theId = id;  
}  
else if(tinyId != null)  
{  
theId = tinyId;  
idType = DeleteAlertRequest.IdentifierTypeEnum.TINY;  
}  
if(theId == null)  
{  
throw new IllegalArgumentException("id and tinyId are null. Use -id ############## or -tid ### in command line");  
}  
if( entity == null )  
entity = "System";  
if( user == null)  
user = "dt.pham@parsons.com";  
DeleteAlertRequest request = new DeleteAlertRequest();  
request.setIdentifier(theId);  
request.setIdentifierType(idType);  
request.setSource(entity);  
request.setUser(user);  

try {  
SuccessResponse response = client.deleteAlert(request);  
printResp(response);  
return response;  
} catch (ApiException e) {  
// TODO Auto-generated catch block  
e.printStackTrace();  
}  
return null;  
}  

public SuccessResponse closeAlert(String\[\] params) throws IllegalArgumentException  
{  
AlertApi client = new OpsGenieClient().alertV2();  
client.getApiClient().setApiKey(getKey());  

CloseAlertRequest request = new CloseAlertRequest();  

String idType = "id";  
String theId = null;  
if(id != null)  
{  
theId = id;  
}  
else if(tinyId != null)  
{  
theId = tinyId;  
idType = "tiny";  
}  
if(theId == null)  
{  
throw new IllegalArgumentException("id and tinyId are null. Use -id ############## or -tid ### in command line");  
}  
if( entity == null )  
entity = "System";  
if( user == null)  
user = "dt.pham@parsons.com";  
request.setUser(user);  
request.user(user);  
request.setNote("Alert was unnecessary, closed by Java API");  
request.setSource(entity);  
try {  
SuccessResponse response = client.closeAlert(theId, idType, request);  
printResp(response);  
return response;  
} catch (ApiException e) {  
e.printStackTrace();  
}  
return null;  
}  

public List\<BaseAlert\> getAlerts(String\[\] params)  
{  
System.out.println("Calling get first 20 alerts");  
AlertApi client = new OpsGenieClient().alertV2();  
client.getApiClient().setApiKey(getKey());  

ListAlertsRequest request = new ListAlertsRequest();  
//request.setQuery("status: open");  
request.setLimit(20);  
request.setOrder(ListAlertsRequest.OrderEnum.DESC);  
request.setSort(ListAlertsRequest.SortEnum.CREATEDAT);  

ListAlertsResponse response;  
try {  

response = client.listAlerts(request);  

List\<BaseAlert\> alerts = response.getData();  
for(BaseAlert alert: alerts)  
{  
System.out.println(alert.getId() + ". " + alert.getTinyId() + ". " + alert.getMessage() + ". " + alert.getOwner() + ". " + alert.getStatus());  
}  
return alerts;  
} catch (ApiException e) {  
e.printStackTrace();  
}  
return null;  
}  

public SuccessResponse ackAlert(String\[\] params) throws IllegalArgumentException  
{  
AlertApi client = new OpsGenieClient().alertV2();  
client.getApiClient().setApiKey(getKey());  

AcknowledgeAlertRequest request = new AcknowledgeAlertRequest();  

String idType = "id";  
String theId = null;  
if(id != null)  
{  
theId = id;  
}  
else if(tinyId != null)  
{  
theId = tinyId;  
idType = "tiny";  
}  
if(theId == null)  
{  
throw new IllegalArgumentException("id and tinyId are null. Use -id ############## or -tid ### in command line");  
}  
if( entity == null )  
entity = "System";  
if( user == null)  
user = "dt.pham@parsons.com";  
request.setUser(user);  
request.user(user);  
request.setNote("Alert was unnecessary, closed by Java API");  
request.setSource(entity);  
try {  
SuccessResponse response = client.acknowledgeAlert(theId, idType, request);  
printResp(response);  
return response;  
} catch (ApiException e) {  
e.printStackTrace();  
}  
return null;  
}  

public Object process(String\[\] params)  
{  
if("create".equalsIgnoreCase(action))  
return createAlert(params);  
else if("delete".equalsIgnoreCase(action))  
return deleteAlert(params);  
else if("close".equalsIgnoreCase(action))  
return closeAlert(params);  
else if("get".equalsIgnoreCase(action))  
return getAlerts(params);  
else if("ack".equalsIgnoreCase(action))  
return ackAlert(params);  
else  
throw new RuntimeException("Unexpect action " + action);  
}  
public static void main(String\[\] args) {  
OpsGenieAlertMgr alertMgr = new OpsGenieAlertMgr(args);  
alertMgr.process(args);  
}  
}


Answers:
author: DT_Pham
body: I got it working. Just don't populate TeamRecipient id and id-type.

comments: author: Kate Clavet
body: [@DT_Pham](/t5/user/viewprofilepage/user-id/3700065) glad you got it working! Thanks for posting and sharing your experience. :)