|
| 1 | +data "google_project" "project" { |
| 2 | +} |
| 3 | + |
| 4 | +resource "google_project" "cross-project-dataset" { |
| 5 | + project_id = "tf-test%{random_suffix}" |
| 6 | + name = "tf-test%{random_suffix}" |
| 7 | + org_id = "{{index $.TestEnvVars "org_id"}}" |
| 8 | + billing_account = "{{index $.TestEnvVars "billing_account"}}" |
| 9 | + deletion_policy = "DELETE" |
| 10 | +} |
| 11 | + |
| 12 | +resource "time_sleep" "wait_60_seconds" { |
| 13 | + create_duration = "60s" |
| 14 | + depends_on = [google_project.cross-project-dataset] |
| 15 | +} |
| 16 | + |
| 17 | +resource "google_project_service" "bigquery" { |
| 18 | + project = google_project.cross-project-dataset.project_id |
| 19 | + service = "bigquery.googleapis.com" |
| 20 | + disable_on_destroy = false |
| 21 | + depends_on = [time_sleep.wait_60_seconds] |
| 22 | +} |
| 23 | + |
| 24 | +resource "google_project_iam_member" "datastream_bigquery_admin" { |
| 25 | + project = google_project.cross-project-dataset.project_id |
| 26 | + role = "roles/bigquery.admin" |
| 27 | + member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-datastream.iam.gserviceaccount.com" |
| 28 | + depends_on = [time_sleep.wait_60_seconds] |
| 29 | +} |
| 30 | + |
| 31 | +resource "google_sql_database_instance" "instance" { |
| 32 | + name = "{{index $.Vars "database_instance_name"}}" |
| 33 | + database_version = "MYSQL_8_0" |
| 34 | + region = "us-central1" |
| 35 | + settings { |
| 36 | + tier = "db-f1-micro" |
| 37 | + backup_configuration { |
| 38 | + enabled = true |
| 39 | + binary_log_enabled = true |
| 40 | + } |
| 41 | + |
| 42 | + ip_configuration { |
| 43 | + |
| 44 | + // Datastream IPs will vary by region. |
| 45 | + authorized_networks { |
| 46 | + value = "34.71.242.81" |
| 47 | + } |
| 48 | + |
| 49 | + authorized_networks { |
| 50 | + value = "34.72.28.29" |
| 51 | + } |
| 52 | + |
| 53 | + authorized_networks { |
| 54 | + value = "34.67.6.157" |
| 55 | + } |
| 56 | + |
| 57 | + authorized_networks { |
| 58 | + value = "34.67.234.134" |
| 59 | + } |
| 60 | + |
| 61 | + authorized_networks { |
| 62 | + value = "34.72.239.218" |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + deletion_protection = {{index $.Vars "deletion_protection"}} |
| 68 | +} |
| 69 | + |
| 70 | +resource "google_sql_database" "db" { |
| 71 | + instance = google_sql_database_instance.instance.name |
| 72 | + name = "db" |
| 73 | +} |
| 74 | + |
| 75 | +resource "random_password" "pwd" { |
| 76 | + length = 16 |
| 77 | + special = false |
| 78 | +} |
| 79 | + |
| 80 | +resource "google_sql_user" "user" { |
| 81 | + name = "user" |
| 82 | + instance = google_sql_database_instance.instance.name |
| 83 | + host = "%" |
| 84 | + password = random_password.pwd.result |
| 85 | +} |
| 86 | + |
| 87 | +resource "google_datastream_connection_profile" "source_connection_profile" { |
| 88 | + display_name = "Source connection profile" |
| 89 | + location = "us-central1" |
| 90 | + connection_profile_id = "{{index $.Vars "source_connection_profile_id"}}" |
| 91 | + |
| 92 | + mysql_profile { |
| 93 | + hostname = google_sql_database_instance.instance.public_ip_address |
| 94 | + username = google_sql_user.user.name |
| 95 | + password = google_sql_user.user.password |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +resource "google_datastream_connection_profile" "destination_connection_profile" { |
| 100 | + display_name = "Connection profile" |
| 101 | + location = "us-central1" |
| 102 | + connection_profile_id = "{{index $.Vars "destination_connection_profile_id"}}" |
| 103 | + |
| 104 | + bigquery_profile {} |
| 105 | +} |
| 106 | + |
| 107 | +resource "google_datastream_stream" "{{$.PrimaryResourceId}}" { |
| 108 | + stream_id = "{{index $.Vars "stream_id"}}" |
| 109 | + location = "us-central1" |
| 110 | + display_name = "my stream" |
| 111 | + source_config { |
| 112 | + source_connection_profile = google_datastream_connection_profile.source_connection_profile.id |
| 113 | + mysql_source_config {} |
| 114 | + } |
| 115 | + destination_config { |
| 116 | + destination_connection_profile = google_datastream_connection_profile.destination_connection_profile.id |
| 117 | + bigquery_destination_config { |
| 118 | + source_hierarchy_datasets { |
| 119 | + dataset_template { |
| 120 | + location = "us-central1" |
| 121 | + } |
| 122 | + project_id = google_project.cross-project-dataset.project_id |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + backfill_none { |
| 128 | + } |
| 129 | +} |
0 commit comments