swagger: "2.0"
info: { version: "", title: "" }

paths:
  /automations:
    get:
      tags: [ "automations" ]
      summary: "List automations"
      operationId: "listAutomations"
      responses:
        "200":
          description: "successful operation"
          schema: { type: array, items: { $ref: "#/definitions/AutomationResponse" } }
          examples:
            test:
              - id: comment
                image: "docker.io/python:3"
                script: ""
                type: [ playbook ]
              - id: hash.sha1
                image: "docker.io/python:3"
                script: ""
                type: [ global, artifact, playbook ]
                schema: "{\"title\":\"Input\",\"type\":\"object\",\"properties\":{\"default\":{\"type\":\"string\",\"title\":\"Value\"}},\"required\":[\"default\"]}"
              - id: vt.hash
                image: "docker.io/python:3"
                script: ""
                type: [ global, artifact, playbook ]
                schema: "{\"title\":\"Input\",\"type\":\"object\",\"properties\":{\"default\":{\"type\":\"string\",\"title\":\"Value\"}},\"required\":[\"default\"]}"
      security: [ { roles: [ "automation:read" ] } ]
    post:
      tags: [ "automations" ]
      summary: "Create a new automation"
      operationId: "createAutomation"
      parameters:
        - { name: "automation", in: "body", description: "New automation", required: true, schema: { $ref: "#/definitions/AutomationForm" }, x-example: { id: "hash-sha-256", image: "docker.io/python:3", script: "import sys\nimport json\nimport hashlib\n\n\ndef run(msg):\n    sha256 = hashlib.sha256(msg['payload']['default'].encode('utf-8'))\n    return {'hash': sha256.hexdigest()}\n\n\nprint(json.dumps(run(json.loads(sys.argv[1]))))\n", type: [ global ] } }
      responses:
        "200":
          description: "successful operation"
          schema: { $ref: "#/definitions/AutomationResponse" }
          examples:
            test:
              id: "hash-sha-256"
              image: "docker.io/python:3"
              type: [ global ]
              script: |
                import sys
                import json
                import hashlib


                def run(msg):
                    sha256 = hashlib.sha256(msg['payload']['default'].encode('utf-8'))
                    return {'hash': sha256.hexdigest()}


                print(json.dumps(run(json.loads(sys.argv[1]))))

      security: [ { roles: [ "automation:write" ] } ]

  /automations/{id}:
    get:
      tags: [ "automations" ]
      summary: "Get a single automation"
      operationId: "getAutomation"
      parameters:
        - { name: "id", in: "path", description: "Automation ID", required: true, type: string, x-example: "hash.sha1" }
      responses:
        "200":
          description: "successful operation"
          schema: { $ref: "#/definitions/AutomationResponse" }
          examples:
            test:
              id: hash.sha1
              image: "docker.io/python:3"
              type: [ global, artifact, playbook ]
              schema: "{\"title\":\"Input\",\"type\":\"object\",\"properties\":{\"default\":{\"type\":\"string\",\"title\":\"Value\"}},\"required\":[\"default\"]}"
              script: |
                #!/usr/bin/env python

                import sys
                import json
                import hashlib


                def run(msg):
                    sha1 = hashlib.sha1(msg['payload']['default'].encode('utf-8'))
                    return {"hash": sha1.hexdigest()}


                print(json.dumps(run(json.loads(sys.argv[1]))))
      security: [ { roles: [ "automation:read" ] } ]
    put:
      tags: [ "automations" ]
      summary: "Update an existing automation"
      operationId: "updateAutomation"
      parameters:
        - { name: "id", in: "path", description: "Automation ID", required: true, type: string, x-example: "hash.sha1" }
        - { name: "automation", in: "body", description: "Automation object that needs to be added", required: true, schema: { $ref: "#/definitions/AutomationForm" }, x-example: { id: hash.sha1, image: "docker.io/python:3", script: "import sys\nimport json\nimport hashlib\n\n\ndef run(msg):\n    sha1 = hashlib.sha1(msg['payload'].encode('utf-8'))\n    return {'hash': sha1.hexdigest()}\n\n\nprint(json.dumps(run(json.loads(sys.argv[1]))))\n", type: [ global, artifact, playbook ] } }
      responses:
        "200":
          description: "successful operation"
          schema: { $ref: "#/definitions/AutomationResponse" }
          examples:
            test:
              id: hash.sha1
              image: "docker.io/python:3"
              type: [ global, artifact, playbook ]
              script: |
                import sys
                import json
                import hashlib


                def run(msg):
                    sha1 = hashlib.sha1(msg['payload'].encode('utf-8'))
                    return {'hash': sha1.hexdigest()}


                print(json.dumps(run(json.loads(sys.argv[1]))))
      security: [ { roles: [ "automation:write" ] } ]
    delete:
      tags: [ "automations" ]
      summary: "Delete a automation"
      operationId: "deleteAutomation"
      parameters:
        - { name: "id", in: "path", description: "Automation ID", required: true, type: string, x-example: "hash.sha1" }
      responses:
        "204": { description: "successful operation" }
      security: [ { roles: [ "automation:write" ] } ]

definitions:
  AutomationForm:
    type: object
    required: [ id, image, script, type ]
    properties:
      id: { type: string }
      image: { type: string }
      script: { type: string }
      type: { type: array, items: { type: string, enum: [ artifact, playbook, global ] } }
      schema: { type: string, example: "{}" }

  Automation:
    type: object
    required: [ image, script, type ]
    properties:
      image: { type: string }
      script: { type: string }
      type: { type: array, items: { type: string, enum: [ artifact, playbook, global ] } }
      schema: { type: string, example: "{}" }

  AutomationResponse:
    type: object
    required: [ id, image, script, type ]
    properties:
      id: { type: string }
      image: { type: string }
      script: { type: string }
      type: { type: array, items: { type: string, enum: [ artifact, playbook, global ] } }
      schema: { type: string, example: "{}" }
