templates: persist appliance metadata on install

Appliance fields that describe the appliance (vendor information, default
credentials, installation instructions...) were dropped when installing a
template. Keep them in a new appliance_metadata JSON column on the
templates table, filled by the appliance-to-template conversion for both
registry v1-6 and v8 (version level values override the appliance level
ones). The nested schema allows extra fields so future registry fields
persist without a migration.
This commit is contained in:
YueGuobin committed 2026-08-21 21:41:42 +08:00
1 parent 1c68a52856
commit 300c53e6fb
7 files changed
+216

No files matched your search

@@ -173,6 +173,44 @@ class TestTemplateRoutes:
assert response.status_code == status.HTTP_200_OK
assert response.json()["netmiko_device_type"] == ""
async def test_template_appliance_metadata_roundtrip(self, app: FastAPI, client: AsyncClient) -> None:
"""
Appliance metadata persists with the template: create, read back,
and replace on update. Unknown fields are kept (extra=allow) so that
future appliance registry fields do not vanish.
"""
template_id = str(uuid.uuid4())
params = {
"template_id": template_id,
"name": "VPCS_METADATA",
"compute_id": "local",
"template_type": "vpcs",
"appliance_metadata": {
"vendor_name": "Test vendor",
"default_username": "admin",
"future_field": "kept",
},
}
response = await client.post(app.url_path_for("create_template"), json=params)
assert response.status_code == status.HTTP_201_CREATED
metadata = response.json()["appliance_metadata"]
assert metadata["vendor_name"] == "Test vendor"
assert metadata["default_username"] == "admin"
assert metadata["future_field"] == "kept"
# read back from the database
response = await client.get(app.url_path_for("get_template", template_id=template_id))
assert response.status_code == status.HTTP_200_OK
assert response.json()["appliance_metadata"] == metadata
# the metadata object is replaced as a whole on update
params = {"appliance_metadata": {"default_username": "root"}}
response = await client.put(app.url_path_for("update_template", template_id=template_id), json=params)
assert response.status_code == status.HTTP_200_OK
assert response.json()["appliance_metadata"] == {"default_username": "root"}
async def test_template_delete(self, app: FastAPI, client: AsyncClient) -> None:
template_id = str(uuid.uuid4())
@@ -97,6 +97,10 @@ async def test_install_docker_version_skips_image_resolution(monkeypatch):
assert template["template_type"] == "docker"
# the version image name is injected into the template
assert template["image"] == "xrd:1.0"
# appliance metadata survives the install and validates through TemplateCreate
metadata = template["appliance_metadata"]
assert metadata["vendor_name"] == "Test vendor"
assert metadata["appliance_id"] == appliance.id
@pytest.mark.asyncio
@@ -555,3 +555,69 @@ def test_v8_get_template_type():
v6 = {"registry_version": 6, "docker": {"image": "test:latest"}}
assert converter.get_template_type(v6, None) == "docker"
def test_v8_appliance_metadata_copied_to_template():
"""
Appliance metadata (vendor information, default credentials...) is kept
on the template instead of being dropped, with version level values
overriding the appliance level ones.
"""
appliance = dict(VYOS_V8, default_username="vyos", default_password="vyospass")
version = dict(VYOS_V8["versions"][1], default_username="vyos145")
template = ApplianceToTemplate().new_template(appliance, version, "local")
metadata = template["appliance_metadata"]
assert metadata["appliance_id"] == VYOS_V8["appliance_id"]
assert metadata["vendor_name"] == "VyOS Inc."
assert metadata["status"] == "stable"
# the version level default_username overrides the appliance level one
assert metadata["default_username"] == "vyos145"
assert metadata["default_password"] == "vyospass"
def test_v8_appliance_metadata_version_level_overrides():
appliance = dict(VYOS_V8, installation_instructions="appliance instructions")
version = dict(VYOS_V8["versions"][1], installation_instructions="version instructions")
template = ApplianceToTemplate().new_template(appliance, version, "local")
assert template["appliance_metadata"]["installation_instructions"] == "version instructions"
def test_v6_appliance_metadata_copied_to_template():
"""
Registry versions 1-6 appliances keep their metadata too (the fields
they have: the v8-only ones are simply absent).
"""
appliance = {
"registry_version": 6,
"name": "SRLinux",
"category": "router",
"description": "Nokia SR Linux",
"vendor_name": "Nokia",
"docker": {"adapters": 35, "image": "ghcr.io/nokia/srlinux:latest"},
}
template = ApplianceToTemplate().new_template(appliance, None, "local")
metadata = template["appliance_metadata"]
assert metadata["description"] == "Nokia SR Linux"
assert metadata["vendor_name"] == "Nokia"
assert "default_username" not in metadata
def test_no_appliance_metadata_when_appliance_has_none():
appliance = {
"registry_version": 8,
"name": "Test",
"category": "guest",
"settings": [{"name": "only", "template_type": "qemu", "template_properties": {"ram": 512}}],
}
template = ApplianceToTemplate().new_template(appliance, None, "local")
assert "appliance_metadata" not in template