-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from hawry/add-ignore-and-cleanup
add ignore feature and refactor code
- Loading branch information
Showing
12 changed files
with
383 additions
and
238 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
VERSION = "0.1.1" | ||
LOGGER = "deforest" |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import logging | ||
|
||
|
||
class CloudFormationCleaner: | ||
KEY_CF_FIELD = "AWSTemplateFormatVersion" | ||
KEY_CF_RESOURCES = "Resources" | ||
KEY_CF_BODY = "Body" | ||
KEY_CF_TYPE = "Type" | ||
KEY_CF_PROPERTIES = "Properties" | ||
CF_APIGW_TYPE = "AWS::ApiGateway::RestApi" | ||
|
||
def __init__(self, caller): | ||
logging.debug("created {}".format(self)) | ||
self.caller = caller | ||
|
||
def clean(self): | ||
logging.debug("{} clean".format(self)) | ||
logging.debug("checking {}".format(self.caller.result)) | ||
if not CloudFormationCleaner.KEY_CF_FIELD in self.caller.result: | ||
self.caller.result = [self.caller.result] | ||
return | ||
rval = [] | ||
copy = self.caller.result | ||
res = copy[CloudFormationCleaner.KEY_CF_RESOURCES] | ||
for k in res: | ||
if isinstance(res[k], dict): | ||
if self._is_apigw(res[k]) and self._has_body(res[k]): | ||
logging.info( | ||
"identified resource '{}' as an API Gateway".format(k)) | ||
rval.append( | ||
res[k][CloudFormationCleaner.KEY_CF_PROPERTIES][CloudFormationCleaner.KEY_CF_BODY]) | ||
self.caller.result = rval | ||
|
||
def _is_apigw(self, node): | ||
if CloudFormationCleaner.KEY_CF_TYPE in node: | ||
return node[CloudFormationCleaner.KEY_CF_TYPE] == CloudFormationCleaner.CF_APIGW_TYPE | ||
return False | ||
|
||
def _has_body(self, node): | ||
return CloudFormationCleaner.KEY_CF_PROPERTIES in node and CloudFormationCleaner.KEY_CF_BODY in node[CloudFormationCleaner.KEY_CF_PROPERTIES] | ||
|
||
|
||
class DefaultCleaner: | ||
keys = ["x-amazon"] | ||
copy = None | ||
|
||
def __init__(self, caller): | ||
logging.debug("created {}".format(self)) | ||
self.caller = caller | ||
|
||
def clean(self): | ||
logging.debug("{} clean".format(self)) | ||
logging.debug("checking {}".format(self.caller.result)) | ||
self.copy = self.caller.result | ||
for i in self.copy: | ||
self._clean(i) | ||
self.caller.result = self.copy | ||
|
||
def _clean(self, v): | ||
for k in v.keys(): | ||
if any(m in k for m in self.keys): | ||
del v[k] | ||
else: | ||
if isinstance(v[k], dict): | ||
self._clean(v[k]) | ||
|
||
|
||
class IgnoreCleaner: | ||
DEFOREST_IGNORE_KEY = "x-deforest-ignore" | ||
copy = None | ||
|
||
def __init__(self, caller): | ||
logging.debug("created {}".format(self)) | ||
self.caller = caller | ||
|
||
def clean(self): | ||
self.copy = self.caller.result | ||
for i in self.copy: | ||
self._clean(i) | ||
self.caller.result = self.copy | ||
|
||
def _clean(self, v): | ||
for k in v.keys(): | ||
if isinstance(v[k], dict): | ||
if IgnoreCleaner.DEFOREST_IGNORE_KEY in v[k]: | ||
if v[k][IgnoreCleaner.DEFOREST_IGNORE_KEY] is True: | ||
logging.debug("removing {}".format(v[k])) | ||
del v[k] | ||
else: | ||
self._clean(v[k]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
VERSION = "0.1.1" | ||
VERSION = "0.2.0" | ||
LOGGER = "deforest" | ||
EXIT_NOTFOUND = 1 | ||
EXIT_PARSEERR = 2 |
Oops, something went wrong.