Skip to content

Commit 5768e3c

Browse files
committed
feat: support create vpc if not specified
1 parent 6a864f5 commit 5768e3c

File tree

8 files changed

+66
-81
lines changed

8 files changed

+66
-81
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ No modules.
4949

5050
| Name | Type |
5151
|------|------|
52+
| [google_compute_global_address.default](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_global_address) | resource |
53+
| [google_compute_network.default](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_network) | resource |
54+
| [google_service_networking_connection.default](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/service_networking_connection) | resource |
5255
| [google_sql_database.database](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database) | resource |
5356
| [google_sql_database_instance.primary](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance) | resource |
5457
| [google_sql_database_instance.secondary](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/sql_database_instance) | resource |
@@ -64,7 +67,7 @@ No modules.
6467
| <a name="input_context"></a> [context](#input\_context) | Receive contextual information. When Walrus deploys, Walrus will inject specific contextual information into this field.<br><br>Examples:<pre>context:<br> project:<br> name: string<br> id: string<br> environment:<br> name: string<br> id: string<br> resource:<br> name: string<br> id: string</pre> | `map(any)` | `{}` | no |
6568
| <a name="input_database"></a> [database](#input\_database) | Specify the database name. The database name must be 1-60 characters long and start with any lower letter, combined with number, or symbols: - \_.<br>The database name cannot be PostgreSQL forbidden keyword. | `string` | `"mydb"` | no |
6669
| <a name="input_engine_version"></a> [engine\_version](#input\_engine\_version) | Specify the deployment engine version, select from https://cloud.google.com/sql/docs/db-versions. | `string` | `"15.0"` | no |
67-
| <a name="input_infrastructure"></a> [infrastructure](#input\_infrastructure) | Specify the infrastructure information for deploying.<br><br>Examples:<pre>infrastructure:<br> vpc_id: string # the ID of the VPC where the PostgreSQL service applies. It is a fully-qualified resource name, such as projects/{project_id}/global/networks/{network_id}.</pre> | <pre>object({<br> vpc_id = string<br> })</pre> | n/a | yes |
70+
| <a name="input_infrastructure"></a> [infrastructure](#input\_infrastructure) | Specify the infrastructure information for deploying.<br><br>Examples:<pre>infrastructure:<br> vpc_id: string, optional # the ID of the VPC where the PostgreSQL service applies. It is a fully-qualified resource name, such as projects/{project_id}/global/networks/{network_id}.</pre> | <pre>object({<br> vpc_id = optional(string)<br> })</pre> | `{}` | no |
6871
| <a name="input_password"></a> [password](#input\_password) | Specify the account password. The password must be 8-128 characters long and start with any letter, number, or symbols: ! # $ % ^ & * ( ) \_ + - =.<br>If not specified, it will generate a random password. | `string` | `null` | no |
6972
| <a name="input_replication_readonly_replicas"></a> [replication\_readonly\_replicas](#input\_replication\_readonly\_replicas) | Specify the number of read-only replicas under the replication deployment. | `number` | `1` | no |
7073
| <a name="input_resources"></a> [resources](#input\_resources) | Specify the computing resources.<br>The computing resource design of Google Cloud is very complex, it also needs to consider on the storage resource, please view the specification document for more information.<br><br>Examples:<pre>resources:<br> class: string, optional # https://cloud.google.com/sql/docs/postgres/instance-settings</pre> | <pre>object({<br> class = optional(string, "db-f1-micro")<br> })</pre> | <pre>{<br> "class": "db-f1-micro"<br>}</pre> | no |

examples/replication/README.md

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ $ terraform apply -auto-approve
1717

1818
## Providers
1919

20-
| Name | Version |
21-
|------|---------|
22-
| <a name="provider_google"></a> [google](#provider\_google) | >= 3.5.0 |
20+
No providers.
2321

2422
## Modules
2523

@@ -29,12 +27,7 @@ $ terraform apply -auto-approve
2927

3028
## Resources
3129

32-
| Name | Type |
33-
|------|------|
34-
| [google_compute_firewall.private_network_firewall](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_firewall) | resource |
35-
| [google_compute_global_address.private_ip_address](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_global_address) | resource |
36-
| [google_compute_network.private_network](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_network) | resource |
37-
| [google_service_networking_connection.private_vpc_connection](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/service_networking_connection) | resource |
30+
No resources.
3831

3932
## Inputs
4033

examples/replication/main.tf

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,16 @@ terraform {
1616
provider "google" {
1717
}
1818

19-
// create network.
20-
resource "google_compute_network" "private_network" {
21-
name = "private-network"
22-
}
23-
24-
resource "google_compute_firewall" "private_network_firewall" {
25-
name = "private-network-firewall-policy"
26-
network = google_compute_network.private_network.id
27-
allow {
28-
protocol = "tcp"
29-
ports = ["22"]
30-
}
31-
32-
source_ranges = ["0.0.0.0/0"]
33-
34-
depends_on = [google_compute_network.private_network]
35-
}
36-
37-
resource "google_compute_global_address" "private_ip_address" {
38-
name = "private-ip-address"
39-
purpose = "VPC_PEERING"
40-
address_type = "INTERNAL"
41-
prefix_length = 16
42-
network = google_compute_network.private_network.id
43-
}
44-
45-
// create private vpc connection.
46-
resource "google_service_networking_connection" "private_vpc_connection" {
47-
network = google_compute_network.private_network.id
48-
service = "servicenetworking.googleapis.com"
49-
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
50-
deletion_policy = "ABANDON"
51-
}
52-
5319
# create postgresql service.
5420

5521
module "this" {
5622
source = "../.."
5723

58-
infrastructure = {
59-
vpc_id = google_compute_network.private_network.id
60-
}
61-
6224
architecture = "replication"
6325
database = "mydb"
6426
username = "rdsuser"
6527

66-
depends_on = [google_service_networking_connection.private_vpc_connection]
28+
replication_readonly_replicas = 3
6729
}
6830

6931
output "context" {

examples/standalone/README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
Deploy PostgreSQL service in standalone architecture by root moudle.
44

55
```bash
6+
# setup infrastructure
7+
$ terraform apply -auto-approve \
8+
-target=google_compute_network.example \
9+
-target=google_compute_firewall.example \
10+
-target=google_compute_global_address.example \
11+
-target=google_service_networking_connection.example
12+
13+
# create service
614
$ terraform apply -auto-approve
715
```
816

@@ -31,10 +39,10 @@ $ terraform apply -auto-approve
3139

3240
| Name | Type |
3341
|------|------|
34-
| [google_compute_firewall.private_network_firewall](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_firewall) | resource |
35-
| [google_compute_global_address.private_ip_address](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_global_address) | resource |
36-
| [google_compute_network.private_network](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_network) | resource |
37-
| [google_service_networking_connection.private_vpc_connection](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/service_networking_connection) | resource |
42+
| [google_compute_firewall.example](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_firewall) | resource |
43+
| [google_compute_global_address.example](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_global_address) | resource |
44+
| [google_compute_network.example](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_network) | resource |
45+
| [google_service_networking_connection.example](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/service_networking_connection) | resource |
3846

3947
## Inputs
4048

examples/standalone/main.tf

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,37 @@ terraform {
1616
provider "google" {
1717
}
1818

19-
// create network.
20-
resource "google_compute_network" "private_network" {
19+
# create network.
20+
resource "google_compute_network" "example" {
2121
name = "private-network"
2222
}
2323

24-
resource "google_compute_firewall" "private_network_firewall" {
24+
resource "google_compute_firewall" "example" {
2525
name = "private-network-firewall-policy"
26-
network = google_compute_network.private_network.id
26+
network = google_compute_network.example.id
2727
allow {
2828
protocol = "tcp"
2929
ports = ["22"]
3030
}
3131

3232
source_ranges = ["0.0.0.0/0"]
3333

34-
depends_on = [google_compute_network.private_network]
34+
depends_on = [google_compute_network.example]
3535
}
3636

37-
resource "google_compute_global_address" "private_ip_address" {
37+
resource "google_compute_global_address" "example" {
3838
name = "private-ip-address"
3939
purpose = "VPC_PEERING"
4040
address_type = "INTERNAL"
4141
prefix_length = 16
42-
network = google_compute_network.private_network.id
42+
network = google_compute_network.example.id
4343
}
4444

45-
// create private vpc connection.
46-
resource "google_service_networking_connection" "private_vpc_connection" {
47-
network = google_compute_network.private_network.id
45+
# create private vpc connection.
46+
resource "google_service_networking_connection" "example" {
47+
network = google_compute_network.example.id
4848
service = "servicenetworking.googleapis.com"
49-
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
49+
reserved_peering_ranges = [google_compute_global_address.example.name]
5050
deletion_policy = "ABANDON"
5151
}
5252

@@ -57,14 +57,14 @@ module "this" {
5757
source = "../.."
5858

5959
infrastructure = {
60-
vpc_id = google_compute_network.private_network.id
60+
vpc_id = google_compute_network.example.id
6161
}
6262

6363
architecture = "standalone"
6464
database = "mydb"
6565
username = "rdsuser"
6666

67-
depends_on = [google_service_networking_connection.private_vpc_connection]
67+
depends_on = [google_service_networking_connection.example]
6868
}
6969

7070
output "context" {

main.tf

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ locals {
3535
}, var.engine_version, "POSTGRES_15")
3636
}
3737

38+
# create network.
39+
resource "google_compute_network" "default" {
40+
count = var.infrastructure.vpc_id == null ? 1 : 0
41+
42+
name = "default-vpc"
43+
}
44+
45+
resource "google_compute_global_address" "default" {
46+
count = var.infrastructure.vpc_id == null ? 1 : 0
47+
48+
name = "default"
49+
purpose = "VPC_PEERING"
50+
address_type = "INTERNAL"
51+
prefix_length = 16
52+
network = google_compute_network.default[0].id
53+
}
54+
55+
# create private vpc connection.
56+
resource "google_service_networking_connection" "default" {
57+
count = var.infrastructure.vpc_id == null ? 1 : 0
58+
59+
network = google_compute_network.default[0].id
60+
service = "servicenetworking.googleapis.com"
61+
reserved_peering_ranges = [google_compute_global_address.default[0].name]
62+
deletion_policy = "ABANDON"
63+
}
64+
3865
#
3966
# Random
4067
#
@@ -86,7 +113,7 @@ resource "google_sql_database_instance" "primary" {
86113
#tfsec:ignore:google-sql-encrypt-in-transit-data
87114
ip_configuration {
88115
ipv4_enabled = false
89-
private_network = var.infrastructure.vpc_id
116+
private_network = var.infrastructure.vpc_id == null ? google_compute_network.default[0].id : var.infrastructure.vpc_id
90117
}
91118

92119
backup_configuration {
@@ -112,7 +139,7 @@ resource "google_sql_database_instance" "secondary" {
112139
#tfsec:ignore:google-sql-encrypt-in-transit-data
113140
ip_configuration {
114141
ipv4_enabled = false
115-
private_network = var.infrastructure.vpc_id
142+
private_network = var.infrastructure.vpc_id == null ? google_compute_network.default[0].id : var.infrastructure.vpc_id
116143
}
117144
}
118145

schema.yaml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,18 @@ components:
55
schemas:
66
variables:
77
type: object
8-
required:
9-
- infrastructure
108
properties:
119
infrastructure:
1210
title: Infrastructure
1311
type: object
1412
description: |
1513
Specify the infrastructure information for deploying.
16-
17-
Examples:
18-
```
19-
infrastructure:
20-
vpc_id: string # the ID of the VPC where the PostgreSQL service applies. It is a fully-qualified resource name, such as projects/{project_id}/global/networks/{network_id}.
21-
```
22-
required:
23-
- vpc_id
14+
default: {}
2415
properties:
2516
vpc_id:
2617
description: |
2718
The ID of the VPC where the PostgreSQL service applies. It is
28-
a fully-qualified resource name, such as projects/{project_id}/global/networks/{network_id}.
19+
a fully-qualified resource name, such as projects/{project_id}/global/networks/{network_id}. If not specified, a new VPC will be created.
2920
title: Vpc Id
3021
type: string
3122
x-walrus-ui:

variables.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ Specify the infrastructure information for deploying.
3535
Examples:
3636
```
3737
infrastructure:
38-
vpc_id: string # the ID of the VPC where the PostgreSQL service applies. It is a fully-qualified resource name, such as projects/{project_id}/global/networks/{network_id}.
38+
vpc_id: string, optional # the ID of the VPC where the PostgreSQL service applies. It is a fully-qualified resource name, such as projects/{project_id}/global/networks/{network_id}.
3939
```
4040
EOF
4141
type = object({
42-
vpc_id = string
42+
vpc_id = optional(string)
4343
})
44+
default = {}
4445
}
4546

4647
#

0 commit comments

Comments
 (0)