Skip to content

Commit be455c0

Browse files
authored
Merge pull request #193 from splunk/revert-v2-changes
Updated version checks to enable v2 search APIs
2 parents b91a623 + 9423ce6 commit be455c0

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed

splunk/src/main/java/com/splunk/Job.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,15 @@ private InputStream getEventsMethod(String methodPath, Map args) {
372372
// v1(GET), v2(POST)
373373
String fullPath;
374374
ResponseMessage response;
375-
if (service.versionIsEarlierThan("9.0")) {
375+
if (!service.enableV2SearchApi()) {
376376
fullPath = path.replace(JobCollection.REST_PATH_V2, JobCollection.REST_PATH) + methodPath;
377377
response = service.get(fullPath, args);
378378
}
379379
else {
380380
fullPath = path.replace(JobCollection.REST_PATH, JobCollection.REST_PATH_V2) + methodPath;
381381
response = service.post(fullPath, args);
382382
}
383-
383+
384384
return response.getContent();
385385
}
386386

splunk/src/main/java/com/splunk/JobCollection.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class JobCollection extends EntityCollection<Job> {
3434
* @param service The connected {@code Service} instance.
3535
*/
3636
JobCollection(Service service) {
37-
super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class);
37+
super(service, service.enableV2SearchApi() ? REST_PATH_V2 : REST_PATH, Job.class);
3838
this.refreshArgs.put("count", "0");
3939
}
4040

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

@@ -87,7 +87,7 @@ public Job create(String query, Map args) {
8787
.item(0)
8888
.getTextContent();
8989

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

splunk/src/main/java/com/splunk/Service.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public class Service extends BaseService {
6666
/** The version of this Splunk instance, once logged in. */
6767
public String version = null;
6868

69+
/** The type of this Splunk instance, once logged in. */
70+
public String instanceType = null;
71+
6972
/** The default host name, which is used when a host name is not provided.*/
7073
public static String DEFAULT_HOST = "localhost";
7174

@@ -225,7 +228,7 @@ public InputStream export(String search, Map args) {
225228
}
226229
ResponseMessage response;
227230

228-
if (versionIsAtLeast("9.0"))
231+
if(enableV2SearchApi())
229232
response = post(JobCollection.REST_PATH_V2 + "/export", args);
230233
else {
231234
response = post(JobCollection.REST_PATH + "/export", args);
@@ -1147,6 +1150,7 @@ public Service login(String username, String password) {
11471150
.getTextContent();
11481151
this.token = "Splunk " + sessionKey;
11491152
this.version = this.getInfo().getVersion();
1153+
this.instanceType = this.getInfo().getInstanceType();
11501154
if (versionCompare("4.3") >= 0)
11511155
this.passwordEndPoint = "storage/passwords";
11521156

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

1261-
if (versionIsAtLeast("9.0"))
1265+
if(enableV2SearchApi())
12621266
return post("search/v2/parser", args);
12631267
else
12641268
return get("search/parser", args);
@@ -1350,6 +1354,15 @@ public void setBearerToken(String value) {
13501354
this.token = value.contains("Splunk") || value.contains("Bearer") ? value : "Bearer " + value;
13511355
}
13521356

1357+
1358+
public boolean enableV2SearchApi(){
1359+
if(this.instanceType.equalsIgnoreCase("cloud")) {
1360+
return versionIsAtLeast("9.0.2209");
1361+
}else{
1362+
return versionIsAtLeast("9.0.2");
1363+
}
1364+
}
1365+
13531366
/**
13541367
* Returns true if this Splunk instance's version is no earlier than
13551368
* the version specified in {@code version}.

splunk/src/main/java/com/splunk/ServiceInfo.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ public String getVersion() {
155155
return getString("version");
156156
}
157157

158+
public String getInstanceType() {return getString("instance_type", "");}
159+
158160
/**
159161
* Indicates whether this Splunk instance is running under a free license.
160162
*

splunk/src/test/java/com/splunk/ServiceTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,4 +715,24 @@ public void testPost() {
715715
Assert.assertTrue(firstLineIsXmlDtd(response.getContent()));
716716
}
717717

718+
/*
719+
Test whether the V2 and V1 Search APIs are switched properly based on the Type of Splunk Instance and Version.
720+
*/
721+
@Test
722+
public void testEnableV2Api(){
723+
if(service.instanceType.equalsIgnoreCase("cloud")) {
724+
if(service.versionIsEarlierThan("9.0.2209")){
725+
Assert.assertFalse(service.enableV2SearchApi());
726+
}else{
727+
Assert.assertTrue(service.enableV2SearchApi());
728+
}
729+
}else{
730+
if(service.versionIsEarlierThan("9.0.2")){
731+
Assert.assertFalse(service.enableV2SearchApi());
732+
}else{
733+
Assert.assertTrue(service.enableV2SearchApi());
734+
}
735+
}
736+
}
737+
718738
}

0 commit comments

Comments
 (0)