summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorMatija Čupić <matteeyah@gmail.com>2018-02-08 16:57:14 +0100
committerMatija Čupić <matteeyah@gmail.com>2018-02-12 21:32:55 +0100
commit0215435058ea9f9ebcc1b793425fccd22317e651 (patch)
tree8cfb0ca8442da31e4ed86726c72bd6b81cf39ee7 /app
parentbafab35e84e88f77d850ea13973d8fddc110225a (diff)
downloadgitlab-ce-0215435058ea9f9ebcc1b793425fccd22317e651.tar.gz
Refactor CheckGcpProjectBillingWorker
Diffstat (limited to 'app')
-rw-r--r--app/controllers/projects/clusters/gcp_controller.rb14
-rw-r--r--app/workers/check_gcp_project_billing_worker.rb43
2 files changed, 28 insertions, 29 deletions
diff --git a/app/controllers/projects/clusters/gcp_controller.rb b/app/controllers/projects/clusters/gcp_controller.rb
index 94d33b91562..0f41af7d87b 100644
--- a/app/controllers/projects/clusters/gcp_controller.rb
+++ b/app/controllers/projects/clusters/gcp_controller.rb
@@ -39,12 +39,12 @@ class Projects::Clusters::GcpController < Projects::ApplicationController
def verify_billing
case google_project_billing_status
- when 'true'
- return
- when 'false'
- flash[:alert] = _('Please <a href=%{link_to_billing} target="_blank" rel="noopener noreferrer">enable billing for one of your projects to be able to create a Kubernetes cluster</a>, then try again.').html_safe % { link_to_billing: "https://console.cloud.google.com/freetrial?utm_campaign=2018_cpanel&utm_source=gitlab&utm_medium=referral" }
- else
+ when nil
flash[:alert] = _('We could not verify that one of your projects on GCP has billing enabled. Please try again.')
+ when false
+ flash[:alert] = _('Please <a href=%{link_to_billing} target="_blank" rel="noopener noreferrer">enable billing for one of your projects to be able to create a Kubernetes cluster</a>, then try again.').html_safe % { link_to_billing: "https://console.cloud.google.com/freetrial?utm_campaign=2018_cpanel&utm_source=gitlab&utm_medium=referral" }
+ when true
+ return
end
@cluster = ::Clusters::Cluster.new(create_params)
@@ -81,9 +81,7 @@ class Projects::Clusters::GcpController < Projects::ApplicationController
end
def google_project_billing_status
- Gitlab::Redis::SharedState.with do |redis|
- redis.get(CheckGcpProjectBillingWorker.redis_shared_state_key_for(token_in_session))
- end
+ CheckGcpProjectBillingWorker.get_billing_state(token_in_session)
end
def token_in_session
diff --git a/app/workers/check_gcp_project_billing_worker.rb b/app/workers/check_gcp_project_billing_worker.rb
index c3a7929bd14..1a2894f6268 100644
--- a/app/workers/check_gcp_project_billing_worker.rb
+++ b/app/workers/check_gcp_project_billing_worker.rb
@@ -22,8 +22,11 @@ class CheckGcpProjectBillingWorker
end
end
- def self.redis_shared_state_key_for(token)
- "gitlab:gcp:#{Digest::SHA1.hexdigest(token)}:billing_enabled"
+ def self.get_billing_state(token)
+ Gitlab::Redis::SharedState.with do |redis|
+ value = redis.get(redis_shared_state_key_for(token))
+ ActiveRecord::Type::Boolean.new.type_cast_from_user(value)
+ end
end
def perform(token_key)
@@ -33,15 +36,9 @@ class CheckGcpProjectBillingWorker
return unless token
return unless try_obtain_lease_for(token)
- billing_enabled_projects = CheckGcpProjectBillingService.new.execute(token)
-
- update_billing_change_counter(check_previous_state(token), !billing_enabled_projects.empty?)
-
- Gitlab::Redis::SharedState.with do |redis|
- redis.set(self.class.redis_shared_state_key_for(token),
- !billing_enabled_projects.empty?,
- ex: BILLING_TIMEOUT)
- end
+ billing_enabled_state = !CheckGcpProjectBillingService.new.execute(token).empty?
+ update_billing_change_counter(self.class.get_billing_state(token), billing_enabled_state)
+ self.class.set_billing_state(token, billing_enabled_state)
end
private
@@ -54,11 +51,14 @@ class CheckGcpProjectBillingWorker
"gitlab:gcp:session:#{token_key}"
end
- def billing_changed_counter
- @billing_changed_counter ||= Gitlab::Metrics.counter(
- :gcp_billing_change_count,
- "Counts the number of times a GCP project changed billing_enabled state from false to true"
- )
+ def self.redis_shared_state_key_for(token)
+ "gitlab:gcp:#{Digest::SHA1.hexdigest(token)}:billing_enabled"
+ end
+
+ def self.set_billing_state(token, value)
+ Gitlab::Redis::SharedState.with do |redis|
+ redis.set(redis_shared_state_key_for(token), value, ex: BILLING_TIMEOUT)
+ end
end
def try_obtain_lease_for(token)
@@ -67,14 +67,15 @@ class CheckGcpProjectBillingWorker
.try_obtain
end
- def check_previous_state(token)
- Gitlab::Redis::SharedState.with do |redis|
- redis.get(self.class.redis_shared_state_key_for(token))
- end
+ def billing_changed_counter
+ @billing_changed_counter ||= Gitlab::Metrics.counter(
+ :gcp_billing_change_count,
+ "Counts the number of times a GCP project changed billing_enabled state from false to true"
+ )
end
def update_billing_change_counter(previous_state, current_state)
- return unless previous_state == 'false' && current_state
+ return unless !previous_state && current_state
billing_changed_counter.increment
end