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

added primary id validation #482

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions src/rpdk/core/data_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ def get_file_base_uri(file):
return path.resolve().as_uri()


def _is_in(schema, key):
def contains(values):
return set(values).issubset(set(schema.get(key, [])))

return contains


def load_resource_spec(resource_spec_file): # noqa: C901
"""Load a resource provider definition from a file, and validate it."""
try:
Expand All @@ -106,6 +113,16 @@ def load_resource_spec(resource_spec_file): # noqa: C901
LOG.debug("Resource spec validation failed", exc_info=True)
raise SpecValidationError(str(e)) from e

in_readonly = _is_in(resource_spec, "readOnlyProperties")
in_createonly = _is_in(resource_spec, "createOnlyProperties")

primary_id = resource_spec["primaryIdentifier"]
if not in_readonly(primary_id) and not in_createonly(primary_id):
Copy link
Contributor

Choose a reason for hiding this comment

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

can simplify a bit to make condition primaryIdentifier - (createOnlyProperties | readOnlyProperties) where they are all sets

Copy link
Contributor Author

Choose a reason for hiding this comment

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

used closure to reuse the subset operation but we might use a disjoint of createOnly | readOnly

Copy link
Contributor

Choose a reason for hiding this comment

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

raise SpecValidationError(
"Property 'primaryIdentifier' must be specified \
as either readOnly or createOnly"
)

# TODO: more general validation framework
if "remote" in resource_spec:
raise SpecValidationError(
Expand Down
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_additional_items.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
6 changes: 6 additions & 0 deletions tests/data/schema/invalid/invalid_additional_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@
}
}
},
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
"additionalProperties": true
}
},
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
6 changes: 6 additions & 0 deletions tests/data/schema/invalid/invalid_boolean_property.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
"properties": {
"property1": true
},
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@
"type": "integer"
}
},
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false,
"unsupportedKey": 1
}
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_handler_definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_items_list.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_no_description.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 0 additions & 3 deletions tests/data/schema/invalid/invalid_no_primary_identifier.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,5 @@
"additionalItems": true
}
},
"primaryIdentifier": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_no_properties_defined.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_no_type_const.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_no_type_enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
13 changes: 13 additions & 0 deletions tests/data/schema/invalid/invalid_primary_id.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"typeName": "AWS::Valid::TypeName",
"description": "a test schema",
"properties": {
"property1": {
"type": "array"
}
},
"primaryIdentifier": [
"/properties/property1"
],
"additionalProperties": false
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_property_name_invalid.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_property_not_an_object.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_property_type_invalid.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_remote_reserved_word.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"remote": {
"schema01": {}
},
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/invalid/invalid_typename.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/valid/valid_array_no_items.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/valid/valid_nested_property_object.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 2 additions & 1 deletion tests/data/schema/valid/valid_no_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"/properties/enum1"
],
"readOnlyProperties": [
"/properties/str3"
"/properties/str3",
"/properties/enum1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/valid/valid_no_type.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 2 additions & 1 deletion tests/data/schema/valid/valid_pattern_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@
"/properties/enum1"
],
"readOnlyProperties": [
"/properties/str3"
"/properties/str3",
"/properties/enum1"
],
"additionalIdentifiers": [
[
Expand Down
3 changes: 2 additions & 1 deletion tests/data/schema/valid/valid_type_complex.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
"/properties/enum1"
],
"readOnlyProperties": [
"/properties/str3"
"/properties/str3",
"/properties/enum1"
],
"additionalIdentifiers": [
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
"/properties/property1",
"/properties/property2"
],
"readOnlyProperties": [
"/properties/property1",
"/properties/property2"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/valid/valid_type_const.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/valid/valid_type_enum.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false
}
3 changes: 3 additions & 0 deletions tests/data/schema/valid/valid_with_handlers.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"primaryIdentifier": [
"/properties/property1"
],
"readOnlyProperties": [
"/properties/property1"
],
"additionalProperties": false,
"handlers": {
"create": {
Expand Down
2 changes: 2 additions & 0 deletions tests/test_data_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"description": "test schema",
"properties": {"foo": {"type": "string"}},
"primaryIdentifier": ["/properties/foo"],
"readOnlyProperties": ["/properties/foo"],
"additionalProperties": False,
}

Expand Down Expand Up @@ -112,6 +113,7 @@ def test_load_resource_spec_remote_key_is_invalid():
"description": "test schema",
"properties": {"foo": {"type": "string"}},
"primaryIdentifier": ["/properties/foo"],
"readOnlyProperties": ["/properties/foo"],
"remote": {},
}
with pytest.raises(SpecValidationError) as excinfo:
Expand Down