summaryrefslogtreecommitdiff
path: root/app/controllers/projects/google_cloud/databases_controller.rb
blob: fb442f87666619a8ace99ced4e666325e7711367 (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
# 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 = js_data.to_json

        track_event('databases#index', 'success', nil)
      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
        }.to_json

        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('databases#cloudsql_create', 'error_enable_cloudsql_service', enable_response)
          flash[:error] = error_message(enable_response[:message])
        else
          permitted_params = params.permit(:gcp_project, :ref, :database_version, :tier)
          create_response = ::GoogleCloud::CreateCloudsqlInstanceService
                              .new(project, current_user, create_service_params(permitted_params))
                              .execute

          if create_response[:status] == :error
            track_event('databases#cloudsql_create', 'error_create_cloudsql_instance', create_response)
            flash[:warning] = error_message(create_response[:message])
          else
            track_event('databases#cloudsql_create', 'success', nil)
            flash[:notice] = success_message
          end
        end

        redirect_to project_google_cloud_databases_path(project)
      end

      private

      def enable_service_params
        { google_oauth2_token: token_in_session }
      end

      def create_service_params(permitted_params)
        {
          google_oauth2_token: token_in_session,
          gcp_project_id: permitted_params[:gcp_project],
          environment_name: permitted_params[:ref],
          database_version: permitted_params[:database_version],
          tier: permitted_params[: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