#
# Tsuru Router API Specification
#
openapi: 3.0.0

info:
  title: Tsuru Router API
  description: Tsuru Router API integration to external routers
  version: "1.0.0"

paths:
  /backend/{name}:
    get:
      summary: Application backend address
      description: |
        The backend endpoint returns the address that the
        application is published.
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
      tags:
        - Backends
      responses:
        200:
          description: An Address object
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Address'
        default:
          $ref: '#/components/schemas/Error'
          
    delete:
      summary: Delete application backend
      description: |
        The backend endpoint deletes the backend.
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
      tags:
        - Backends
      responses:
        200:
          description: An Address object
        404:
          description: Backend not found
        400:
          description: Backend was swapped to other
        default:
          $ref: '#/components/schemas/Error'
    post:
      summary: Add application backend
      description: |
        The backend endpoint the backend.
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
      tags:
        - Backends
      responses:
        200:
          description: An Address object
        404:
          description: Backend not found
        409:
          description: Backend already exists
        default:
          $ref: '#/components/schemas/Error'
  
  /backend/{name}/routes:
    get:
      summary: Application backend routes
      description: |
        The backend endpoint returns information about the
        backends routes for a given application.
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
        - name: route
          in: path
          description: Route name.
          required: true
          schema:
            type: string
      tags:
        - Backends
      responses:
        200:
          description: An array of backends
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Addresses'
        default:
          $ref: '#/components/schemas/Error'
            
  /backend/{name}/routes/{route}:
    post:
      summary: Add application backend routes
      description: |
        The backend endpoint returns information about the
        backends routes for a given application.
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
        - name: route
          in: path
          description: Route name.
          required: true
          schema:
            type: string
      tags:
        - Backends
      responses:
        200:
          description: An array of backends
        404:
          description: Backend not found
        default:
          $ref: '#/components/schemas/Error'
    
  /backend/{name}/swap:
    post:
      summary: Application backend
      description: |
        The backend endpoint returns information about the
        backends for a given application.
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
      requestBody:
        description: Swap parameters
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Swap'
      tags:
        - Backends
      responses:
        200:
          description: Swapped backends
        404:
          description: Backend not found
        default:
          $ref: '#/components/schemas/Error'
            
  /healthcheck:
    get:
      summary: Application backend
      description: |
        Health check endpoint.
      tags:
        - Healthcheck
      responses:
        200:
          description: Return OK
        default:
          $ref: '#/components/schemas/Error'
            
  /support/{type}:
    get:
      summary: CNAME Application backend
      description: |
        The backend endpoint returns information about the
        application cname.
      parameters:
        - name: type
          in: path
          description: Application name.
          required: true
          schema:
            type: string
      tags:
        - Support
      responses:
        200:
          description: Check if support types.
          content:
            text/plain:
              schema:
                type: string
        default:
          $ref: '#/components/schemas/Error'  
            
  /backend/{name}/cname:
    get:
      summary: Application backend cnames info
      description: |
        The backend endpoint returns information about the
        application cname.
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
      tags:
        - Backends
      responses:
        200:
          description: An array of cnames
          content:
            application/json:
              schema:
                type: array
                items:
                  type: string
        default:
          $ref: '#/components/schemas/Error'
            
  /backend/{name}/cname/{cnameAddr}:
    post:
      summary: CNAME Application backend
      description: |
        The backend endpoint adds CNAME to application.
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
        - name: cnameAddr
          in: path
          description: Application name.
          required: true
          schema:
            type: string
      tags:
        - Backends
      responses:
        200:
          description: Cname added ok
        400:
          description: Backend does not support CNAME
        404:
          description: Backend not found
        409:
          description: CNAME already exists
        default:
          $ref: '#/components/schemas/Error'  
      
  /backend/{name}/certificate/{cname}:
    get:
      summary: HTTPS Certificate management endpoint
      description: |
        The backend endpoint returns information about the
        HTTPS certificate. 
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
        - name: cname
          in: path
          description: CNAME to get certificate.
          required: true
          schema:
            type: string
      tags:
        - Certificate
      responses:
        200:
          description: Certificate Object
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Certificate'
        default:
          $ref: '#/components/schemas/Error'
    post:
      summary: HTTPS Certificate management endpoint
      description: |
        The backend endpoint returns information about the
        HTTPS certificate. 
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
        - name: cname
          in: path
          description: CNAME to get certificate.
          required: true
          schema:
            type: string
      requestBody:
        description: Swap parameters
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Certificate'
      tags:
        - Certificate
      responses:
        200:
          description: Certificate Object
        default:
          $ref: '#/components/schemas/Error'
            
  /backend/{name}/healthcheck:
    put:
      summary: Application backend healthcheck
      description: |
        The backend endpoint to set healthcheck.
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
      tags:
        - Healthcheck
      responses:
        200:
          description: Healthcheck setted properly.
        default:
          $ref: '#/components/schemas/Error'
            
  /info:
    get:
      summary: Application backend
      description: |
        The backend endpoint returns information about
        the router.
      tags:
        - Info
      responses:
        200:
          description: An array of backends
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Info'
        default:
          $ref: '#/components/schemas/Error'
            
  /backend/{name}/status:
    get:
      summary: Application backend
      description: |
        The backend endpoint returns information about the backends
        for a given application.
      parameters:
        - name: name
          in: path
          description: Application name.
          required: true
          schema:
            type: string
      tags:
        - Backends
      responses:
        200:
          description: An array of backends
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Status'
        default:
          $ref: '#/components/schemas/Error'
            
# Object definitions          
components:
  schemas:
    Certificate:
      type: object
      properties:
        certificate:
          type: string
          description: PEM encoded certificate
        key:
          type: string
          description: PEM encoded key
    Swap:
      type: object
      properties:
        target:
          type: string
          description: Swap target to swap to.
        cnameOnly:
          type: boolean
          description: Is the swap cname only.
    Address:
      type: object
      properties:
        address:
          type: string
          description: Address that the application is published.
    Addresses:
      type: object
      properties:
        addresses:
          type: array
          items:
            type: string
    Info:
      type: object
      additionalProperties:
        type: string
    Status:
      type: object
      properties:
        status:
          type: string
        detail:
          type: string
    Error:
      type: object
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
        fields:
          type: string
