Welcome to mirror list, hosted at ThFree Co, Russian Federation.

create_cloudsql_instance_service.rb « google_cloud « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f7fca277c52557f4494437cb9b473bab4b61174d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# frozen_string_literal: true

module GoogleCloud
  DEFAULT_REGION = 'us-east1'

  class CreateCloudsqlInstanceService < ::GoogleCloud::BaseService
    WORKER_INTERVAL = 30.seconds

    def execute
      create_cloud_instance
      trigger_instance_setup_worker
      success
    rescue Google::Apis::Error => err
      error(err.to_json)
    end

    private

    def create_cloud_instance
      google_api_client.create_cloudsql_instance(gcp_project_id,
                                                 instance_name,
                                                 root_password,
                                                 database_version,
                                                 region,
                                                 tier)
    end

    def trigger_instance_setup_worker
      GoogleCloud::CreateCloudsqlInstanceWorker.perform_in(WORKER_INTERVAL,
                                                           current_user.id,
                                                           project.id,
                                                           {
                                                             'google_oauth2_token': google_oauth2_token,
                                                             'gcp_project_id': gcp_project_id,
                                                             'instance_name': instance_name,
                                                             'database_version': database_version,
                                                             'environment_name': environment_name,
                                                             'is_protected': protected?
                                                           })
    end

    def protected?
      project.protected_for?(environment_name)
    end

    def instance_name
      # Generates an `instance_name` for the to-be-created Cloud SQL instance
      # Example: `gitlab-34647-postgres-14-staging`
      environment_alias = environment_name == '*' ? 'ALL' : environment_name
      name = "gitlab-#{project.id}-#{database_version}-#{environment_alias}"
      name.tr("_", "-").downcase
    end

    def root_password
      SecureRandom.hex(16)
    end

    def database_version
      params[:database_version]
    end

    def region
      region = ::Ci::VariablesFinder
                 .new(project, { key: Projects::GoogleCloud::GcpRegionsController::GCP_REGION_CI_VAR_KEY,
                                 environment_scope: environment_name })
                 .execute.first
      region&.value || DEFAULT_REGION
    end

    def tier
      params[:tier]
    end
  end
end