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

Modified action.py in order to have filters in a more conveinent way #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions actions/lib/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,19 @@ def do_method(self, module_path, cls, action, **kwargs):
method_fqdn = '%s.%s.%s' % (module_path, cls, action)
self.logger.debug('Calling method "%s" with kwargs: %s' % (method_fqdn, str(kwargs)))

if 'Filters' in kwargs.keys():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is the same copy/paste code below. Please make this a function.

boto3_compliant_filters = []
for specified_filter in kwargs['Filters']:
keyval = specified_filter.split('=')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean its not possible to filter on anything with a = in the value now, as it will get split? For example what happens if I can give an AWS resource a tag or value with an = in?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah you're right, I haven't thought of that. It seems that the equal sign is an allowed character in both tag names and values...
Will try to find a solution to that...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion, make filters an object, then you can just use the key/value pairs in that dict/object

vals = []
for entry in boto3_compliant_filters:
if entry['Name'] == keyval[0]:
vals = entry['Values']
boto3_compliant_filters.remove(entry)
vals.append(keyval[1])
boto3_compliant_filters.append({'Name': keyval[0], 'Values': vals})
kwargs['Filters'] = boto3_compliant_filters

resultset = getattr(obj, action)(**kwargs)
formatted = self.resultsets.formatter(resultset)
return formatted if isinstance(formatted, list) else [formatted]
Expand All @@ -232,5 +245,17 @@ def do_function(self, module_path, action, **kwargs):
function_fqdn = '%s.%s' % (module_path, action)
self.logger.debug('Calling function "%s" with kwargs: %s' % (function_fqdn,
str(kwargs)))
if 'Filters' in kwargs.keys():
boto3_compliant_filters = []
for specified_filter in kwargs['Filters']:
keyval = specified_filter.split('=')
vals = []
for entry in boto3_compliant_filters:
if entry['Name'] == keyval[0]:
vals = entry['Values']
boto3_compliant_filters.remove(entry)
vals.append(keyval[1])
boto3_compliant_filters.append({'Name': keyval[0], 'Values': vals})
kwargs['Filters'] = boto3_compliant_filters

return getattr(module, action)(**kwargs)