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

core - add ability to add custom functions to jmespath #8533

Merged
merged 9 commits into from May 11, 2023

Conversation

thisisshi
Copy link
Member

@thisisshi thisisshi commented May 3, 2023

Adds the ability to include custom jmespath functions for use anywhere we express jmespath queries

This came up in the finops slack where there was a question around copy-related-tag(s) from a lambda function to its corresponding cloudwatch log group. The Cloudwatch log group follows the convention of /aws/lambda/$function_name but there's no way to do that in jmespath currently. There are enhancement proposals/issues here:

jmespath's python library includes a way to include custom functions via its jmespath.functions.Functions class, which is utilized in this PR.

extant usage of jmespath should be preserved, as we're utilizing functools.partial to inject the additional options into the call, so extant call sites should continue to behave as normal and new jmespath.search and jmespath.compile.search calls should also be fine.

policies:
  - name: copy-related-tag-with-split
    resource: aws.log-group
    filters:
      - type: value
        key: logGroupName
        value: "/aws/lambda/"
        op: in
        value_type: swap
    actions:
      - type: copy-related-tag
        resource: aws.lambda
        key: "split(`/`, logGroupName)[-1]"
        tags: "*"

Screen Shot 2023-05-05 at 11 03 06 AM

Copy link
Member

@ajkerrigan ajkerrigan left a comment

Choose a reason for hiding this comment

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

Love it. I'm all for giving jmespath some more smarts and this seems like a reasonable way to handle it 👍 . I know the idea of extending jmespath from the c7n side has come up before, with split() and slicing being recurring patterns. Thanks for jumping on this.

@thisisshi thisisshi marked this pull request as ready for review May 4, 2023 01:17
@thisisshi thisisshi requested a review from kapilt as a code owner May 4, 2023 01:17
@kapilt
Copy link
Collaborator

kapilt commented May 4, 2023

seems cool, but needs docs, else its not discoverable.

@thisisshi
Copy link
Member Author

@kapilt updated with docs and test

c7n/__init__.py Outdated Show resolved Hide resolved
from dateutil import parser
from dateutil.tz import gettz, tzutc

from c7n.utils import jmespath_search
Copy link
Collaborator

Choose a reason for hiding this comment

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

technically mailer doesn't have a runtime dependency on c7n, so we should just leave the default here afaics.

Copy link
Collaborator

@kapilt kapilt left a comment

Choose a reason for hiding this comment

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

lgtm, one minor

@thisisshi thisisshi merged commit 3fdc7c3 into cloud-custodian:main May 11, 2023
21 checks passed
@thisisshi thisisshi deleted the jmespath-custom-functions branch May 11, 2023 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants