{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://drim.waldur.com/schemas/v1beta1/manifest.schema.json",
  "title": "DRIM Infosystem Manifest",
  "type": "object",
  "required": [
    "apiVersion",
    "kind",
    "metadata",
    "spec"
  ],
  "additionalProperties": false,
  "properties": {
    "apiVersion": {
      "enum": [
        "drim/v1beta1",
        "drim/v1alpha1"
      ]
    },
    "kind": {
      "const": "Infosystem"
    },
    "metadata": {
      "type": "object",
      "required": [
        "name",
        "id"
      ],
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string",
          "pattern": "^[a-z0-9-]{1,63}$"
        },
        "id": {
          "type": "string",
          "format": "uuid"
        },
        "owner": {
          "type": "string"
        },
        "runbook": {
          "type": "string",
          "description": "Free text shown to the operator at WAITING_INPUT. Promoted to a first-class field in v1beta1; v1alpha1's protocol referenced it but never defined it."
        },
        "labels": {
          "type": "object",
          "additionalProperties": {
            "type": "string"
          }
        }
      }
    },
    "spec": {
      "type": "object",
      "required": [
        "components",
        "validation",
        "launchModes"
      ],
      "additionalProperties": false,
      "properties": {
        "components": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/component"
          }
        },
        "relationships": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/relationship"
          }
        },
        "secretsRequired": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/secretRequirement"
          }
        },
        "validation": {
          "$ref": "#/$defs/validation"
        },
        "launchModes": {
          "$ref": "#/$defs/launchModes"
        },
        "profiles": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/profile"
          }
        },
        "backupPolicy": {
          "$ref": "#/$defs/backupPolicy"
        },
        "secrets": {
          "not": {},
          "description": "RESERVED for v2. Rejected by v1beta1 validators."
        },
        "vaults": {
          "not": {},
          "description": "RESERVED for v2. Rejected by v1beta1 validators."
        },
        "recovery": {
          "$ref": "#/$defs/recovery",
          "deprecated": true,
          "description": "Earlier draft: recovery gates only. Read as spec.gates with operation recover; writers emit spec.gates."
        },
        "gates": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/gate"
          },
          "description": "Where recover, validate and failback stop for people (section 9.6)."
        }
      }
    }
  },
  "$defs": {
    "component": {
      "type": "object",
      "required": [
        "name",
        "type"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "type": {
          "enum": [
            "vm",
            "k8s",
            "external"
          ]
        },
        "targetRef": {
          "type": "string",
          "description": "Names a key under profiles[].platform. Defaults to `type`, preserving v1alpha1 behaviour."
        },
        "requirements": {
          "type": "object"
        },
        "consistency": {
          "$ref": "#/$defs/consistency"
        },
        "disks": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/disk"
          }
        },
        "networks": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/network"
          }
        },
        "workloads": {
          "type": "array"
        },
        "persistentVolumes": {
          "type": "array"
        },
        "resourceCapture": {
          "$ref": "#/$defs/resourceCapture"
        },
        "artifacts": {
          "type": "object"
        },
        "contract": {
          "type": "object"
        },
        "requiredFor": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "licenseRebindRequired": {
          "type": "boolean"
        },
        "source": {
          "$ref": "#/$defs/source"
        },
        "restore": {
          "$ref": "#/$defs/restore"
        },
        "role": {
          "enum": [
            "web",
            "application",
            "database",
            "storage",
            "access",
            "infrastructure",
            "other"
          ],
          "description": "What the component does. Recovery boots application and web components (and whatever depends on them) after the before-applications gates (§9.6)."
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "type": {
                "const": "vm"
              }
            },
            "required": [
              "type"
            ]
          },
          "then": {
            "required": [
              "requirements",
              "disks",
              "restore"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "k8s"
              }
            },
            "required": [
              "type"
            ]
          },
          "then": {
            "required": [
              "workloads",
              "resourceCapture"
            ]
          }
        }
      ]
    },
    "consistency": {
      "type": "object",
      "description": "v1beta1 replaces v1alpha1's proposed single `consistency` enum. `achieved` and `method` are written by the DR service at capture time; a service MUST NOT report achieved=quiesced for an order-dependent method.",
      "additionalProperties": false,
      "properties": {
        "requested": {
          "enum": [
            "crash",
            "quiesced"
          ]
        },
        "achieved": {
          "enum": [
            "crash",
            "quiesced"
          ]
        },
        "method": {
          "enum": [
            "fs-freeze",
            "vm-poweroff",
            "group-snapshot",
            "independent-snapshots",
            "unknown"
          ],
          "description": "Only fs-freeze and vm-poweroff are order-independent. group-snapshot is NOT necessarily atomic — measured: a Cinder consistent group snapshot issued two ordinary per-volume snapshots."
        },
        "skewObservedMs": {
          "type": "integer",
          "minimum": 0
        }
      }
    },
    "disk": {
      "type": "object",
      "required": [
        "name",
        "role",
        "sizeGiB"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "role": {
          "enum": [
            "system",
            "data",
            "swap",
            "other"
          ]
        },
        "sizeGiB": {
          "type": "integer",
          "minimum": 1
        },
        "performance": {
          "type": "object",
          "properties": {
            "minIops": {
              "type": "integer"
            }
          }
        },
        "guestIdentity": {
          "type": "object",
          "description": "Identity the guest expects. Device paths and order are NOT preserved across restore; a target that can honour this SHOULD, and one that cannot MUST say so in the validation summary.",
          "additionalProperties": false,
          "properties": {
            "uuid": {
              "type": "string"
            },
            "label": {
              "type": "string"
            },
            "serial": {
              "type": "string"
            }
          }
        }
      }
    },
    "network": {
      "type": "object",
      "required": [
        "name",
        "addresses"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "addresses": {
          "type": "object",
          "required": [
            "mode"
          ],
          "properties": {
            "mode": {
              "enum": [
                "static-remap",
                "dhcp",
                "preserve"
              ]
            },
            "fallback": {
              "enum": [
                "dhcp",
                "static-remap"
              ]
            }
          },
          "allOf": [
            {
              "if": {
                "properties": {
                  "mode": {
                    "enum": [
                      "static-remap",
                      "preserve"
                    ]
                  }
                },
                "required": [
                  "mode"
                ]
              },
              "then": {
                "required": [
                  "fallback"
                ]
              }
            }
          ]
        }
      }
    },
    "resourceCapture": {
      "type": "object",
      "properties": {
        "include": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "exclude": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "clusterScoped": {
          "type": "object",
          "description": "Cluster-scoped restore is MERGE semantics, not create.",
          "properties": {
            "include": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "exclude": {
              "type": "array",
              "items": {
                "type": "string"
              }
            },
            "onConflict": {
              "enum": [
                "fail",
                "skip",
                "overwrite"
              ],
              "default": "fail"
            }
          }
        },
        "stripFields": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "ADDITIVE to the normative baseline in spec §6.3, which implementations MUST apply first."
        }
      }
    },
    "source": {
      "type": "object",
      "description": "Discriminated union on `platform`. Platform-level identity only; storage-backend facts are resolved at capture time and recorded in disk-N.meta.json.",
      "required": [
        "platform"
      ],
      "properties": {
        "platform": {
          "type": "string"
        }
      },
      "oneOf": [
        {
          "properties": {
            "platform": {
              "const": "openstack"
            },
            "region": {
              "type": "string"
            },
            "serverId": {
              "type": "string"
            },
            "volumes": {
              "type": "array"
            }
          }
        },
        {
          "properties": {
            "platform": {
              "const": "kubevirt"
            },
            "cluster": {
              "type": "string"
            },
            "namespace": {
              "type": "string"
            },
            "vmName": {
              "type": "string"
            },
            "claims": {
              "type": "array"
            }
          }
        },
        {
          "properties": {
            "platform": {
              "const": "kubernetes"
            },
            "cluster": {
              "type": "string"
            },
            "namespace": {
              "type": "string"
            },
            "storageClass": {
              "type": "string"
            }
          }
        },
        {
          "properties": {
            "platform": {
              "const": "vmware"
            },
            "vcenter": {
              "type": "string"
            },
            "moref": {
              "type": "string"
            }
          }
        }
      ]
    },
    "restore": {
      "type": "object",
      "properties": {
        "method": {
          "enum": [
            "volume-import",
            "qcow2",
            "image-service"
          ]
        },
        "bootOrder": {
          "type": "integer"
        },
        "guestAgent": {
          "type": "string",
          "description": "OPTIONAL. A level-1 guest-agent check MUST NOT fail a component that leaves this unset."
        },
        "guestAdaptation": {
          "type": "array",
          "items": {
            "type": "object",
            "required": [
              "type"
            ],
            "properties": {
              "type": {
                "enum": [
                  "ssh-ca-trust",
                  "reset-instance-id",
                  "agent-enrolment",
                  "custom"
                ]
              },
              "required": {
                "type": "boolean",
                "default": false
              },
              "note": {
                "type": "string"
              }
            }
          }
        },
        "quiesce": {
          "type": "object",
          "description": "How failback stops writes to this component at the DR site (section 9.8). Without it, the VM is stopped.",
          "additionalProperties": false,
          "properties": {
            "method": {
              "enum": [
                "stop-service",
                "read-only",
                "stop-vm"
              ]
            },
            "unit": {
              "type": "string",
              "description": "Service unit to stop or switch, for stop-service and read-only."
            }
          }
        }
      }
    },
    "relationship": {
      "type": "object",
      "required": [
        "from",
        "to",
        "type"
      ],
      "properties": {
        "from": {
          "type": "string"
        },
        "to": {
          "type": "string"
        },
        "type": {
          "enum": [
            "depends-on",
            "connects-to",
            "mounts"
          ]
        },
        "startupGate": {
          "type": "object",
          "properties": {
            "check": {
              "enum": [
                "tcp",
                "http",
                "script",
                "none"
              ]
            },
            "from": {
              "type": "string",
              "description": "Who probes: `dr-service` or a component name. A cross-landing-zone gate may be unverifiable; `check: none` with a delay is an honest declaration."
            },
            "target": {
              "type": "string"
            },
            "timeoutSeconds": {
              "type": "integer"
            }
          }
        }
      }
    },
    "secretRequirement": {
      "type": "object",
      "description": "NAMES ONLY. Any value-bearing key here is a validation error.",
      "required": [
        "name"
      ],
      "additionalProperties": false,
      "properties": {
        "name": {
          "type": "string"
        },
        "kind": {
          "type": "string"
        },
        "consumedBy": {
          "type": "array",
          "items": {
            "type": "string"
          }
        },
        "description": {
          "type": "string"
        }
      }
    },
    "validation": {
      "type": "object",
      "required": [
        "levels"
      ],
      "additionalProperties": false,
      "properties": {
        "levels": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/level"
          }
        },
        "policy": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "failFast": {
              "type": "boolean"
            },
            "report": {
              "enum": [
                "summary",
                "full"
              ]
            }
          }
        }
      }
    },
    "level": {
      "type": "object",
      "description": "v1alpha1 and early v1beta1 left this as a free-form object. Measured consequence: a one-character typo in `name` caused every check in the level to be silently skipped and the run to report PASSED. The vocabulary is closed and additionalProperties is false so that a misspelling is a manifest error rather than a silent no-op.",
      "required": [
        "name",
        "checks"
      ],
      "additionalProperties": false,
      "properties": {
        "name": {
          "enum": [
            "package",
            "infrastructure",
            "connectivity",
            "application"
          ]
        },
        "checks": {
          "type": "array",
          "minItems": 1,
          "items": {
            "$ref": "#/$defs/check"
          }
        }
      }
    },
    "check": {
      "type": "object",
      "required": [
        "type"
      ],
      "additionalProperties": false,
      "properties": {
        "type": {
          "enum": [
            "checksum",
            "manifest-schema",
            "vm-running",
            "guest-agent",
            "k8s-workload-ready",
            "tcp",
            "http",
            "script"
          ]
        },
        "target": {
          "type": "string",
          "description": "MUST resolve to a components[].name (optionally `name:port` for tcp/http). Not expressible in JSON Schema; validators MUST enforce it."
        },
        "from": {
          "type": "string"
        },
        "expect": {
          "enum": [
            "reachable",
            "blocked"
          ],
          "default": "reachable",
          "description": "A `blocked` assertion is evidence ONLY if the target is independently known to be listening (§8.2a). An unestablished negative is reported `inconclusive`, never `passed`."
        },
        "expectStatus": {
          "type": "integer",
          "minimum": 100,
          "maximum": 599
        },
        "condition": {
          "type": "string"
        },
        "script": {
          "type": "string"
        },
        "runOn": {
          "type": "string"
        },
        "timeoutSeconds": {
          "type": "integer",
          "minimum": 1
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "type": {
                "const": "vm-running"
              }
            },
            "required": [
              "type"
            ]
          },
          "then": {
            "required": [
              "target"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "guest-agent"
              }
            },
            "required": [
              "type"
            ]
          },
          "then": {
            "required": [
              "target"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "k8s-workload-ready"
              }
            },
            "required": [
              "type"
            ]
          },
          "then": {
            "required": [
              "target",
              "condition"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "tcp"
              }
            },
            "required": [
              "type"
            ]
          },
          "then": {
            "required": [
              "target"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "http"
              }
            },
            "required": [
              "type"
            ]
          },
          "then": {
            "required": [
              "target",
              "expectStatus"
            ]
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "script"
              }
            },
            "required": [
              "type"
            ]
          },
          "then": {
            "required": [
              "script",
              "runOn"
            ]
          }
        }
      ]
    },
    "launchModes": {
      "type": "object",
      "properties": {
        "validation": {
          "type": "object"
        },
        "recovery": {
          "type": "object"
        }
      }
    },
    "profile": {
      "type": "object",
      "required": [
        "name",
        "platform"
      ],
      "properties": {
        "name": {
          "type": "string"
        },
        "platform": {
          "type": "object",
          "description": "Map of LANDING ZONES keyed by the names components reference via targetRef. v1alpha1 fixed this to `vm` and `k8s`.",
          "additionalProperties": {
            "type": "object",
            "properties": {
              "provider": {
                "type": "string"
              },
              "region": {
                "type": "string"
              },
              "flavorMapping": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "match": {
                      "type": "object"
                    },
                    "matchAtLeast": {
                      "type": "object",
                      "description": "Nearest-fit. Exact `match` alone cannot be satisfied by a finite catalog."
                    },
                    "flavor": {
                      "type": "string"
                    }
                  }
                }
              },
              "storage": {
                "type": "object"
              },
              "kubeconfigRef": {
                "type": "string"
              },
              "storageClass": {
                "type": "string"
              },
              "provisioning": {
                "type": "object",
                "properties": {
                  "onMissing": {
                    "enum": [
                      "create",
                      "fail"
                    ],
                    "default": "fail"
                  },
                  "readyCondition": {
                    "type": "string"
                  }
                }
              }
            }
          }
        },
        "networks": {
          "type": "object"
        },
        "externals": {
          "type": "object"
        },
        "s3": {
          "type": "object",
          "properties": {
            "endpoint": {
              "type": "string"
            },
            "bucket": {
              "type": "string"
            },
            "deliveryMode": {
              "enum": [
                "presigned-url",
                "credentials"
              ],
              "default": "presigned-url",
              "description": "presigned-url is RECOMMENDED: an s3:// source needs credentials in the restore target's namespace, where the tenant can read them. Sign for the endpoint the IMPORTER will use — sigv4 covers the Host header."
            }
          }
        },
        "switchover": {
          "type": "object",
          "description": "How failback moves traffic back to this profile (section 9.8). Without it, switchover is the before-switchover gate, done by people.",
          "additionalProperties": false,
          "properties": {
            "method": {
              "enum": [
                "dns",
                "load-balancer",
                "floating-address",
                "manual"
              ]
            },
            "ref": {
              "type": "string",
              "description": "Name of the record, pool or address; never credentials."
            }
          }
        }
      }
    },
    "backupPolicy": {
      "type": "object",
      "properties": {
        "schedule": {
          "type": "string"
        },
        "retention": {
          "type": "object"
        },
        "validateEvery": {
          "type": "string"
        },
        "encryption": {
          "type": "object",
          "properties": {
            "mode": {
              "enum": [
                "sse-kms",
                "client-side"
              ]
            },
            "keyRef": {
              "type": "string"
            }
          }
        },
        "immutability": {
          "type": "object"
        },
        "chain": {
          "type": "object",
          "description": "Capture policy. Does NOT change the archive layout: published packages remain standalone.",
          "properties": {
            "mode": {
              "enum": [
                "full",
                "synthetic-full"
              ],
              "default": "full"
            },
            "fullEvery": {
              "type": "string"
            },
            "maxChainLength": {
              "type": "integer",
              "minimum": 1
            }
          },
          "allOf": [
            {
              "if": {
                "properties": {
                  "mode": {
                    "const": "synthetic-full"
                  }
                },
                "required": [
                  "mode"
                ]
              },
              "then": {
                "required": [
                  "fullEvery"
                ]
              }
            }
          ]
        }
      }
    },
    "recovery": {
      "type": "object",
      "description": "Deprecated: the earlier draft's recovery gates. Use spec.gates.",
      "additionalProperties": false,
      "properties": {
        "gates": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/legacyRecoveryGate"
          }
        }
      }
    },
    "gate": {
      "type": "object",
      "required": [
        "id",
        "position",
        "kind",
        "title"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9-]{1,63}$"
        },
        "operation": {
          "enum": [
            "recover",
            "validate",
            "failback"
          ],
          "default": "recover"
        },
        "position": {
          "enum": [
            "before-freeze",
            "before-transfer",
            "after-restore",
            "before-level",
            "before-applications",
            "before-checks",
            "before-switchover",
            "before-handover",
            "before-teardown",
            "before-decommission"
          ]
        },
        "level": {
          "type": "integer",
          "minimum": 2,
          "description": "Boot level to stop before; required with position before-level, forbidden otherwise."
        },
        "kind": {
          "enum": [
            "approval",
            "secret",
            "manual"
          ]
        },
        "title": {
          "type": "string"
        },
        "timeoutSeconds": {
          "type": "integer",
          "minimum": 1
        },
        "approvalsRequired": {
          "type": "integer",
          "minimum": 1
        },
        "secrets": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Names from secretsRequired; never values."
        },
        "confirmationsRequired": {
          "type": "integer",
          "minimum": 1
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "position": {
                "const": "before-level"
              }
            },
            "required": [
              "position"
            ]
          },
          "then": {
            "required": [
              "level"
            ]
          },
          "else": {
            "not": {
              "required": [
                "level"
              ]
            }
          }
        },
        {
          "if": {
            "properties": {
              "operation": {
                "const": "recover"
              }
            },
            "required": [
              "operation"
            ]
          },
          "then": {
            "properties": {
              "position": {
                "enum": [
                  "before-transfer",
                  "after-restore",
                  "before-level",
                  "before-applications",
                  "before-checks",
                  "before-handover"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "operation": {
                "const": "validate"
              }
            },
            "required": [
              "operation"
            ]
          },
          "then": {
            "properties": {
              "position": {
                "enum": [
                  "before-transfer",
                  "after-restore",
                  "before-level",
                  "before-applications",
                  "before-checks",
                  "before-teardown"
                ]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "operation": {
                "const": "failback"
              }
            },
            "required": [
              "operation"
            ]
          },
          "then": {
            "properties": {
              "position": {
                "enum": [
                  "before-freeze",
                  "before-transfer",
                  "after-restore",
                  "before-level",
                  "before-applications",
                  "before-checks",
                  "before-switchover",
                  "before-handover",
                  "before-decommission"
                ]
              }
            }
          }
        },
        {
          "if": {
            "not": {
              "required": [
                "operation"
              ]
            }
          },
          "then": {
            "properties": {
              "position": {
                "enum": [
                  "before-transfer",
                  "after-restore",
                  "before-level",
                  "before-applications",
                  "before-checks",
                  "before-handover"
                ]
              }
            }
          }
        }
      ]
    },
    "legacyRecoveryGate": {
      "type": "object",
      "required": [
        "id",
        "position",
        "kind",
        "title"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[a-z0-9-]{1,63}$"
        },
        "position": {
          "enum": [
            "before-transfer",
            "after-restore",
            "before-applications",
            "before-handover"
          ]
        },
        "kind": {
          "enum": [
            "approval",
            "secret",
            "manual"
          ]
        },
        "title": {
          "type": "string"
        },
        "timeoutSeconds": {
          "type": "integer",
          "minimum": 1
        },
        "approvalsRequired": {
          "type": "integer",
          "minimum": 1
        },
        "secrets": {
          "type": "array",
          "items": {
            "type": "string"
          },
          "description": "Names from secretsRequired; never values."
        },
        "confirmationsRequired": {
          "type": "integer",
          "minimum": 1
        }
      },
      "description": "Earlier draft's recovery gate (section 9.6, Compatibility)."
    }
  }
}
