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

Return list of billing enabled projects

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