Skip to content

Commit

Permalink
feat: add SQL Server support with new submodule (#112)
Browse files Browse the repository at this point in the history
Co-authored-by: Andriy Kopachevskyy <kopachevsky@gmail.com>
Co-authored-by: Morgante Pell <morgantep@google.com>
  • Loading branch information
3 people committed May 19, 2020
1 parent d433995 commit 4a775fb
Show file tree
Hide file tree
Showing 28 changed files with 1,334 additions and 122 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
terraform-google-sql makes it easy to create Google CloudSQL instance and implement high availability settings.
This module consists of the following submodules:

- [mssql](./modules/mssql)
- [mysql](./modules/mysql)
- [postgresql](./modules/postgresql)

Expand Down Expand Up @@ -34,6 +35,10 @@ The root module has been deprecated. Please switch to using one of the submodule
- [terraform](https://www.terraform.io/downloads.html) 0.12.x
- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v2.5.x

The following dependency must be available for SQL Server module:

- [Terraform Provider Beta for GCP](https://github.com/terraform-providers/terraform-provider-google-beta) plugin v3.10

### Configure a Service Account

In order to execute this module you must have a Service Account with the following:
Expand Down
284 changes: 165 additions & 119 deletions build/int.cloudbuild.yaml

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions examples/mssql-public/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Cloud MS SQL Server database Example

This example shows how create MS SQL Server database using the Terraform module.

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| name | The name for Cloud SQL instance | string | `"tf-mssql-public"` | no |
| project\_id | The project to run tests against | string | n/a | yes |
| region | | string | `"us-central1"` | no |

## Outputs

| Name | Description |
|------|-------------|
| instance\_name | |
| mssql\_connection | The connection name of the master instance to be used in connection strings |
| project\_id | |
| public\_ip\_address | Public ip address |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

## Run Terraform

```
terraform init
terraform plan
terraform apply
```

## Test connection to database

```bash
gcloud sql connect $(terraform output instance_name) --user=simpleuser
```
## Cleanup

Remove all resources created by terraform:

```bash
terraform destroy
```
41 changes: 41 additions & 0 deletions examples/mssql-public/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

provider "google-beta" {
version = "~> 3.1.0"
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"
}
33 changes: 33 additions & 0 deletions examples/mssql-public/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

output "project_id" {
value = var.project_id
}

output "instance_name" {
value = local.instance_name
}

output "mssql_connection" {
value = module.mssql.instance_connection_name
description = "The connection name of the master instance to be used in connection strings"
}

output "public_ip_address" {
value = module.mssql.instance_first_ip_address
description = "Public ip address"
}
31 changes: 31 additions & 0 deletions examples/mssql-public/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

variable "project_id" {
type = string
description = "The project to run tests against"
}

variable "name" {
type = string
description = "The name for Cloud SQL instance"
default = "tf-mssql-public"
}

variable "region" {
default = "us-central1"
type = string
}
19 changes: 19 additions & 0 deletions examples/mssql-public/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

terraform {
required_version = ">= 0.12"
}
10 changes: 10 additions & 0 deletions kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ suites:
name: terraform
root_module_directory: test/fixtures/mysql-private
command_timeout: 1800
- name: mssql-public
driver:
name: terraform
root_module_directory: test/fixtures/mssql-public
command_timeout: 1800
- name: mysql-ha
driver:
name: terraform
Expand All @@ -56,3 +61,8 @@ suites:
name: terraform
root_module_directory: test/fixtures/postgresql-ha
command_timeout: 1800
- name: mssql-ha
driver:
name: terraform
root_module_directory: test/fixtures/mssql-ha
command_timeout: 1800
59 changes: 59 additions & 0 deletions modules/mssql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# terraform-google-sql for MSSQL Server

The following dependency must be available for SQL Server module:

- [Terraform Provider Beta for GCP](https://github.com/terraform-providers/terraform-provider-google-beta) plugin v3.10

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| activation\_policy | The activation policy for the master instance.Can be either `ALWAYS`, `NEVER` or `ON_DEMAND`. | string | `"ALWAYS"` | no |
| additional\_databases | A list of databases to be created in your cluster | object | `<list>` | no |
| additional\_users | A list of users to be created in your cluster | object | `<list>` | no |
| authorized\_gae\_applications | The authorized gae applications for the Cloud SQL instances | list(string) | `<list>` | no |
| availability\_type | The availability type for the master instance.This is only used to set up high availability for the MSSQL instance. Can be either `ZONAL` or `REGIONAL`. | string | `"ZONAL"` | no |
| create\_timeout | The optional timout that is applied to limit long database creates. | string | `"15m"` | no |
| database\_flags | The database flags for the master instance. See [more details](https://cloud.google.com/sql/docs/sqlserver/flags) | object | `<list>` | no |
| database\_version | The database version to use: SQLSERVER_2017_STANDARD, SQLSERVER_2017_ENTERPRISE, SQLSERVER_2017_EXPRESS, or SQLSERVER_2017_WEB | string | `"SQLSERVER_2017_STANDARD"` | no |
| db\_charset | The charset for the default database | string | `""` | no |
| db\_collation | The collation for the default database. Example: 'en_US.UTF8' | string | `""` | no |
| db\_name | The name of the default database to create | string | `"default"` | no |
| delete\_timeout | The optional timout that is applied to limit long database deletes. | string | `"30m"` | no |
| disk\_autoresize | Configuration to increase storage size. | bool | `"true"` | no |
| disk\_size | The disk size for the master instance. | string | `"10"` | no |
| disk\_type | The disk type for the master instance. | string | `"PD_SSD"` | no |
| ip\_configuration | The ip configuration for the master instances. | object | `<map>` | no |
| maintenance\_window\_day | The day of week (1-7) for the master instance maintenance. | number | `"1"` | no |
| maintenance\_window\_hour | The hour of day (0-23) maintenance window for the master instance maintenance. | number | `"23"` | no |
| maintenance\_window\_update\_track | The update track of maintenance window for the master instance maintenance.Can be either `canary` or `stable`. | string | `"canary"` | no |
| name | The name of the Cloud SQL resources | string | n/a | yes |
| peering\_completed | Optional. This is used to ensure that resources are created in the proper order when using private IPs and service network peering. | string | `""` | no |
| 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 |
| 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 |
| update\_timeout | The optional timout that is applied to limit long database updates. | string | `"15m"` | no |
| user\_labels | The key/value labels for the master instances. | map(string) | `<map>` | no |
| user\_name | The name of the default user | string | `"default"` | no |
| user\_password | The password for the default user. If not set, a random one will be generated and available in the generated_user_password output variable. | string | `""` | no |
| zone | The zone for the master instance. | string | `"us-central1-a"` | no |

## Outputs

| Name | Description |
|------|-------------|
| generated\_user\_password | The auto generated default user password if not input password was provided |
| instance\_address | The IPv4 addesses assigned for the master instance |
| instance\_connection\_name | The connection name of the master instance to be used in connection strings |
| instance\_first\_ip\_address | The first IPv4 address of the addresses assigned. |
| instance\_name | The instance name for the master instance |
| instance\_self\_link | The URI of the master instance |
| instance\_server\_ca\_cert | The CA certificate information used to connect to the SQL instance via SSL |
| instance\_service\_account\_email\_address | The service account email address assigned to the master instance |
| private\_address | The private IP address assigned for the master instance |
| 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. |

<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

0 comments on commit 4a775fb

Please sign in to comment.