Skip to content

SDK Support for splunkd search API changes #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ jobs:
server-id: splunk-artifactory
- name: build
run: mvn package --file pom.xml -DskipTests=true
- name: Create temp artifacts apidocs folder
run: mkdir apidocs
- name: Create GitHub Release
uses: softprops/action-gh-release@v1
with:
Expand All @@ -32,3 +34,12 @@ jobs:
env:
MAVEN_USERNAME: ${{ secrets.ARTIFACTORY_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.ARTIFACTORY_PASSWORD }}

- name: Zip docs
run: zip -r apidocs/docs.zip splunk/target/apidocs

- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: apidocs
path: apidocs/docs.zip
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
java-version:
- 1.8
splunk-version:
- "8.0"
- "8.2"
- "latest"
runs-on: ${{ matrix.os }}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Here's what you need to get going with the Splunk SDK for Java.
If you haven't already installed Splunk, download it
[here](http://www.splunk.com/download). For more about installing and running
Splunk and system requirements, see
[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.
[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.

#### Splunk SDK for Java

Expand Down
2 changes: 1 addition & 1 deletion splunk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.7</version>
<version>0.8.8</version>
<executions>
<execution>
<goals>
Expand Down
2 changes: 1 addition & 1 deletion splunk/src/main/java/com/splunk/HttpService.java
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public int getPort() {
/**
* Sets Custom Headers of this service
*
* @param headers
* @param headers The custom headers.
*/
public void setCustomHeaders(Map<String, String> headers) {
if (Objects.nonNull(headers)) {
Expand Down
15 changes: 14 additions & 1 deletion splunk/src/main/java/com/splunk/Job.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class Job extends Entity {
*
* @param service The connected {@code Service} instance.
* @param path The search jobs endpoint.
* @param sid The sid of the job.
*/
Job(Service service, String path) {
super(service, path);
Expand Down Expand Up @@ -367,7 +368,19 @@ private InputStream getEventsMethod(String methodPath, Map args) {
args.put("segmentation", "none");
}

ResponseMessage response = service.get(path + methodPath, args);
// Splunk version pre-9.0 doesn't support v2
// v1(GET), v2(POST)
String fullPath;
ResponseMessage response;
if (service.versionIsEarlierThan("9.0")) {
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
8 changes: 5 additions & 3 deletions splunk/src/main/java/com/splunk/JobCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ public class JobCollection extends EntityCollection<Job> {
static String oneShotNotAllowed = String.format(
"Oneshot not allowed, use service oneshot search method");
static final String REST_PATH = "search/jobs";
static final String REST_PATH_V2 = "search/v2/jobs";
/**
* Class constructor.
*
* @param service The connected {@code Service} instance.
*/
JobCollection(Service service) {
super(service, REST_PATH, Job.class);
super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class);
this.refreshArgs.put("count", "0");
}

Expand All @@ -45,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, REST_PATH, Job.class, args);
super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class, args);
this.refreshArgs.put("count", "0");
}

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

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

return job;
Expand Down
2 changes: 1 addition & 1 deletion splunk/src/main/java/com/splunk/OutputServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public String getStatus() {
/**
* Returns client certificate path.
*
* @return
* @return Path of client certificate.
*/
public String getClientCert() {
return getString("clientCert", "");
Expand Down
14 changes: 12 additions & 2 deletions splunk/src/main/java/com/splunk/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,13 @@ public InputStream export(String search, Map args) {
if (!args.containsKey("segmentation")) {
args.put("segmentation", "none");
}
ResponseMessage response = get(JobCollection.REST_PATH + "/export", args);
ResponseMessage response;

if (versionIsAtLeast("9.0"))
response = post(JobCollection.REST_PATH_V2 + "/export", args);
else {
response = post(JobCollection.REST_PATH + "/export", args);
}
return new ExportResultsStream(response.getContent());
}

Expand Down Expand Up @@ -1251,7 +1257,11 @@ public ResponseMessage parse(String query) {
*/
public ResponseMessage parse(String query, Map args) {
args = Args.create(args).add("q", query);
return get("search/parser", args);

if (versionIsAtLeast("9.0"))
return post("search/v2/parser", args);
else
return get("search/parser", args);
}

/**
Expand Down
44 changes: 44 additions & 0 deletions splunk/src/test/java/com/splunk/SearchJobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public void testEventsFromJob() {
job.cancel();
}

@Test
public void testEventsWithSearchParams() {
Job job = jobs.create(QUERY);
waitUntilDone(job);

Map args = new HashMap<String, Object>();
args.put("search","| head 1");
Assert.assertEquals(1, countEvents(job.getEvents(args)));

job.cancel();
}

@Test
public void testResultsFromJob() {
Job job = jobs.create(QUERY);
Expand All @@ -71,6 +83,18 @@ public void testResultsFromJob() {

job.cancel();
}

@Test
public void testResultsWithSearchParams() {
Job job = jobs.create(QUERY);
waitUntilDone(job);

Map args = new HashMap<String, Object>();
args.put("search","| head 1");
Assert.assertEquals(1, countEvents(job.getResults(args)));

job.cancel();
}

@Test
public void testGetJobBySid() {
Expand Down Expand Up @@ -584,6 +608,26 @@ public void testPreview() throws InterruptedException {
job.cancel();
}

@Test
public void testPreviewWithSearchParams() throws InterruptedException {
JobArgs args = new JobArgs();
args.put("field_list", "source,host,sourcetype");
args.setStatusBuckets(100);

Job job = jobs.create(QUERY, args);

while (!job.isReady()) {
Thread.sleep(100);
}

Map argsSearchQuery = new HashMap<String, Object>();
argsSearchQuery.put("search","| head 1");
// Assert.assertTrue(1 >= countEvents(job.getResultsPreview(args)));
Assert.assertEquals(1, countEvents(job.getResultsPreview(argsSearchQuery)));

job.cancel();
}

@Test
public void testSearchLog() {
Job job = jobs.create(QUERY);
Expand Down