GitHub PullRequestReviewCommentEvent at 2014-04-11 09:07:54
Repository: aws-analysis-tools
Actor: jib
Payload: {"comment":{"url":"https://api.github.com/repos/jib/aws-analysis-tools/pulls/comments/11524130","id":11524130,"diff_hunk":"@@ -0,0 +1,133 @@\n+### -*- coding: utf-8 -*-\n+###\n+### © 2014 Krux Digital, Inc.\n+### Author: Jeff Pierce <jeff.pierce@krux.com>\n+###\n+\n+\"\"\"\n+Searches EC2 tags and returns a list of servers that match the query terms.\n+Use double colons (::) rather than single colons like the previous version.\n+\"\"\"\n+\n+##########################\n+### Standard Libraries ###\n+##########################\n+\n+from __future__ import absolute_import\n+\n+######################\n+### Krux Libraries ###\n+######################\n+\n+import krux.cli\n+\n+#############################\n+### Third Party Libraries ###\n+#############################\n+\n+import boto.ec2\n+\n+\n+def parse_query(query_to_parse):\n+    \"\"\"\n+    Converts comma-separated string of query terms into a list to use as filters.\n+    \"\"\"\n+    return query_to_parse.split(',')\n+\n+def search_tags(query_terms,passed_regions=False):\n+    \"\"\"\n+    Searches EC2 instances based on search terms.\n+    \"\"\"\n+    regions    = boto.ec2.regions()\n+    filters    = []\n+    query      = {}\n+    inst_names = []\n+\n+    ### Set filters\n+    if passed_regions:\n+        ### lambda:  if we've specified a region, only pick regions that\n+        ### match the provided regions\n+        filters.extend([lambda r: r.name in passed_regions.split(',')])\n+\n+    ### lambdas:  remove govcloud and China to improve search speed\n+    filters.extend([\n+        lambda r: '-gov-' not in r.name,\n+        lambda r: not r.name.startswith('cn-')\n+    ])\n+\n+    ### Filter out unneeded regions from our regions list.\n+    for some_filter in filters:\n+        regions = filter(some_filter, regions)\n+\n+    ### Populate query dictionary to send to AWS\n+    for search_param in query_terms:\n+        ### Split the last term from the query, as the new tagging system\n+        ### will create keys for each step of the way up to last one, which\n+        ### will be a value.\n+        ###\n+        ### Examples:\n+        ###     Name::period* searches for tag Name and value period*\n+        ###     s_periodic::components::s2s::zanox_sync searches for\n+        ###         tag s_periodic::components::s2s and value zanox_sync.\n+        search_term = search_param.rsplit('::',1)\n+\n+        ### If there's nothing to split, like with a query for s_periodic,\n+        ### add it to the value search.\n+        if len(search_term) == 1:\n+            if \"tag-value\" not in query:\n+                query.update({\"tag-value\": ['*' + search_term[0] + '*']})\n+            else:\n+                query[\"tag-value\"] = query.get('tag-value') + ['*' + search_term[0] + '*']\n+        ### But, if there is something that was split (like s_classes::s_periodic),\n+        ### add the first value to tag-key and the second value to tag-value.\n+        else:\n+            tag, val = search_term\n+            if 'tag-key' not in query:\n+                query.update({'tag-key': ['*' + tag + '*']})\n+            else:\n+                query['tag-key'] = query.get('tag-key') + ['*' + tag + '*']\n+\n+            if 'tag-value' not in query:\n+                query.update({\"tag-value\": ['*' + val +'*']})\n+            else:\n+                query['tag-value'] = query.get('tag-value') + ['*' + val + '*']\n+\n+    ### Search each region for matching tags/values and return them as a list.\n+    for region in regions:\n+        ec2 = region.connect()\n+\n+        for res in ec2.get_all_instances(filters=query):\n+            instance = res.instances[0]\n+            inst_names.append(instance.tags.get('Name'))\n+\n+    return inst_names\n+\n+class Application(krux.cli.Application):\n+    def __init__(self):\n+        ### Call superclass to get krux-stdlib\n+        super(Application, self).__init__(name = 'search_ec2_tags')\n+\n+    def add_cli_arguments(self, parser):\n+        group = krux.cli.get_group(parser, self.name)\n+        group.add_argument(","path":"search_ec2_tags.py","position":112,"original_position":112,"commit_id":"c3e7ba80384b44edfc45b4955517aea4230db1df","original_commit_id":"c3e7ba80384b44edfc45b4955517aea4230db1df","user":{"login":"jib","id":90691,"avatar_url":"https://avatars.githubusercontent.com/u/90691?","gravatar_id":"6cd037b11b31e54cba118e977affb65b","url":"https://api.github.com/users/jib","html_url":"https://github.com/jib","followers_url":"https://api.github.com/users/jib/followers","following_url":"https://api.github.com/users/jib/following{/other_user}","gists_url":"https://api.github.com/users/jib/gists{/gist_id}","starred_url":"https://api.github.com/users/jib/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jib/subscriptions","organizations_url":"https://api.github.com/users/jib/orgs","repos_url":"https://api.github.com/users/jib/repos","events_url":"https://api.github.com/users/jib/events{/privacy}","received_events_url":"https://api.github.com/users/jib/received_events","type":"User","site_admin":false},"body":"One of the annoying parts of the old script was that I always had to look at the code to remember how to use it - can we add some examples in the usage please?","created_at":"2014-04-11T09:07:54Z","updated_at":"2014-04-11T09:07:54Z","html_url":"https://github.com/jib/aws-analysis-tools/pull/3#discussion_r11524130","pull_request_url":"https://api.github.com/repos/jib/aws-analysis-tools/pulls/3","_links":{"self":{"href":"https://api.github.com/repos/jib/aws-analysis-tools/pulls/comments/11524130"},"html":{"href":"https://github.com/jib/aws-analysis-tools/pull/3#discussion_r11524130"},"pull_request":{"href":"https://api.github.com/repos/jib/aws-analysis-tools/pulls/3"}}}}