gitlab-org--gitlab-foss/app/controllers/projects/clusters_controller.rb

123 lines
2.9 KiB
Ruby
Raw Normal View History

class Projects::ClustersController < Projects::ApplicationController
2017-12-03 09:00:33 -05:00
before_action :cluster, except: [:index, :new]
before_action :authorize_read_cluster!
before_action :authorize_create_cluster!, only: [:new]
before_action :authorize_update_cluster!, only: [:update]
before_action :authorize_admin_cluster!, only: [:destroy]
before_action :update_applications_status, only: [:status]
2017-12-03 15:34:00 -05:00
STATUS_POLLING_INTERVAL = 10_000
def index
clusters = ClustersFinder.new(project, current_user, :all).execute
@clusters = clusters.page(params[:page]).per(20)
end
2017-12-03 09:00:33 -05:00
def new
end
def status
respond_to do |format|
format.json do
2017-12-04 08:21:23 -05:00
Gitlab::PollingInterval.set_header(response, interval: STATUS_POLLING_INTERVAL)
2017-10-03 17:21:54 -04:00
render json: ClusterSerializer
.new(project: @project, current_user: @current_user)
.represent_status(@cluster)
end
end
end
def show
end
def update
2017-10-23 04:36:35 -04:00
Clusters::UpdateService
.new(project, current_user, update_params)
.execute(cluster)
2017-10-04 15:13:01 -04:00
if cluster.valid?
2017-11-24 08:43:00 -05:00
respond_to do |format|
format.json do
head :no_content
end
format.html do
flash[:notice] = _('Kubernetes cluster was successfully updated.')
redirect_to project_cluster_path(project, cluster)
2017-11-24 08:43:00 -05:00
end
end
2017-10-04 15:13:01 -04:00
else
2017-11-24 08:43:00 -05:00
respond_to do |format|
format.json { head :bad_request }
format.html { render :show }
end
2017-10-04 15:13:01 -04:00
end
end
2017-09-27 09:17:41 -04:00
def destroy
if cluster.destroy
flash[:notice] = _('Kubernetes cluster integration was successfully removed.')
redirect_to project_clusters_path(project), status: 302
else
flash[:notice] = _('Kubernetes cluster integration was not removed.')
render :show
end
2017-09-27 09:17:41 -04:00
end
private
def cluster
2017-12-05 04:02:15 -05:00
@cluster ||= project.clusters.find(params[:id])
.present(current_user: current_user)
end
def create_params
params.require(:cluster).permit(
:enabled,
2017-10-23 04:36:35 -04:00
:name,
:provider_type,
2017-10-23 04:36:35 -04:00
provider_gcp_attributes: [
:gcp_project_id,
:zone,
:num_nodes,
:machine_type
])
end
2017-09-26 04:46:09 -04:00
def update_params
2017-12-03 09:10:18 -05:00
if cluster.managed?
params.require(:cluster).permit(
:enabled,
:environment_scope,
2017-12-03 09:10:18 -05:00
platform_kubernetes_attributes: [
:namespace
]
)
else
params.require(:cluster).permit(
:enabled,
:name,
:environment_scope,
2017-12-03 09:10:18 -05:00
platform_kubernetes_attributes: [
:api_url,
:token,
:ca_cert,
:namespace
2017-12-03 09:33:10 -05:00
]
2017-12-03 09:10:18 -05:00
)
end
end
def authorize_update_cluster!
2017-10-03 17:21:54 -04:00
access_denied! unless can?(current_user, :update_cluster, cluster)
end
def authorize_admin_cluster!
2017-10-03 17:21:54 -04:00
access_denied! unless can?(current_user, :admin_cluster, cluster)
end
def update_applications_status
@cluster.applications.each(&:schedule_status_update)
end
end