Skip to content

Commit

Permalink
fix: create random suffix resource on demand (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tensho authored Jul 5, 2023
1 parent c8111a4 commit 5050c91
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
42 changes: 24 additions & 18 deletions examples/multiple_buckets/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ resource "random_string" "prefix" {
module "cloud_storage" {
source = "../.."
project_id = var.project_id
prefix = "multiple-buckets-${random_string.prefix.result}"

names = ["one", "two"]
prefix = "multiple-buckets-${random_string.prefix.result}"
names = ["one", "two"]
randomize_suffix = true

bucket_policy_only = {
"one" = true
"two" = false
Expand All @@ -35,26 +37,30 @@ module "cloud_storage" {
"two" = ["dev", "prod"]
}

lifecycle_rules = [{
action = {
type = "SetStorageClass"
storage_class = "NEARLINE"
}
condition = {
age = "10"
matches_storage_class = "MULTI_REGIONAL,STANDARD,DURABLE_REDUCED_AVAILABILITY"
}
}]

bucket_lifecycle_rules = {
"one" = [{
lifecycle_rules = [
{
action = {
type = "Delete"
type = "SetStorageClass"
storage_class = "NEARLINE"
}
condition = {
age = "90"
age = "10"
matches_storage_class = "MULTI_REGIONAL,STANDARD,DURABLE_REDUCED_AVAILABILITY"
}
}
]

bucket_lifecycle_rules = {
"one" = [
{
action = {
type = "Delete"
}
condition = {
age = "90"
}
}
}]
]
}

default_event_based_hold = {
Expand Down
3 changes: 2 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
Bucket random id suffix configuration
*****************************************/
resource "random_id" "bucket_suffix" {
count = var.randomize_suffix ? 1 : 0
byte_length = 2
}

locals {
suffix = var.randomize_suffix ? random_id.bucket_suffix.hex : ""
suffix = var.randomize_suffix ? random_id.bucket_suffix[0].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 Down
17 changes: 10 additions & 7 deletions test/integration/multiple_buckets/multiple_buckets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func TestMultipleBuckets(t *testing.T) {

projectID := buckets.GetStringOutput("project_id")
names := terraform.OutputList(t, buckets.GetTFOptions(), "names_list")
for _, bucketName := range names {
for _, fullBucketName := range names {
// alpha command to list buckets has --json instead of format=json
gcloudArgs := gcloud.WithCommonArgs([]string{"--project", projectID, "--json"})
op := gcloud.Run(t, fmt.Sprintf("alpha storage ls --buckets gs://%s", bucketName), gcloudArgs).Array()[0]
op := gcloud.Run(t, fmt.Sprintf("alpha storage ls --buckets gs://%s", fullBucketName), gcloudArgs).Array()[0]

// verify lifecycle rules
lifecycle := op.Get("metadata.lifecycle.rule").Array()[0]
Expand All @@ -53,8 +53,11 @@ func TestMultipleBuckets(t *testing.T) {
assert.Equal("10", lifecycle.Get("condition.age").String(), "lifecycle condition is age 10")
assert.ElementsMatch([]string{"MULTI_REGIONAL", "STANDARD", "DURABLE_REDUCED_AVAILABILITY"}, utils.GetResultStrSlice(lifecycle.Get("condition.matchesStorageClass").Array()), "lifecycle conditions match")

bucketSuffix := bucketName[strings.LastIndex(bucketName, "-")+1:]
switch bucketSuffix {
// peel bucket name from prefix and randomized suffix
parts := strings.Split(fullBucketName, "-")
bucketName := parts[len(parts) - 2]

switch bucketName {
case "one":
// bucket with suffix one
assert.True(op.Get("metadata.iamConfiguration.bucketPolicyOnly.enabled").Bool(), "bucketPolicyOnly is enabled")
Expand All @@ -66,13 +69,13 @@ func TestMultipleBuckets(t *testing.T) {
// bucket with suffix two
assert.False(op.Get("metadata.iamConfiguration.bucketPolicyOnly.enabled").Bool(), "bucketPolicyOnly is disabled")
assert.False(op.Get("metadata.defaultEventBasedHold").Bool(), "defaultEventBasedHold is disabled")
gcloud.Run(t, fmt.Sprintf("alpha storage ls --buckets gs://%s/dev/", bucketName), gcloudArgs)
gcloud.Run(t, fmt.Sprintf("alpha storage ls --buckets gs://%s/prod/", bucketName), gcloudArgs)
gcloud.Run(t, fmt.Sprintf("alpha storage ls --buckets gs://%s/dev/", fullBucketName), gcloudArgs)
gcloud.Run(t, fmt.Sprintf("alpha storage ls --buckets gs://%s/prod/", fullBucketName), gcloudArgs)
bucket_lifecycles := op.Get("metadata.lifecycle.rule").Array()
assert.Equal(1, len(bucket_lifecycles), "Bucket 'two' has 1 lifecycle rule")
default:
// fail test if unknown suffix
t.Fatalf("Only expected two buckets with suffixes one and two. Found: %s", bucketName)
t.Fatalf("Only expected two buckets with suffixes one and two. Found: %s", fullBucketName)
}
}
})
Expand Down

0 comments on commit 5050c91

Please sign in to comment.