Commit edb89f55 authored by Dennis Tang's avatar Dennis Tang

split into gcp and user controllers again

parent 151b6b2b
import initGkeDropdowns from '~/projects/gke_cluster_dropdowns';
document.addEventListener('DOMContentLoaded', () => {
initGkeDropdowns();
});
...@@ -5,7 +5,11 @@ import ShortcutsNavigation from '../../shortcuts_navigation'; ...@@ -5,7 +5,11 @@ import ShortcutsNavigation from '../../shortcuts_navigation';
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const page = document.body.dataset.page; const page = document.body.dataset.page;
const newClusterViews = ['projects:clusters:new', 'projects:clusters:create']; const newClusterViews = [
'projects:clusters:new',
'projects:clusters:gcp:create',
'projects:clusters:user:create',
];
if (newClusterViews.indexOf(page) > -1) { if (newClusterViews.indexOf(page) > -1) {
gcpSignupOffer(); gcpSignupOffer();
......
class Projects::Clusters::GcpController < Projects::ApplicationController
include ClustersHelper
before_action :authorize_read_cluster!
before_action :authorize_create_cluster!, only: [:create]
helper_method :gcp_authorize_url
helper_method :token_in_session
helper_method :valid_gcp_token
def create
@cluster = ::Clusters::CreateService
.new(project, current_user, create_params)
.execute(token_in_session)
if @cluster.persisted?
redirect_to project_cluster_path(project, @cluster)
else
@gcp_cluster = @cluster
user_cluster
render 'projects/clusters/new', locals: { active_tab: 'gcp' }
end
end
private
def create_params
params.require(:cluster).permit(
:enabled,
:name,
:environment_scope,
provider_gcp_attributes: [
:gcp_project_id,
:zone,
:num_nodes,
:machine_type
]).merge(
provider_type: :gcp,
platform_type: :kubernetes
)
end
end
class Projects::Clusters::UserController < Projects::ApplicationController
include ClustersHelper
before_action :authorize_read_cluster!
before_action :authorize_create_cluster!, only: [:create]
helper_method :gcp_authorize_url
helper_method :token_in_session
helper_method :valid_gcp_token
def create
@cluster = ::Clusters::CreateService
.new(project, current_user, create_params)
.execute
if @cluster.persisted?
redirect_to project_cluster_path(project, @cluster)
else
@user_cluster = @cluster
gcp_cluster
render 'projects/clusters/new', locals: { active_tab: 'user' }
end
end
private
def create_params
params.require(:cluster).permit(
:enabled,
:name,
:environment_scope,
platform_kubernetes_attributes: [
:namespace,
:api_url,
:token,
:ca_cert
]).merge(
provider_type: :user,
platform_type: :kubernetes
)
end
end
class Projects::ClustersController < Projects::ApplicationController class Projects::ClustersController < Projects::ApplicationController
before_action :cluster, except: [:index, :new, :create] include ClustersHelper
before_action :cluster, except: [:index, :new]
before_action :authorize_read_cluster! before_action :authorize_read_cluster!
before_action :generate_gcp_authorize_url, only: [:new] before_action :gcp_cluster, only: [:new]
before_action :validate_gcp_token, only: [:new] before_action :user_cluster, only: [:new]
before_action :new_cluster, only: [:new]
before_action :existing_cluster, only: [:new]
before_action :authorize_create_cluster!, only: [:new] before_action :authorize_create_cluster!, only: [:new]
before_action :authorize_update_cluster!, only: [:update] before_action :authorize_update_cluster!, only: [:update]
before_action :authorize_admin_cluster!, only: [:destroy] before_action :authorize_admin_cluster!, only: [:destroy]
before_action :update_applications_status, only: [:status] before_action :update_applications_status, only: [:status]
helper_method :gcp_authorize_url
helper_method :token_in_session helper_method :token_in_session
helper_method :valid_gcp_token
STATUS_POLLING_INTERVAL = 10_000 STATUS_POLLING_INTERVAL = 10_000
...@@ -69,37 +70,6 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -69,37 +70,6 @@ class Projects::ClustersController < Projects::ApplicationController
end end
end end
def create
case params[:type]
when 'new'
cluster_params = create_new_cluster_params
when 'existing'
cluster_params = create_existing_cluster_params
end
@cluster = ::Clusters::CreateService
.new(project, current_user, cluster_params)
.execute(token_in_session)
if @cluster.persisted?
redirect_to project_cluster_path(project, @cluster)
else
generate_gcp_authorize_url
validate_gcp_token
case params[:type]
when 'new'
@new_cluster = @cluster
existing_cluster
when 'existing'
@existing_cluster = @cluster
new_cluster
end
render :new, locals: { active_tab: params[:type] }
end
end
private private
def cluster def cluster
...@@ -131,80 +101,6 @@ class Projects::ClustersController < Projects::ApplicationController ...@@ -131,80 +101,6 @@ class Projects::ClustersController < Projects::ApplicationController
end end
end end
def create_new_cluster_params
params.require(:cluster).permit(
:enabled,
:name,
:environment_scope,
provider_gcp_attributes: [
:gcp_project_id,
:zone,
:num_nodes,
:machine_type
]).merge(
provider_type: :gcp,
platform_type: :kubernetes
)
end
def create_existing_cluster_params
params.require(:cluster).permit(
:enabled,
:name,
:environment_scope,
platform_kubernetes_attributes: [
:namespace,
:api_url,
:token,
:ca_cert
]).merge(
provider_type: :user,
platform_type: :kubernetes
)
end
def generate_gcp_authorize_url
state = generate_session_key_redirect(new_project_cluster_path(@project).to_s)
@authorize_url = GoogleApi::CloudPlatform::Client.new(
nil, callback_google_api_auth_url,
state: state).authorize_url
rescue GoogleApi::Auth::ConfigMissingError
# no-op
end
def new_cluster
@new_cluster = ::Clusters::Cluster.new.tap do |cluster|
cluster.build_provider_gcp
end
end
def existing_cluster
@existing_cluster = ::Clusters::Cluster.new.tap do |cluster|
cluster.build_platform_kubernetes
end
end
def validate_gcp_token
@valid_gcp_token = GoogleApi::CloudPlatform::Client.new(token_in_session, nil)
.validate_token(expires_at_in_session)
end
def token_in_session
session[GoogleApi::CloudPlatform::Client.session_key_for_token]
end
def expires_at_in_session
@expires_at_in_session ||=
session[GoogleApi::CloudPlatform::Client.session_key_for_expires_at]
end
def generate_session_key_redirect(uri)
GoogleApi::CloudPlatform::Client.new_session_key_for_redirect_uri do |key|
session[key] = uri
end
end
def authorize_update_cluster! def authorize_update_cluster!
access_denied! unless can?(current_user, :update_cluster, cluster) access_denied! unless can?(current_user, :update_cluster, cluster)
end end
......
...@@ -10,4 +10,46 @@ module ClustersHelper ...@@ -10,4 +10,46 @@ module ClustersHelper
render 'projects/clusters/gcp_signup_offer_banner' render 'projects/clusters/gcp_signup_offer_banner'
end end
end end
def gcp_cluster
@gcp_cluster = ::Clusters::Cluster.new.tap do |cluster|
cluster.build_provider_gcp
end
end
def user_cluster
@user_cluster = ::Clusters::Cluster.new.tap do |cluster|
cluster.build_platform_kubernetes
end
end
def gcp_authorize_url
state = generate_session_key_redirect(new_project_cluster_path(@project).to_s)
GoogleApi::CloudPlatform::Client.new(
nil, callback_google_api_auth_url,
state: state).authorize_url
rescue GoogleApi::Auth::ConfigMissingError
# no-op
end
def generate_session_key_redirect(uri)
GoogleApi::CloudPlatform::Client.new_session_key_for_redirect_uri do |key|
session[key] = uri
end
end
def token_in_session
session[GoogleApi::CloudPlatform::Client.session_key_for_token]
end
def expires_at_in_session
@expires_at_in_session ||=
session[GoogleApi::CloudPlatform::Client.session_key_for_expires_at]
end
def valid_gcp_token
GoogleApi::CloudPlatform::Client.new(token_in_session, nil)
.validate_token(expires_at_in_session)
end
end end
...@@ -4,10 +4,10 @@ ...@@ -4,10 +4,10 @@
- link_to_help_page = link_to(s_('ClusterIntegration|help page'), help_page_path('user/project/clusters/index'), target: '_blank', rel: 'noopener noreferrer') - link_to_help_page = link_to(s_('ClusterIntegration|help page'), help_page_path('user/project/clusters/index'), target: '_blank', rel: 'noopener noreferrer')
= s_('ClusterIntegration|Read our %{link_to_help_page} on Kubernetes cluster integration.').html_safe % { link_to_help_page: link_to_help_page} = s_('ClusterIntegration|Read our %{link_to_help_page} on Kubernetes cluster integration.').html_safe % { link_to_help_page: link_to_help_page}
%p= link_to('Select a different Google account', @authorize_url) %p= link_to('Select a different Google account', gcp_authorize_url)
= form_for @new_cluster, html: { class: 'js-gke-cluster-creation prepend-top-20', data: { token: token_in_session } }, url: namespace_project_clusters_path(@project.namespace, @project, { type: 'new' }), as: :cluster do |field| = form_for @gcp_cluster, html: { class: 'js-gke-cluster-creation prepend-top-20', data: { token: token_in_session } }, url: gcp_namespace_project_clusters_path(@project.namespace, @project), as: :cluster do |field|
= form_errors(@new_cluster) = form_errors(@gcp_cluster)
.form-group .form-group
= field.label :name, s_('ClusterIntegration|Kubernetes cluster name') = field.label :name, s_('ClusterIntegration|Kubernetes cluster name')
= field.text_field :name, class: 'form-control', placeholder: s_('ClusterIntegration|Kubernetes cluster name') = field.text_field :name, class: 'form-control', placeholder: s_('ClusterIntegration|Kubernetes cluster name')
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
= field.label :environment_scope, s_('ClusterIntegration|Environment scope') = field.label :environment_scope, s_('ClusterIntegration|Environment scope')
= field.text_field :environment_scope, class: 'form-control', readonly: !has_multiple_clusters?(@project), placeholder: s_('ClusterIntegration|Environment scope') = field.text_field :environment_scope, class: 'form-control', readonly: !has_multiple_clusters?(@project), placeholder: s_('ClusterIntegration|Environment scope')
= field.fields_for :provider_gcp, @new_cluster.provider_gcp do |provider_gcp_field| = field.fields_for :provider_gcp, @gcp_cluster.provider_gcp do |provider_gcp_field|
.form-group .form-group
= provider_gcp_field.label :gcp_project_id, s_('ClusterIntegration|Google Cloud Platform project ID') = provider_gcp_field.label :gcp_project_id, s_('ClusterIntegration|Google Cloud Platform project ID')
.js-gcp-project-id-dropdown-entry-point{ data: { docsUrl: 'https://console.cloud.google.com/home/dashboard' } } .js-gcp-project-id-dropdown-entry-point{ data: { docsUrl: 'https://console.cloud.google.com/home/dashboard' } }
......
- breadcrumb_title 'Kubernetes' - breadcrumb_title 'Kubernetes'
- page_title _("Kubernetes Cluster") - page_title _("Kubernetes Cluster")
- active_tab = local_assigns.fetch(:active_tab, 'new') - active_tab = local_assigns.fetch(:active_tab, 'gcp')
= javascript_include_tag 'https://apis.google.com/js/api.js' = javascript_include_tag 'https://apis.google.com/js/api.js'
= render_gcp_signup_offer = render_gcp_signup_offer
.row.prepend-top-default .row.prepend-top-default
.col-md-3 .col-md-3
= render 'sidebar' = render 'projects/clusters/sidebar'
.col-md-9.js-toggle-container .col-md-9.js-toggle-container
%ul.nav-links.nav-tabs.gitlab-tabs.nav{ role: 'tablist' } %ul.nav-links.nav-tabs.gitlab-tabs.nav{ role: 'tablist' }
%li.nav-item{ role: 'presentation' } %li.nav-item{ role: 'presentation' }
%a.nav-link{ href: '#create-new-cluster-pane', id: 'create-new-cluster-tab', class: active_when(active_tab == 'new'), data: { toggle: 'tab' }, role: 'tab' } %a.nav-link{ href: '#create-gcp-cluster-pane', id: 'create-gcp-cluster-tab', class: active_when(active_tab == 'gcp'), data: { toggle: 'tab' }, role: 'tab' }
%span Create new Cluster on GKE %span Create new Cluster on GKE
%li.nav-item{ role: 'presentation' } %li.nav-item{ role: 'presentation' }
%a.nav-link{ href: '#add-existing-cluster-pane', id: 'add-existing-cluster-tab', class: active_when(active_tab == 'existing'), data: { toggle: 'tab' }, role: 'tab' } %a.nav-link{ href: '#add-user-cluster-pane', id: 'add-user-cluster-tab', class: active_when(active_tab == 'user'), data: { toggle: 'tab' }, role: 'tab' }
%span Add existing cluster %span Add existing cluster
.tab-content.gitlab-tab-content .tab-content.gitlab-tab-content
.tab-pane{ id: 'create-new-cluster-pane', class: active_when(active_tab == 'new'), role: 'tabpanel' } .tab-pane{ id: 'create-gcp-cluster-pane', class: active_when(active_tab == 'gcp'), role: 'tabpanel' }
= render 'projects/clusters/gcp/header' = render 'projects/clusters/gcp/header'
- if @valid_gcp_token - if valid_gcp_token
= render 'projects/clusters/gcp/form' = render 'projects/clusters/gcp/form'
- elsif @authorize_url - elsif gcp_authorize_url
.signin-with-google .signin-with-google
= link_to(image_tag('auth_buttons/signin_with_google.png', width: '191px'), @authorize_url) = link_to(image_tag('auth_buttons/signin_with_google.png', width: '191px'), gcp_authorize_url)
= _('or') = _('or')
= link_to('create a new Google account', 'https://accounts.google.com/SignUpWithoutGmail?service=cloudconsole&continue=https%3A%2F%2Fconsole.cloud.google.com%2Ffreetrial%3Futm_campaign%3D2018_cpanel%26utm_source%3Dgitlab%26utm_medium%3Dreferral', target: '_blank', rel: 'noopener noreferrer') = link_to('create a new Google account', 'https://accounts.google.com/SignUpWithoutGmail?service=cloudconsole&continue=https%3A%2F%2Fconsole.cloud.google.com%2Ffreetrial%3Futm_campaign%3D2018_cpanel%26utm_source%3Dgitlab%26utm_medium%3Dreferral', target: '_blank', rel: 'noopener noreferrer')
- else - else
- link = link_to(s_('ClusterIntegration|properly configured'), help_page_path("integration/google"), target: '_blank', rel: 'noopener noreferrer') - link = link_to(s_('ClusterIntegration|properly configured'), help_page_path("integration/google"), target: '_blank', rel: 'noopener noreferrer')
= s_('Google authentication is not %{link_to_documentation}. Ask your GitLab administrator if you want to use this service.').html_safe % { link_to_documentation: link } = s_('Google authentication is not %{link_to_documentation}. Ask your GitLab administrator if you want to use this service.').html_safe % { link_to_documentation: link }
.tab-pane{ id: 'add-existing-cluster-pane', class: active_when(active_tab == 'existing'), role: 'tabpanel' } .tab-pane{ id: 'add-user-cluster-pane', class: active_when(active_tab == 'user'), role: 'tabpanel' }
= render 'projects/clusters/user/header' = render 'projects/clusters/user/header'
= render 'projects/clusters/user/form' = render 'projects/clusters/user/form'
= form_for @existing_cluster, url: namespace_project_clusters_path(@project.namespace, @project, { type: 'existing' }), as: :cluster do |field| = form_for @user_cluster, url: user_namespace_project_clusters_path(@project.namespace, @project), as: :cluster do |field|
= form_errors(@existing_cluster) = form_errors(@user_cluster)
.form-group .form-group
= field.label :name, s_('ClusterIntegration|Kubernetes cluster name') = field.label :name, s_('ClusterIntegration|Kubernetes cluster name')
= field.text_field :name, class: 'form-control', placeholder: s_('ClusterIntegration|Kubernetes cluster name') = field.text_field :name, class: 'form-control', placeholder: s_('ClusterIntegration|Kubernetes cluster name')
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
= field.label :environment_scope, s_('ClusterIntegration|Environment scope') = field.label :environment_scope, s_('ClusterIntegration|Environment scope')
= field.text_field :environment_scope, class: 'form-control', readonly: !has_multiple_clusters?(@project), placeholder: s_('ClusterIntegration|Environment scope') = field.text_field :environment_scope, class: 'form-control', readonly: !has_multiple_clusters?(@project), placeholder: s_('ClusterIntegration|Environment scope')
= field.fields_for :platform_kubernetes, @existing_cluster.platform_kubernetes do |platform_kubernetes_field| = field.fields_for :platform_kubernetes, @user_cluster.platform_kubernetes do |platform_kubernetes_field|
.form-group .form-group
= platform_kubernetes_field.label :api_url, s_('ClusterIntegration|API URL') = platform_kubernetes_field.label :api_url, s_('ClusterIntegration|API URL')
= platform_kubernetes_field.text_field :api_url, class: 'form-control', placeholder: s_('ClusterIntegration|API URL') = platform_kubernetes_field.text_field :api_url, class: 'form-control', placeholder: s_('ClusterIntegration|API URL')
......
...@@ -204,7 +204,14 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do ...@@ -204,7 +204,14 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
end end
end end
resources :clusters, except: [:edit] do resources :clusters, except: [:edit, :create] do
collection do
scope :providers do
post '/user', to: 'clusters/user#create'
post '/gcp', to: 'clusters/gcp#create'
end
end
member do member do
get :status, format: :json get :status, format: :json
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment