summaryrefslogtreecommitdiff
path: root/app/services/google_cloud/gcp_region_add_or_replace_service.rb
blob: 467f818bcc754ef371c62d0a83c2af5282eda0ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# frozen_string_literal: true

module GoogleCloud
  class GcpRegionAddOrReplaceService < ::BaseService
    def execute(environment, region)
      gcp_region_key = Projects::GoogleCloudController::GCP_REGION_CI_VAR_KEY

      change_params = { variable_params: { key: gcp_region_key, value: region, environment_scope: environment } }
      filter_params = { key: gcp_region_key, filter: { environment_scope: environment } }

      existing_variable = ::Ci::VariablesFinder.new(project, filter_params).execute.first

      if existing_variable
        change_params[:action] = :update
        change_params[:variable] = existing_variable
      else
        change_params[:action] = :create
      end

      ::Ci::ChangeVariableService.new(container: project, current_user: current_user, params: change_params).execute
    end
  end
end