summaryrefslogtreecommitdiff
path: root/app/controllers/projects/google_cloud/databases_controller.rb
blob: 9c20f10809cf0bc9544050f65ccad3a297136758 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# frozen_string_literal: true

module Projects
  module GoogleCloud
    class DatabasesController < Projects::GoogleCloud::BaseController
      before_action :validate_gcp_token!
      before_action :validate_product, only: :new

      def index
        js_data = {
          configurationUrl: project_google_cloud_configuration_path(project),
          deploymentsUrl: project_google_cloud_deployments_path(project),
          databasesUrl: project_google_cloud_databases_path(project),
          cloudsqlPostgresUrl: new_project_google_cloud_database_path(project, :postgres),
          cloudsqlMysqlUrl: new_project_google_cloud_database_path(project, :mysql),
          cloudsqlSqlserverUrl: new_project_google_cloud_database_path(project, :sqlserver),
          cloudsqlInstances: ::GoogleCloud::GetCloudsqlInstancesService.new(project).execute,
          emptyIllustrationUrl: ActionController::Base.helpers.image_path('illustrations/pipelines_empty.svg')
        }

        @js_data = Gitlab::Json.dump(js_data)

        track_event(:render_page)
      end

      def new
        product = permitted_params[:product].to_sym

        @title = title(product)

        js_data = {
          gcpProjects: gcp_projects,
          refs: refs,
          cancelPath: project_google_cloud_databases_path(project),
          formTitle: form_title(product),
          formDescription: description(product),
          databaseVersions: Projects::GoogleCloud::CloudsqlHelper::VERSIONS[product],
          tiers: Projects::GoogleCloud::CloudsqlHelper::TIERS
        }

        @js_data = Gitlab::Json.dump(js_data)

        track_event(:render_form)
        render template: 'projects/google_cloud/databases/cloudsql_form', formats: :html
      end

      def create
        enable_response = ::GoogleCloud::EnableCloudsqlService
                            .new(project, current_user, enable_service_params)
                            .execute

        if enable_response[:status] == :error
          track_event(:error_enable_cloudsql_services)
          flash[:alert] = error_message(enable_response[:message])
        else
          create_response = ::GoogleCloud::CreateCloudsqlInstanceService
                              .new(project, current_user, create_service_params)
                              .execute

          if create_response[:status] == :error
            track_event(:error_create_cloudsql_instance)
            flash[:warning] = error_message(create_response[:message])
          else
            track_event(:create_cloudsql_instance, permitted_params_create.to_s)
            flash[:notice] = success_message
          end
        end

        redirect_to project_google_cloud_databases_path(project)
      end

      private

      def permitted_params_create
        params.permit(:gcp_project, :ref, :database_version, :tier)
      end

      def enable_service_params
        {
          google_oauth2_token: token_in_session,
          gcp_project_id: permitted_params_create[:gcp_project],
          environment_name: permitted_params_create[:ref]
        }
      end

      def create_service_params
        {
          google_oauth2_token: token_in_session,
          gcp_project_id: permitted_params_create[:gcp_project],
          environment_name: permitted_params_create[:ref],
          database_version: permitted_params_create[:database_version],
          tier: permitted_params_create[:tier]
        }
      end

      def error_message(message)
        format(s_("CloudSeed|Google Cloud Error - %{message}"), message: message)
      end

      def success_message
        s_('CloudSeed|Cloud SQL instance creation request successful. Expected resolution time is ~5 minutes.')
      end

      def validate_product
        not_found unless permitted_params[:product].in?(%w[postgres mysql sqlserver])
      end

      def permitted_params
        params.permit(:product)
      end

      def title(product)
        case product
        when :postgres
          s_('CloudSeed|Create Postgres Instance')
        when :mysql
          s_('CloudSeed|Create MySQL Instance')
        else
          s_('CloudSeed|Create MySQL Instance')
        end
      end

      def form_title(product)
        case product
        when :postgres
          s_('CloudSeed|Cloud SQL for Postgres')
        when :mysql
          s_('CloudSeed|Cloud SQL for MySQL')
        else
          s_('CloudSeed|Cloud SQL for SQL Server')
        end
      end

      def description(product)
        case product
        when :postgres
          s_('CloudSeed|Cloud SQL instances are fully managed, relational PostgreSQL databases. '\
             'Google handles replication, patch management, and database management '\
             'to ensure availability and performance.')
        when :mysql
          s_('Cloud SQL instances are fully managed, relational MySQL databases. '\
             'Google handles replication, patch management, and database management '\
             'to ensure availability and performance.')
        else
          s_('Cloud SQL instances are fully managed, relational SQL Server databases. ' \
             'Google handles replication, patch management, and database management ' \
             'to ensure availability and performance.')
        end
      end
    end
  end
end