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

azure - schema - period modes cron regex #4695

Merged
merged 20 commits into from Aug 30, 2019

Conversation

jomalsan
Copy link
Collaborator

Adds regex expressions to the schemas for azure-periodic and container-periodic that check the validity of the cron expression. Note: The two regexs are different because the interpreter used by the two modes for the cron expressions are different. Included comment linking to the interpreters

Closes #4552

schedule={
'type': 'string',
# Pattern based on apscheduler's CronTrigger https://github.com/agronholm/apscheduler/blob/master/apscheduler/triggers/cron/expressions.py
'pattern': '(\*|[0-9]|\,|\/|\-)+ (\*|[0-9]|\,|\/|\-)+ (\*|[0-9]|[1-2][0-9]|3[0-1]|\,|\*\/|\-)+ (jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|\,|\*\/|[1-9]|1[0-2]|\*)+ (mon|tue|wed|thu|fri|sat|sun|[0-6]|\,|\*|\-)+'
Copy link
Collaborator

Choose a reason for hiding this comment

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

100 char line max, will fail lint.

Copy link
Collaborator Author

@jomalsan jomalsan Aug 27, 2019

Choose a reason for hiding this comment

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

Yes that is what I am working on addressing now. One of the unit tests for function periodic was misformatted and was failing as well

Changes are coming

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@stefangordon This should be fixed now

@stefangordon
Copy link
Collaborator

You might look at some of the validate tests we have for most resources, they are very simple and just verify schema.

e.g.


    def test_aks_schema_validate(self):
        with self.sign_out_patch():
            p = self.load_policy({
                'name': 'test-azure-aks',
                'resource': 'azure.aks'
            }, validate=True)
            self.assertTrue(p)

Not a bad idea to toss some of those in to make sure the regex seems to be working right.

You can also test the negative by loading the policy inside a with like this:

        with self.assertRaises(ValidationError):
            self.load_policy(data=p, validate=True)

schedule_regex += r'(\*|[1-9]|[1-2][0-9]|3[0-1]|\,|\*\/|\-)+ '
schedule_regex += r'([Jj](an|anuary)|[Ff](eb|ebruary)|[Mm](ar|arch)|[Aa](pr|pril)|[Mm]ay|'
schedule_regex += r'[Jj](un|une)|[Jj](ul|uly)|[Aa](ug|ugust)|[Ss](ep|ept|eptember)|[Oo]'
schedule_regex += r'(ct|ctober)|[Nn](ov|ovember)|[Dd](ec|ecember)|\,|\*\/|[1-9]|1[0-2]|\*)+ '
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can simplify it by using regex for months, days of the week from container.
Yes, functions allow you to use whatever, but we can keep some consistency here.

Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: You don't have to do the +=, they will be auto-concatenated.

https://docs.python.org/3/reference/lexical_analysis.html#string-literal-concatenation

regex = 'line1'
        'line2'
        'line3' 

is valid

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@stefangordon that method of string concatenation was not working. It does not throw a compile error, but the resulting regex matched with all of the invalid cases. To replicate the style I put everything in a list and then join the list when I need the regex. This passes all of the unit tests for it

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we can simplify it by using regex for months, days of the week from container.
Yes, functions allow you to use whatever, but we can keep some consistency here.

@logachev The current version is working for the functionality of the function timer cron expression. Are you thinking that we should simplify it for maintainability?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes.. c7n is not primary functions provisioning tool, so there is no need to support everything

Copy link
Collaborator

Choose a reason for hiding this comment

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

While I agree with the sentiment, I think in this case we are already committed to exposing the azure trigger functionality here, I think we even link to it in the docs somewhere. It would be an unnecessary breaking change to block certain syntax.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We do link to the Azure Functions cron expression documentation in the Function Host Options doc. If we change the implementation here, then we need to change the documentation as well

Copy link
Collaborator

Choose a reason for hiding this comment

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

good point, lets keep it as-is in that case

@@ -49,8 +49,15 @@ def provision(self):
@execution.register(CONTAINER_TIME_TRIGGER_MODE)
class AzureContainerPeriodicMode(AzureContainerHostMode, PullMode):
"""A policy that runs at specified time intervals."""
# Pattern based on apscheduler's CronTrigger: https://github.com/agronholm/apscheduler
Copy link
Collaborator

Choose a reason for hiding this comment

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

a better link might be to apscheduler trigger parser code

r'[Jj](un|une)|[Jj](ul|uly)|[Aa](ug|ugust)|[Ss](ep|eptember)|[Oo](ct',
r'|ctober)|[Nn](ov|ovember)|[Dd](ec|ecember)|\,|\*\/|[1-9]|(1[0-2])|\*)+ ',
r'([Mm](on|onday)|[Tt](u|ue|ues|uesday)|[Ww](ed|ednesday)|[Tt](hu|hursday)|',
r'[Ff](ri|riday)|[Ss](at|aturday)|[Ss](un|unday)|[0-6]|\,|\*|\-)+\s?$']
Copy link
Collaborator

Choose a reason for hiding this comment

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

@stefangordon
Copy link
Collaborator

Azure triggers don't allow * * * * * * ?

@jomalsan
Copy link
Collaborator Author

Azure triggers don't allow * * * * * * ?

I don't see anywhere where they specifically say it is not allowed, but none of their examples have * in the first slot (seconds). I figured blocking a function (that takes 2-3 seconds to run) from running every second seemed ok. Also if you want the every second functionality "0-59 * * * * *" does the same thing

@stefangordon
Copy link
Collaborator

Azure triggers don't allow * * * * * * ?

I don't see anywhere where they specifically say it is not allowed, but none of their examples have * in the first slot (seconds). I figured blocking a function (that takes 2-3 seconds to run) from running every second seemed ok. Also if you want the every second functionality "0-59 * * * * *" does the same thing

Agree it would almost certainly crash Functions so fine with blocking it.

@erwelch
Copy link
Collaborator

erwelch commented Aug 29, 2019

I think this is great! My only thought is that perhaps if we actually do the check in the validator method instead of the schema we might be able to give the user a better error message. If it didn't fit in the schema I can say personally with 100% confidence I would not be able to tell from that regex that I had an extra * or something haha.

@stefangordon
Copy link
Collaborator

I think this is great! My only thought is that perhaps if we actually do the check in the validator method instead of the schema we might be able to give the user a better error message. If it didn't fit in the schema I can say personally with 100% confidence I would not be able to tell from that regex that I had an extra * or something haha.

Yeah I had the same thought this error is going to be nasty, but I guess its 10000x better than getting no validation error and having functions just broken. I was actually seeing there might be mechanisms in jsonschema to customize the validation error now, so this might be the best of both worlds. (might require adding code to the validation error handler to print out the custom message)

Lets not block the PR on it as its a net-improvement, but we can research that.

@stefangordon stefangordon merged commit 536c55a into cloud-custodian:master Aug 30, 2019
@jomalsan jomalsan deleted the feat/4552-periodic-regex branch September 10, 2019 16:23
fidelito pushed a commit to fidelito/cloud-custodian that referenced this pull request May 29, 2020
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.

azure - add a check for Function periodic syntax
5 participants