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 15, 2024
1 parent 99394ec commit 66f8234
Show file tree
Hide file tree
Showing 3 changed files with 523 additions and 18 deletions.
26 changes: 10 additions & 16 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,14 @@ 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
schema = get_schema(schema_type, network_version)

if schema is None:
schema = get_schema(schema_type)
Expand Down Expand Up @@ -1125,15 +1127,6 @@ 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
Expand Down Expand Up @@ -1557,13 +1550,14 @@ 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: int = None) -> dict:
"""Return jsonschema for a specific type.
Return empty schema when no specific schema file exists.
"""
version = version or "latest"
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 66f8234

Please sign in to comment.