|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2011-2015 Splunk, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 6 | +# not use this file except in compliance with the License. You may obtain |
| 7 | +# a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | + |
| 17 | +"""A command line utility for interacting with Splunk KV Store Collections.""" |
| 18 | + |
| 19 | +import sys, os, json |
| 20 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..")) |
| 21 | + |
| 22 | +from splunklib.client import connect |
| 23 | + |
| 24 | +try: |
| 25 | + from utils import parse |
| 26 | +except ImportError: |
| 27 | + raise Exception("Add the SDK repository to your PYTHONPATH to run the examples " |
| 28 | + "(e.g., export PYTHONPATH=~/splunk-sdk-python.") |
| 29 | + |
| 30 | +def main(): |
| 31 | + opts = parse(sys.argv[1:], {}, ".splunkrc") |
| 32 | + opts.kwargs["owner"] = "nobody" |
| 33 | + opts.kwargs["app"] = "search" |
| 34 | + service = connect(**opts.kwargs) |
| 35 | + |
| 36 | + print "KV Store Collections:" |
| 37 | + for collection in service.kvstore: |
| 38 | + print " %s" % collection.name |
| 39 | + |
| 40 | + # Let's delete a collection if it already exists, and then create it |
| 41 | + collection_name = "example_collection" |
| 42 | + if collection_name in service.kvstore: |
| 43 | + service.kvstore.delete(collection_name) |
| 44 | + |
| 45 | + # Let's create it and then make sure it exists |
| 46 | + service.kvstore.create(collection_name) |
| 47 | + collection = service.kvstore[collection_name] |
| 48 | + |
| 49 | + # Let's make sure it doesn't have any data |
| 50 | + print "Should be empty: %s" % json.dumps(collection.data.query()) |
| 51 | + |
| 52 | + # Let's add some data |
| 53 | + collection.data.insert(json.dumps({"_key": "item1", "somekey": 1, "otherkey": "foo"})) |
| 54 | + collection.data.insert(json.dumps({"_key": "item2", "somekey": 2, "otherkey": "foo"})) |
| 55 | + collection.data.insert(json.dumps({"somekey": 3, "otherkey": "bar"})) |
| 56 | + |
| 57 | + # Let's make sure it has the data we just entered |
| 58 | + print "Should have our data: %s" % json.dumps(collection.data.query(), indent=1) |
| 59 | + |
| 60 | + # Let's run some queries |
| 61 | + print "Should return item1: %s" % json.dumps(collection.data.query_by_id("item1"), indent=1) |
| 62 | + |
| 63 | + query = json.dumps({"otherkey": "foo"}) |
| 64 | + print "Should return item1 and item2: %s" % json.dumps(collection.data.query(query=query), indent=1) |
| 65 | + |
| 66 | + query = json.dumps({"otherkey": "bar"}) |
| 67 | + print "Should return third item with auto-generated _key: %s" % json.dumps(collection.data.query(query=query), indent=1) |
| 68 | + |
| 69 | + # Let's delete the collection |
| 70 | + collection.delete() |
| 71 | + |
| 72 | +if __name__ == "__main__": |
| 73 | + main() |
| 74 | + |
| 75 | + |
0 commit comments