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