Skip to content

Commit

Permalink
fix: remove location from bucket name
Browse files Browse the repository at this point in the history
  • Loading branch information
Tensho committed Jan 6, 2023
1 parent 4dfa917 commit be55023
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Functional examples are included in the
| location | Bucket location. | `string` | `"EU"` | no |
| logging | Map of lowercase unprefixed name => bucket logging config object. Format is the same as described in provider documentation https://www.terraform.io/docs/providers/google/r/storage_bucket.html#logging | `any` | `{}` | no |
| names | Bucket name suffixes. | `list(string)` | n/a | yes |
| prefix | Prefix used to generate the bucket name. | `string` | n/a | yes |
| prefix | Prefix used to generate the bucket name. | `string` | `""` | no |
| 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 |
Expand Down
8 changes: 8 additions & 0 deletions docs/upgrading_to_v2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The v2.0 release of the terraform-google-cloud-storage module is a backwards inc

- The module now uses for_each instead of count.
- The minimum Terraform version was increased to 0.13.
- The bucket doesn't have location as mandatory part of the name prefix.

## Migration Instructions

Expand Down Expand Up @@ -36,6 +37,13 @@ terraform state mv module.cloud_storage.google_storage_bucket.buckets[0] 'module
terraform state mv module.cloud_storage.google_storage_bucket.buckets[1] 'module.cloud_storage.google_storage_bucket.buckets["two"]'
```

Bucket prefix contained `location` before, so to preserve the same bucket name with `location` in prefix you should make some adjustments:

```diff
- prefix = var.project_id
+ prefix ="${var.project_id}-${var.location}"
```

Re-running the plan should show that the storage bucket resources are no longer showing a diff.

```
Expand Down
7 changes: 3 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ resource "random_id" "bucket_suffix" {
}

locals {
prefix = var.prefix == "" ? "" : join("-", [var.prefix, lower(var.location), ""])
suffix = var.randomize_suffix ? "-${random_id.bucket_suffix.hex}" : ""
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 @@ -40,11 +39,11 @@ locals {
resource "google_storage_bucket" "buckets" {
for_each = local.names_set

name = "${local.prefix}${lower(each.value)}${local.suffix}"
name = join("-", compact([var.prefix, each.value, local.suffix]))
project = var.project_id
location = var.location
storage_class = var.storage_class
labels = merge(var.labels, { name = replace("${local.prefix}${lower(each.value)}", ".", "-") })
labels = merge(var.labels, { name = replace(join("-", compact([var.prefix, each.value])), ".", "-") })
force_destroy = lookup(
var.force_destroy,
lower(each.value),
Expand Down
1 change: 1 addition & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ variable "project_id" {
variable "prefix" {
description = "Prefix used to generate the bucket name."
type = string
default = ""
}

variable "names" {
Expand Down

0 comments on commit be55023

Please sign in to comment.