| 1 |
# Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
# you may not use this file except in compliance with the License. |
| 5 |
# You may obtain a copy of the License at |
| 6 |
# |
| 7 |
# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
# See the License for the specific language governing permissions and |
| 13 |
# limitations under the License. |
| 14 |
|
| 15 |
# Harness for MySQL tests. |
| 16 |
|
| 17 |
terraform { |
| 18 |
required_version = "~>1.13.2" |
| 19 |
required_providers { |
| 20 |
google = { |
| 21 |
source = "hashicorp/google" |
| 22 |
version = "7.9.0" |
| 23 |
} |
| 24 |
random = { |
| 25 |
source = "random" |
| 26 |
version = "3.7.2" |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
provider "google" { |
| 32 |
project = var.project |
| 33 |
} |
| 34 |
|
| 35 |
variable "project" { |
| 36 |
type = string |
| 37 |
description = "Project to set up." |
| 38 |
} |
| 39 |
|
| 40 |
variable "region" { |
| 41 |
default = "us-central1" |
| 42 |
description = "GCP region to create database and storage in, for example 'us-central1'. See https://cloud.google.com/compute/docs/regions-zones/ for valid values." |
| 43 |
} |
| 44 |
|
| 45 |
locals { |
| 46 |
sql_instance = "go-cloud-test-${random_id.sql_instance.hex}" |
| 47 |
} |
| 48 |
|
| 49 |
resource "google_project_service" "sql" { |
| 50 |
service = "sql-component.googleapis.com" |
| 51 |
disable_on_destroy = false |
| 52 |
} |
| 53 |
|
| 54 |
resource "google_project_service" "sqladmin" { |
| 55 |
service = "sqladmin.googleapis.com" |
| 56 |
disable_on_destroy = false |
| 57 |
} |
| 58 |
|
| 59 |
resource "random_id" "sql_instance" { |
| 60 |
keepers = { |
| 61 |
project = var.project |
| 62 |
region = var.region |
| 63 |
} |
| 64 |
|
| 65 |
byte_length = 12 |
| 66 |
} |
| 67 |
|
| 68 |
resource "google_sql_database_instance" "main" { |
| 69 |
name = local.sql_instance |
| 70 |
database_version = "MYSQL_5_6" |
| 71 |
region = var.region |
| 72 |
project = var.project |
| 73 |
deletion_protection = false |
| 74 |
settings { |
| 75 |
tier = "db-f1-micro" |
| 76 |
disk_size = 10 # GiB |
| 77 |
} |
| 78 |
|
| 79 |
depends_on = [ |
| 80 |
google_project_service.sql, |
| 81 |
google_project_service.sqladmin, |
| 82 |
] |
| 83 |
} |
| 84 |
|
| 85 |
resource "google_sql_database" "main" { |
| 86 |
name = "testdb" |
| 87 |
instance = google_sql_database_instance.main.name |
| 88 |
} |
| 89 |
|
| 90 |
resource "random_string" "db_password" { |
| 91 |
keepers = { |
| 92 |
project = var.project |
| 93 |
db_name = local.sql_instance |
| 94 |
region = var.region |
| 95 |
} |
| 96 |
|
| 97 |
special = false |
| 98 |
length = 20 |
| 99 |
} |
| 100 |
|
| 101 |
resource "google_sql_user" "root" { |
| 102 |
name = "root" |
| 103 |
instance = google_sql_database_instance.main.name |
| 104 |
password = random_string.db_password.result |
| 105 |
} |
| 106 |
|
| 107 |
output "project" { |
| 108 |
value = var.project |
| 109 |
description = "The GCP project ID." |
| 110 |
} |
| 111 |
|
| 112 |
output "region" { |
| 113 |
value = var.region |
| 114 |
description = "The Cloud SQL instance region." |
| 115 |
} |
| 116 |
|
| 117 |
output "instance" { |
| 118 |
value = local.sql_instance |
| 119 |
description = "The Cloud SQL instance region." |
| 120 |
} |
| 121 |
|
| 122 |
output "username" { |
| 123 |
value = "root" |
| 124 |
description = "The Cloud SQL username to connect with." |
| 125 |
} |
| 126 |
|
| 127 |
output "password" { |
| 128 |
value = random_string.db_password.result |
| 129 |
sensitive = true |
| 130 |
description = "The Cloud SQL instance password for the user." |
| 131 |
} |
| 132 |
|
| 133 |
output "database" { |
| 134 |
value = "testdb" |
| 135 |
description = "The name of the database inside the Cloud SQL instance." |
| 136 |
} |
| 137 |
|