Skip to content

Commit

Permalink
Merge pull request #2 from konstantin-recurly/instance_name_random_id
Browse files Browse the repository at this point in the history
Instance name random
  • Loading branch information
konstantin-recurly authored May 28, 2020
2 parents 1a297b0 + c42b1f2 commit d6da8e2
Show file tree
Hide file tree
Showing 18 changed files with 81 additions and 92 deletions.
24 changes: 6 additions & 18 deletions examples/mssql-public/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,11 @@ provider "google-beta" {
region = var.region
}

resource "random_id" "instance_name_suffix" {
byte_length = 5
}

locals {
/*
Random instance name needed because:
"You cannot reuse an instance name for up to a week after you have deleted an instance."
See https://cloud.google.com/sql/docs/mysql/delete-instance for details.
*/
instance_name = "${var.name}-${random_id.instance_name_suffix.hex}"
}

module "mssql" {
source = "../../modules/mssql"
name = local.instance_name
project_id = var.project_id
user_name = "simpleuser"
user_password = "foobar"
source = "../../modules/mssql"
name = var.name
random_instance_name = true
project_id = var.project_id
user_name = "simpleuser"
user_password = "foobar"
}
24 changes: 6 additions & 18 deletions examples/mysql-ha/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,13 @@ provider "random" {
version = "~> 2.2"
}

resource "random_id" "instance_name_suffix" {
byte_length = 5
}

locals {
/*
Random instance name needed because:
"You cannot reuse an instance name for up to a week after you have deleted an instance."
See https://cloud.google.com/sql/docs/mysql/delete-instance for details.
*/
instance_name = "${var.mysql_ha_name}-${random_id.instance_name_suffix.hex}"
}

module "mysql" {
source = "../../modules/mysql"
name = local.instance_name
project_id = var.project_id
database_version = "MYSQL_5_7"
region = "us-central1"
source = "../../modules/mysql"
name = var.mysql_ha_name
random_instance_name = true
project_id = var.project_id
database_version = "MYSQL_5_7"
region = "us-central1"

// Master configurations
tier = "db-n1-standard-1"
Expand Down
10 changes: 5 additions & 5 deletions examples/mysql-private/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ locals {
"You cannot reuse an instance name for up to a week after you have deleted an instance."
See https://cloud.google.com/sql/docs/mysql/delete-instance for details.
*/
instance_name = "${var.db_name}-${random_id.suffix.hex}"
network_name = "${var.network_name}-safer-${random_id.suffix.hex}"
network_name = "${var.network_name}-safer-${random_id.suffix.hex}"
}

module "network-safer-mysql-simple" {
Expand All @@ -61,9 +60,10 @@ module "private-service-access" {
}

module "safer-mysql-db" {
source = "../../modules/safer_mysql"
name = local.instance_name
project_id = var.project_id
source = "../../modules/safer_mysql"
name = var.db_name
random_instance_name = true
project_id = var.project_id

database_version = "MYSQL_5_6"
region = "us-central1"
Expand Down
15 changes: 8 additions & 7 deletions examples/mysql-public/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ locals {
}

module "mysql-db" {
source = "../../modules/mysql"
name = local.instance_name
database_version = "MYSQL_5_6"
project_id = var.project_id
zone = "c"
region = "us-central1"
tier = "db-n1-standard-1"
source = "../../modules/mysql"
name = var.db_name
random_instance_name = true
database_version = "MYSQL_5_6"
project_id = var.project_id
zone = "c"
region = "us-central1"
tier = "db-n1-standard-1"

ip_configuration = {
ipv4_enabled = true
Expand Down
23 changes: 6 additions & 17 deletions examples/postgresql-ha/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,14 @@ provider "random" {
version = "~> 2.2"
}

resource "random_id" "instance_name_suffix" {
byte_length = 5
}

locals {
/*
Random instance name needed because:
"You cannot reuse an instance name for up to a week after you have deleted an instance."
See https://cloud.google.com/sql/docs/mysql/delete-instance for details.
*/
instance_name = "${var.pg_ha_name}-${random_id.instance_name_suffix.hex}"
}

module "pg" {
source = "../../modules/postgresql"
name = local.instance_name
project_id = var.project_id
database_version = "POSTGRES_9_6"
region = "us-central1"
source = "../../modules/postgresql"
name = var.pg_ha_name
random_instance_name = true
project_id = var.project_id
database_version = "POSTGRES_9_6"
region = "us-central1"

// Master configurations
tier = "db-custom-2-13312"
Expand Down
29 changes: 8 additions & 21 deletions examples/postgresql-public/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,15 @@ provider "random" {
version = "~> 2.2"
}

resource "random_id" "name" {
byte_length = 2
}


locals {
/*
Random instance name needed because:
"You cannot reuse an instance name for up to a week after you have deleted an instance."
See https://cloud.google.com/sql/docs/mysql/delete-instance for details.
*/
instance_name = "${var.db_name}-${random_id.name.hex}"
}

module "postgresql-db" {
source = "../../modules/postgresql"
name = local.instance_name
database_version = "POSTGRES_9_6"
project_id = var.project_id
zone = "c"
region = "us-central1"
tier = "db-f1-micro"
source = "../../modules/postgresql"
name = var.db_name
random_instance_name = true
database_version = "POSTGRES_9_6"
project_id = var.project_id
zone = "c"
region = "us-central1"
tier = "db-f1-micro"

ip_configuration = {
ipv4_enabled = true
Expand Down
1 change: 1 addition & 0 deletions modules/mssql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The following dependency must be available for SQL Server module:
| name | The name of the Cloud SQL resources | string | n/a | yes |
| pricing\_plan | The pricing plan for the master instance. | string | `"PER_USE"` | no |
| project\_id | The project ID to manage the Cloud SQL resources | string | n/a | yes |
| random\_instance\_name | Sets random suffix at the end of the Cloud SQL resource name | bool | `"false"` | no |
| region | The region of the Cloud SQL resources | string | `"us-central1"` | no |
| root\_password | MSSERVER password for the root user. If not set, a random one will be generated and available in the root_password output variable. | string | `""` | no |
| tier | The tier for the master instance. | string | `"db-custom-2-3840"` | no |
Expand Down
8 changes: 7 additions & 1 deletion modules/mssql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ locals {
users = { for u in var.additional_users : u.name => u }
}

resource "random_id" "suffix" {
count = var.random_instance_name ? 1 : 0

byte_length = 4
}

resource "random_password" "root-password" {
length = 8
special = true
Expand All @@ -34,7 +40,7 @@ resource "random_password" "root-password" {
resource "google_sql_database_instance" "default" {
provider = google-beta
project = var.project_id
name = var.name
name = var.random_instance_name ? "${var.name}-${random_id.suffix[0].hex}" : var.name
database_version = var.database_version
region = var.region
root_password = coalesce(var.root_password, random_password.root-password.result)
Expand Down
6 changes: 6 additions & 0 deletions modules/mssql/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ variable "name" {
description = "The name of the Cloud SQL resources"
}

variable "random_instance_name" {
type = bool
description = "Sets random suffix at the end of the Cloud SQL resource name"
default = false
}

// required
variable "database_version" {
description = "The database version to use: SQLSERVER_2017_STANDARD, SQLSERVER_2017_ENTERPRISE, SQLSERVER_2017_EXPRESS, or SQLSERVER_2017_WEB"
Expand Down
2 changes: 1 addition & 1 deletion modules/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
| name | The name of the Cloud SQL resources | string | n/a | yes |
| pricing\_plan | The pricing plan for the master instance. | string | `"PER_USE"` | no |
| project\_id | The project ID to manage the Cloud SQL resources | string | n/a | yes |
| random\_instance\_name | Sets random prefix at the end of the Cloud SQL resource name | bool | `"false"` | no |
| random\_instance\_name | Sets random suffix at the end of the Cloud SQL resource name | bool | `"false"` | no |
| read\_replica\_activation\_policy | The activation policy for the read replica instances. Can be either `ALWAYS`, `NEVER` or `ON_DEMAND`. | string | `"ALWAYS"` | no |
| read\_replica\_configuration | The replica configuration for use in all read replica instances. | object | `<map>` | no |
| read\_replica\_crash\_safe\_replication | The crash safe replication is to indicates when crash-safe replication flags are enabled. | bool | `"true"` | no |
Expand Down
6 changes: 4 additions & 2 deletions modules/mysql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ locals {
backups_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "enabled", null)
}

resource "random_id" "prefix" {
resource "random_id" "suffix" {
count = var.random_instance_name ? 1 : 0

byte_length = 4
}

resource "google_sql_database_instance" "default" {
provider = google-beta
project = var.project_id
name = var.random_instance_name ? "${var.name}-${random_id.prefix.hex}" : var.name
name = var.random_instance_name ? "${var.name}-${random_id.suffix[0].hex}" : var.name
database_version = var.database_version
region = var.region
encryption_key_name = var.encryption_key_name
Expand Down
2 changes: 1 addition & 1 deletion modules/mysql/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ variable "name" {

variable "random_instance_name" {
type = bool
description = "Sets random prefix at the end of the Cloud SQL resource name"
description = "Sets random suffix at the end of the Cloud SQL resource name"
default = false
}

Expand Down
1 change: 1 addition & 0 deletions modules/postgresql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
| name | The name of the Cloud SQL resources | string | n/a | yes |
| pricing\_plan | The pricing plan for the master instance. | string | `"PER_USE"` | no |
| project\_id | The project ID to manage the Cloud SQL resources | string | n/a | yes |
| random\_instance\_name | Sets random suffix at the end of the Cloud SQL resource name | bool | `"false"` | no |
| read\_replica\_activation\_policy | The activation policy for the read replica instances.Can be either `ALWAYS`, `NEVER` or `ON_DEMAND`. | string | `"ALWAYS"` | no |
| read\_replica\_availability\_type | The availability type for the read replica instances.This is only used to set up high availability for the PostgreSQL instances. Can be either `ZONAL` or `REGIONAL`. | string | `"ZONAL"` | no |
| read\_replica\_configuration | The replica configuration for use in all read replica instances. | object | `<map>` | no |
Expand Down
8 changes: 7 additions & 1 deletion modules/postgresql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ locals {
users = { for u in var.additional_users : u.name => u }
}

resource "random_id" "suffix" {
count = var.random_instance_name ? 1 : 0

byte_length = 4
}

resource "google_sql_database_instance" "default" {
provider = google-beta
project = var.project_id
name = var.name
name = var.random_instance_name ? "${var.name}-${random_id.suffix[0].hex}" : var.name
database_version = var.database_version
region = var.region
encryption_key_name = var.encryption_key_name
Expand Down
6 changes: 6 additions & 0 deletions modules/postgresql/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ variable "name" {
description = "The name of the Cloud SQL resources"
}

variable "random_instance_name" {
type = bool
description = "Sets random suffix at the end of the Cloud SQL resource name"
default = false
}

// required
variable "database_version" {
description = "The database version to use"
Expand Down
1 change: 1 addition & 0 deletions modules/safer_mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ mysql -S $HOME/mysql_sockets/myproject:region:instance -u user -p
| name | The name of the Cloud SQL resources | string | n/a | yes |
| pricing\_plan | The pricing plan for the master instance. | string | `"PER_USE"` | no |
| project\_id | The project ID to manage the Cloud SQL resources | string | n/a | yes |
| random\_instance\_name | Sets random suffix at the end of the Cloud SQL resource name | bool | `"false"` | no |
| read\_replica\_activation\_policy | The activation policy for the read replica instances. Can be either `ALWAYS`, `NEVER` or `ON_DEMAND`. | string | `"ALWAYS"` | no |
| read\_replica\_configuration | The replica configuration for use in all read replica instances. | object | `<map>` | no |
| read\_replica\_crash\_safe\_replication | The crash safe replication is to indicates when crash-safe replication flags are enabled. | bool | `"true"` | no |
Expand Down
1 change: 1 addition & 0 deletions modules/safer_mysql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module "safer_mysql" {
source = "../mysql"
project_id = var.project_id
name = var.name
random_instance_name = true
database_version = var.database_version
region = var.region
zone = var.zone
Expand Down
6 changes: 6 additions & 0 deletions modules/safer_mysql/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ variable "name" {
type = string
}

variable "random_instance_name" {
type = bool
description = "Sets random suffix at the end of the Cloud SQL resource name"
default = false
}

// required
variable "database_version" {
description = "The database version to use"
Expand Down

0 comments on commit d6da8e2

Please sign in to comment.