diff --git a/aws_project_costs/main.py b/aws_project_costs/main.py index 9688fa3..7880178 100644 --- a/aws_project_costs/main.py +++ b/aws_project_costs/main.py @@ -1,3 +1,4 @@ +import logging from argparse import ArgumentParser import yaml @@ -13,9 +14,12 @@ def main() -> None: ) parser.add_argument("--costs", required=True, help="Project costs CSV") parser.add_argument("--output", help="Output file") + parser.add_argument("--verbose", "-v", action="store_true", help="Verbose output") args = parser.parse_args() + logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO) + with open(args.config) as f: config = yaml.safe_load(f) diff --git a/aws_project_costs/project-cost-schema.json b/aws_project_costs/project-cost-schema.json index bfb466f..ed0bfba 100644 --- a/aws_project_costs/project-cost-schema.json +++ b/aws_project_costs/project-cost-schema.json @@ -5,7 +5,7 @@ "type": "object", "properties": { "proj-tag-names": { - "description": "A mapping of AWS project tags to the canonical project name", + "description": "A mapping of AWS project tags to the canonical project name. Optionally use '*' to map unknown tags to a defined project name", "type": "object", "additionalProperties": { "type": "string", diff --git a/aws_project_costs/project_costs.py b/aws_project_costs/project_costs.py index 0f255a4..56e6d4b 100644 --- a/aws_project_costs/project_costs.py +++ b/aws_project_costs/project_costs.py @@ -1,10 +1,13 @@ import json +import logging from enum import Enum from operator import itemgetter from typing import Any, Hashable, Optional import pandas as pd +logger = logging.getLogger(__name__) + PROJECT_TAG = "Proj" @@ -82,9 +85,15 @@ def _shared_account( project_tag = costs_tag[5:] if project_tagname and project_tag and (project_tag not in shared_tag_values): - if project_tag not in proj_tag_names_map: + if project_tag in proj_tag_names_map: + project_name = proj_tag_names_map[project_tag] + elif "*" in proj_tag_names_map: + project_name = proj_tag_names_map["*"] + logger.warn( + "Project tag %s not found, using %s", project_tag, project_name + ) + else: raise ValueError(f"{project_tag} is not in proj-tag-names") - project_name = proj_tag_names_map[project_tag] rows.append( ( start.date().isoformat(), @@ -177,7 +186,7 @@ def analyse_costs_csv( for start in sorted(df["START"].unique()): month_costs = df[df["START"] == start] for accountname in month_costs["ACCOUNTNAME"].unique(): - # print(f"Processing {accountname} {start}") + logger.debug(f"Processing {accountname} {start}") try: acc_itemised_rows = allocate_costs( accountname=accountname, @@ -187,7 +196,9 @@ def analyse_costs_csv( ) itemised_rows.extend(sorted(acc_itemised_rows, key=itemgetter(1, 2, 0))) except Exception as e: - print(f"ERROR: Failed to analyse account {accountname} {start} {e}") + logger.error( + f"ERROR: Failed to analyse account {accountname} {start} {e}" + ) raise if output_csv_filename: