Last active
October 28, 2016 13:36
-
-
Save codyaray/d782c370de3fa2a4c7f0e27f67b3c395 to your computer and use it in GitHub Desktop.
example st2search.py (don't forget to update the default URL from stackstorm.example.com)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Search st2 using a pattern. | |
Usage: | |
st2search.py [-u=<url>] (action|key|rule|trigger) <pattern> | |
Positional Arguments: | |
<pattern> a regex to search against | |
Optional Arguments: | |
-u --url=<endpoint> StackStorm URL [default: stackstorm.example.com] | |
Alternative until st2 natively supports search. | |
See https://github.com/StackStorm/st2/issues/2973. Please upvote! :) | |
""" | |
from docopt import docopt | |
from pymongo import MongoClient | |
# only the following entities have a uid field | |
# for i in action apikey rule sensor trace trigger trigger_instance; do mongo st2 --eval "db.${i}_d_b.find({}, {"uid":1,"_id":0}).limit(1).shellPrint()" | tail -n +3 | grep $i >/dev/null 2>&1 && echo "$i yes" || echo "$i no"; done | |
SUPPORTED_ENTITIES = { | |
'action': 'action_d_b', | |
'rule': 'rule_d_b', | |
'trigger': 'trigger_d_b', | |
'key': 'key_value_pair_d_b' | |
} | |
def main(collection, pattern, url): | |
db = MongoClient("mongodb://%s:27017" % url)['st2'] | |
for obj in db[collection].find({"uid": {"$regex": ".*%s.*" % pattern}}, {"uid": 1, "_id": 0}): | |
print obj['uid'] | |
def parse_collection(options): | |
return [SUPPORTED_ENTITIES[k] for k, v in options.iteritems() | |
if k in SUPPORTED_ENTITIES.keys() and v is True][0] | |
if __name__ == '__main__': | |
args = docopt(__doc__, version='st2search 0.1') | |
main(parse_collection(args), args['<pattern>'], args['--url']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment