Commit fe2d63fc authored by Matija Čupić's avatar Matija Čupić

Implement GCP billing check in cluster form

parent e6234503
class Projects::Clusters::GcpController < Projects::ApplicationController
before_action :authorize_read_cluster!
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]
STATUS_POLLING_INTERVAL = 1.minute.to_i
......@@ -25,15 +25,20 @@ class Projects::Clusters::GcpController < Projects::ApplicationController
end
def create
@cluster = ::Clusters::CreateService
.new(project, current_user, create_params)
.execute(token_in_session)
case google_project_billing_status
when 'true'
@cluster = ::Clusters::CreateService
.new(project, current_user, create_params)
.execute(token_in_session)
if @cluster.persisted?
redirect_to project_cluster_path(project, @cluster)
return redirect_to project_cluster_path(project, @cluster) if @cluster.persisted?
when 'false'
flash[:error] = _('Please enable billing for one of your projects to be able to create a cluster.')
else
render :new
flash[:error] = _('We could not verify that one of your projects on GCP has billing enabled. Please try again.')
end
render :new
end
private
......@@ -61,6 +66,15 @@ class Projects::Clusters::GcpController < Projects::ApplicationController
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
@token_in_session ||=
......
......@@ -77,6 +77,8 @@ describe Projects::Clusters::GcpController do
end
it 'has new object' do
expect(controller).to receive(:authorize_google_project_billing)
go
expect(assigns(:cluster)).to be_an_instance_of(Clusters::Cluster)
......@@ -137,7 +139,11 @@ describe Projects::Clusters::GcpController do
stub_google_api_validate_token
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
expect(ClusterProvisionWorker).to receive(:perform_async)
expect { go }.to change { Clusters::Cluster.count }
......@@ -147,6 +153,15 @@ describe Projects::Clusters::GcpController do
expect(project.clusters.first).to be_kubernetes
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
context 'when access token is expired' do
......
......@@ -13,7 +13,7 @@ module GoogleApi
def stub_google_project_billing_status
redis_double = 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
def stub_cloud_platform_get_zone_cluster(project_id, zone, cluster_id, **options)
......
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