summaryrefslogtreecommitdiff
path: root/app/controllers/projects/serverless/functions_controller.rb
blob: 39eca10134fe0043540a0d6b7652a23e6e822220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true

module Projects
  module Serverless
    class FunctionsController < Projects::ApplicationController
      include ProjectUnauthorized

      before_action :authorize_read_cluster!

      INDEX_PRIMING_INTERVAL = 15_000
      INDEX_POLLING_INTERVAL = 60_000

      def index
        respond_to do |format|
          format.json do
            functions = finder.execute

            if functions.any?
              Gitlab::PollingInterval.set_header(response, interval: INDEX_POLLING_INTERVAL)
              render json: serialize_function(functions)
            else
              Gitlab::PollingInterval.set_header(response, interval: INDEX_PRIMING_INTERVAL)
              head :no_content
            end
          end

          format.html do
            @installed = finder.installed?
            render
          end
        end
      end

      def show
        @service = serialize_function(finder.service(params[:environment_id], params[:id]))
        return not_found if @service.nil?

        respond_to do |format|
          format.json do
            render json: @service
          end

          format.html
        end
      end

      private

      def finder
        Projects::Serverless::FunctionsFinder.new(project.clusters)
      end

      def serialize_function(function)
        Projects::Serverless::ServiceSerializer.new(current_user: @current_user, project: project).represent(function)
      end
    end
  end
end