Skip to content

Commit

Permalink
Update rule processor API (#105)
Browse files Browse the repository at this point in the history
* Update rule processor API

* Move into a constant
  • Loading branch information
jsoucheiron committed Feb 25, 2020
1 parent e68452d commit 8528077
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

## [0.14.1] - 2020-02-24
### Improvements
- Rule processor now accepts an extras parameter that will be forwarded to the rules
- Main gets extra information from the event and forwards it to the rule formatter

## [0.14.0] - 2020-02-07
### Breaking changes
- Completely changed base `Rule` abstract class signature and adapted rule classes to match it:
Expand Down
2 changes: 1 addition & 1 deletion cfripper/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (0, 14, 0)
VERSION = (0, 14, 1)

__version__ = ".".join(map(str, VERSION))
9 changes: 8 additions & 1 deletion cfripper/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"""
import json
import logging
from typing import Any, Dict

import pycfmodel

Expand All @@ -26,6 +27,7 @@
from .rules import DEFAULT_RULES

logger = logging.getLogger(__file__)
DEFAULT_EXTRA_KEYS_FROM_EVENT = ("account", "event", "project", "region", "stack", "tags")


def log_results(project_name, service_name, stack_name, rules, _type, warnings, template_url):
Expand Down Expand Up @@ -89,6 +91,7 @@ def handler(event, context):
raise ValueError("Invalid event type: no parameter 'stack_template_url' or 'stack::name' in request.")

template = get_template(event)
extras = get_extras(event)

if not template:
# In case of an invalid script log a warning and return early
Expand Down Expand Up @@ -119,7 +122,7 @@ def handler(event, context):
# TODO get AWS variables/parameters and pass them to resolve
cfmodel = pycfmodel.parse(template).resolve()

result = processor.process_cf_template(cfmodel, config)
result = processor.process_cf_template(cfmodel=cfmodel, config=config, extras=extras)

perform_logging(result, config, event)

Expand All @@ -136,6 +139,10 @@ def handler(event, context):
}


def get_extras(event) -> Dict[str, Any]:
return {k: v for k, v in event.items() if k in DEFAULT_EXTRA_KEYS_FROM_EVENT}


def get_template(event):
try:
account_id = event.get("account", {}).get("id")
Expand Down
6 changes: 3 additions & 3 deletions cfripper/rule_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"""
import logging
import re
from typing import List
from typing import Dict, List, Optional

from pycfmodel.model.cf_model import CFModel

Expand All @@ -29,11 +29,11 @@ class RuleProcessor:
def __init__(self, *args):
self.rules = args

def process_cf_template(self, cfmodel: CFModel, config: Config) -> Result:
def process_cf_template(self, cfmodel: CFModel, config: Config, extras: Optional[Dict] = None) -> Result:
result = Result()
for rule in self.rules:
try:
result += rule.invoke(cfmodel)
result += rule.invoke(cfmodel, extras)
except Exception as other_exception:
result.add_exception(other_exception)
logger.exception(
Expand Down
4 changes: 3 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ def test_correct_event():
mock_created_s3_adapter_object.download_template_to_dictionary.assert_called_once_with(
"https://asdfasdfasdf/bucket/key"
)
mock_created_rule_processor_object.process_cf_template.assert_called_once_with(cfmodel, ANY)
mock_created_rule_processor_object.process_cf_template.assert_called_once_with(
cfmodel=cfmodel, config=ANY, extras={"stack": {"name": "blooblah"}}
)


@mock_s3
Expand Down

0 comments on commit 8528077

Please sign in to comment.