Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Migrate bigquery and storage destination to modules #52

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions modules/bigquery/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@
#-----------------#

locals {
dataset_name = element(
concat(google_bigquery_dataset.dataset.*.dataset_id, [""]),
0,
)
destination_uri = "bigquery.googleapis.com/projects/${var.project_id}/datasets/${local.dataset_name}"
destination_uri = "bigquery.googleapis.com/projects/${var.project_id}/datasets/${module.dataset.bigquery_dataset.dataset_id}"
}

#----------------#
Expand All @@ -38,21 +34,26 @@ resource "google_project_service" "enable_destination_api" {
#------------------#
# Bigquery dataset #
#------------------#
resource "google_bigquery_dataset" "dataset" {
module "dataset" {
source = "terraform-google-modules/bigquery/google"
version = "~> 4.0"

dataset_id = var.dataset_name
project = google_project_service.enable_destination_api.project
project_id = google_project_service.enable_destination_api.project
location = var.location
access = var.access
description = var.description
delete_contents_on_destroy = var.delete_contents_on_destroy
default_table_expiration_ms = var.default_table_expiration_ms
labels = var.labels

# labels = var.labels
# delete_contents_on_destroy = var.delete_contents_on_destroy
}

#--------------------------------#
# Service account IAM membership #
#--------------------------------#
resource "google_project_iam_member" "bigquery_sink_member" {
project = google_bigquery_dataset.dataset.project
project = module.dataset.project
role = "roles/bigquery.dataEditor"
member = var.log_sink_writer_identity
}
11 changes: 5 additions & 6 deletions modules/bigquery/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,30 @@

output "console_link" {
description = "The console link to the destination bigquery dataset"
value = "https://bigquery.cloud.google.com/dataset/${var.project_id}:${local.dataset_name}"
value = "https://bigquery.cloud.google.com/dataset/${var.project_id}:${module.dataset.bigquery_dataset.dataset_id}"
}

output "project" {
description = "The project in which the bigquery dataset was created."
value = google_bigquery_dataset.dataset.project
value = module.dataset.project
}

output "resource_name" {
description = "The resource name for the destination bigquery dataset"
value = local.dataset_name
value = module.dataset.bigquery_dataset.dataset_id
}

output "resource_id" {
description = "The resource id for the destination bigquery dataset"
value = google_bigquery_dataset.dataset.id
value = module.dataset.bigquery_dataset.id
}

output "self_link" {
description = "The self_link URI for the destination bigquery dataset"
value = google_bigquery_dataset.dataset.self_link
value = module.dataset.bigquery_dataset.self_link
}

output "destination_uri" {
description = "The destination URI for the bigquery dataset."
value = local.destination_uri
}

8 changes: 7 additions & 1 deletion modules/bigquery/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,17 @@ variable "project_id" {
}

variable "location" {
description = "The location of the storage bucket."
description = "The location of the bigquery dataset."
type = string
default = "US"
}

variable "access" {
description = "The access for the bigquery dataset."
type = any
default = null
}

variable "delete_contents_on_destroy" {
description = "(Optional) If set to true, delete all the tables in the dataset when destroying the resource; otherwise, destroying the resource will fail if tables are present."
type = bool
Expand Down
17 changes: 11 additions & 6 deletions modules/storage/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
*/

locals {
storage_bucket_name = element(concat(google_storage_bucket.bucket.*.name, [""]), 0)
destination_uri = "storage.googleapis.com/${local.storage_bucket_name}"
destination_uri = "storage.googleapis.com/${module.bucket.bucket.name}"
}

#----------------#
Expand All @@ -31,13 +30,20 @@ resource "google_project_service" "enable_destination_api" {
#----------------#
# Storage bucket #
#----------------#
resource "google_storage_bucket" "bucket" {
module "bucket" {
source = "terraform-google-modules/cloud-storage/google//modules/simple_bucket"
version = "~> 1.4"

name = var.storage_bucket_name
project = google_project_service.enable_destination_api.project
project_id = google_project_service.enable_destination_api.project
storage_class = var.storage_class
location = var.location
force_destroy = true
force_destroy = var.force_destroy
bucket_policy_only = var.bucket_policy_only
iam_members = var.iam_members + [{
role = "roles/storage.objectCreator"
member = var.log_sink_writer_identity
}]
}

#--------------------------------#
Expand All @@ -48,4 +54,3 @@ resource "google_storage_bucket_iam_member" "storage_sink_member" {
role = "roles/storage.objectCreator"
member = var.log_sink_writer_identity
}

11 changes: 5 additions & 6 deletions modules/storage/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,30 @@

output "console_link" {
description = "The console link to the destination storage bucket"
value = "https://console.cloud.google.com/storage/browser/${local.storage_bucket_name}?project=${var.project_id}"
value = "https://console.cloud.google.com/storage/browser/${module.bucket.bucket.name}?project=${var.project_id}"
}

output "project" {
description = "The project in which the storage bucket was created."
value = google_storage_bucket.bucket.project
value = module.bucket.bucket.project
}

output "resource_name" {
description = "The resource name for the destination storage bucket"
value = local.storage_bucket_name
value = module.bucket.bucket.name
}

output "resource_id" {
description = "The resource id for the destination storage bucket"
value = google_storage_bucket.bucket.id
value = module.bucket.bucket.id
}

output "self_link" {
description = "The self_link URI for the destination storage bucket"
value = google_storage_bucket.bucket.self_link
value = module.bucket.bucket.self_link
}

output "destination_uri" {
description = "The destination URI for the storage bucket."
value = local.destination_uri
}

8 changes: 7 additions & 1 deletion modules/storage/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ variable "location" {
variable "storage_class" {
description = "The storage class of the storage bucket."
type = string
default = "MULTI_REGIONAL"
default = null
}

variable "bucket_policy_only" {
description = "Enables Bucket Policy Only access to a bucket."
type = bool
default = true
}

variable "force_destroy" {
description = "Whether to delete all contained objects when the bucket is deleted."
type = bool
default = false
}