Skip to content

Commit

Permalink
feat: Added random_id option for instance name (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantin-recurly committed May 28, 2020
1 parent 74ec0ab commit 7c8c799
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 4 deletions.
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
1 change: 1 addition & 0 deletions modules/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +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 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
8 changes: 7 additions & 1 deletion modules/mysql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ locals {
backups_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "enabled", null)
}

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/mysql/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/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
7 changes: 6 additions & 1 deletion modules/safer_mysql/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
resource "random_id" "suffix" {
count = var.random_instance_name ? 1 : 0

byte_length = 4
}

module "safer_mysql" {
source = "../mysql"
project_id = 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
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 7c8c799

Please sign in to comment.