Class: GoogleCloud::SetupCloudsqlInstanceService

Inherits:
BaseService show all
Defined in:
app/services/google_cloud/setup_cloudsql_instance_service.rb

Constant Summary collapse

INSTANCE_STATE_RUNNABLE =
'RUNNABLE'
OPERATION_STATE_DONE =
'DONE'
DEFAULT_DATABASE_NAME =
'main_db'
DEFAULT_DATABASE_USER =
'main_user'

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods inherited from BaseService

#initialize

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#executeObject



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
# File 'app/services/google_cloud/setup_cloudsql_instance_service.rb', line 10

def execute
  return error('Unauthorized user') unless Ability.allowed?(current_user, :admin_project_google_cloud, project)

  get_instance_response = google_api_client.get_cloudsql_instance(gcp_project_id, instance_name)

  if get_instance_response.state != INSTANCE_STATE_RUNNABLE
    return error("CloudSQL instance not RUNNABLE: #{Gitlab::Json.dump(get_instance_response)}")
  end

  save_instance_ci_vars(get_instance_response)

  list_database_response = google_api_client.list_cloudsql_databases(gcp_project_id, instance_name)
  list_user_response = google_api_client.list_cloudsql_users(gcp_project_id, instance_name)

  existing_database = list_database_response.items.find { |database| database.name == database_name }
  existing_user = list_user_response.items.find { |user| user.name == username }

  if existing_database && existing_user
    save_database_ci_vars
    save_user_ci_vars(existing_user)
    return success
  end

  database_response = execute_database_setup(existing_database)
  return database_response if database_response[:status] == :error

  save_database_ci_vars

  user_response = execute_user_setup(existing_user)
  return user_response if user_response[:status] == :error

  save_user_ci_vars(existing_user)

  success
rescue Google::Apis::Error => err
  error(message: Gitlab::Json.dump(err))
end