Skip to content

Commit

Permalink
feat!: location inverse control in bucket name prefix (#186)
Browse files Browse the repository at this point in the history
Co-authored-by: g-awmalik <malik.awais@gmail.com>
  • Loading branch information
Tensho and g-awmalik authored Jan 18, 2023
1 parent 8db2eb3 commit dbd3c35
Show file tree
Hide file tree
Showing 4 changed files with 47 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
42 changes: 42 additions & 0 deletions docs/upgrading_to_v4.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Upgrading to v4.0

The v4.0 release of the terraform-google-cloud-storage module is a backwards incompatible release, due to the following breaking changes:

- The bucket doesn't have location as mandatory part of the name prefix.

## Migration Instructions

A migration from 3.x to 4.x as showcased below

```diff
module "cloud_storage" {
source = "terraform-google-modules/cloud-storage/google"
- version = "~> 3.0"
+ version = "~> 4.0"
```
will produce the following output during a `terraform plan`:

```bash
# module.cloud_storage.google_storage_bucket.buckets["one"] must be replaced
...
~ name = "multiple-buckets-iost-eu-one" -> "multiple-buckets-iost-one" # forces replacement
...
# module.cloud_storage.google_storage_bucket.buckets["two"] must be replaced
...
~ name = "multiple-buckets-iost-eu-two" -> "multiple-buckets-iost-two" # forces replacement
...
Plan: 2 to add, 0 to change, 2 to destroy.
```

In the module bucket prefix contained `location` before. To preserve the same bucket name with `location` in prefix you should make some code adjustments:

```diff
- prefix = "multiple-buckets-${random_string.prefix.result}"
+ prefix = "multiple-buckets-${random_string.prefix.result}-eu"
```

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

```
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
```
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 dbd3c35

Please sign in to comment.