Skip to content

Commit 68b8ec3

Browse files
authored
Merge pull request #405 from splunk/fix-searchcommands_app-example
Fix searchcommands_app example
2 parents 4a1d31a + 828fc1e commit 68b8ec3

28 files changed

+145
-1177
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ jobs:
4545
echo password=changed! >> .splunkrc
4646
echo scheme=https >> .splunkrc
4747
echo version=${{ matrix.splunk }} >> .splunkrc
48-
- name: Create build dir for ExamplesTestCase::test_build_dir_exists test case
49-
run: |
50-
cd ~
51-
cd /home/runner/work/splunk-sdk-python/splunk-sdk-python/
52-
python setup.py build
53-
python setup.py dist
5448
- name: Install tox
5549
run: pip install tox
5650
- name: Test Execution

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ MANIFEST
1616
coverage_report
1717
test.log
1818
examples/*/local
19-
examples/*/metadata/local.meta
19+
examples/**/local.meta
20+
examples/**/*.log
2021
tests/searchcommands_data/log/
2122
tests/searchcommands_data/output/
2223
examples/searchcommands_app/searchcommand_app.log
2324
Test Results*.html
2425
tests/searchcommands/data/app/app.log
2526
splunk_sdk.egg-info/
2627
dist/
27-
examples/searchcommands_app/package/default/commands.conf
2828
examples/searchcommands_app/package/lib/splunklib
2929
tests/searchcommands/apps/app_with_logging_configuration/*.log
3030
*.observed

Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,11 @@ DATE := `date "+%FT%T%z"`
1818
CONTAINER_NAME := 'splunk'
1919

2020
.PHONY: all
21-
all: build_app test
21+
all: test
2222

2323
init:
2424
@echo "$(ATTN_COLOR)==> init $(NO_COLOR)"
2525

26-
.PHONY: build_app
27-
build_app:
28-
@echo "$(ATTN_COLOR)==> build_app $(NO_COLOR)"
29-
@python setup.py build dist
30-
3126
.PHONY: docs
3227
docs:
3328
@echo "$(ATTN_COLOR)==> docs $(NO_COLOR)"

docker-compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ services:
99
- SPLUNK_HEC_TOKEN=11111111-1111-1111-1111-1111111111113
1010
- SPLUNK_PASSWORD=changed!
1111
- SPLUNK_APPS_URL=https://github.com/splunk/sdk-app-collection/releases/download/v1.1.0/sdkappcollection.tgz
12+
volumes:
13+
- ./examples/github_forks:/opt/splunk/etc/apps/github_forks
14+
- ./splunklib:/opt/splunk/etc/apps/github_forks/lib/splunklib
15+
- ./examples/random_numbers:/opt/splunk/etc/apps/random_numbers
16+
- ./splunklib:/opt/splunk/etc/apps/random_numbers/lib/splunklib
17+
- ./examples/searchcommands_app/package:/opt/splunk/etc/apps/searchcommands_app
18+
- ./splunklib:/opt/splunk/etc/apps/searchcommands_app/lib/splunklib
19+
- ./examples/twitted/twitted:/opt/splunk/etc/apps/twitted
1220
ports:
1321
- 8000:8000
1422
- 8088:8088

examples/github_forks/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
splunk-sdk-python github_forks example
2+
========================================
3+
4+
This app provides an example of a modular input that generates the number of repository forks according to the Github API based on the owner and repo_name provided by the user during setup of the input.
5+
6+
To run this example locally run `SPLUNK_VERSION=latest docker compose up -d` from the root of this repository which will mount this example alongside the latest version of splunklib within `/opt/splunk/etc/apps/github_forks` and `/opt/splunk/etc/apps/github_forks/lib/splunklib` within the `splunk` container.
7+
8+
Once the docker container is up and healthy log into the Splunk UI and setup a new `Github Repository Forks` input by visiting this page: http://localhost:8000/en-US/manager/github_forks/datainputstats and selecting the "Add new..." button next to the Local Inputs > Random Inputs. Enter values for a Github Repository owner and repo_name, for example owner = `splunk` repo_name = `splunk-sdk-python`.
9+
10+
NOTE: If no Github Repository Forks input appears then the script is likely not running properly, see https://docs.splunk.com/Documentation/SplunkCloud/latest/AdvancedDev/ModInputsDevTools for more details on debugging the modular input using the command line and relevant logs.
11+
12+
Once the input is created you should be able to see an event when running the following search: `source="github_forks://*"` the event should contain fields for `owner` and `repository` matching the values you input during setup and then a `fork_count` field corresponding to the number of forks the repo has according to the Github API.

examples/github_forks/github_forks.py renamed to examples/github_forks/bin/github_forks.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@
1515
# under the License.
1616

1717
from __future__ import absolute_import
18-
import sys, urllib2, json
18+
import os
19+
import sys
20+
import json
21+
# NOTE: splunklib must exist within github_forks/lib/splunklib for this
22+
# example to run! To run this locally use `SPLUNK_VERSION=latest docker compose up -d`
23+
# from the root of this repo which mounts this example and the latest splunklib
24+
# code together at /opt/splunk/etc/apps/github_forks
25+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
1926

2027
from splunklib.modularinput import *
2128
from splunklib import six
29+
from six.moves import http_client
2230

2331
class MyScript(Script):
2432
"""All modular inputs should inherit from the abstract base class Script
@@ -87,11 +95,9 @@ def validate_input(self, validation_definition):
8795
# Get the values of the parameters, and construct a URL for the Github API
8896
owner = validation_definition.parameters["owner"]
8997
repo_name = validation_definition.parameters["repo_name"]
90-
repo_url = "https://api.github.com/repos/%s/%s" % (owner, repo_name)
9198

92-
# Read the response from the Github API, then parse the JSON data into an object
93-
response = urllib2.urlopen(repo_url).read()
94-
jsondata = json.loads(response)
99+
# Call Github to retrieve repo information
100+
jsondata = _get_github_repos(owner, repo_name)
95101

96102
# If there is only 1 field in the jsondata object,some kind or error occurred
97103
# with the Github API.
@@ -125,9 +131,7 @@ def stream_events(self, inputs, ew):
125131
repo_name = input_item["repo_name"]
126132

127133
# Get the fork count from the Github API
128-
repo_url = "https://api.github.com/repos/%s/%s" % (owner, repo_name)
129-
response = urllib2.urlopen(repo_url).read()
130-
jsondata = json.loads(response)
134+
jsondata = _get_github_repos(owner, repo_name)
131135
fork_count = jsondata["forks_count"]
132136

133137
# Create an Event object, and set its fields
@@ -139,5 +143,20 @@ def stream_events(self, inputs, ew):
139143
# Tell the EventWriter to write this event
140144
ew.write_event(event)
141145

146+
147+
def _get_github_repos(owner, repo_name):
148+
# Read the response from the Github API, then parse the JSON data into an object
149+
repo_path = "/repos/%s/%s" % (owner, repo_name)
150+
connection = http_client.HTTPSConnection('api.github.com')
151+
headers = {
152+
'Content-type': 'application/json',
153+
'User-Agent': 'splunk-sdk-python',
154+
}
155+
connection.request('GET', repo_path, headers=headers)
156+
response = connection.getresponse()
157+
body = response.read().decode()
158+
return json.loads(body)
159+
160+
142161
if __name__ == "__main__":
143162
sys.exit(MyScript().run(sys.argv))

examples/random_numbers/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
splunk-sdk-python random_numbers example
2+
========================================
3+
4+
This app provides an example of a modular input that generates a random number between the min and max values provided by the user during setup of the input.
5+
6+
To run this example locally run `SPLUNK_VERSION=latest docker compose up -d` from the root of this repository which will mount this example alongside the latest version of splunklib within `/opt/splunk/etc/apps/random_numbers` and `/opt/splunk/etc/apps/random_numbers/lib/splunklib` within the `splunk` container.
7+
8+
Once the docker container is up and healthy log into the Splunk UI and setup a new `Random Numbers` input by visiting this page: http://localhost:8000/en-US/manager/random_numbers/datainputstats and selecting the "Add new..." button next to the Local Inputs > Random Inputs. Enter values for the `min` and `max` values which the random number should be generated between.
9+
10+
NOTE: If no Random Numbers input appears then the script is likely not running properly, see https://docs.splunk.com/Documentation/SplunkCloud/latest/AdvancedDev/ModInputsDevTools for more details on debugging the modular input using the command line and relevant logs.
11+
12+
Once the input is created you should be able to see an event when running the following search: `source="random_numbers://*"` the event should contain a `number` field with a float between the min and max specified when the input was created.

examples/random_numbers/random_numbers.py renamed to examples/random_numbers/bin/random_numbers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
from __future__ import absolute_import
1818
import random, sys
1919
import os
20+
# NOTE: splunklib must exist within random_numbers/lib/splunklib for this
21+
# example to run! To run this locally use `SPLUNK_VERSION=latest docker compose up -d`
22+
# from the root of this repo which mounts this example and the latest splunklib
23+
# code together at /opt/splunk/etc/apps/random_numbers
2024
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "lib"))
2125

2226
from splunklib.modularinput import *

examples/searchcommands_app/Build-App

Lines changed: 0 additions & 31 deletions
This file was deleted.

examples/searchcommands_app/Build-App.ps1

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)