Implement GCP billing check in cluster form

This commit is contained in:
Matija Čupić 2017-12-21 19:25:28 +01:00
parent 59c7f46e2a
commit e395a2c190
No known key found for this signature in database
GPG key ID: 4BAF84FFACD2E5DE
3 changed files with 38 additions and 9 deletions

View file

@ -1,7 +1,7 @@
class Projects::Clusters::GcpController < Projects::ApplicationController class Projects::Clusters::GcpController < Projects::ApplicationController
before_action :authorize_read_cluster! before_action :authorize_read_cluster!
before_action :authorize_google_api, except: [:login] before_action :authorize_google_api, except: [:login]
before_action :authorize_google_project_billing, except: [:login, :check, :run_check] before_action :authorize_google_project_billing, only: [:new]
before_action :authorize_create_cluster!, only: [:new, :create] before_action :authorize_create_cluster!, only: [:new, :create]
STATUS_POLLING_INTERVAL = 1.minute.to_i STATUS_POLLING_INTERVAL = 1.minute.to_i
@ -25,15 +25,20 @@ class Projects::Clusters::GcpController < Projects::ApplicationController
end end
def create def create
@cluster = ::Clusters::CreateService case google_project_billing_status
.new(project, current_user, create_params) when 'true'
.execute(token_in_session) @cluster = ::Clusters::CreateService
.new(project, current_user, create_params)
.execute(token_in_session)
if @cluster.persisted? return redirect_to project_cluster_path(project, @cluster) if @cluster.persisted?
redirect_to project_cluster_path(project, @cluster) when 'false'
flash[:error] = _('Please enable billing for one of your projects to be able to create a cluster.')
else else
render :new flash[:error] = _('We could not verify that one of your projects on GCP has billing enabled. Please try again.')
end end
render :new
end end
private private
@ -61,6 +66,15 @@ class Projects::Clusters::GcpController < Projects::ApplicationController
end end
end end
def authorize_google_project_billing
CheckGcpProjectBillingWorker.perform_async(token_in_session)
end
def google_project_billing_status
Gitlab::Redis::SharedState.with do |redis|
redis.get(CheckGcpProjectBillingWorker.redis_shared_state_key_for(token_in_session))
end
end
def token_in_session def token_in_session
@token_in_session ||= @token_in_session ||=

View file

@ -77,6 +77,8 @@ describe Projects::Clusters::GcpController do
end end
it 'has new object' do it 'has new object' do
expect(controller).to receive(:authorize_google_project_billing)
go go
expect(assigns(:cluster)).to be_an_instance_of(Clusters::Cluster) expect(assigns(:cluster)).to be_an_instance_of(Clusters::Cluster)
@ -137,7 +139,11 @@ describe Projects::Clusters::GcpController do
stub_google_api_validate_token stub_google_api_validate_token
end end
context 'when creates a cluster on gke' do context 'when google project billing is enabled' do
before do
stub_google_project_billing_status
end
it 'creates a new cluster' do it 'creates a new cluster' do
expect(ClusterProvisionWorker).to receive(:perform_async) expect(ClusterProvisionWorker).to receive(:perform_async)
expect { go }.to change { Clusters::Cluster.count } expect { go }.to change { Clusters::Cluster.count }
@ -147,6 +153,15 @@ describe Projects::Clusters::GcpController do
expect(project.clusters.first).to be_kubernetes expect(project.clusters.first).to be_kubernetes
end end
end end
context 'when google project billing is not enabled' do
it 'renders the cluster form with an error' do
go
expect(response).to set_flash[:error]
expect(response).to render_template('new')
end
end
end end
context 'when access token is expired' do context 'when access token is expired' do

View file

@ -13,7 +13,7 @@ module GoogleApi
def stub_google_project_billing_status def stub_google_project_billing_status
redis_double = double redis_double = double
allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double) allow(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
allow(redis_double).to receive(:get).with(CheckGcpProjectBillingWorker.redis_shared_state_key_for).and_return('true') allow(redis_double).to receive(:get).with(CheckGcpProjectBillingWorker.redis_shared_state_key_for('token')).and_return('true')
end end
def stub_cloud_platform_get_zone_cluster(project_id, zone, cluster_id, **options) def stub_cloud_platform_get_zone_cluster(project_id, zone, cluster_id, **options)