Skip to content

Commit

Permalink
migrated java examples from Java SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
ashah-splunk committed Sep 7, 2023
1 parent cb0e0a0 commit 278f5ae
Show file tree
Hide file tree
Showing 75 changed files with 5,374 additions and 0 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/java-examples-test.yml
@@ -0,0 +1,69 @@
name: Java Examples CI

on:
push:
paths:
- 'python/**'

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os:
- ubuntu-latest
java-version:
- 1.8
splunk-version:
- "8.2"
- "latest"
fail-fast: false

services:
splunk:
image: splunk/splunk:${{matrix.splunk-version}}
env:
SPLUNK_START_ARGS: --accept-license
SPLUNK_PASSWORD: changed!
SPLUNK_HEC_TOKEN: 11111111-1111-1111-1111-1111111111113
TEST_TCP_PORT: 10667
TEST_UDP_PORT: 10668
SPLUNK_HOME: "/opt/splunk"
SPLUNK_APPS_URL: https://github.com/splunk/sdk-app-collection/releases/download/v1.1.0/sdkappcollection.tgz
ports:
- 8000:8000
- 8089:8089
- 8088:8088
- 10667:10667
- 10668:10668/udp

steps:
- uses: actions/checkout@v2
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: Cache local Maven repository
working-directory: ./java
uses: actions/cache@v2
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-maven-
- name: Create .splunkrc file
working-directory: ./java
run: |
cd ~
echo host=localhost >> .splunkrc
echo port=8089 >> .splunkrc
echo username=admin >> .splunkrc
echo password=changed! >> .splunkrc
echo scheme=https >> .splunkrc
echo version=${{ matrix.splunk }} >> .splunkrc
- name: Test using maven
working-directory: ./java
run: make test
env:
SPLUNK_HOME: "/opt/splunk"
TEST_TCP_PORT: 10667
TEST_UDP_PORT: 10668
19 changes: 19 additions & 0 deletions java/Makefile
@@ -0,0 +1,19 @@
# ifeq ($(OS), Windows_NT)
# SEPERATOR=;
# else
# SEPERATOR=:
# endif
clean:
mvn clean

package:
mvn package

test:
mvn test -fae

run:
java -classpath './target/classes:./target/lib/*' com.splunk.examples.$(TARGET).Program $(ARGUMENTS)

test_specific:
@sh ./scripts/test_specific.sh
61 changes: 61 additions & 0 deletions java/pom.xml
@@ -0,0 +1,61 @@
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.splunk</groupId>
<artifactId>examples</artifactId>
<version>1.0</version>

<dependencies>
<dependency>
<groupId>com.splunk</groupId>
<artifactId>splunk</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-nodes</artifactId>
<version>RELEASE124</version>
</dependency>
<dependency>
<groupId>org.netbeans.api</groupId>
<artifactId>org-openide-explorer</artifactId>
<version>RELEASE124</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
<repository>
<id>splunk-artifactory</id>
<name>Splunk Releases</name>
<url>https://splunk.jfrog.io/splunk/ext-releases-local</url>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
4 changes: 4 additions & 0 deletions java/scripts/test_specific.sh
@@ -0,0 +1,4 @@
echo "To run a specific example:"
echo " make run TARGET=[example_name] ARGUMENTS=\"[cli arguments to pass for examples]\""
echo "For Instance, To run example for searching a particular index execute:"
echo " make run TARGET=search ARGUMENTS=\"'search index=_audit source=audittrail'\""
48 changes: 48 additions & 0 deletions java/src/main/java/com/splunk/examples/conf/Program.java
@@ -0,0 +1,48 @@
package com.splunk.examples.conf;

import com.splunk.*;

public class Program {

private static void list(Service service) {
ConfCollection confs = service.getConfs();
int count = 0;
for (EntityCollection conf : confs.values()){

System.out.println(count++ + " " + conf.getName());
}
}

private static void create(Service service, String name){
ConfCollection confs = service.getConfs();
if(confs.containsKey(name)){
Command.error("Conf " + name + " already exists");
}
confs.create(name);
}

public static void main(String[] args){
Command command = Command.splunk("conf").parse(args);
Service.setValidateCertificates(false);
Service service = Service.connect(command.opts);

if(command.args.length == 0){
list(service);
return;
}

if(command.args.length != 2){
Command.error("Action and conf-name required");
}

String action = command.args[0];
String name = command.args[1];

if(action.equals("create")){
create(service,name);
return;
}

Command.error("Please enter a valid action");
}
}
@@ -0,0 +1,62 @@
/*
* Copyright 2015 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.splunk.examples.endpoint_instantiation;

import com.splunk.*;

/**
* This example shows how to access any Splunk REST API endpoint.
* Here, we are just getting an EntityCollection of Entity objects representing
* apps on the Splunk server.
*
* You can also write a class which inherits from the Entity class.
* A minimal example of this is:
*
* public class MyEntity extends Entity {
* MyEntity(Service service, String path) {
* super(service, path);
* }
* }
*
* Then, you can write a class which inherits from the EntityCollection class.
* A minimal example of this is:
*
* public class MyEntityCollection extends EntityCollection<MyEntity> {
* MyEntityCollection(Service service) {
* super(service, "path/hardcoded", MyEntity.class, new Args());
* }
* }
*/

public class Program {
public static void main(String[] args) {
Command command = Command.splunk("info").parse(args);
Service.setValidateCertificates(false);
Service service = Service.connect(command.opts);

String mySplunkRESTPath = "apps/local";

EntityCollection myCollection = new EntityCollection(service, mySplunkRESTPath, Entity.class, new Args());

System.out.println("Found " + myCollection.size() + " Splunk apps:");

for (Object myEntity : myCollection.values()) {
Entity entity = (Entity) myEntity;
System.out.println("\t" + entity.getName());
}
}
}
41 changes: 41 additions & 0 deletions java/src/main/java/com/splunk/examples/explorer/AppNode.java
@@ -0,0 +1,41 @@
/*
* Copyright 2011 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.splunk.examples.explorer;

import com.splunk.Application;
import com.splunk.Entity;

class AppNode extends EntityNode {
public AppNode(Entity value) {
super(value);
Application app = (Application)value;
String displayName = app.getLabel();
if (displayName == null) displayName = app.getName();
setDisplayName(displayName);
}

@Override protected PropertyList getMetadata() {
PropertyList list = super.getMetadata();
list.add(boolean.class, "getCheckForUpdates");
list.add(String.class, "getLabel");
list.add(String.class, "getVersion");
list.add(boolean.class, "isConfigured");
list.add(boolean.class, "isManageable");
list.add(boolean.class, "isVisible");
return list;
}
}
@@ -0,0 +1,44 @@
/*
* Copyright 2011 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.splunk.examples.explorer;

import com.splunk.ConfCollection;
import com.splunk.Entity;
import com.splunk.EntityCollection;

import java.util.Collection;
import org.openide.nodes.Children;
import org.openide.nodes.Node;

class ConfCollectionKids extends Children.Keys<EntityCollection> {
ConfCollection confs;

ConfCollectionKids(ConfCollection confs) {
this.confs = confs;
}

@Override protected void addNotify() {
Collection<EntityCollection<Entity>> values = confs.values();
setKeys(values);
}

@Override protected Node[] createNodes(EntityCollection conf) {
return new Node[] {
new EntityCollectionNode(conf.getTitle(), conf, StanzaNode.class)
};
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2011 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.splunk.examples.explorer;

import com.splunk.ConfCollection;

// UNDONE: Some duplication of EntityConnectionNode below, eg: title count
// and size property - could probably be refactored into a shared
// CollectionNode base class.
class ConfCollectionNode extends ResourceNode {
ConfCollectionNode(ConfCollection value) {
super(value, new ConfCollectionKids(value));
setDisplayName(String.format("Configuration (%d)", value.size()));
}

@Override protected PropertyList getMetadata() {
PropertyList list = super.getMetadata();
list.add(int.class, "size");
return list;
}
}

0 comments on commit 278f5ae

Please sign in to comment.