From 7af2161ca50484fea77ef816dc98ba66ff848681 Mon Sep 17 00:00:00 2001
From: Kamil Trzcinski <ayufan@ayufan.eu>
Date: Sun, 3 Dec 2017 15:00:33 +0100
Subject: [PATCH] Fix controllers and links

---
 app/controllers/projects/clusters/gcp_controller.rb | 10 ++++++++--
 app/controllers/projects/clusters_controller.rb     |  6 +++++-
 app/models/clusters/cluster.rb                      |  9 ++++++---
 app/views/projects/clusters/_dropdown.html.haml     |  4 ++--
 app/views/projects/clusters/gcp/_form.html.haml     |  2 +-
 app/views/projects/clusters/show.html.haml          |  2 +-
 app/views/projects/clusters/user/_form.html.haml    |  2 +-
 app/views/projects/clusters/user/_show.html.haml    | 10 +++++-----
 config/routes/project.rb                            | 10 ++++++----
 9 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/app/controllers/projects/clusters/gcp_controller.rb b/app/controllers/projects/clusters/gcp_controller.rb
index 54eef2426a5..5e909a57345 100644
--- a/app/controllers/projects/clusters/gcp_controller.rb
+++ b/app/controllers/projects/clusters/gcp_controller.rb
@@ -2,8 +2,6 @@ class Projects::Clusters::GcpController < Projects::ApplicationController
   before_action :authorize_read_cluster!
   before_action :authorize_google_api, except: [:login]
   before_action :authorize_create_cluster!, only: [:new, :create]
-  before_action :authorize_update_cluster!, only: [:update]
-  before_action :authorize_admin_cluster!, only: [:destroy]
 
   def login
     begin
@@ -52,6 +50,14 @@ class Projects::Clusters::GcpController < Projects::ApplicationController
       )
   end
 
+  def update_params
+    params.require(:cluster).permit(
+      :enabled,
+      platform_kubernetes_attributes: [
+        :namespace
+      ])
+  end
+
   def authorize_google_api
     unless GoogleApi::CloudPlatform::Client.new(token_in_session, nil)
                                            .validate_token(expires_at_in_session)
diff --git a/app/controllers/projects/clusters_controller.rb b/app/controllers/projects/clusters_controller.rb
index def7342b262..f451d4c9774 100644
--- a/app/controllers/projects/clusters_controller.rb
+++ b/app/controllers/projects/clusters_controller.rb
@@ -1,5 +1,5 @@
 class Projects::ClustersController < Projects::ApplicationController
-  before_action :cluster, except: [:login, :index, :new]
+  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]
@@ -13,6 +13,9 @@ class Projects::ClustersController < Projects::ApplicationController
     end
   end
 
+  def new
+  end
+
   def status
     respond_to do |format|
       format.json do
@@ -60,6 +63,7 @@ class Projects::ClustersController < Projects::ApplicationController
   def update_params
     params.require(:cluster).permit(
       :enabled,
+      :name,
       platform_kubernetes_attributes: [
         :namespace,
         :api_url,
diff --git a/app/models/clusters/cluster.rb b/app/models/clusters/cluster.rb
index e13e367fb92..441cbcb701d 100644
--- a/app/models/clusters/cluster.rb
+++ b/app/models/clusters/cluster.rb
@@ -17,7 +17,7 @@ module Clusters
     # we force autosave to happen when we save `Cluster` model
     has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp', autosave: true
 
-    has_one :platform_kubernetes, class_name: 'Clusters::Platforms::Kubernetes'
+    has_one :platform_kubernetes, class_name: 'Clusters::Platforms::Kubernetes', autosave: true
 
     has_one :application_helm, class_name: 'Clusters::Applications::Helm'
     has_one :application_ingress, class_name: 'Clusters::Applications::Ingress'
@@ -28,8 +28,6 @@ module Clusters
     validates :name, cluster_name: true
     validate :restrict_modification, on: :update
 
-    validates_associated :provider_gcp, :platform_kubernetes
-
     delegate :status, to: :provider, allow_nil: true
     delegate :status_reason, to: :provider, allow_nil: true
     delegate :on_creation?, to: :provider, allow_nil: true
@@ -95,6 +93,11 @@ module Clusters
         return false
       end
 
+      if managed? && name_changed?
+        errors.add(:base, "cannot modify cluster name")
+        return false
+      end
+
       true
     end
   end
diff --git a/app/views/projects/clusters/_dropdown.html.haml b/app/views/projects/clusters/_dropdown.html.haml
index 12d68ea98ea..39188c7ca27 100644
--- a/app/views/projects/clusters/_dropdown.html.haml
+++ b/app/views/projects/clusters/_dropdown.html.haml
@@ -7,6 +7,6 @@
     = icon('chevron-down')
   %ul.dropdown-menu.clusters-dropdown-menu.dropdown-menu-full-width
     %li
-      = link_to(s_('ClusterIntegration|Create cluster on Google Container Engine'), new_namespace_project_gcp_path(@project.namespace, @project))
+      = link_to(s_('ClusterIntegration|Create cluster on Google Container Engine'), gcp_new_namespace_project_clusters_path(@project.namespace, @project))
     %li
-      = link_to(s_('ClusterIntegration|Add an existing cluster'), new_namespace_project_user_path(@project.namespace, @project))
+      = link_to(s_('ClusterIntegration|Add an existing cluster'), user_new_namespace_project_clusters_path(@project.namespace, @project))
diff --git a/app/views/projects/clusters/gcp/_form.html.haml b/app/views/projects/clusters/gcp/_form.html.haml
index 6085a4c0e28..3bc1abefc8f 100644
--- a/app/views/projects/clusters/gcp/_form.html.haml
+++ b/app/views/projects/clusters/gcp/_form.html.haml
@@ -3,7 +3,7 @@
   - 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 cluster integration.').html_safe % { link_to_help_page: link_to_help_page}
 
-= form_for @cluster, html: { class: 'prepend-top-20' }, url: namespace_project_gcp_path(@project.namespace, @project), as: :cluster do |field|
+= form_for @cluster, html: { class: 'prepend-top-20' }, url: gcp_namespace_project_clusters_path(@project.namespace, @project), as: :cluster do |field|
   = form_errors(@cluster)
   .form-group
     = field.label :name, s_('ClusterIntegration|Cluster name')
diff --git a/app/views/projects/clusters/show.html.haml b/app/views/projects/clusters/show.html.haml
index b201eadd1fb..d73324b8863 100644
--- a/app/views/projects/clusters/show.html.haml
+++ b/app/views/projects/clusters/show.html.haml
@@ -69,7 +69,7 @@
       - if @cluster.managed?
         = render 'projects/clusters/gcp/show'
       - else
-        = render 'projects/clusters/kubernetes/show'
+        = render 'projects/clusters/user/show'
 
   %section.settings.no-animate#js-cluster-advanced-settings{ class: ('expanded' if expanded) }
     .settings-header
diff --git a/app/views/projects/clusters/user/_form.html.haml b/app/views/projects/clusters/user/_form.html.haml
index 0884cc0b4c3..4a9bd5186c6 100644
--- a/app/views/projects/clusters/user/_form.html.haml
+++ b/app/views/projects/clusters/user/_form.html.haml
@@ -1,4 +1,4 @@
-= form_for @cluster, url: namespace_project_user_path(@project.namespace, @project), as: :cluster do |field|
+= form_for @cluster, url: user_namespace_project_clusters_path(@project.namespace, @project), as: :cluster do |field|
   = form_errors(@cluster)
   .form-group
     = field.label :name, s_('ClusterIntegration|Cluster name')
diff --git a/app/views/projects/clusters/user/_show.html.haml b/app/views/projects/clusters/user/_show.html.haml
index 72c5417e9b2..afdea391de0 100644
--- a/app/views/projects/clusters/user/_show.html.haml
+++ b/app/views/projects/clusters/user/_show.html.haml
@@ -1,10 +1,10 @@
-.form-group
-  %label.append-bottom-10{ for: 'cluster-name' }
-    = s_('ClusterIntegration|Cluster name')
-  %input.form-control.cluster-name{ value: @cluster.name }
-
 = form_for @cluster, url: namespace_project_cluster_path(@project.namespace, @project, @cluster), as: :cluster do |field|
   = form_errors(@cluster)
+
+  .form-group
+    = field.label :name, s_('ClusterIntegration|Cluster name')
+    = field.text_field :name, class: 'form-control', placeholder: s_('ClusterIntegration|Cluster name')
+
   = field.fields_for :platform_kubernetes, @cluster.platform_kubernetes do |platform_kubernetes_field|
     .form-group
       = platform_kubernetes_field.label :api_url, s_('ClusterIntegration|API URL')
diff --git a/config/routes/project.rb b/config/routes/project.rb
index 2ca5bfca1e7..45474034822 100644
--- a/config/routes/project.rb
+++ b/config/routes/project.rb
@@ -186,10 +186,12 @@ constraints(ProjectUrlConstrainer.new) do
       resources :clusters, except: [:edit, :create] do
         collection do
           scope :providers do
-            resource :user, only: [:new, :create], controller: 'clusters/user'
-            resource :gcp, only: [:new, :create], controller: 'clusters/gcp' do
-              get :login
-            end
+            get '/user/new', to: 'clusters/user#new'
+            post '/user', to: 'clusters/user#create'
+
+            get '/gcp/new', to: 'clusters/gcp#new'
+            get '/gcp/login', to: 'clusters/gcp#login'
+            post '/gcp', to: 'clusters/gcp#create'
           end
         end
 
-- 
2.30.9