Skip to content

Commit

Permalink
feature(schema): add networkv2 schema
Browse files Browse the repository at this point in the history
Adds networkv2 schema definition file, removes skipping of schema
validation for networkv2, and adds unit tests for same.
  • Loading branch information
catmsred committed Feb 20, 2024
1 parent 99394ec commit 31d6182
Show file tree
Hide file tree
Showing 3 changed files with 539 additions and 25 deletions.
35 changes: 18 additions & 17 deletions cloudinit/config/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
# 3. Add the new version definition to versions.schema.cloud-config.json
USERDATA_SCHEMA_FILE = "schema-cloud-config-v1.json"
NETWORK_CONFIG_V1_SCHEMA_FILE = "schema-network-config-v1.json"
NETWORK_CONFIG_V2_SCHEMA_FILE = "schema-network-config-v2.json"

_YAML_MAP = {True: "true", False: "false", None: "null"}
SCHEMA_DOC_TMPL = """
Expand Down Expand Up @@ -169,7 +170,9 @@ class SchemaType(Enum):
"latest": USERDATA_SCHEMA_FILE,
},
SchemaType.NETWORK_CONFIG: {
"latest": NETWORK_CONFIG_V1_SCHEMA_FILE,
"latest": NETWORK_CONFIG_V2_SCHEMA_FILE,
"1": NETWORK_CONFIG_V1_SCHEMA_FILE,
"2": NETWORK_CONFIG_V2_SCHEMA_FILE,
},
}

Expand Down Expand Up @@ -717,15 +720,15 @@ def validate_cloudconfig_schema(
NETWORK_CONFIG
"""
if schema_type == SchemaType.NETWORK_CONFIG:
if network_schema_version(config) == 2:
network_version = network_schema_version(config)
if network_version == 2:
if netplan_validate_network_schema(
network_config=config, strict=strict, log_details=log_details
):
# Schema was validated by netplan
return True
# network-config schema version 2 but no netplan.
# TODO(add JSON schema definition for network version 2)
return False
if network_version is not None:
schema = get_schema(schema_type, str(network_version))

if schema is None:
schema = get_schema(schema_type)
Expand Down Expand Up @@ -1125,18 +1128,13 @@ def validate_cloudconfig_file(
network_config=cloudconfig, strict=True, annotate=annotate
):
return True # schema validation performed by netplan
if network_version != 1:
# Validation requires JSON schema definition in
# cloudinit/config/schemas/schema-network-config-v1.json
print(
"Skipping network-config schema validation."
" No network schema for version:"
f" {network_schema_version(cloudconfig)}"
)
return False
try:
if not validate_cloudconfig_schema(
cloudconfig, schema=schema, strict=True, log_deprecations=False
cloudconfig,
schema=schema,
schema_type=schema_type,
strict=True,
log_deprecations=False,
):
print(
f"Skipping {schema_type.value} schema validation."
Expand Down Expand Up @@ -1557,13 +1555,16 @@ def get_schema_dir() -> str:
return os.path.join(os.path.dirname(os.path.abspath(__file__)), "schemas")


def get_schema(schema_type: SchemaType = SchemaType.CLOUD_CONFIG) -> dict:
def get_schema(
schema_type: SchemaType = SchemaType.CLOUD_CONFIG,
version: str = "latest",
) -> dict:
"""Return jsonschema for a specific type.
Return empty schema when no specific schema file exists.
"""
schema_file = os.path.join(
get_schema_dir(), SCHEMA_FILES_BY_TYPE[schema_type]["latest"]
get_schema_dir(), SCHEMA_FILES_BY_TYPE[schema_type][version]
)
full_schema = None
try:
Expand Down
Loading

0 comments on commit 31d6182

Please sign in to comment.