#
# Tsuru Service API Specification
#
openapi: 3.0.0

info:
  title: Tsuru Service API
  description: Tsuru Service API used to integration with external services
  version: "1.0.0"

paths:
  /resources/plans:
    get:
      summary: List plans available
      description: |
        The service endpoint returns available service plans
      responses:
        200:
          description: An array of plans
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Plan'
        default:
          $ref: '#/components/schemas/Error'
  /resources:
    post:
      summary: Create a new service instance
      description: |
        The service endpoint creates a new service instance
      tags:
      - Service Instance
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/InstanceForm'
      responses:
        201:
          description: Instance successfully created
        default:
          $ref: '#/components/schemas/Error'
  /resources/{name}:
    parameters:
    - name: name
      in: path
      description: Instance name
      required: true
      schema:
        type: string
    get:
      summary: Service instance additional information
      description: |
        The service endpoint returns additional information about the instance
      tags:
      - Service Instance
      responses:
        404:
          description: No additional information available
        200:
          description: Additional information available
          content:
            application/json:
              schema:
                type: array
                items:
                  type: object
                  description: Additional service instance information
                  additionalProperties:
                    type: string
    put:
      summary: Update a service instance
      description: |
        The service endpoint updates a service instance
      tags:
      - Service Instance
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/InstanceForm'
      responses:
        200:
          description: Instance updated
        404:
          description: Instance not found
        default:
          $ref: '#/components/schemas/Error'
    delete:
      summary: Delete a service instance
      description: |
        The service endpoint removes the service instance
      tags:
      - Service Instance
      responses:
        200:
          description: Instance removed
        404:
          description: Instance not found
        default:
          $ref: '#/components/schemas/Error'
  /resources/{name}/bind:
    parameters:
    - name: name
      in: path
      description: Instance name
      required: true
      schema:
        type: string
    post:
      summary: Bind Unit
      description: |
        The service endpoint binds a unit to the service instance
      tags:
      - Binding
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/BindUnitForm'
      responses:
        201:
          description: Unit successfully bound to the service instance
          content:
            application/json:
              schema:
                type: object
                description: Environments to be inject on the unit
                additionalProperties:
                  type: string
        404:
          description: Service instance does not exist
        412:
          description: Service instance not ready
        default:
          $ref: '#/components/schemas/Error'
    delete:
      summary: Unbind Unit
      description: |
        The service endpoint unbinds the unit from the service instance
      tags:
      - Binding
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/BindUnitForm'
      responses:
        200:
          description: Unit successfully unbound from service instance
        404:
          description: Service instance does not exist
        default:
          $ref: '#/components/schemas/Error'
  /resources/{name}/bind-app:
    parameters:
    - name: name
      in: path
      description: Instance name
      required: true
      schema:
        type: string
    post:
      summary: Bind App
      description: |
        The service endpoint binds the app to the service instance
      tags:
      - Binding
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/BindAppForm'
      responses:
        201:
          description: App successfully bound to the service instance
          content:
            application/json:
              schema:
                type: object
                description: Environments to be inject on the app
                additionalProperties:
                  type: string
        404:
          description: Service instance does not exist
        412:
          description: Service instance not ready
        default:
          $ref: '#/components/schemas/Error'

    delete:
      summary: Unbind App
      description: |
        The service endpoint unbinds the app from the service instance
      tags:
      - Binding
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              $ref: '#/components/schemas/BindAppForm'
      responses:
        200:
          description: App successfully unbound from service instance
        404:
          description: Service instance does not exist
        default:
          $ref: '#/components/schemas/Error'
  /resources/{name}/status:
    get:
      summary: Service Instance Status
      description: |
        The service endpoint returns the service instance status
      tags:
      - Service Instance
      parameters:
      - name: name
        in: path
        description: Instance name
        required: true
        schema:
          type: string
      responses:
        202:
          description: Instance not available yet
        204:
          description: Instance running
        default:
          $ref: '#/components/schemas/Error'


# Object definitions          
components:
  schemas:
    Plan:
      type: object
      properties:
        name:
          type: string
          description: Plan name
        description:
          type: string
          description: Plan Description
    InstanceForm:
      type: object
      properties:
        name:
          type: string
          description: Instance name
        plan:
          type: string
          description: Plan name
        team:
          type: string
          description: Team name
        user:
          type: string
          description: User name
        tag:
          type: array
          items:
            type: string
      required:
      - name
      - plan
      - team
      - user
    BindUnitForm:
      type: object
      properties:
        app-host:
          type: string
          description: App host
        app-name:
          type: string
          description: App name
        unit-host:
          type: string
          description: Unit address
      required:
      - app-host
      - app-name
      - unit-host
    BindAppForm:
      type: object
      properties:
        app-host:
          type: string
          description: App host
        app-name:
          type: string
          description: App name
      required:
      - app-host
      - app-name
    Error:
      type: object
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
        fields:
          type: string
