
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "TuranOrdo infra.resources.json",
  "description": "Infrastructure resources an app asks TuranOrdo to provision. Apps list only resources they own.",
  "type": "object",
  "additionalProperties": false,
  "required": ["version", "system", "app"],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "version": {
      "const": 1
    },
    "description": {
      "type": "string"
    },
    "system": {
      "$ref": "#/definitions/name"
    },
    "app": {
      "$ref": "#/definitions/name"
    },
    "postgres": {
      "type": "boolean"
    },
    "mongo": {
      "type": "boolean"
    },
    "redis": {
      "type": "boolean"
    },
    "rabbitmq": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "queues": {
          "type": "array",
          "uniqueItems": true,
          "items": {
            "$ref": "#/definitions/name"
          }
        }
      }
    },
    "kafka": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "topics": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["name"],
            "properties": {
              "name": {
                "$ref": "#/definitions/name"
              },
              "write": {
                "enum": ["owner", "open"],
                "default": "owner"
              },
              "read": {
                "enum": ["owner", "open"],
                "default": "owner"
              }
            }
          }
        }
      }
    },
    "web": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "sites", "profiles"],
      "properties": {
        "type": {
          "enum": ["php", "static"]
        },
        "source": {
          "type": "string",
          "minLength": 1
        },
        "php": {
          "type": "object",
          "additionalProperties": false,
          "required": ["version"],
          "properties": {
            "version": {
              "type": "string",
              "pattern": "^[0-9]+\\.[0-9]+$"
            }
          }
        },
        "artifact": {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "image", "tag_env", "path"],
          "properties": {
            "type": {
              "const": "docker-image"
            },
            "image": {
              "type": "string",
              "minLength": 1
            },
            "tag_env": {
              "type": "string",
              "pattern": "^[A-Za-z_][A-Za-z0-9_]*$"
            },
            "path": {
              "type": "string",
              "pattern": "^/",
              "not": {
                "pattern": "\\.\\."
              }
            }
          }
        },
        "sites": {
          "type": "array",
          "minItems": 1,
          "maxItems": 1,
          "items": {
            "$ref": "#/definitions/webSite"
          }
        },
        "profiles": {
          "type": "object",
          "additionalProperties": false,
          "required": ["local", "production"],
          "properties": {
            "local": {
              "$ref": "#/definitions/webProfile"
            },
            "production": {
              "$ref": "#/definitions/webProfile"
            }
          }
        },
        "middlewares": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          }
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "type": {
                "const": "php"
              }
            },
            "required": ["type"]
          },
          "then": {
            "required": ["source", "php"],
            "properties": {
              "sites": {
                "items": {
                  "properties": {
                    "document_root": {
                      "const": "public"
                    },
                    "spa_fallback": {
                      "const": false
                    }
                  }
                }
              }
            },
            "not": {
              "required": ["artifact"]
            }
          }
        },
        {
          "if": {
            "properties": {
              "type": {
                "const": "static"
              }
            },
            "required": ["type"]
          },          
          "then": {
            "oneOf": [
              {
                "required": ["artifact"],
                "not": {
                  "required": ["source"]
                }
              },
              {
                "required": ["source"],
                "not": {
                  "required": ["artifact"]
                },
                "properties": {
                  "source": {
                    "type": "string",
                    "minLength": 1,
                    "pattern": "^(?!/)(?!.*(^|/)\\.\\.(/|$)).+"
                  }
                }
              }
            ],
            "properties": {
              "sites": {
                "items": {
                  "properties": {
                    "document_root": {
                      "const": "."
                    },
                    "spa_fallback": {
                      "const": true
                    }
                  }
                }
              }
            },
            "not": {
              "required": ["php"]
            }
          }
        }
      ]
    }
  },
  "definitions": {
    "name": {
      "type": "string",
      "pattern": "^[a-z0-9]+([._-][a-z0-9]+)*$"
    },
    "host": {
      "type": "string",
      "pattern": "^[A-Za-z0-9.-]+$"
    },
    "webSite": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "name",
        "document_root",
        "index",
        "spa_fallback"
      ],
      "properties": {
        "name": {
          "$ref": "#/definitions/name"
        },
        "document_root": {
          "type": "string",
          "minLength": 1
        },
        "index": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "string",
            "minLength": 1
          }
        },
        "spa_fallback": {
          "type": "boolean"
        }
      }
    },
    "webProfile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["sites", "tls"],
      "properties": {
        "sites": {
          "type": "object",
          "minProperties": 1,
          "patternProperties": {
            "^[a-z0-9]+([._-][a-z0-9]+)*$": {
              "$ref": "#/definitions/webSiteProfile"
            }
          },
          "additionalProperties": false
        },
        "tls": {
          "$ref": "#/definitions/webTls"
        }
      }
    },
    "webSiteProfile": {
      "type": "object",
      "additionalProperties": false,
      "required": ["domains"],
      "properties": {
        "domains": {
          "type": "object",
          "additionalProperties": false,
          "required": ["canonical"],
          "properties": {
            "canonical": {
              "$ref": "#/definitions/host"
            },
            "redirect": {
              "type": "array",
              "items": {
                "$ref": "#/definitions/host"
              }
            },
            "redirect_status": {
              "enum": [301, 302],
              "default": 301
            }
          }
        },
        "routes": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/webRoute"
          }
        }
      }
    },
    "webRoute": {
      "type": "object",
      "additionalProperties": false,
      "required": ["host", "path"],
      "properties": {
        "host": {
          "$ref": "#/definitions/host"
        },
        "path": {
          "type": "string",
          "pattern": "^/",
          "not": {
            "pattern": "\\|"
          }
        }
      }
    },
    "webTls": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "enabled",
        "entrypoint",
        "certificate_resolver",
        "ssl_redirect"
      ],
      "properties": {
        "enabled": {
          "type": "boolean"
        },
        "entrypoint": {
          "type": "string",
          "minLength": 1
        },
        "certificate_resolver": {
          "type": "string"
        },
        "ssl_redirect": {
          "type": "boolean"
        }
      },
      "allOf": [
        {
          "if": {
            "properties": {
              "enabled": {
                "const": true
              }
            },
            "required": ["enabled"]
          },
          "then": {
            "properties": {
              "certificate_resolver": {
                "enum": ["letsencrypt", "hosted"]
              }
            }
          }
        },
        {
          "if": {
            "properties": {
              "enabled": {
                "const": false
              }
            },
            "required": ["enabled"]
          },
          "then": {
            "properties": {
              "certificate_resolver": {
                "const": ""
              },
              "ssl_redirect": {
                "const": false
              }
            }
          }
        }
      ]
    }
  }
}
