Commit e3188eb1 authored by Thong Kuah's avatar Thong Kuah

Shift to class methods for RefreshService

As we don't use any instance attributes and we don't seem to have any
commonalities between the cluster and the project variant.
parent ba2d8a3f
......@@ -2,27 +2,31 @@
module Clusters
class RefreshService
def create_or_update_namespaces_for_cluster(cluster)
cluster_namespaces = cluster.kubernetes_namespaces
# Create all namespaces that are missing for each project
cluster.all_projects.missing_kubernetes_namespace(cluster_namespaces).each do |project|
def self.create_or_update_namespaces_for_cluster(cluster)
projects_with_missing_kubernetes_namespaces_for_cluster(cluster).each do |project|
create_or_update_namespace(cluster, project)
end
end
def create_or_update_namespaces_for_project(project)
project_namespaces = project.kubernetes_namespaces
# Create all namespaces that are missing for each cluster
project.all_clusters.missing_kubernetes_namespace(project_namespaces).each do |cluster|
def self.create_or_update_namespaces_for_project(project)
clusters_with_missing_kubernetes_namespaces_for_project(project).each do |cluster|
create_or_update_namespace(cluster, project)
end
end
private
def self.projects_with_missing_kubernetes_namespaces_for_cluster(cluster)
cluster.all_projects.missing_kubernetes_namespace(cluster.kubernetes_namespaces)
end
private_class_method :projects_with_missing_kubernetes_namespaces_for_cluster
def create_or_update_namespace(cluster, project)
def self.clusters_with_missing_kubernetes_namespaces_for_project(project)
project.all_clusters.missing_kubernetes_namespace(project.kubernetes_namespaces)
end
private_class_method :clusters_with_missing_kubernetes_namespaces_for_project
def self.create_or_update_namespace(cluster, project)
kubernetes_namespace = cluster.find_or_initialize_kubernetes_namespace_for_project(project)
::Clusters::Gcp::Kubernetes::CreateOrUpdateNamespaceService.new(
......@@ -30,5 +34,7 @@ module Clusters
kubernetes_namespace: kubernetes_namespace
).execute
end
private_class_method :create_or_update_namespace
end
end
......@@ -6,7 +6,7 @@ class ClusterPlatformConfigureWorker
def perform(cluster_id)
Clusters::Cluster.find_by_id(cluster_id).try do |cluster|
Clusters::RefreshService.new.create_or_update_namespaces_for_cluster(cluster)
Clusters::RefreshService.create_or_update_namespaces_for_cluster(cluster)
end
end
end
......@@ -7,6 +7,6 @@ class ClusterProjectConfigureWorker
def perform(project_id)
project = Project.find(project_id)
::Clusters::RefreshService.new.create_or_update_namespaces_for_project(project)
::Clusters::RefreshService.create_or_update_namespaces_for_project(project)
end
end
......@@ -29,11 +29,11 @@ describe Clusters::RefreshService do
end
end
describe '#create_or_update_namespaces_for_cluster' do
describe '.create_or_update_namespaces_for_cluster' do
let(:cluster) { create(:cluster, :provided_by_user, :project) }
let(:project) { cluster.project }
subject { described_class.new.create_or_update_namespaces_for_cluster(cluster) }
subject { described_class.create_or_update_namespaces_for_cluster(cluster) }
context 'cluster is project level' do
include_examples 'creates a kubernetes namespace'
......@@ -64,10 +64,10 @@ describe Clusters::RefreshService do
end
end
describe '#create_or_update_namespaces_for_project' do
describe '.create_or_update_namespaces_for_project' do
let(:project) { create(:project) }
subject { described_class.new.create_or_update_namespaces_for_project(project) }
subject { described_class.create_or_update_namespaces_for_project(project) }
it 'creates no kubernetes namespaces' do
expect { subject }.not_to change(project.kubernetes_namespaces, :count)
......
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