Skip to content

Commit 9715fbd

Browse files
databasemigrationservice: Add objects config field to DMS migration job resource for selective migration (#17302) (#1305)
[upstream:43948c1f29df13567c3d4d3497aa776cc9b1dfa3] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 1d20621 commit 9715fbd

4 files changed

Lines changed: 209 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file has some scaffolding to make sure that names are unique and that
2+
# a region and zone are selected when you try to create your Terraform resources.
3+
4+
locals {
5+
name_suffix = "${random_pet.suffix.id}"
6+
}
7+
8+
resource "random_pet" "suffix" {
9+
length = 2
10+
}
11+
12+
provider "google" {
13+
region = "us-central1"
14+
zone = "us-central1-c"
15+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
data "google_project" "project" {
2+
}
3+
4+
resource "google_sql_database_instance" "source_csql" {
5+
name = "source-csql-${local.name_suffix}"
6+
database_version = "POSTGRES_15"
7+
settings {
8+
tier = "db-custom-2-13312"
9+
deletion_protection_enabled = false
10+
}
11+
deletion_protection = false
12+
}
13+
14+
resource "google_sql_ssl_cert" "source_sql_client_cert" {
15+
common_name = "cert-${local.name_suffix}"
16+
instance = google_sql_database_instance.source_csql.name
17+
18+
depends_on = [google_sql_database_instance.source_csql]
19+
}
20+
21+
resource "google_sql_user" "source_sqldb_user" {
22+
name = "username-${local.name_suffix}"
23+
instance = google_sql_database_instance.source_csql.name
24+
password = "password-${local.name_suffix}"
25+
26+
depends_on = [google_sql_ssl_cert.source_sql_client_cert]
27+
}
28+
29+
resource "google_database_migration_service_connection_profile" "source_cp" {
30+
location = "us-central1"
31+
connection_profile_id = "source-cp-${local.name_suffix}"
32+
display_name = "source-cp-${local.name_suffix}_display"
33+
labels = {
34+
foo = "bar"
35+
}
36+
postgresql {
37+
host = google_sql_database_instance.source_csql.ip_address.0.ip_address
38+
port = 3306
39+
username = google_sql_user.source_sqldb_user.name
40+
password = google_sql_user.source_sqldb_user.password
41+
ssl {
42+
client_key = google_sql_ssl_cert.source_sql_client_cert.private_key
43+
client_certificate = google_sql_ssl_cert.source_sql_client_cert.cert
44+
ca_certificate = google_sql_ssl_cert.source_sql_client_cert.server_ca_cert
45+
type = "SERVER_CLIENT"
46+
}
47+
cloud_sql_id = "source-csql-${local.name_suffix}"
48+
}
49+
50+
depends_on = [google_sql_user.source_sqldb_user]
51+
}
52+
53+
resource "google_sql_database_instance" "destination_csql" {
54+
name = "destination-csql-${local.name_suffix}"
55+
database_version = "POSTGRES_15"
56+
settings {
57+
tier = "db-custom-2-13312"
58+
deletion_protection_enabled = false
59+
}
60+
deletion_protection = false
61+
}
62+
63+
resource "google_database_migration_service_connection_profile" "destination_cp" {
64+
location = "us-central1"
65+
connection_profile_id = "destination-cp-${local.name_suffix}"
66+
display_name = "destination-cp-${local.name_suffix}_display"
67+
labels = {
68+
foo = "bar"
69+
}
70+
postgresql {
71+
cloud_sql_id = "destination-csql-${local.name_suffix}"
72+
}
73+
depends_on = [google_sql_database_instance.destination_csql]
74+
}
75+
76+
resource "google_database_migration_service_migration_job" "psqltopsqlobjects" {
77+
location = "us-central1"
78+
migration_job_id = "my-migrationid-${local.name_suffix}"
79+
display_name = "my-migrationid-${local.name_suffix}_display"
80+
labels = {
81+
foo = "bar"
82+
}
83+
static_ip_connectivity {
84+
}
85+
source = google_database_migration_service_connection_profile.source_cp.name
86+
destination = google_database_migration_service_connection_profile.destination_cp.name
87+
type = "CONTINUOUS"
88+
89+
objects_config {
90+
source_objects_config {
91+
objects_selection_type = "SPECIFIED_OBJECTS"
92+
object_configs {
93+
object_identifier {
94+
type = "DATABASE"
95+
database = "my_database"
96+
}
97+
}
98+
object_configs {
99+
object_identifier {
100+
type = "TABLE"
101+
database = "my_other_database"
102+
schema = "public"
103+
table = "users"
104+
}
105+
}
106+
}
107+
}
108+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
===
2+
3+
These examples use real resources that will be billed to the
4+
Google Cloud Platform project you use - so make sure that you
5+
run "terraform destroy" before quitting!
6+
7+
===
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Database Migration Service Migration Job Postgres To Postgres Objects - Terraform
2+
3+
## Setup
4+
5+
<walkthrough-author name="rileykarson@google.com" analyticsId="UA-125550242-1" tutorialName="database_migration_service_migration_job_postgres_to_postgres_objects" repositoryUrl="https://github.com/terraform-google-modules/docs-examples"></walkthrough-author>
6+
7+
Welcome to Terraform in Google Cloud Shell! We need you to let us know what project you'd like to use with Terraform.
8+
9+
<walkthrough-project-billing-setup></walkthrough-project-billing-setup>
10+
11+
Terraform provisions real GCP resources, so anything you create in this session will be billed against this project.
12+
13+
## Terraforming!
14+
15+
Let's use {{project-id}} with Terraform! Click the Cloud Shell icon below to copy the command
16+
to your shell, and then run it from the shell by pressing Enter/Return. Terraform will pick up
17+
the project name from the environment variable.
18+
19+
```bash
20+
export GOOGLE_CLOUD_PROJECT={{project-id}}
21+
```
22+
23+
After that, let's get Terraform started. Run the following to pull in the providers.
24+
25+
```bash
26+
terraform init
27+
```
28+
29+
With the providers downloaded and a project set, you're ready to use Terraform. Go ahead!
30+
31+
```bash
32+
terraform apply
33+
```
34+
35+
Terraform will show you what it plans to do, and prompt you to accept. Type "yes" to accept the plan.
36+
37+
```bash
38+
yes
39+
```
40+
41+
42+
## Post-Apply
43+
44+
### Editing your config
45+
46+
Now you've provisioned your resources in GCP! If you run a "plan", you should see no changes needed.
47+
48+
```bash
49+
terraform plan
50+
```
51+
52+
So let's make a change! Try editing a number, or appending a value to the name in the editor. Then,
53+
run a 'plan' again.
54+
55+
```bash
56+
terraform plan
57+
```
58+
59+
Afterwards you can run an apply, which implicitly does a plan and shows you the intended changes
60+
at the 'yes' prompt.
61+
62+
```bash
63+
terraform apply
64+
```
65+
66+
```bash
67+
yes
68+
```
69+
70+
## Cleanup
71+
72+
Run the following to remove the resources Terraform provisioned:
73+
74+
```bash
75+
terraform destroy
76+
```
77+
```bash
78+
yes
79+
```

0 commit comments

Comments
 (0)