Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource tagging for CSV files with mention of tag value pairs at runtime #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ echo 'i-22222222,us-east-1,Foobar' >> my-resources.csv
aws-tagger --csv my-resources.csv
```

### Tag multiple resources from a CSV file with no need to specify tags in CSV file
AS AWS Tagger can take input from CSV file but for now, one has to spcify the tag values within the csv file. The support is extended for cases where resource ids and regions need to be sourced from CSV file and bulk tagging needs to be done for all resources,e.g. specify a single tag value for all resources present in CSV file.

```
python cli.py --csv my-resources.csv --tag "tag:value"
```

## AWS Resource Support
AWS Tagger supports the following AWS resource types.

Expand Down
5 changes: 3 additions & 2 deletions tagger/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
@click.option('--csv', help='CSV file to read data from.')
def cli(dryrun, verbose, region, role, resource, tag, csv):
if csv and (len(resource) > 0 or len(tag) > 0):
print("Cannot use --resource or --tag with --csv option")
sys.exit(1)
tagger = CSVResourceTaggerWithTags(dryrun, verbose, role, region, tag_volumes=True)
tags = _tag_options_to_dict(tag)
tagger.tag(csv, tags)
if csv:
tagger = CSVResourceTagger(dryrun, verbose, role, region, tag_volumes=True)
tagger.tag(csv)
Expand Down
54 changes: 54 additions & 0 deletions tagger/tagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,60 @@ def _lookup_tagger(self, tag_index, row):
self.regional_tagger[region] = tagger

return tagger


class CSVResourceTaggerWithTags(object):
def __init__(self, dryrun, verbose, role=None, region=None, tag_volumes=False):
self.dryrun = dryrun
self.verbose = verbose
self.tag_volumes = tag_volumes
self.role = role
self.region = region
self.regional_tagger = {}
self.resource_id_column = 'Id'
self.region_column = 'Region'

def tag(self, filename,tags):
with open(filename, 'rU') as csv_file:
reader = csv.reader(csv_file)
header_row = True
tag_index = None

for row in reader:
if header_row:
header_row = False
tag_index = self._parse_header(row)
else:
self._tag_resource(tag_index, row,tags)

def _parse_header(self, header_row):
tag_index = {}
for index, name in enumerate(header_row):
tag_index[name] = index

return tag_index

def _tag_resource(self, tag_index, row, tags):
resource_id = row[tag_index[self.resource_id_column]]
tagger = self._lookup_tagger(tag_index, row)
tagger.tag(resource_id, tags)

def _lookup_tagger(self, tag_index, row):
region = self.region
region_index = tag_index.get(self.region_column)

if region_index is not None:
region = row[region_index]
if region == '':
region = None

tagger = self.regional_tagger.get(region)
if tagger is None:
tagger = SingleResourceTagger(self.dryrun, self.verbose, role=self.role, region=region, tag_volumes=self.tag_volumes)
self.regional_tagger[region] = tagger

return tagger


class EC2Tagger(object):
def __init__(self, dryrun, verbose, role=None, region=None, tag_volumes=False):
Expand Down