Skip to content

Schema processing changes (SC-676)#1144

Merged
blackboxsw merged 4 commits into
canonical:mainfrom
TheRealFalcon:schema-updates
Dec 14, 2021
Merged

Schema processing changes (SC-676)#1144
blackboxsw merged 4 commits into
canonical:mainfrom
TheRealFalcon:schema-updates

Conversation

@TheRealFalcon
Copy link
Copy Markdown
Contributor

Proposed Commit Message

Schema processing changes

* Use proper logging
* Add parsing for patternProperties
* Add label to annotate patternProperties
* Log warning if schema parsing fails during metaschema processing
* Some schema test fixes

Additional Context

Test Steps

Checklist:

  • My code follows the process laid out in the documentation
  • I have updated or added any unit tests accordingly
  • I have updated or added any documentation accordingly

* Use proper logging
* Add parsing for patternProperties
* Add label to annotate patternProperties
* Log warning if schema parsing fails during metaschema processing
* Some schema test fixes
@TheRealFalcon
Copy link
Copy Markdown
Contributor Author

My use of "darker" has resulted in a few formatting-only changes. If it's annoying enough I can change them back.

Comment thread cloudinit/config/schema.py
all_text_files = (f for f in examples_dir.glob('cloud-config*.txt')
if not f.name.startswith('cloud-config-archive'))
return all_text_files
return (
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Without this, pytest will name the parametrized tests "example_path1", "example_path2", etc. With this, we see the actual path of the schema being tested.

@TheRealFalcon TheRealFalcon requested a review from holmanb December 9, 2021 17:52
@TheRealFalcon TheRealFalcon changed the title Schema processing changes Schema processing changes (SC-676) Dec 9, 2021
Copy link
Copy Markdown
Member

@holmanb holmanb left a comment

Choose a reason for hiding this comment

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

This looks good to me.

I have a couple of minor nits and comments, but nothing worth holding up this PR over. It might be worthwhile to add a test to test_schema.py demonstrating how a a label in the schema overrides the property key. I would be fine with it getting merged as-is, however.

description = prop_config.get("description", "")

# Define prop_name and description for SCHEMA_PROPERTY_TMPL
label = prop_config.get("label", prop_key)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Glad to see this change is non-breaking (falls back to prop_key when label isn't defined).

Comment thread cloudinit/config/schema.py Outdated

# This additional label allows us to specify a different name
# than the property key. This is especially useful when using a
# "patternProperties" regex.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: This change happens in the get_jsonschema_validator(), but the purpose for this change is for generating meaningful doc labels. It might be helpful to explain "why it's helpful when using a patternProperties regex"

perhaps:

    # "patternProperties" regex, otherwise the property label in the generated docs will be a regular expression.

Comment thread cloudinit/config/schema.py
Also fix bad rebase causing some tests to get skipped
Copy link
Copy Markdown
Collaborator

@blackboxsw blackboxsw left a comment

Choose a reason for hiding this comment

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

Good idea with the label.
Only thing I think is really worth fixing

non-blocking comments:

  1. early skip from for loop if properties or patternProperties are empty
  2. If you can find someway in metaschema to define that our objects need either "properties" or "patternProperties" go4it. Otherwise no concerns there as we do raise errors during processing of docs

# otherwise the property label in the generated docs will be a
# regular expression.
# http://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties
strict_metaschema["properties"]["label"] = {"type": "string"}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Initially, I was wondering if we could also prescribe that either "properties" or "patternProperties" would satisfy our meta schema definition, but there is an interaction which breaks any config modules (cc_bootcmd) which defines a "oneOf" composition.

diff --git a/cloudinit/config/schema.py b/cloudinit/config/schema.py
index 597107497..bfdc9c9f0 100644
--- a/cloudinit/config/schema.py
+++ b/cloudinit/config/schema.py
@@ -101,6 +101,9 @@ def get_jsonschema_validator():
     # regular expression.
     # http://json-schema.org/understanding-json-schema/reference/object.html#pattern-properties
     strict_metaschema["properties"]["label"] = {"type": "string"}
+    strict_metaschema["anyOf"] = [
+        {"required": ["properties"]}, {"required": ["patternProperties"]}
+    ]
 
     if hasattr(Draft4Validator, 'TYPE_CHECKER'):  # jsonschema 3.0+
         type_checker = Draft4Validator.TYPE_CHECKER.redefine(

Copy link
Copy Markdown
Contributor Author

@TheRealFalcon TheRealFalcon Dec 13, 2021

Choose a reason for hiding this comment

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

Nothing trivial comes to mind. I'm ok skipping it.

Comment thread cloudinit/config/schema.py
Comment thread cloudinit/config/schema.py Outdated
_get_property_doc(prop_config, prefix=new_prefix))
return '\n\n'.join(properties)
SCHEMA_LIST_ITEM_TMPL.format(
prefix=new_prefix, prop_name=prop_key
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This needs label I think right?

Suggested change
prefix=new_prefix, prop_name=prop_key
prefix=new_prefix, prop_name=label

@blackboxsw
Copy link
Copy Markdown
Collaborator

My use of "darker" has resulted in a few formatting-only changes. If it's annoying enough I can change them back.

I think this is fine here as I don't expect any downstreams to have patched the schema tool. It's worth us announcing the use of darker on the mailing list to align future development and make coding style standard. I think it is totally worth us doing this work to darken cloud-init code, but I want to generally do that in a separate non-functional set of PRs. This way downstreams won't have to sift through white-space related content versus functional changes in multiple PRs.

Copy link
Copy Markdown
Collaborator

@blackboxsw blackboxsw left a comment

Choose a reason for hiding this comment

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

Thanks for the unit test on the last commit. LGTM.

@blackboxsw blackboxsw merged commit 9a6e65a into canonical:main Dec 14, 2021
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.

3 participants