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
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 ||=

View file

@ -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

View file

@ -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)