File size: 5,412 Bytes
fbf3c28 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
#!/usr/bin/env python3
# DTO Schema Validator
# Validates DTO manifests against schema definition
import argparse
import json
import os
import sys
import yaml
from pathlib import Path
from typing import Dict, Any
class DTOValidator:
def __init__(self):
self.schema = self._load_schema()
def _load_schema(self) -> Dict[str, Any]:
"""Load DTO schema definition"""
schema_path = Path("/data/adaptai/deployment/dto-schema/schema.yaml")
if not schema_path.exists():
# Basic schema for initial implementation
return {
"type": "object",
"required": ["manifest", "services"],
"properties": {
"manifest": {
"type": "object",
"required": ["version", "domain", "owner"],
"properties": {
"version": {"type": "string"},
"domain": {"type": "string"},
"owner": {"type": "string"}
}
},
"services": {
"type": "array",
"items": {
"type": "object",
"required": ["name", "description", "owner"],
"properties": {
"name": {"type": "string"},
"description": {"type": "string"},
"owner": {"type": "string"},
"ports": {"type": "array"},
"health_probes": {"type": "array"},
"supervisor_unit": {"type": "string"},
"slos": {"type": "array"}
}
}
}
}
}
with open(schema_path, 'r') as f:
return yaml.safe_load(f)
def validate_dto_file(self, dto_path: Path) -> bool:
"""Validate a single DTO file"""
try:
with open(dto_path, 'r') as f:
dto_content = yaml.safe_load(f)
return self._validate_against_schema(dto_content)
except Exception as e:
print(f"Error validating {dto_path}: {e}")
return False
def _validate_against_schema(self, dto_content: Dict[str, Any]) -> bool:
"""Validate DTO content against schema"""
# Basic validation logic
# In production, use a proper JSON schema validator like jsonschema
# Check required top-level fields
if "manifest" not in dto_content:
print("Missing 'manifest' section")
return False
if "services" not in dto_content:
print("Missing 'services' section")
return False
manifest = dto_content["manifest"]
if not all(field in manifest for field in ["version", "domain", "owner"]):
print("Manifest missing required fields: version, domain, or owner")
return False
# Validate services
for service in dto_content["services"]:
if not all(field in service for field in ["name", "description", "owner"]):
print(f"Service missing required fields: {service.get('name', 'unknown')}")
return False
print("DTO validation passed")
return True
def find_dto_files(self) -> List[Path]:
"""Find all DTO files in the repository"""
dto_files = []
# Look in platform/*/dto/ directories
platform_dir = Path("/data/adaptai/platform")
for domain_dir in platform_dir.iterdir():
if domain_dir.is_dir():
dto_dir = domain_dir / "dto"
if dto_dir.exists():
for dto_file in dto_dir.glob("*.yaml"):
dto_files.append(dto_file)
for dto_file in dto_dir.glob("*.yml"):
dto_files.append(dto_file)
return dto_files
def main():
parser = argparse.ArgumentParser(description="DTO Schema Validator")
parser.add_argument("--file", help="Specific DTO file to validate")
parser.add_argument("--all", action="store_true", help="Validate all DTO files")
args = parser.parse_args()
validator = DTOValidator()
if args.file:
dto_path = Path(args.file)
if not dto_path.exists():
print(f"DTO file not found: {args.file}")
sys.exit(1)
success = validator.validate_dto_file(dto_path)
sys.exit(0 if success else 1)
elif args.all:
dto_files = validator.find_dto_files()
all_valid = True
for dto_file in dto_files:
print(f"Validating {dto_file}...")
valid = validator.validate_dto_file(dto_file)
if not valid:
all_valid = False
print(f"❌ Validation failed for {dto_file}")
else:
print(f"✅ Validation passed for {dto_file}")
sys.exit(0 if all_valid else 1)
else:
parser.print_help()
sys.exit(1)
if __name__ == "__main__":
main() |