openapi: 3.0.0
info:
  description: |
    This is the API for authn-tenants
    Authn-tenants is a sub-module of Authn that covers all tenants operations outside authn-admin members. Authn-tenants is an API designed to be used from outside. It will let cableoperadores users to create, update and delete tenants; and everything is simplified as posible.
  
  version: 1.0.2
  title: Tenants API
  contact:
    email: authn@masmovil.com

tags:
  - name: Tenants
    description: API for managing tenants CRUD operations

paths:
  /tenants:
    get:
      tags: [Tenants]
      description: Retrieves all tenants througth an public endpoint. The request doesn't need a JWT to work
      operationId: Get all tenants
      parameters:
      - name: bitmask
        in: query
        description:  Filtering tenants by a valid bitmask
        example: v1::1
        schema:
          type: string
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: "#/components/schemas/tenant"
        "400":
          description: Invalid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"
    post:
      security:
        - bearerAuth: []       
      tags: [Tenants]
      description: It creates a new Tenant in database. It should responde an id of the tenant created
      operationId: Adds a tenant
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/inboundTenant"
        description: Tenant to add
        required: true
      responses:
        "201":
          description: item created
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/createdTenant"
        "400":
          description: invalid input, object invalid
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"
        "409":
          description: tenant already exists
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"
        "500":
          description: Internal Server Error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"


  "/tenants/{tenantID}":
    get:
      tags: [Tenants]
      description: It retrieves a single tenant by it's id. This operation is public and passing a JWT is not mandatory
      operationId: Get tenant identified by id
      parameters:
        - in: path
          name: tenantID
          required: true
          schema:
            type: integer
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/tenant"
        "400":
          description: Invalid tenant id
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"
        "404":
          description: Tenant not found
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"
        "500":
          description: Internal Server Error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"
    put:
      security:
        - bearerAuth: []       
      tags: [Tenants]
      description: Update the name or/and the org of a tenant.
      operationId: Update tenant info identified by id
      parameters:
        - in: path
          name: tenantID
          required: true
          schema:
            type: integer
      requestBody:
        content:
          application/json:
            schema:
              $ref: "#/components/schemas/inboundTenant"
        required: true
      responses:
        "200":
          description: OK
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/tenant"
        "400":
          description: Invalid tenant
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"
        "404":
          description: Tenant not found
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"
        "500":
          description: Internal Server Error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"

    delete:
      security:
        - bearerAuth: []       
      tags: [Tenants]
      description: Delete tenant from database by it's id
      operationId: Delete tenant
      parameters:
        - in: path
          name: tenantID
          required: true
          schema:
            type: integer
      responses:
        "204":
          description: OK
        "400":
          description: Invalid tenant
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"
        "404":
          description: Tenant not found
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"

        "500":
          description: Internal Server Error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/goError"

servers:
  - url: https://authn-tenants.prod.k8s.masmovil.com/
    description: production
  - url: https://authn-tenants.sta.k8s.masmovil.com/
    description: staging

components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT
  schemas:
    inboundTenant:
      type: object
      required:
        - org
        - name
      properties:
        org:
          type: string
          format: uuid
          example: "123e4567-e89b-12d3-a456-426614174000"
        name:
          type: string
          example: J24352718
  
    tenant:
      type: object
      properties: 
        tenant_id:
          type: integer
          minimum: 0
          example: 16
        position_of_bit:
          type: integer
          example: 8
        org:
          type: string
          example: "guuk"
        name:
          type: string
          example: "guuk"
        iam:
          type: string
          example: "authz.guuk"
        tier:
          type: integer
          example: 3

    tenantPublic:
      type: object
      properties: 
        tenant_id:
          type: integer
          minimum: 0
          example: 16
        org:
          type: string
          example: "guuk"

    goError:
      type: object
      required:
        - id
        - status
      properties:
        id:
          type: string
          description: Unique identifier of error
          example: MM0000001
        status:
          type: integer
          description: Status code of the response
          example: 400
        msg:
          type: string
          description: Final user message
          example: Invalid inbound entity
        componentMsg:
          type: string
          description: Technical message
          example: EOF. Inbound tenant malformed
        sentryCode:
          type: string
          description: Unique identifier to use in sentry
          example: fake-req-id
    createdTenant:
      type: object
      properties:
        id:
          type: integer
          description: Internal id
          example: 1