Skip to content

Commit dd0a78b

Browse files
authored
Merge pull request #189 from splunk/DVPL-10898-develop
SDK Support for splunkd search API changes
2 parents f05a40f + a7f3457 commit dd0a78b

File tree

7 files changed

+78
-9
lines changed

7 files changed

+78
-9
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
java-version:
1414
- 1.8
1515
splunk-version:
16-
- "8.0"
16+
- "8.2"
1717
- "latest"
1818
runs-on: ${{ matrix.os }}
1919

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Here's what you need to get going with the Splunk SDK for Java.
4040
If you haven't already installed Splunk, download it
4141
[here](http://www.splunk.com/download). For more about installing and running
4242
Splunk and system requirements, see
43-
[Installing & Running Splunk](http://dev.splunk.com/view/SP-CAAADRV). The Splunk SDK for Java has been tested with Splunk Enterprise 8.0 and 8.2.0.
43+
[Installing & Running Splunk](http://dev.splunk.com/view/SP-CAAADRV). The Splunk SDK for Java has been tested with Splunk Enterprise 9.0 and 8.2.
4444

4545
#### Splunk SDK for Java
4646

splunk/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
<plugin>
7272
<groupId>org.jacoco</groupId>
7373
<artifactId>jacoco-maven-plugin</artifactId>
74-
<version>0.8.7</version>
74+
<version>0.8.8</version>
7575
<executions>
7676
<execution>
7777
<goals>

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class Job extends Entity {
3535
*
3636
* @param service The connected {@code Service} instance.
3737
* @param path The search jobs endpoint.
38+
* @param sid The sid of the job.
3839
*/
3940
Job(Service service, String path) {
4041
super(service, path);
@@ -367,7 +368,19 @@ private InputStream getEventsMethod(String methodPath, Map args) {
367368
args.put("segmentation", "none");
368369
}
369370

370-
ResponseMessage response = service.get(path + methodPath, args);
371+
// Splunk version pre-9.0 doesn't support v2
372+
// v1(GET), v2(POST)
373+
String fullPath;
374+
ResponseMessage response;
375+
if (service.versionIsEarlierThan("9.0")) {
376+
fullPath = path.replace(JobCollection.REST_PATH_V2, JobCollection.REST_PATH) + methodPath;
377+
response = service.get(fullPath, args);
378+
}
379+
else {
380+
fullPath = path.replace(JobCollection.REST_PATH, JobCollection.REST_PATH_V2) + methodPath;
381+
response = service.post(fullPath, args);
382+
}
383+
371384
return response.getContent();
372385
}
373386

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ public class JobCollection extends EntityCollection<Job> {
2727
static String oneShotNotAllowed = String.format(
2828
"Oneshot not allowed, use service oneshot search method");
2929
static final String REST_PATH = "search/jobs";
30+
static final String REST_PATH_V2 = "search/v2/jobs";
3031
/**
3132
* Class constructor.
3233
*
3334
* @param service The connected {@code Service} instance.
3435
*/
3536
JobCollection(Service service) {
36-
super(service, REST_PATH, Job.class);
37+
super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class);
3738
this.refreshArgs.put("count", "0");
3839
}
3940

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

@@ -86,7 +87,8 @@ public Job create(String query, Map args) {
8687
.item(0)
8788
.getTextContent();
8889

89-
Job job = new Job(service, REST_PATH + "/" + sid);
90+
String path = service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH;
91+
Job job = new Job(service, path + "/" + sid);
9092
job.refresh();
9193

9294
return job;

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,13 @@ public InputStream export(String search, Map args) {
223223
if (!args.containsKey("segmentation")) {
224224
args.put("segmentation", "none");
225225
}
226-
ResponseMessage response = get(JobCollection.REST_PATH + "/export", args);
226+
ResponseMessage response;
227+
228+
if (versionIsAtLeast("9.0"))
229+
response = post(JobCollection.REST_PATH_V2 + "/export", args);
230+
else {
231+
response = post(JobCollection.REST_PATH + "/export", args);
232+
}
227233
return new ExportResultsStream(response.getContent());
228234
}
229235

@@ -1251,7 +1257,11 @@ public ResponseMessage parse(String query) {
12511257
*/
12521258
public ResponseMessage parse(String query, Map args) {
12531259
args = Args.create(args).add("q", query);
1254-
return get("search/parser", args);
1260+
1261+
if (versionIsAtLeast("9.0"))
1262+
return post("search/v2/parser", args);
1263+
else
1264+
return get("search/parser", args);
12551265
}
12561266

12571267
/**

splunk/src/test/java/com/splunk/SearchJobTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ public void testEventsFromJob() {
6262
job.cancel();
6363
}
6464

65+
@Test
66+
public void testEventsWithSearchParams() {
67+
Job job = jobs.create(QUERY);
68+
waitUntilDone(job);
69+
70+
Map args = new HashMap<String, Object>();
71+
args.put("search","| head 1");
72+
Assert.assertEquals(1, countEvents(job.getEvents(args)));
73+
74+
job.cancel();
75+
}
76+
6577
@Test
6678
public void testResultsFromJob() {
6779
Job job = jobs.create(QUERY);
@@ -71,6 +83,18 @@ public void testResultsFromJob() {
7183

7284
job.cancel();
7385
}
86+
87+
@Test
88+
public void testResultsWithSearchParams() {
89+
Job job = jobs.create(QUERY);
90+
waitUntilDone(job);
91+
92+
Map args = new HashMap<String, Object>();
93+
args.put("search","| head 1");
94+
Assert.assertEquals(1, countEvents(job.getResults(args)));
95+
96+
job.cancel();
97+
}
7498

7599
@Test
76100
public void testGetJobBySid() {
@@ -584,6 +608,26 @@ public void testPreview() throws InterruptedException {
584608
job.cancel();
585609
}
586610

611+
@Test
612+
public void testPreviewWithSearchParams() throws InterruptedException {
613+
JobArgs args = new JobArgs();
614+
args.put("field_list", "source,host,sourcetype");
615+
args.setStatusBuckets(100);
616+
617+
Job job = jobs.create(QUERY, args);
618+
619+
while (!job.isReady()) {
620+
Thread.sleep(100);
621+
}
622+
623+
Map argsSearchQuery = new HashMap<String, Object>();
624+
argsSearchQuery.put("search","| head 1");
625+
// Assert.assertTrue(1 >= countEvents(job.getResultsPreview(args)));
626+
Assert.assertEquals(1, countEvents(job.getResultsPreview(argsSearchQuery)));
627+
628+
job.cancel();
629+
}
630+
587631
@Test
588632
public void testSearchLog() {
589633
Job job = jobs.create(QUERY);

0 commit comments

Comments
 (0)