debian-mirror-gitlab/app/serializers/environment_entity.rb

126 lines
3.6 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class EnvironmentEntity < Grape::Entity
include RequestAwareEntity
2021-02-22 17:27:13 +05:30
UNNECESSARY_ENTRIES_FOR_UPCOMING_DEPLOYMENT =
%i[manual_actions scheduled_actions playable_build cluster].freeze
2017-08-17 22:00:37 +05:30
expose :id
2021-01-29 00:20:46 +05:30
expose :global_id do |environment|
environment.to_global_id.to_s
end
2017-08-17 22:00:37 +05:30
expose :name
expose :state
expose :external_url
expose :environment_type
2019-07-07 11:18:12 +05:30
expose :name_without_type
2017-08-17 22:00:37 +05:30
expose :last_deployment, using: DeploymentEntity
2018-11-18 11:00:15 +05:30
expose :stop_action_available?, as: :has_stop_action
2021-03-08 18:12:59 +05:30
expose :rollout_status, if: -> (*) { can_read_deploy_board? }, using: RolloutStatusEntity
2017-08-17 22:00:37 +05:30
2021-02-22 17:27:13 +05:30
expose :upcoming_deployment, expose_nil: false do |environment, ops|
DeploymentEntity.represent(environment.upcoming_deployment,
ops.merge(except: UNNECESSARY_ENTRIES_FOR_UPCOMING_DEPLOYMENT))
end
2019-07-07 11:18:12 +05:30
expose :metrics_path, if: -> (*) { environment.has_metrics? } do |environment|
2017-09-10 17:25:29 +05:30
metrics_project_environment_path(environment.project, environment)
2017-08-17 22:00:37 +05:30
end
expose :environment_path do |environment|
2017-09-10 17:25:29 +05:30
project_environment_path(environment.project, environment)
2017-08-17 22:00:37 +05:30
end
expose :stop_path do |environment|
2017-09-10 17:25:29 +05:30
stop_project_environment_path(environment.project, environment)
2017-08-17 22:00:37 +05:30
end
2020-01-01 13:55:28 +05:30
expose :cancel_auto_stop_path, if: -> (*) { can_update_environment? } do |environment|
cancel_auto_stop_project_environment_path(environment.project, environment)
end
2020-04-22 19:07:51 +05:30
expose :delete_path do |environment|
environment_delete_path(environment)
end
2019-02-15 15:39:39 +05:30
expose :cluster_type, if: ->(environment, _) { cluster_platform_kubernetes? } do |environment|
cluster.cluster_type
end
2018-11-20 20:47:30 +05:30
expose :terminal_path, if: ->(*) { environment.has_terminals? && can_access_terminal? } do |environment|
terminal_project_environment_path(environment.project, environment)
2017-09-10 17:25:29 +05:30
end
expose :folder_path do |environment|
folder_project_environments_path(environment.project, environment.folder_name)
2017-08-17 22:00:37 +05:30
end
expose :created_at, :updated_at
2020-01-01 13:55:28 +05:30
expose :auto_stop_at, expose_nil: false
2018-11-18 11:00:15 +05:30
expose :can_stop do |environment|
environment.available? && can?(current_user, :stop_environment, environment)
end
2020-04-08 14:13:33 +05:30
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
2020-04-22 19:07:51 +05:30
expose :can_delete do |environment|
can?(current_user, :destroy_environment, environment)
end
2018-11-18 11:00:15 +05:30
private
2018-11-20 20:47:30 +05:30
alias_method :environment, :object
2018-11-18 11:00:15 +05:30
def current_user
request.current_user
end
2018-11-20 20:47:30 +05:30
def can_access_terminal?
can?(request.current_user, :create_environment_terminal, environment)
end
2019-02-15 15:39:39 +05:30
2020-01-01 13:55:28 +05:30
def can_update_environment?
can?(current_user, :update_environment, environment)
end
2020-04-08 14:13:33 +05:30
def can_read_pod_logs?
can?(current_user, :read_pod_logs, environment.project)
end
2021-03-08 18:12:59 +05:30
def can_read_deploy_board?
can?(current_user, :read_deploy_board, environment.project)
end
2019-02-15 15:39:39 +05:30
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
2017-08-17 22:00:37 +05:30
end
2019-12-04 20:38:33 +05:30
EnvironmentEntity.prepend_if_ee('::EE::EnvironmentEntity')