Skip to content

Commit

Permalink
feat: Add ability to append a randomized suffix to all bucket names (t…
Browse files Browse the repository at this point in the history
  • Loading branch information
tpdownes authored Sep 10, 2021
1 parent e6559e9 commit 93ff75f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Functional examples are included in the
| names | Bucket name suffixes. | `list(string)` | n/a | yes |
| prefix | Prefix used to generate the bucket name. | `string` | n/a | yes |
| project\_id | Bucket project id. | `string` | n/a | yes |
| randomize\_suffix | Adds an identical, but randomized 4-character suffix to all bucket names | `bool` | `false` | no |
| retention\_policy | Map of retention policy values. Format is the same as described in provider documentation https://www.terraform.io/docs/providers/google/r/storage_bucket#retention_policy | `any` | `{}` | no |
| set\_admin\_roles | Grant roles/storage.objectAdmin role to admins and bucket\_admins. | `bool` | `false` | no |
| set\_creator\_roles | Grant roles/storage.objectCreator role to creators and bucket\_creators. | `bool` | `false` | no |
Expand Down
10 changes: 9 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@
* limitations under the License.
*/

/******************************************
Bucket random id suffix configuration
*****************************************/
resource "random_id" "bucket_suffix" {
byte_length = 2
}

locals {
prefix = var.prefix == "" ? "" : join("-", [var.prefix, lower(var.location), ""])
suffix = var.randomize_suffix ? "-${random_id.bucket_suffix.hex}" : ""
names_set = toset(var.names)
buckets_list = [for name in var.names : google_storage_bucket.buckets[name]]
first_bucket = local.buckets_list[0]
Expand All @@ -32,7 +40,7 @@ locals {
resource "google_storage_bucket" "buckets" {
for_each = local.names_set

name = "${local.prefix}${lower(each.value)}"
name = "${local.prefix}${lower(each.value)}${local.suffix}"
project = var.project_id
location = var.location
storage_class = var.storage_class
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ variable "names" {
type = list(string)
}

variable "randomize_suffix" {
description = "Adds an identical, but randomized 4-character suffix to all bucket names"
type = bool
default = false
}

variable "location" {
description = "Bucket location."
type = string
Expand Down

0 comments on commit 93ff75f

Please sign in to comment.