diff options
author | Dennis Tang <dtang@gitlab.com> | 2018-06-05 20:04:40 -0700 |
---|---|---|
committer | Dennis Tang <dtang@gitlab.com> | 2018-06-05 20:04:40 -0700 |
commit | edb89f555b25aa4d4aad7a3bdf5a0bd75bd076fc (patch) | |
tree | b186f1b139edc5fa71d7bd3c9469761380fa4c41 | |
parent | 151b6b2b2b60debccdf09eda5dbe9be31cc395f6 (diff) | |
download | gitlab-ce-edb89f555b25aa4d4aad7a3bdf5a0bd75bd076fc.tar.gz |
split into gcp and user controllers again
-rw-r--r-- | app/assets/javascripts/pages/projects/clusters/gcp/new/index.js | 5 | ||||
-rw-r--r-- | app/assets/javascripts/pages/projects/index.js | 6 | ||||
-rw-r--r-- | app/controllers/projects/clusters/gcp_controller.rb | 41 | ||||
-rw-r--r-- | app/controllers/projects/clusters/user_controller.rb | 42 | ||||
-rw-r--r-- | app/controllers/projects/clusters_controller.rb | 116 | ||||
-rw-r--r-- | app/helpers/clusters_helper.rb | 42 | ||||
-rw-r--r-- | app/views/projects/clusters/gcp/_form.html.haml | 8 | ||||
-rw-r--r-- | app/views/projects/clusters/new.html.haml | 18 | ||||
-rw-r--r-- | app/views/projects/clusters/user/_form.html.haml | 6 | ||||
-rw-r--r-- | config/routes/project.rb | 9 |
10 files changed, 160 insertions, 133 deletions
diff --git a/app/assets/javascripts/pages/projects/clusters/gcp/new/index.js b/app/assets/javascripts/pages/projects/clusters/gcp/new/index.js deleted file mode 100644 index d4f34e32a48..00000000000 --- a/app/assets/javascripts/pages/projects/clusters/gcp/new/index.js +++ /dev/null @@ -1,5 +0,0 @@ -import initGkeDropdowns from '~/projects/gke_cluster_dropdowns'; - -document.addEventListener('DOMContentLoaded', () => { - initGkeDropdowns(); -}); diff --git a/app/assets/javascripts/pages/projects/index.js b/app/assets/javascripts/pages/projects/index.js index ba8bc5bc5fa..7bbba9acb38 100644 --- a/app/assets/javascripts/pages/projects/index.js +++ b/app/assets/javascripts/pages/projects/index.js @@ -5,7 +5,11 @@ import ShortcutsNavigation from '../../shortcuts_navigation'; document.addEventListener('DOMContentLoaded', () => { 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) { gcpSignupOffer(); diff --git a/app/controllers/projects/clusters/gcp_controller.rb b/app/controllers/projects/clusters/gcp_controller.rb new file mode 100644 index 00000000000..ac9a906fe47 --- /dev/null +++ b/app/controllers/projects/clusters/gcp_controller.rb @@ -0,0 +1,41 @@ +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 diff --git a/app/controllers/projects/clusters/user_controller.rb b/app/controllers/projects/clusters/user_controller.rb new file mode 100644 index 00000000000..9da6a60e6b3 --- /dev/null +++ b/app/controllers/projects/clusters/user_controller.rb @@ -0,0 +1,42 @@ +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 + diff --git a/app/controllers/projects/clusters_controller.rb b/app/controllers/projects/clusters_controller.rb index 1e8f3ac1433..38140fe9f27 100644 --- a/app/controllers/projects/clusters_controller.rb +++ b/app/controllers/projects/clusters_controller.rb @@ -1,15 +1,16 @@ 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 :generate_gcp_authorize_url, only: [:new] - before_action :validate_gcp_token, only: [:new] - before_action :new_cluster, only: [:new] - before_action :existing_cluster, only: [:new] + before_action :gcp_cluster, only: [:new] + before_action :user_cluster, only: [:new] 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] + helper_method :gcp_authorize_url helper_method :token_in_session + helper_method :valid_gcp_token STATUS_POLLING_INTERVAL = 10_000 @@ -69,37 +70,6 @@ class Projects::ClustersController < Projects::ApplicationController 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 def cluster @@ -131,80 +101,6 @@ class Projects::ClustersController < Projects::ApplicationController 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! access_denied! unless can?(current_user, :update_cluster, cluster) end diff --git a/app/helpers/clusters_helper.rb b/app/helpers/clusters_helper.rb index c24d340d184..e1be917f298 100644 --- a/app/helpers/clusters_helper.rb +++ b/app/helpers/clusters_helper.rb @@ -10,4 +10,46 @@ module ClustersHelper render 'projects/clusters/gcp_signup_offer_banner' 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 diff --git a/app/views/projects/clusters/gcp/_form.html.haml b/app/views/projects/clusters/gcp/_form.html.haml index 4a73ac24072..a6589cca529 100644 --- a/app/views/projects/clusters/gcp/_form.html.haml +++ b/app/views/projects/clusters/gcp/_form.html.haml @@ -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') = 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_errors(@new_cluster) += 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(@gcp_cluster) .form-group = field.label :name, s_('ClusterIntegration|Kubernetes cluster name') = field.text_field :name, class: 'form-control', placeholder: s_('ClusterIntegration|Kubernetes cluster name') @@ -15,7 +15,7 @@ = 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.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 = 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' } } diff --git a/app/views/projects/clusters/new.html.haml b/app/views/projects/clusters/new.html.haml index b322ad9a844..11b7c363553 100644 --- a/app/views/projects/clusters/new.html.haml +++ b/app/views/projects/clusters/new.html.haml @@ -1,36 +1,36 @@ - breadcrumb_title 'Kubernetes' - 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' = render_gcp_signup_offer .row.prepend-top-default .col-md-3 - = render 'sidebar' + = render 'projects/clusters/sidebar' .col-md-9.js-toggle-container %ul.nav-links.nav-tabs.gitlab-tabs.nav{ role: 'tablist' } %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 %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 .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' - - if @valid_gcp_token + - if valid_gcp_token = render 'projects/clusters/gcp/form' - - elsif @authorize_url + - elsif gcp_authorize_url .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') = 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 - 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 } - .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/form' diff --git a/app/views/projects/clusters/user/_form.html.haml b/app/views/projects/clusters/user/_form.html.haml index bf1fd1f8898..4167c8787ff 100644 --- a/app/views/projects/clusters/user/_form.html.haml +++ b/app/views/projects/clusters/user/_form.html.haml @@ -1,5 +1,5 @@ -= form_for @existing_cluster, url: namespace_project_clusters_path(@project.namespace, @project, { type: 'existing' }), as: :cluster do |field| - = form_errors(@existing_cluster) += form_for @user_cluster, url: user_namespace_project_clusters_path(@project.namespace, @project), as: :cluster do |field| + = form_errors(@user_cluster) .form-group = field.label :name, s_('ClusterIntegration|Kubernetes cluster name') = field.text_field :name, class: 'form-control', placeholder: s_('ClusterIntegration|Kubernetes cluster name') @@ -7,7 +7,7 @@ = 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.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 = 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') diff --git a/config/routes/project.rb b/config/routes/project.rb index 7df2d4abb75..f672291a9f1 100644 --- a/config/routes/project.rb +++ b/config/routes/project.rb @@ -204,7 +204,14 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do 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 get :status, format: :json |