From 037b9603928cffb4ca983a9eb07ab5748f56f4d0 Mon Sep 17 00:00:00 2001 From: Simon Li Date: Wed, 22 May 2024 15:17:22 +0100 Subject: [PATCH] Ignore tag values in `shared-tag-values` --- aws_project_costs/project-cost-schema.json | 8 ++++++++ aws_project_costs/project_costs.py | 7 ++++++- example/2024-01-01_2024-02-01.csv | 1 + example/output.csv | 5 +++++ example/projects.yaml | 3 +++ 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/aws_project_costs/project-cost-schema.json b/aws_project_costs/project-cost-schema.json index e116b06..90c3e6c 100644 --- a/aws_project_costs/project-cost-schema.json +++ b/aws_project_costs/project-cost-schema.json @@ -12,6 +12,14 @@ "minLength": 1 } }, + "shared-tag-values": { + "description": "Project tag values that should be ignored and considered as shared costs", + "type": "array", + "items": { + "type": "string", + "minLength": 1 + } + }, "project-groups": { "description": "Groups of projects which use common infrastructure, a mapping of group-name to list of projects in the group", "type": "object", diff --git a/aws_project_costs/project_costs.py b/aws_project_costs/project_costs.py index 25d3c79..bd4fd90 100644 --- a/aws_project_costs/project_costs.py +++ b/aws_project_costs/project_costs.py @@ -41,6 +41,7 @@ def _shared_account( start, description, proj_tag_names_map, + shared_tag_values, project_tagname, projects_in_group, costs_dict, @@ -53,7 +54,9 @@ def _shared_account( raise NotImplementedError(f"Project tag {costs_tag} not yet implemented") project_tag = costs_tag[5:] - if project_tagname and project_tag: + if project_tagname and project_tag and (project_tag not in shared_tag_values): + if project_tag not in proj_tag_names_map: + raise ValueError(f"{project_tag} is not in proj-tag-names") project_name = proj_tag_names_map[project_tag] rows.append( ( @@ -65,6 +68,7 @@ def _shared_account( ) ) else: + # Either untagged, or a tag that should be considered shared cost_per_project = item["COST"] / len(projects_in_group) for project_name in projects_in_group: rows.append( @@ -111,6 +115,7 @@ def allocate_costs(*, accountname, config, start, df): start, description, config["proj-tag-names"], + config["shared-tag-values"], project_tagname, projects_in_group, costs_dict, diff --git a/example/2024-01-01_2024-02-01.csv b/example/2024-01-01_2024-02-01.csv index 038e442..3028ebe 100644 --- a/example/2024-01-01_2024-02-01.csv +++ b/example/2024-01-01_2024-02-01.csv @@ -15,6 +15,7 @@ START,END,accountname,Proj$,COST 2024-01-01,2024-02-01,aws-training,Proj$project-004,66.66 2024-01-01,2024-02-01,aws-production-1,Proj$project-001,888.22 2024-01-01,2024-02-01,aws-production-1,Proj$project-002,55.668 +2024-01-01,2024-02-01,aws-production-1,Proj$shared,5 2024-01-01,2024-02-01,aws-production-2,Proj$,25.5 2024-01-01,2024-02-01,aws-production-2,Proj$project-003,0.111 2024-01-01,2024-02-01,aws-production-2,Proj$project-004,545.3009 diff --git a/example/output.csv b/example/output.csv index 7689a0e..0174cbd 100644 --- a/example/output.csv +++ b/example/output.csv @@ -17,8 +17,13 @@ start,projectname,account,tag,cost 2024-01-01,SPECIAL-OPS,aws-special-ops-dev [project-specific],Proj$project-001,0.02 2024-01-01,Internal - training,aws-training [project-specific],Proj$project-001,45.2 2024-01-01,Internal - training,aws-training [project-specific],Proj$project-004,66.66 +2024-01-01,Internal - development,aws-production-1 [shared],Proj$shared,1.0 +2024-01-01,Internal - training,aws-production-1 [shared],Proj$shared,1.0 2024-01-01,Project A,aws-production-1 [shared],Proj$project-001,888.22 +2024-01-01,Project A,aws-production-1 [shared],Proj$shared,1.0 2024-01-01,Project B,aws-production-1 [shared],Proj$project-002,55.668 +2024-01-01,Project B,aws-production-1 [shared],Proj$shared,1.0 +2024-01-01,SPECIAL-OPS,aws-production-1 [shared],Proj$shared,1.0 2024-01-01,Internal - development,aws-production-2 [shared],Proj$,5.1 2024-01-01,Internal - training,aws-production-2 [shared],Proj$,5.1 2024-01-01,Project A,aws-production-2 [shared],Proj$,5.1 diff --git a/example/projects.yaml b/example/projects.yaml index 36ae4ad..afa291c 100644 --- a/example/projects.yaml +++ b/example/projects.yaml @@ -4,6 +4,9 @@ proj-tag-names: "project-003": "Project B" "project-004": "SPECIAL-OPS" +shared-tag-values: + - "shared" + project-groups: tre: - "Project A"