diff --git a/modules/gcs/README.md b/modules/gcs/README.md index f7da2c2dc..b35a12e5f 100644 --- a/modules/gcs/README.md +++ b/modules/gcs/README.md @@ -90,14 +90,31 @@ module "bucket-gcs-notification" { } # tftest modules=1 resources=4 inventory=notification.yaml ``` - +### Example with object upload + +```hcl +module "bucket" { + source = "./fabric/modules/gcs" + project_id = "myproject" + name = "my-bucket" + objects_to_upload = { + "sample-data" = { + name = "example-file.csv" + source = "data/example-file.csv" + content_type = "text/csv" + } + } +} +# tftest modules=1 resources=2 inventory=object-upload.yaml +``` + ## Variables | name | description | type | required | default | |---|---|:---:|:---:|:---:| | [name](variables.tf#L116) | Bucket name suffix. | string | ✓ | | -| [project_id](variables.tf#L145) | Bucket project id. | string | ✓ | | +| [project_id](variables.tf#L155) | Bucket project id. | string | ✓ | | | [cors](variables.tf#L17) | CORS configuration for the bucket. Defaults to null. | object({…}) | | null | | [encryption_key](variables.tf#L28) | KMS key that will be used for encryption. | string | | null | | [force_destroy](variables.tf#L34) | Optional map to set force destroy keyed by name, defaults to false. | bool | | false | @@ -107,12 +124,13 @@ module "bucket-gcs-notification" { | [location](variables.tf#L101) | Bucket location. | string | | "EU" | | [logging_config](variables.tf#L107) | Bucket logging configuration. | object({…}) | | null | | [notification_config](variables.tf#L121) | GCS Notification configuration. | object({…}) | | null | -| [prefix](variables.tf#L135) | Optional prefix used to generate the bucket name. | string | | null | -| [retention_policy](variables.tf#L150) | Bucket retention policy. | object({…}) | | null | -| [storage_class](variables.tf#L159) | Bucket storage class. | string | | "MULTI_REGIONAL" | -| [uniform_bucket_level_access](variables.tf#L169) | Allow using object ACLs (false) or not (true, this is the recommended behavior) , defaults to true (which is the recommended practice, but not the behavior of storage API). | bool | | true | -| [versioning](variables.tf#L175) | Enable versioning, defaults to false. | bool | | false | -| [website](variables.tf#L181) | Bucket website. | object({…}) | | null | +| [objects_to_upload](variables.tf#L135) | Objects to be uploaded to bucket | map(object({…})) | | {} | +| [prefix](variables.tf#L145) | Optional prefix used to generate the bucket name. | string | | null | +| [retention_policy](variables.tf#L160) | Bucket retention policy. | object({…}) | | null | +| [storage_class](variables.tf#L169) | Bucket storage class. | string | | "MULTI_REGIONAL" | +| [uniform_bucket_level_access](variables.tf#L179) | Allow using object ACLs (false) or not (true, this is the recommended behavior) , defaults to true (which is the recommended practice, but not the behavior of storage API). | bool | | true | +| [versioning](variables.tf#L185) | Enable versioning, defaults to false. | bool | | false | +| [website](variables.tf#L191) | Bucket website. | object({…}) | | null | ## Outputs @@ -124,5 +142,4 @@ module "bucket-gcs-notification" { | [notification](outputs.tf#L46) | GCS Notification self link. | | | [topic](outputs.tf#L51) | Topic ID used by GCS. | | | [url](outputs.tf#L56) | Bucket URL. | | - diff --git a/modules/gcs/main.tf b/modules/gcs/main.tf index 68b77077d..ab3e6cb53 100644 --- a/modules/gcs/main.tf +++ b/modules/gcs/main.tf @@ -99,6 +99,15 @@ resource "google_storage_bucket" "bucket" { } } +resource "google_storage_bucket_object" "objects" { + for_each = var.objects_to_upload + + bucket = google_storage_bucket.bucket.id + name = each.value.name + source = each.value.source + content_type = each.value.content_type +} + resource "google_storage_bucket_iam_binding" "bindings" { for_each = var.iam bucket = google_storage_bucket.bucket.name diff --git a/modules/gcs/variables.tf b/modules/gcs/variables.tf index 58aec4dfb..e65211833 100644 --- a/modules/gcs/variables.tf +++ b/modules/gcs/variables.tf @@ -132,6 +132,16 @@ variable "notification_config" { default = null } +variable "objects_to_upload" { + description = "Objects to be uploaded to bucket" + type = map(object({ + name = string + source = string + content_type = string + })) + default = {} +} + variable "prefix" { description = "Optional prefix used to generate the bucket name." type = string diff --git a/tests/modules/gcs/examples/object-upload.yaml b/tests/modules/gcs/examples/object-upload.yaml new file mode 100644 index 000000000..10e0ae108 --- /dev/null +++ b/tests/modules/gcs/examples/object-upload.yaml @@ -0,0 +1,26 @@ +# Copyright 2023 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. + +values: + module.bucket.google_storage_bucket.bucket: + name: my-bucket + project: myproject + module.bucket.google_storage_bucket_object.objects["sample-data"]: + name: example-file.csv + source: data/example-file.csv + content_type: text/csv + +counts: + google_storage_bucket: 1 + google_storage_bucket_object: 1