Skip to content

Commit

Permalink
feat: add ability to specify random suffix for projects and GCS (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjerrems committed Apr 7, 2021
1 parent 50ce28f commit da4e8c1
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ For the cloudbuild submodule, see the README [cloudbuild](./modules/cloudbuild).
| project\_id | Custom project ID to use for project created. If not supplied, the default id is {project\_prefix}-seed-{random suffix}. | `string` | `""` | no |
| project\_labels | Labels to apply to the project. | `map(string)` | `{}` | no |
| project\_prefix | Name prefix to use for projects created. | `string` | `"cft"` | no |
| random\_suffix | Appends a 4 character random suffix to project ID and GCS bucket name. | `bool` | `true` | no |
| sa\_enable\_impersonation | Allow org\_admins group to impersonate service account & enable APIs required. | `bool` | `false` | no |
| sa\_org\_iam\_permissions | List of permissions granted to Terraform service account across the GCP organization. | `list(string)` | <pre>[<br> "roles/billing.user",<br> "roles/compute.networkAdmin",<br> "roles/compute.xpnAdmin",<br> "roles/iam.securityAdmin",<br> "roles/iam.serviceAccountAdmin",<br> "roles/logging.configWriter",<br> "roles/orgpolicy.policyAdmin",<br> "roles/resourcemanager.folderAdmin",<br> "roles/resourcemanager.organizationViewer"<br>]</pre> | no |
| state\_bucket\_name | Custom state bucket name. If not supplied, the default name is {project\_prefix}-tfstate-{random suffix}. | `string` | `""` | no |
Expand Down
6 changes: 4 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

locals {
seed_project_id = var.project_id != "" ? var.project_id : format("%s-%s", var.project_prefix, "seed")
state_bucket_name = var.state_bucket_name != "" ? var.state_bucket_name : format("%s-%s-%s", var.project_prefix, "tfstate", random_id.suffix.hex)
generated_bucket_name = var.random_suffix == true ? format("%s-%s-%s", var.project_prefix, "tfstate", random_id.suffix.hex) : format("%s-%s", var.project_prefix, "tfstate")
supplied_bucket_name = var.random_suffix == true ? format("%s-%s", var.state_bucket_name, random_id.suffix.hex) : var.state_bucket_name
state_bucket_name = var.state_bucket_name != "" ? local.supplied_bucket_name : local.generated_bucket_name
impersonation_apis = distinct(concat(var.activate_apis, ["serviceusage.googleapis.com", "iamcredentials.googleapis.com"]))
impersonation_enabled_count = var.sa_enable_impersonation == true ? 1 : 0
activate_apis = var.sa_enable_impersonation == true ? local.impersonation_apis : var.activate_apis
Expand Down Expand Up @@ -56,7 +58,7 @@ module "seed_project" {
source = "terraform-google-modules/project-factory/google"
version = "~> 10.1.1"
name = local.seed_project_id
random_project_id = true
random_project_id = var.random_suffix
disable_services_on_destroy = false
folder_id = var.folder_id
org_id = local.seed_org_depends_on
Expand Down
1 change: 1 addition & 0 deletions modules/cloudbuild/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Functional examples and sample Cloud Build definitions are included in the [exam
| project\_id | Custom project ID to use for project created. | `string` | `""` | no |
| project\_labels | Labels to apply to the project. | `map(string)` | `{}` | no |
| project\_prefix | Name prefix to use for projects created. | `string` | `"cft"` | no |
| random\_suffix | Appends a 4 character random suffix to project ID and GCS bucket name. | `bool` | `true` | no |
| sa\_enable\_impersonation | Allow org\_admins group to impersonate service account & enable APIs required. | `bool` | `false` | no |
| storage\_bucket\_labels | Labels to apply to the storage bucket. | `map(string)` | `{}` | no |
| terraform\_apply\_branches | List of git branches configured to run terraform apply Cloud Build trigger. All other branches will run plan by default. | `list(string)` | <pre>[<br> "master"<br>]</pre> | no |
Expand Down
2 changes: 1 addition & 1 deletion modules/cloudbuild/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ module "cloudbuild_project" {
source = "terraform-google-modules/project-factory/google"
version = "~> 10.1.1"
name = local.cloudbuild_project_id
random_project_id = true
random_project_id = var.random_suffix
disable_services_on_destroy = false
folder_id = var.folder_id
org_id = var.org_id
Expand Down
7 changes: 7 additions & 0 deletions modules/cloudbuild/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,10 @@ variable "gar_repo_name" {
default = ""
type = string
}

variable "random_suffix" {
description = "Appends a 4 character random suffix to project ID and GCS bucket name."
type = bool
default = true
}

6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,9 @@ variable "org_project_creators" {
type = list(string)
default = []
}

variable "random_suffix" {
description = "Appends a 4 character random suffix to project ID and GCS bucket name."
type = bool
default = true
}

0 comments on commit da4e8c1

Please sign in to comment.