Skip to content

Commit

Permalink
Merge pull request #193 from splunk/revert-v2-changes
Browse files Browse the repository at this point in the history
Updated version checks to enable v2 search APIs
  • Loading branch information
ashah-splunk authored Sep 12, 2022
2 parents b91a623 + 9423ce6 commit be455c0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
4 changes: 2 additions & 2 deletions splunk/src/main/java/com/splunk/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -372,15 +372,15 @@ private InputStream getEventsMethod(String methodPath, Map args) {
// v1(GET), v2(POST)
String fullPath;
ResponseMessage response;
if (service.versionIsEarlierThan("9.0")) {
if (!service.enableV2SearchApi()) {
fullPath = path.replace(JobCollection.REST_PATH_V2, JobCollection.REST_PATH) + methodPath;
response = service.get(fullPath, args);
}
else {
fullPath = path.replace(JobCollection.REST_PATH, JobCollection.REST_PATH_V2) + methodPath;
response = service.post(fullPath, args);
}

return response.getContent();
}

Expand Down
6 changes: 3 additions & 3 deletions splunk/src/main/java/com/splunk/JobCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class JobCollection extends EntityCollection<Job> {
* @param service The connected {@code Service} instance.
*/
JobCollection(Service service) {
super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class);
super(service, service.enableV2SearchApi() ? REST_PATH_V2 : REST_PATH, Job.class);
this.refreshArgs.put("count", "0");
}

Expand All @@ -46,7 +46,7 @@ public class JobCollection extends EntityCollection<Job> {
* return and how to sort them (see {@link CollectionArgs}).
*/
JobCollection(Service service, Args args) {
super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class, args);
super(service, service.enableV2SearchApi() ? REST_PATH_V2 : REST_PATH, Job.class);
this.refreshArgs.put("count", "0");
}

Expand Down Expand Up @@ -87,7 +87,7 @@ public Job create(String query, Map args) {
.item(0)
.getTextContent();

String path = service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH;
String path = service.enableV2SearchApi() ? REST_PATH_V2 : REST_PATH;
Job job = new Job(service, path + "/" + sid);
job.refresh();

Expand Down
17 changes: 15 additions & 2 deletions splunk/src/main/java/com/splunk/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ public class Service extends BaseService {
/** The version of this Splunk instance, once logged in. */
public String version = null;

/** The type of this Splunk instance, once logged in. */
public String instanceType = null;

/** The default host name, which is used when a host name is not provided.*/
public static String DEFAULT_HOST = "localhost";

Expand Down Expand Up @@ -225,7 +228,7 @@ public InputStream export(String search, Map args) {
}
ResponseMessage response;

if (versionIsAtLeast("9.0"))
if(enableV2SearchApi())
response = post(JobCollection.REST_PATH_V2 + "/export", args);
else {
response = post(JobCollection.REST_PATH + "/export", args);
Expand Down Expand Up @@ -1147,6 +1150,7 @@ public Service login(String username, String password) {
.getTextContent();
this.token = "Splunk " + sessionKey;
this.version = this.getInfo().getVersion();
this.instanceType = this.getInfo().getInstanceType();
if (versionCompare("4.3") >= 0)
this.passwordEndPoint = "storage/passwords";

Expand Down Expand Up @@ -1258,7 +1262,7 @@ public ResponseMessage parse(String query) {
public ResponseMessage parse(String query, Map args) {
args = Args.create(args).add("q", query);

if (versionIsAtLeast("9.0"))
if(enableV2SearchApi())
return post("search/v2/parser", args);
else
return get("search/parser", args);
Expand Down Expand Up @@ -1350,6 +1354,15 @@ public void setBearerToken(String value) {
this.token = value.contains("Splunk") || value.contains("Bearer") ? value : "Bearer " + value;
}


public boolean enableV2SearchApi(){
if(this.instanceType.equalsIgnoreCase("cloud")) {
return versionIsAtLeast("9.0.2209");
}else{
return versionIsAtLeast("9.0.2");
}
}

/**
* Returns true if this Splunk instance's version is no earlier than
* the version specified in {@code version}.
Expand Down
2 changes: 2 additions & 0 deletions splunk/src/main/java/com/splunk/ServiceInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ public String getVersion() {
return getString("version");
}

public String getInstanceType() {return getString("instance_type", "");}

/**
* Indicates whether this Splunk instance is running under a free license.
*
Expand Down
20 changes: 20 additions & 0 deletions splunk/src/test/java/com/splunk/ServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -715,4 +715,24 @@ public void testPost() {
Assert.assertTrue(firstLineIsXmlDtd(response.getContent()));
}

/*
Test whether the V2 and V1 Search APIs are switched properly based on the Type of Splunk Instance and Version.
*/
@Test
public void testEnableV2Api(){
if(service.instanceType.equalsIgnoreCase("cloud")) {
if(service.versionIsEarlierThan("9.0.2209")){
Assert.assertFalse(service.enableV2SearchApi());
}else{
Assert.assertTrue(service.enableV2SearchApi());
}
}else{
if(service.versionIsEarlierThan("9.0.2")){
Assert.assertFalse(service.enableV2SearchApi());
}else{
Assert.assertTrue(service.enableV2SearchApi());
}
}
}

}

0 comments on commit be455c0

Please sign in to comment.