From e267c5ae3ae9a4dc7e63c1806e3f7f7c8df13da7 Mon Sep 17 00:00:00 2001 From: Patrick Cullen Date: Tue, 6 Jun 2017 17:19:14 -0400 Subject: [PATCH] added support for EBS snapshots --- setup.py | 4 ++-- tagger/tagger.py | 32 ++++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index 82afb30..6ff6410 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name='aws-tagger', - version='0.4.0', + version='0.5.0', packages=find_packages(), include_package_data=True, install_requires=[ @@ -28,7 +28,7 @@ author="Patrick Cullen and the WaPo platform tools team", author_email="opensource@washingtonpost.com", url="https://github.com/washingtonpost/aws-tagger", - download_url = "https://github.com/washingtonpost/aws-tagger/tarball/v0.4.0", + download_url = "https://github.com/washingtonpost/aws-tagger/tarball/v0.5.0", keywords = ['tag', 'tagger', 'tagging', 'aws'], classifiers = [] ) diff --git a/tagger/tagger.py b/tagger/tagger.py index 09aab3d..8d09c53 100644 --- a/tagger/tagger.py +++ b/tagger/tagger.py @@ -80,23 +80,39 @@ def tag(self, resource_id, tags): return tagger = None + if resource_id.startswith('arn:'): + product, resource_id = self._parse_arn(resource_id) + if product: + tagger = self.taggers.get(product) + else: + tagger = self.taggers['s3'] + + if resource_id.startswith('i-'): tagger = self.taggers['ec2'] elif resource_id.startswith('vol-'): tagger = self.taggers['ec2'] - elif resource_id.startswith('arn:'): - parts = resource_id.split(':') - if len(parts) > 4: - product = parts[2] - tagger = self.taggers.get(product) - else: - tagger = self.taggers['s3'] + elif resource_id.startswith('snap-'): + tagger = self.taggers['ec2'] if tagger: tagger.tag(resource_id, tags) else: print "Tagging is not support for this resource %s" % resource_id + def _parse_arn(self, resource_arn): + product = None + resource_id = None + parts = resource_arn.split(':') + if len(parts) > 5: + product = parts[2] + resource_id = parts[5] + resource_parts = resource_id.split('/') + if len(resource_parts) > 1: + resource_id = resource_parts[-1] + + return product, resource_id + class MultipleResourceTagger(object): def __init__(self, dryrun, verbose, role=None, region=None): self.tagger = SingleResourceTagger(dryrun, verbose, role=role, region=region) @@ -192,7 +208,7 @@ def tag(self, instance_id, tags): try: self._ec2_create_tags(Resources=resource_ids, Tags=aws_tags) except botocore.exceptions.ClientError as exception: - if exception.response["Error"]["Code"] in ['InvalidVolume.NotFound', 'InvalidInstanceID.NotFound']: + if exception.response["Error"]["Code"] in ['InvalidSnapshot.NotFound', 'InvalidVolume.NotFound', 'InvalidInstanceID.NotFound']: print "Resource not found: %s" % instance_id else: raise exception