Skip to content
This repository has been archived by the owner on Sep 16, 2024. It is now read-only.

Commit

Permalink
add support for gcr buckets with uniform_bucket_level_access = true (#30
Browse files Browse the repository at this point in the history
)

* apply requested minor changes by @madhifallah and @anouarchattouna

* refactor: remove external

* refactor: apply minor changes requested by @anouarchattouna

* refactor: add newline to EOF of data.tf

requested by @anouarchattouna

Co-authored-by: Anouar Chattouna <[email protected]>

Co-authored-by: patricklubach <[email protected]>
Co-authored-by: Anouar Chattouna <[email protected]>
  • Loading branch information
3 people authored Jan 18, 2022
1 parent 0ea8b25 commit b882a21
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 20 deletions.
29 changes: 14 additions & 15 deletions .terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions data.tf
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
# get project details
data "google_project" "this" {}

data "google_storage_bucket" "bucket" {
for_each = toset(local.buckets)

name = each.value
}

17 changes: 13 additions & 4 deletions iam.tf
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
# Grant cleaner service account access to delete references in Google Container Registry
# for buckets with uniform_bucket_level_access = false
resource "google_storage_bucket_access_control" "this" {
for_each = {
for item in toset(local.project_storage_region) : "${item.storage_region}.${item.project_id}" => item
}
for_each = toset(local.google_storage_bucket_access_control)

bucket = each.value.storage_region != "" ? "${each.value.storage_region}.artifacts.${each.value.project_id}.appspot.com" : "artifacts.${each.value.project_id}.appspot.com"
bucket = each.value
role = "WRITER"
entity = "user-${google_service_account.cleaner.email}"
}

# Grant cleaner service account access to delete references in Google Container Registry
# for buckets with uniform_bucket_level_access = true
resource "google_storage_bucket_iam_member" "this" {
for_each = toset(local.google_storage_bucket_iam_member)

bucket = each.value
role = "roles/storage.legacyBucketWriter"
member = "serviceAccount:${google_service_account.cleaner.email}"
}

# Add IAM policy binding to the Cloud Run service
resource "google_cloud_run_service_iam_binding" "this" {
location = google_cloud_run_service.this.location
Expand Down
14 changes: 14 additions & 0 deletions locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,18 @@ locals {
}
]
])

buckets = [
for repo in var.gcr_repositories : repo.storage_region != null ? "${repo.storage_region}.artifacts.${repo.project_id != null ? repo.project_id : local.google_project_id}.appspot.com" : "artifacts.${repo.project_id != null ? repo.project_id : local.google_project_id}.appspot.com"
]

# Buckets having uniform_bucket_level_access = true
google_storage_bucket_iam_member = [
for bucket in local.buckets : bucket if data.google_storage_bucket.bucket[bucket].uniform_bucket_level_access
]

# Buckets having uniform_bucket_level_access = false
google_storage_bucket_access_control = [
for bucket in local.buckets : bucket if !data.google_storage_bucket.bucket[bucket].uniform_bucket_level_access
]
}
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ terraform {
required_providers {
google = {
source = "hashicorp/google"
version = ">= 4.1.0"
version = ">= 3.88.0"
}
}
}

0 comments on commit b882a21

Please sign in to comment.