Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: create random suffix resource on demand #185

Merged

Conversation

Tensho
Copy link
Contributor

@Tensho Tensho commented Sep 15, 2022

There is no need to create random_id.bucket_suffix resource if user doesn't enable bucket suffix via randomize_suffix input variable.

Before

randomize_suffix is disabled.

$ cd examples/multiple_buckets
$ terraform plan
...
# random_string.prefix will be created
# module.cloud_storage.google_storage_bucket.buckets["one"] will be created
# module.cloud_storage.random_id.bucket_suffix will be created
...
Plan: 3 to add, 0 to change, 0 to destroy.

randomize_suffix is enabled.

$ cd examples/multiple_buckets
$ terraform plan
...
# random_string.prefix will be created
# module.cloud_storage.google_storage_bucket.buckets["one"] will be created
# module.cloud_storage.random_id.bucket_suffix will be created
...
Plan: 3 to add, 0 to change, 0 to destroy.

After

randomize_suffix is disabled.

$ cd examples/multiple_buckets
$ terraform plan
...
# random_string.prefix will be created
# module.cloud_storage.google_storage_bucket.buckets["one"] will be created
...
Plan: 2 to add, 0 to change, 0 to destroy.

randomize_suffix is enabled.

$ cd examples/multiple_buckets
$ terraform plan
...
# random_string.prefix will be created
# module.cloud_storage.google_storage_bucket.buckets["one"] will be created
# module.cloud_storage.random_id.bucket_suffix[0] will be created
...
Plan: 3 to add, 0 to change, 0 to destroy.

Note

Resource moves to new location implicitly by Terraform:

# random_id.bucket_suffix moved to random_id.bucket_suffix[0]

@google-cla
Copy link

google-cla bot commented Sep 15, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@comment-bot-dev
Copy link

@Tensho
Thanks for the PR! 🚀
✅ Lint checks have passed.

@Tensho Tensho changed the title Create random suffix on demand fix: create random suffix resource on demand Sep 15, 2022
@Tensho
Copy link
Contributor Author

Tensho commented Nov 3, 2022

Could we have this merged please?

@tpolekhin
Copy link

@Tensho thank you for this PR - I'm looking forward to use it after the merge.
I would also add that sometimes you want to create buckets conditionally, so var.names could be an empty list []
Could you please also add this as a condition for the random_id.bucket_suffix resource?
I think it might look something like this:

resource "random_id" "bucket_suffix" {
   count       = alltrue([length(var.names) > 0, var.randomize_suffix]) ? 1 : 0
   byte_length = 2
 }

@Tensho
Copy link
Contributor Author

Tensho commented Nov 28, 2022

@tpolekhin I'm glad someone else is interested in this small change. I'm not sure that's necessary – random_id has its own toggler. If the names input variable is set to an empty list, the bucket list would be also an empty list, ergo no resources are created. The randomized_suffix input variable represents opt-in behaviour, ergo if it's not set/enabled there is no random_id resource is created. Would you mind elaborating on your use case more, please?

@tpolekhin
Copy link

@Tensho yes, sorry for not being more clear on that.
Here is an example:

module "gcs" {
  source  = "terraform-google-modules/cloud-storage/google"
  version = "3.3.0"

  names            = local.create_gcs_bucket ? ["testbucket"] : []
  randomize_suffix = true
}

I want to create bucket conditionally and I want it to have a randomised suffix if we do create it
But while I was writing this response I realised that I can use the same condition for the randomize_suffix as well

module "gcs" {
  source  = "terraform-google-modules/cloud-storage/google"
  version = "3.3.0"

  names            = local.create_gcs_bucket ? ["testbucket"] : []
  randomize_suffix = local.create_gcs_bucket
}

This will prevent the creation of the random_id if we're not creating any buckets 👍
Thanks for your help! 😄

Copy link
Member

@bharathkkb bharathkkb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @Tensho and apologies for the late reply. Just a question if there is any regression with this change.

matches_storage_class = "MULTI_REGIONAL,STANDARD,DURABLE_REDUCED_AVAILABILITY"
}
}]
# FIXME: This doesn't work:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure why this would happen with the proposed code change in the module? Did this happen for you locally?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to flag the current example didn't work for me, hence I changed it. If you're fine with the proposed example I'm happy to drop the comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough, I'll make adjustments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@apeabody The test passes now:

[root@bb37621fcfea workspace]# cft test run all
ok  	github.com/terraform-google-modules/terraform-google-cloud-storage/test/integration	65.892s
ok  	github.com/terraform-google-modules/terraform-google-cloud-storage/test/integration/multiple_buckets	92.755s

@Tensho
Copy link
Contributor Author

Tensho commented Dec 8, 2022

@bharathkkb There is no regression.

@github-actions
Copy link

github-actions bot commented Feb 6, 2023

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days

@github-actions github-actions bot added the Stale label Feb 6, 2023
@Tensho Tensho requested a review from bharathkkb February 7, 2023 09:21
@github-actions github-actions bot removed the Stale label Feb 7, 2023
@apeabody
Copy link
Contributor

Thanks for the contribution @Tensho! Can you please resolve the conflicts so I can trigger the INT tests.

@Tensho Tensho force-pushed the conditional-random-suffix branch from d11d7aa to 89a7ac7 Compare March 21, 2023 21:35
@Tensho Tensho requested a review from a team as a code owner March 21, 2023 21:35
@Tensho
Copy link
Contributor Author

Tensho commented Mar 21, 2023

@apeabody Done

@apeabody
Copy link
Contributor

/gcbrun

@apeabody
Copy link
Contributor

/gcbrun

@apeabody
Copy link
Contributor

Thanks @Tensho - From the LINT:

Checking for tflint
Working in . ...
2 issue(s) found:

Warning: List items should be accessed using square brackets (terraform_deprecated_index)

  on main.tf line 26:
  26:   suffix       = var.randomize_suffix ? random_id.bucket_suffix.0.hex : ""

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_deprecated_index.md

Warning: List items should be accessed using square brackets (terraform_deprecated_index)

  on main.tf line 26:
  26:   suffix       = var.randomize_suffix ? random_id.bucket_suffix.0.hex : ""

Reference: https://github.com/terraform-linters/tflint-ruleset-terraform/blob/v0.1.1/docs/rules/terraform_deprecated_index.md

tflint failed . 

@Tensho Tensho force-pushed the conditional-random-suffix branch from abf5d93 to 150cbb7 Compare April 10, 2023 22:46
@Tensho
Copy link
Contributor Author

Tensho commented Apr 10, 2023

@apeabody Fixed.

@apeabody
Copy link
Contributor

/gcbrun

@Tensho Tensho force-pushed the conditional-random-suffix branch from 150cbb7 to ae55809 Compare April 28, 2023 20:54
@Tensho Tensho requested a review from apeabody May 1, 2023 13:08
@apeabody
Copy link
Contributor

apeabody commented May 1, 2023

/gcbrun

@github-actions
Copy link

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days

@github-actions github-actions bot added the Stale label Jun 30, 2023
@Tensho
Copy link
Contributor Author

Tensho commented Jul 1, 2023

@apeabody Hey 👋 Could we have some traction here please? 🙂

@github-actions github-actions bot removed the Stale label Jul 1, 2023
@apeabody
Copy link
Contributor

apeabody commented Jul 5, 2023

/gcbrun

@apeabody
Copy link
Contributor

apeabody commented Jul 5, 2023

Pre-Req: #257

@apeabody
Copy link
Contributor

apeabody commented Jul 5, 2023

/gcbrun

@apeabody
Copy link
Contributor

apeabody commented Jul 5, 2023

/gcbrun

@apeabody apeabody changed the title fix: create random suffix resource on demand fix!: create random suffix resource on demand Jul 5, 2023
Copy link
Contributor

@apeabody apeabody left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution @Tensho!

It looks like this will be a breaking change for existing users, can you include how to migrate (details in the note below) called docs/upgrading_to_v4.0.md similar to https://github.com/terraform-google-modules/terraform-google-cloud-storage/blob/master/docs/upgrading_to_v4.0.md

Alternatively the minimum terraform version could be bumped (v1.1) to use https://developer.hashicorp.com/terraform/tutorials/configuration-language/move-config#move-your-resources-with-the-moved-configuration-block

byte_length = 2
}

locals {
suffix = var.randomize_suffix ? random_id.bucket_suffix.hex : ""
suffix = var.randomize_suffix ? random_id.bucket_suffix[0].hex : ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: This will be a breaking change for existing users. Probably can use something like terraform state mv 'random_id.bucket_suffix.hex' 'random_id.bucket_suffix[0].hex' to migrate the existing suffix to the new resource location.

Copy link
Contributor Author

@Tensho Tensho Jul 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I specified it in the PR description "Note" section – it won't be a breaking change. Terraform implicitly modifies the state. Try to run the following experiment:

  1. Draft a simple configuration without count and apply it:

    resource "random_id" "x" {
      byte_length = 1
    }
    $ terraform apply -auto-approve
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # random_id.x will be created
      + resource "random_id" "x" {
          + b64_std     = (known after apply)
          + b64_url     = (known after apply)
          + byte_length = 1
          + dec         = (known after apply)
          + hex         = (known after apply)
          + id          = (known after apply)
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    random_id.x: Creating...
    random_id.x: Creation complete after 0s [id=wA]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  2. Modify resource with count and apply it:

    resource "random_id" "x" {
     count = 1
     byte_length = 1
    }
    $ terraform apply -auto-approve
    random_id.x[0]: Refreshing state... [id=wA]
    
    Terraform will perform the following actions:
    
      # random_id.x has moved to random_id.x[0]
        resource "random_id" "x" {
            id          = "wA"
            # (5 unchanged attributes hidden)
        }
    
    Plan: 0 to add, 0 to change, 0 to destroy.
    
    Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Pay attention resource has been moved, no replacement happened.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for explanation @Tensho!

I just confirmed this worked on TF 0.13 (https://github.com/terraform-google-modules/terraform-google-cloud-storage/blob/master/versions.tf#L18), although the messaging is not as clear:

./terraform apply
random_id.x[0]: Refreshing state... [id=9w]

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

@apeabody
Copy link
Contributor

apeabody commented Jul 5, 2023

/gcbrun

@apeabody apeabody changed the title fix!: create random suffix resource on demand fix: create random suffix resource on demand Jul 5, 2023
@apeabody apeabody merged commit 5050c91 into terraform-google-modules:master Jul 5, 2023
@Tensho Tensho deleted the conditional-random-suffix branch July 6, 2023 08:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants