From f02c1cc1d2f39c2128ae80bf12eaa181887a8ace Mon Sep 17 00:00:00 2001 From: marcelorobj Date: Tue, 17 Jun 2025 09:59:33 -0300 Subject: [PATCH] First CFT commit --- README.md | 1 + examples/multiple_buckets/main.tf | 3 ++ main.tf | 3 +- .../multiple_buckets/controls/gsutils.rb | 29 +++++++++++++++++++ variables.tf | 10 +++++++ 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 test/integration/multiple_buckets/controls/gsutils.rb diff --git a/README.md b/README.md index 33803710..f8bdf3ef 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ Functional examples are included in the | set\_hmac\_key\_admin\_roles | Grant roles/storage.hmacKeyAdmin role to hmac\_key\_admins and bucket\_hmac\_key\_admins. | `bool` | `false` | no | | set\_storage\_admin\_roles | Grant roles/storage.admin role to storage\_admins and bucket\_storage\_admins. | `bool` | `false` | no | | set\_viewer\_roles | Grant roles/storage.objectViewer role to viewers and bucket\_viewers. | `bool` | `false` | no | +| silly\_label | Sample label for bucket. | `string` | n/a | yes | | soft\_delete\_policy | Soft delete policies to apply. Map of lowercase unprefixed name => soft delete policy. Format is the same as described in provider documentation https://www.terraform.io/docs/providers/google/r/storage_bucket.html#nested_soft_delete_policy | `map(any)` | `{}` | no | | storage\_admins | IAM-style members who will be granted roles/storage.admin on all buckets. | `list(string)` | `[]` | no | | storage\_class | Bucket storage class. | `string` | `"STANDARD"` | no | diff --git a/examples/multiple_buckets/main.tf b/examples/multiple_buckets/main.tf index ae951c0a..8fc5d7ad 100644 --- a/examples/multiple_buckets/main.tf +++ b/examples/multiple_buckets/main.tf @@ -24,6 +24,9 @@ module "cloud_storage" { source = "terraform-google-modules/cloud-storage/google" version = "~> 10.0" + // CODELAB: Add "silly_label" as an example to main.tf. + silly_label = "awesome" + project_id = var.project_id prefix = "multiple-buckets-${random_string.prefix.result}" diff --git a/main.tf b/main.tf index 345fd3be..0a046722 100644 --- a/main.tf +++ b/main.tf @@ -24,7 +24,8 @@ resource "random_id" "bucket_suffix" { locals { suffix = var.randomize_suffix ? random_id.bucket_suffix[0].hex : "" - names_set = toset(var.names) + // CODELAB:Add silly label in labels variable + labels = merge(var.labels, { name = replace("${local.prefix}${lower(element(var.names, count.index))}", ".", "-") }, { "silly" = var.silly_label }) buckets_list = [for name in var.names : google_storage_bucket.buckets[name]] first_bucket = local.buckets_list[0] folder_list = flatten([ diff --git a/test/integration/multiple_buckets/controls/gsutils.rb b/test/integration/multiple_buckets/controls/gsutils.rb new file mode 100644 index 00000000..a925476e --- /dev/null +++ b/test/integration/multiple_buckets/controls/gsutils.rb @@ -0,0 +1,29 @@ +control "gsutil" do + <..> + +# CODELAB: Copy paste the below test in gsutil.rb to test silly_label feature. + +# command to get the labels for bucket_1 +describe command("gsutil label get gs://#{attribute("names_list")[0]}") do + //check if the command gave a valid response + its(:exit_status) { should eq 0 } + its(:stderr) { should eq "" } + + //parse the command's output into JSON + let!(:data) do + if subject.exit_status == 0 + JSON.parse(subject.stdout) + else + {} + end + end + + # check if bucket_1 has the new "silly" label with the value "awesome" + describe "bucket_1" do + it "has label" do + data.each do |bucket| + expect(data["silly"]).to include("awesome") + end + end + end + end diff --git a/variables.tf b/variables.tf index 1a8b00b0..247b1ac4 100644 --- a/variables.tf +++ b/variables.tf @@ -30,6 +30,16 @@ variable "names" { type = list(string) } +// CODELAB: Add "silly_label" variable to variables.tf between "names" and "location" +variable "silly_label" { + description = "Sample label for bucket." + type = string +} +variable "location" { + description = "Bucket location." + default = "EU" +} + variable "randomize_suffix" { description = "Adds an identical, but randomized 4-character suffix to all bucket names" type = bool