Skip to content

Commit f2c91fa

Browse files
q2wimrannayer
andauthored
feat!: Add connection metadata, output and support for ml integration for Mysql module (#637)
Co-authored-by: Imran Nayer <[email protected]>
1 parent 99341e8 commit f2c91fa

File tree

18 files changed

+124
-25
lines changed

18 files changed

+124
-25
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ docker_test_lint:
7777
.PHONY: docker_generate_docs
7878
docker_generate_docs:
7979
docker run --rm -it \
80+
-e ENABLE_BPMETADATA \
8081
-v "$(CURDIR)":/workspace \
8182
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
8283
/bin/bash -c 'source /usr/local/bin/task_helper_functions.sh && generate_docs'

examples/mysql-public/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ terraform destroy
3131

3232
| Name | Description |
3333
|------|-------------|
34+
| env\_vars | Exported environment variables |
3435
| mysql\_conn | The connection name of the master instance to be used in connection strings |
3536
| mysql\_user\_pass | The password for the default user. If not set, a random one will be generated and available in the generated\_user\_password output variable. |
3637
| name | The name for Cloud SQL instance |

examples/mysql-public/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ output "private_ip_address" {
4545
value = module.mysql-db.private_ip_address
4646
}
4747

48+
output "env_vars" {
49+
value = module.mysql-db.env_vars
50+
description = "Exported environment variables"
51+
}

metadata.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ spec:
6868
location: examples/postgresql-public-iam
6969
- name: postgresql-with-cross-region-failover
7070
location: examples/postgresql-with-cross-region-failover
71+
- name: private_service_access
72+
location: examples/private_service_access
7173
interfaces: {}
7274
requirements:
7375
roles:

modules/backup/metadata.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ spec:
5757
location: examples/postgresql-public-iam
5858
- name: postgresql-with-cross-region-failover
5959
location: examples/postgresql-with-cross-region-failover
60+
- name: private_service_access
61+
location: examples/private_service_access
6062
interfaces:
6163
variables:
6264
- name: backup_monitoring_frequency

modules/mssql/metadata.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ spec:
5858
location: examples/postgresql-public-iam
5959
- name: postgresql-with-cross-region-failover
6060
location: examples/postgresql-with-cross-region-failover
61+
- name: private_service_access
62+
location: examples/private_service_access
6163
interfaces:
6264
variables:
6365
- name: activation_policy

modules/mysql/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Note: CloudSQL provides [disk autoresize](https://cloud.google.com/sql/docs/mysq
1616
| create\_timeout | The optional timout that is applied to limit long database creates. | `string` | `"30m"` | no |
1717
| data\_cache\_enabled | Whether data cache is enabled for the instance. Defaults to false. Feature is only available for ENTERPRISE\_PLUS tier and supported database\_versions | `bool` | `false` | no |
1818
| database\_flags | List of Cloud SQL flags that are applied to the database server. See [more details](https://cloud.google.com/sql/docs/mysql/flags) | <pre>list(object({<br> name = string<br> value = string<br> }))</pre> | `[]` | no |
19+
| database\_integration\_roles | The roles required by default database instance service account for integration with GCP services | `list(string)` | `[]` | no |
1920
| database\_version | The database version to use | `string` | n/a | yes |
2021
| db\_charset | The charset for the default database | `string` | `""` | no |
2122
| db\_collation | The collation for the default database. Example: 'utf8\_general\_ci' | `string` | `""` | no |
@@ -31,6 +32,7 @@ Note: CloudSQL provides [disk autoresize](https://cloud.google.com/sql/docs/mysq
3132
| edition | The edition of the instance, can be ENTERPRISE or ENTERPRISE\_PLUS. | `string` | `null` | no |
3233
| enable\_default\_db | Enable or disable the creation of the default database | `bool` | `true` | no |
3334
| enable\_default\_user | Enable or disable the creation of the default user | `bool` | `true` | no |
35+
| enable\_google\_ml\_integration | Enable database ML integration | `bool` | `false` | no |
3436
| enable\_random\_password\_special | Enable special characters in generated random passwords. | `bool` | `false` | no |
3537
| encryption\_key\_name | The full path to the encryption key used for the CMEK disk encryption | `string` | `null` | no |
3638
| follow\_gae\_application | A Google App Engine application whose zone to remain in. Must be in the same region as this instance. | `string` | `null` | no |
@@ -70,6 +72,7 @@ Note: CloudSQL provides [disk autoresize](https://cloud.google.com/sql/docs/mysq
7072
| Name | Description |
7173
|------|-------------|
7274
| additional\_users | List of maps of additional users and passwords |
75+
| env\_vars | Exported environment variables |
7376
| generated\_user\_password | The auto generated default user password if not input password was provided |
7477
| iam\_users | The list of the IAM users with access to the CloudSQL instance |
7578
| instance\_connection\_name | The connection name of the master instance to be used in connection strings |

modules/mysql/main.tf

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ locals {
4141

4242
// Force the usage of connector_enforcement
4343
connector_enforcement = var.connector_enforcement ? "REQUIRED" : "NOT_REQUIRED"
44+
45+
database_name = var.enable_default_db ? var.db_name : (length(var.additional_databases) > 0 ? var.additional_databases[0].name : "")
4446
}
4547

4648
resource "random_id" "suffix" {
@@ -62,12 +64,13 @@ resource "google_sql_database_instance" "default" {
6264
root_password = var.root_password == "" ? null : var.root_password
6365

6466
settings {
65-
tier = var.tier
66-
edition = var.edition
67-
activation_policy = var.activation_policy
68-
availability_type = var.availability_type
69-
deletion_protection_enabled = var.deletion_protection_enabled
70-
connector_enforcement = local.connector_enforcement
67+
tier = var.tier
68+
edition = var.edition
69+
activation_policy = var.activation_policy
70+
availability_type = var.availability_type
71+
deletion_protection_enabled = var.deletion_protection_enabled
72+
connector_enforcement = local.connector_enforcement
73+
enable_google_ml_integration = var.enable_google_ml_integration
7174

7275
dynamic "backup_configuration" {
7376
for_each = [var.backup_configuration]
@@ -308,6 +311,13 @@ resource "google_sql_user" "iam_account" {
308311
deletion_policy = var.user_deletion_policy
309312
}
310313

314+
resource "google_project_iam_member" "database_integration" {
315+
for_each = toset(var.database_integration_roles)
316+
project = var.project_id
317+
role = each.value
318+
member = "serviceAccount:${google_sql_database_instance.default.service_account_email_address}"
319+
}
320+
311321
resource "null_resource" "module_depends_on" {
312322
triggers = {
313323
value = length(var.module_depends_on)

modules/mysql/metadata.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ spec:
5858
location: examples/postgresql-public-iam
5959
- name: postgresql-with-cross-region-failover
6060
location: examples/postgresql-with-cross-region-failover
61+
- name: private_service_access
62+
location: examples/private_service_access
6163
interfaces:
6264
variables:
6365
- name: activation_policy
@@ -122,6 +124,10 @@ spec:
122124
value = string
123125
}))
124126
defaultValue: []
127+
- name: database_integration_roles
128+
description: The roles required by default database instance service account for integration with GCP services
129+
varType: list(string)
130+
defaultValue: []
125131
- name: database_version
126132
description: The database version to use
127133
varType: string
@@ -186,6 +192,10 @@ spec:
186192
description: Enable or disable the creation of the default user
187193
varType: bool
188194
defaultValue: true
195+
- name: enable_google_ml_integration
196+
description: Enable database ML integration
197+
varType: bool
198+
defaultValue: false
189199
- name: enable_random_password_special
190200
description: Enable special characters in generated random passwords.
191201
varType: bool
@@ -204,6 +214,17 @@ spec:
204214
email = string
205215
}))
206216
defaultValue: []
217+
connections:
218+
- source:
219+
source: github.com/terraform-google-modules/terraform-google-service-accounts//modules/simple-sa
220+
version: v4.3.0
221+
spec:
222+
outputExpr: id
223+
- source:
224+
source: github.com/GoogleCloudPlatform/terraform-google-cloud-run//modules/v2
225+
version: v0.13.0
226+
spec:
227+
outputExpr: service_account_id
207228
- name: insights_config
208229
description: The insights_config settings for the database.
209230
varType: |-
@@ -231,6 +252,13 @@ spec:
231252
psc_allowed_consumer_projects = optional(list(string), [])
232253
})
233254
defaultValue: {}
255+
connections:
256+
- source:
257+
source: github.com/terraform-google-modules/terraform-google-network//modules/vpc
258+
version: v9.1.0
259+
spec:
260+
outputExpr: network_id
261+
inputPath: private_network
234262
- name: maintenance_window_day
235263
description: The day of week (1-7) for the master instance maintenance.
236264
varType: number
@@ -377,6 +405,8 @@ spec:
377405
outputs:
378406
- name: additional_users
379407
description: List of maps of additional users and passwords
408+
- name: env_vars
409+
description: Exported environment variables
380410
- name: generated_user_password
381411
description: The auto generated default user password if not input password was provided
382412
- name: iam_users

modules/mysql/outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,12 @@ output "instances" {
145145
description = "A list of all `google_sql_database_instance` resources we've created"
146146
sensitive = true
147147
}
148+
149+
output "env_vars" {
150+
description = "Exported environment variables"
151+
value = {
152+
"CLOUD_SQL_DATABASE_HOST" : google_sql_database_instance.default.first_ip_address,
153+
"CLOUD_SQL_DATABASE_CONNECTION_NAME" : google_sql_database_instance.default.connection_name,
154+
"CLOUD_SQL_DATABASE_NAME" : local.database_name
155+
}
156+
}

0 commit comments

Comments
 (0)