
import splunklib.client as client

LAPTOP_HOST = "192.168.10.131"
DESKTOP_HOST = "192.168.100.131"
PORT = 8089
USERNAME = "admin"
PASSWORD = "<PASSWORD>"

# Create a Service instance and log in 
service = client.connect(
    host=DESKTOP_HOST,
    port=PORT,
    username=USERNAME,
    password=PASSWORD)

# Switch to nobody context
service.namespace['owner'] = 'nobody'
# Print installed apps to the console to verify login
print("Installed Apps")
print("-" * 10)
for app in service.apps:
    print(app.name)


# Create a test KV store
collections = service.kvstore
fields = {
    "ip": "string"
}
newkv = collections.create("test", fields=fields )
# Print out the list of KV store collections in Splunk
print("KVStore Collections")
print("-" * 10)
for a in collections.iter():
    print(a.name)
