{
  "$id": "adapter-db_mongo",
  "type": "object",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "properties": {
    "db": {
      "type": "string",
      "description": "The database used for data storage.",
      "default": "itential",
      "examples": [
        "pronghorn"
      ]
    },
    "url": {
      "type": "string",
      "description": "Mongo's complete connection URL. This property overrides db, host, port, credentials and replSet",
      "examples": [
        "mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]"
      ]
    },
    "host": {
      "type": "string",
      "description": "Host where the Mongo database resides.",
      "default": "localhost",
      "examples": [
        "localhost"
      ]
    },
    "port": {
      "type": "integer",
      "description": "Port to be used to connect to Mongo.",
      "default": 27017,
      "minimum": 1,
      "maximum": 65535
    },
    "databaseConnection": {
      "type": "string",
      "description": "Establish single connection to mongo db upon adapter start or new connection to mongo db upon each database request",
      "default": "connect on startup",
      "enum": [
        "connect on startup",
        "connect on request"
      ]
    },
    "createUuid": {
      "type": "boolean",
      "description": "Whether the adapter should create a uuid for documents on create and createMany",
      "default": true
    },
    "convertObjectId": {
      "type": "boolean",
      "description": "Set to true if you want fields with type ObjectId to be returned in stringified format ('ObjectId(<id>)'. Otherwise only the <id> itself will be returned.",
      "default": false
    },
    "credentials": {
      "$ref":"#/definitions/credentials"
    },
    "ssl": {
      "$ref":"#/definitions/ssl"
    },
    "replSet": {
      "$ref":"#/definitions/replSet"
    },
    "healthcheck": {
      "$ref":"#/definitions/healthcheck"
    }
  },
  "required" : ["db"],
  "oneOf": [
    {
      "required": ["url"]
    },
    {
      "required": ["host", "port"]
    }
  ],
  "definitions": {
    "credentials": {
      "$id": "/credentials",
      "type": "object",
      "properties": {
        "dbAuth" : {
          "type" : "boolean",
          "description": "whether or not to use user authentication to connect to mongo",
          "default" : false
        },
        "passwd": {
          "type": "string",
          "description": "Password used when authentication is enabled.",
          "default": "pronghorn",
          "examples": [
            "itentialPassword"
          ]
        },
        "user": {
          "type": "string",
          "description": "Username used when authentication is enabled.",
          "default": "pronghorn",
          "examples": [
            "itentialUser"
          ]
        }
      },
      "required" : ["dbAuth"]
    },
    "ssl": {
      "$id": "/ssl",
      "type": "object",
      "properties": {
        "enabled": {
          "$id": "/ssl/properties/enabled",
          "type": "boolean",
          "description": "Whether or not ssl is enabled on Mongo",
          "default": false            
        },
        "acceptInvalidCerts": {
          "$id": "/ssl/properties/acceptInvalidCerts",
          "type": "boolean",
          "description": "Whether NSO is configured to accept invalid certificates",
          "default": false
        },
        "sslCA": {
          "$id": "/ssl/properties/sslCA",
          "type": "string",
          "description": "The fully qualified path name to the CA pem file used for SSL",
          "default": "",
          "examples": [
            "keys/key.pem"
          ]
        },
        "checkServerIdentity": {
          "$id": "/ssl/properties/checkServerIdentity",
          "type": "boolean",
          "description": "Whether to validate certificate against the url",
          "default": false
        }
      },
      "required" : ["enabled"],
      "allOf": [
        {
          "if" : { "properties" : { "enabled": { "enum": [true] } } },
          "then" : { "required" : ["acceptInvalidCerts", "checkServerIdentity"] }
        },
        {
          "if" : { "allOf": [ 
            { "properties" : { "enabled": { "enum": [true] } } },
            { "properties" : { "acceptInvalidCerts": { "enum": [ false ] } } }
          ] },
          "then" : { "required" : ["sslCA"] }
        }
      ]
    },
    "replSet":{
      "$id": "/replSet",
      "type": "object",
      "properties": {
        "enabled": {
          "$id": "/replSet/properties/enabled",
          "type": "boolean",
          "description": "Are you using a replication set",
          "default": false            
        },
        "replicaSet": {
          "$id": "/replSet/properties/replicaSet",
          "type": "string",
          "description": "The replication set you are using",
          "default": "",
          "examples": [
            "rs_pronghorn"
          ]
        }
      },
      "required" : ["enabled"]
    },
    "healthcheck": {
      "type": "object",
      "properties": {
        "type": {
          "type": "string",
          "description": "The type of healthcheck to run",
          "default": "startup",
          "enum": [
            "none",
            "startup"
          ]
        }
      }
    }
  }
}
