Commit 571db1a2 authored by Matija Čupić's avatar Matija Čupić

Return list of billing enabled projects

parent 17853757
class CheckGcpProjectBillingService
def execute(token)
client = GoogleApi::CloudPlatform::Client.new(token, nil)
client.projects_list.any? do |project|
client.projects_list.select do |project|
client.projects_get_billing_info(project.name).billingEnabled
end
end
......
......@@ -11,9 +11,9 @@ class CheckGcpProjectBillingWorker
return unless token
return unless try_obtain_lease_for(token)
billing_enabled = CheckGcpProjectBillingService.new.execute(token)
billing_enabled_projects = CheckGcpProjectBillingService.new.execute(token)
Gitlab::Redis::SharedState.with do |redis|
redis.set(self.class.redis_shared_state_key_for(token), billing_enabled)
redis.set(self.class.redis_shared_state_key_for(token), !billing_enabled_projects.empty?)
end
end
......
......@@ -2,13 +2,14 @@ require 'spec_helper'
describe CheckGcpProjectBillingService do
let(:service) { described_class.new }
let(:projects) { [double(name: 'first_project'), double(name: 'second_project')] }
describe '#execute' do
before do
expect_any_instance_of(GoogleApi::CloudPlatform::Client)
.to receive(:projects_list).and_return([double(name: 'project_name')])
.to receive(:projects_list).and_return(projects)
expect_any_instance_of(GoogleApi::CloudPlatform::Client)
allow_any_instance_of(GoogleApi::CloudPlatform::Client)
.to receive_message_chain(:projects_get_billing_info, :billingEnabled)
.and_return(project_billing_enabled)
end
......@@ -18,13 +19,13 @@ describe CheckGcpProjectBillingService do
context 'google account has a billing enabled gcp project' do
let(:project_billing_enabled) { true }
it { is_expected.to eq(true) }
it { is_expected.to eq(projects) }
end
context 'google account does not have a billing enabled gcp project' do
let(:project_billing_enabled) { false }
it { is_expected.to eq(false) }
it { is_expected.to eq([]) }
end
end
end
......@@ -11,7 +11,7 @@ describe CheckGcpProjectBillingWorker do
end
it 'calls the service' do
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute)
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double])
subject
end
......@@ -19,7 +19,7 @@ describe CheckGcpProjectBillingWorker do
it 'stores billing status in redis' do
redis_double = double
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return(true)
expect(CheckGcpProjectBillingService).to receive_message_chain(:new, :execute).and_return([double])
expect(Gitlab::Redis::SharedState).to receive(:with).and_yield(redis_double)
expect(redis_double).to receive(:set).with(described_class.redis_shared_state_key_for(token), anything)
......
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