Skip to content

Commit be1f434

Browse files
Android Build Filesystem (ABFS) Teamsce-taid
authored andcommitted
feat: create a service account if not defined
PiperOrigin-RevId: 786254967
1 parent 3e3ca40 commit be1f434

File tree

10 files changed

+57
-23
lines changed

10 files changed

+57
-23
lines changed

examples/simple/iam.tf

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,32 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
locals {
16+
create_service_account = var.abfs_service_account_id == ""
17+
abfs_service_account_email = local.create_service_account ? google_service_account.abfs[0].email : data.google_service_account.abfs[0].email
18+
abfs_service_account_unique_id = local.create_service_account ? google_service_account.abfs[0].unique_id : data.google_service_account.abfs[0].unique_id
19+
}
20+
1521
data "google_service_account" "abfs" {
22+
count = local.create_service_account ? 0 : 1
23+
1624
project = data.google_project.project.project_id
1725
account_id = var.abfs_service_account_id
1826
}
1927

28+
resource "google_service_account" "abfs" {
29+
count = local.create_service_account ? 1 : 0
30+
31+
project = data.google_project.project.project_id
32+
account_id = var.abfs_service_account_name
33+
display_name = "Service Account for ABFS"
34+
35+
lifecycle {
36+
# Prevent the service account that may have been granted an ABFS license from being deleted.
37+
prevent_destroy = true
38+
}
39+
}
40+
2041
module "project-iam-bindings" {
2142
source = "terraform-google-modules/iam/google//modules/projects_iam"
2243
version = "8.1.0"
@@ -25,16 +46,18 @@ module "project-iam-bindings" {
2546
mode = "authoritative"
2647

2748
bindings = {
28-
"roles/artifactregistry.reader" = [data.google_service_account.abfs.member],
29-
"roles/logging.logWriter" = [data.google_service_account.abfs.member],
30-
"roles/monitoring.metricWriter" = [data.google_service_account.abfs.member],
31-
"roles/monitoring.viewer" = [data.google_service_account.abfs.member],
32-
"roles/spanner.databaseUser" = [data.google_service_account.abfs.member],
33-
"roles/stackdriver.resourceMetadata.writer" = [data.google_service_account.abfs.member],
34-
"roles/storage.objectAdmin" = [data.google_service_account.abfs.member],
49+
"roles/artifactregistry.reader" = ["serviceAccount:${local.abfs_service_account_email}"],
50+
"roles/logging.logWriter" = ["serviceAccount:${local.abfs_service_account_email}"],
51+
"roles/monitoring.metricWriter" = ["serviceAccount:${local.abfs_service_account_email}"],
52+
"roles/monitoring.viewer" = ["serviceAccount:${local.abfs_service_account_email}"],
53+
"roles/spanner.databaseUser" = ["serviceAccount:${local.abfs_service_account_email}"],
54+
"roles/stackdriver.resourceMetadata.writer" = ["serviceAccount:${local.abfs_service_account_email}"],
55+
"roles/storage.objectAdmin" = ["serviceAccount:${local.abfs_service_account_email}"],
3556
}
3657

3758
depends_on = [
38-
module.project-services
59+
module.project-services,
60+
data.google_service_account.abfs,
61+
google_service_account.abfs
3962
]
4063
}

examples/simple/main.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module "abfs_server" {
2727

2828
project_id = data.google_project.project.project_id
2929
zone = var.zone
30-
service_account_email = data.google_service_account.abfs.email
30+
service_account_email = local.abfs_service_account_email
3131
subnetwork = module.abfs-vpc.subnets["${var.region}/abfs-subnet"].name
3232
abfs_docker_image_uri = var.abfs_docker_image_uri
3333
abfs_license = var.abfs_license
@@ -42,7 +42,7 @@ module "abfs_uploaders" {
4242

4343
project_id = data.google_project.project.project_id
4444
zone = var.zone
45-
service_account_email = data.google_service_account.abfs.email
45+
service_account_email = local.abfs_service_account_email
4646
subnetwork = module.abfs-vpc.subnets["${var.region}/abfs-subnet"].name
4747
abfs_docker_image_uri = var.abfs_docker_image_uri
4848
abfs_gerrit_uploader_count = var.abfs_gerrit_uploader_count

examples/simple/network.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module "abfs-vpc" {
2727
name = "allow-egress-google-apis"
2828
priority = 1000
2929
ranges = ["199.36.153.8/30", "34.126.0.0/18"]
30-
target_service_accounts = [data.google_service_account.abfs.email]
30+
target_service_accounts = [local.abfs_service_account_email]
3131

3232
allow = [
3333
{
@@ -59,8 +59,8 @@ module "abfs-vpc" {
5959
priority = 1000
6060

6161
ranges = ["0.0.0.0/0"]
62-
source_service_accounts = [data.google_service_account.abfs.email]
63-
target_service_accounts = [data.google_service_account.abfs.email]
62+
source_service_accounts = [local.abfs_service_account_email]
63+
target_service_accounts = [local.abfs_service_account_email]
6464
allow = [
6565
{
6666
protocol = "icmp"

examples/simple/outputs.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ output "license_information" {
1616
value = {
1717
project_id = data.google_project.project.project_id,
1818
project_number = data.google_project.project.number,
19-
service_account_email = data.google_service_account.abfs.email
20-
service_account_unique_id = data.google_service_account.abfs.unique_id
19+
service_account_email = local.abfs_service_account_email
20+
service_account_unique_id = local.abfs_service_account_unique_id
2121
}
2222
}
2323

examples/simple/terraform.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ terraform {
1717
required_providers {
1818
google = {
1919
source = "hashicorp/google"
20-
version = "~> 6.11.0"
20+
version = ">= 6.11.0"
2121
}
2222
}
2323
# grant Storage Object Admin role to the Google Identity invoking Terraform

examples/simple/variables.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,14 @@ variable "alert_notification_email" {
8686

8787
variable "abfs_service_account_id" {
8888
type = string
89-
description = "ABFS service account ID (e.g. abfs@<project-id>.iam.gserviceaccount.com)"
89+
description = "ABFS service account ID (e.g. abfs@<project-id>.iam.gserviceaccount.com); if not specified, a new service account will be created using the abfs_service_account_name."
90+
default = ""
91+
}
92+
93+
variable "abfs_service_account_name" {
94+
type = string
95+
description = "The name of the service account to create in case abfs_service_account_id is not specified."
96+
default = "abfs"
9097
}
9198

9299
variable "abfs_server_machine_type" {

modules/server/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ resource "google_compute_instance" "abfs_server" {
7373
allow_stopping_for_update = var.abfs_server_allow_stopping_for_update
7474

7575
service_account {
76-
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
76+
# Google recommends custom service accounts with a cloud-platform scope and permissions granted via IAM roles.
7777
email = var.service_account_email
7878
scopes = ["cloud-platform"]
7979
}

modules/server/terraform.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ terraform {
1717
required_providers {
1818
google = {
1919
source = "hashicorp/google"
20-
version = "~> 6.11.0"
20+
version = ">= 6.11.0"
2121
}
2222
}
2323
}

modules/uploaders/main.tf

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ locals {
5959

6060
resource "google_compute_instance" "abfs_gerrit_uploaders" {
6161
count = var.abfs_gerrit_uploader_count
62+
6263
project = var.project_id
6364
name = "${local.goog_cm_deployment_name}${var.abfs_gerrit_uploader_name_prefix}-${count.index}"
6465
machine_type = var.abfs_gerrit_uploader_machine_type
@@ -67,7 +68,7 @@ resource "google_compute_instance" "abfs_gerrit_uploaders" {
6768
allow_stopping_for_update = var.abfs_gerrit_uploader_allow_stopping_for_update
6869

6970
service_account {
70-
# Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
71+
# Google recommends custom service accounts with a cloud-platform scope and permissions granted via IAM roles.
7172
email = var.service_account_email
7273
scopes = ["cloud-platform"]
7374
}
@@ -98,8 +99,9 @@ resource "google_compute_instance" "abfs_gerrit_uploaders" {
9899
}
99100

100101
resource "google_compute_disk" "abfs_gerrit_uploader_datadisks" {
101-
project = var.project_id
102102
count = var.abfs_gerrit_uploader_count
103+
104+
project = var.project_id
103105
name = "${local.goog_cm_deployment_name}${var.abfs_gerrit_uploader_datadisk_name_prefix}-${count.index}"
104106
size = var.abfs_gerrit_uploader_datadisk_size_gb
105107
zone = var.zone
@@ -111,15 +113,17 @@ resource "google_compute_disk" "abfs_gerrit_uploader_datadisks" {
111113
}
112114

113115
resource "google_compute_attached_disk" "abfs_gerrit_uploader_datadisk_attachments" {
114-
project = var.project_id
115116
count = var.abfs_gerrit_uploader_count
117+
118+
project = var.project_id
116119
disk = google_compute_disk.abfs_gerrit_uploader_datadisks[count.index].id
117120
instance = google_compute_instance.abfs_gerrit_uploaders[count.index].id
118121
device_name = local.abfs_datadisk_device_name
119122
}
120123

121124
data "cloudinit_config" "abfs_gerrit_uploader_configs" {
122125
count = var.abfs_gerrit_uploader_count
126+
123127
gzip = false
124128
base64_encode = false
125129

modules/uploaders/terraform.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ terraform {
1717
required_providers {
1818
google = {
1919
source = "hashicorp/google"
20-
version = "~> 6.11.0"
20+
version = ">= 6.11.0"
2121
}
2222
}
2323
}

0 commit comments

Comments
 (0)