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