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

Optionally specify * to handle unknown proj-tag-names #3

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions aws_project_costs/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from argparse import ArgumentParser

import yaml
Expand All @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion aws_project_costs/project-cost-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 15 additions & 4 deletions aws_project_costs/project_costs.py
Original file line number Diff line number Diff line change
@@ -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"


Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand All @@ -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:
Expand Down