summaryrefslogtreecommitdiff
path: root/app/serializers/environment_entity.rb
blob: d9af7af8a8be00243cbaa919b6c084b1039deb52 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# frozen_string_literal: true

class EnvironmentEntity < Grape::Entity
  include RequestAwareEntity

  expose :id
  expose :name
  expose :state
  expose :external_url
  expose :environment_type
  expose :name_without_type
  expose :last_deployment, using: DeploymentEntity
  expose :stop_action_available?, as: :has_stop_action

  expose :metrics_path, if: -> (*) { environment.has_metrics? } do |environment|
    metrics_project_environment_path(environment.project, environment)
  end

  expose :environment_path do |environment|
    project_environment_path(environment.project, environment)
  end

  expose :stop_path do |environment|
    stop_project_environment_path(environment.project, environment)
  end

  expose :cancel_auto_stop_path, if: -> (*) { can_update_environment? } do |environment|
    cancel_auto_stop_project_environment_path(environment.project, environment)
  end

  expose :cluster_type, if: ->(environment, _) { cluster_platform_kubernetes? } do |environment|
    cluster.cluster_type
  end

  expose :terminal_path, if: ->(*) { environment.has_terminals? && can_access_terminal? } do |environment|
    terminal_project_environment_path(environment.project, environment)
  end

  expose :folder_path do |environment|
    folder_project_environments_path(environment.project, environment.folder_name)
  end

  expose :created_at, :updated_at
  expose :auto_stop_at, expose_nil: false

  expose :can_stop do |environment|
    environment.available? && can?(current_user, :stop_environment, environment)
  end

  expose :logs_path, if: -> (*) { can_read_pod_logs? } do |environment|
    project_logs_path(environment.project, environment_name: environment.name)
  end

  expose :logs_api_path, if: -> (*) { can_read_pod_logs? } do |environment|
    if environment.elastic_stack_available?
      elasticsearch_project_logs_path(environment.project, environment_name: environment.name, format: :json)
    else
      k8s_project_logs_path(environment.project, environment_name: environment.name, format: :json)
    end
  end

  expose :enable_advanced_logs_querying, if: -> (*) { can_read_pod_logs? } do |environment|
    environment.elastic_stack_available?
  end

  private

  alias_method :environment, :object

  def current_user
    request.current_user
  end

  def can_access_terminal?
    can?(request.current_user, :create_environment_terminal, environment)
  end

  def can_update_environment?
    can?(current_user, :update_environment, environment)
  end

  def can_read_pod_logs?
    can?(current_user, :read_pod_logs, environment.project)
  end

  def cluster_platform_kubernetes?
    deployment_platform && deployment_platform.is_a?(Clusters::Platforms::Kubernetes)
  end

  def deployment_platform
    environment.deployment_platform
  end

  def cluster
    deployment_platform.cluster
  end
end

EnvironmentEntity.prepend_if_ee('::EE::EnvironmentEntity')