Skip to content

Commit

Permalink
add cookiecutter and tf input validation
Browse files Browse the repository at this point in the history
Co-Authored-By: Faisal Alquaddoomi <[email protected]>
  • Loading branch information
d33bs and falquaddoomi committed Apr 11, 2024
1 parent 35a7cc7 commit 4581607
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
30 changes: 30 additions & 0 deletions hooks/pre_gen_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Hook for checking values from cookiecutter variables before generating the project.
See the following for more information:
https://cookiecutter.readthedocs.io/en/1.7.0/advanced/hooks.html
"""

import sys

project_name = "{{ cookiecutter.project_name }}"
project_gc_project = "{{ cookiecutter.project_gc_project }}"

# checking for proper length of the project name
# note: we provide the limitation here based on constraints
# for Google service accounts and how the variable is used within template.
# See the following for more information:
# https://cloud.google.com/iam/docs/service-accounts-create#creating
if not 6 <= len(project_name) <= 21:
print(
"ERROR: %s Please use a project name of length 6-21 characters!" % project_name
)
sys.exit(1)

# limitation for google project names
# see the following for more information:
# https://cloud.google.com/resource-manager/docs/creating-managing-projects
if not 4 <= len(project_gc_project) <= 30:
print(
"ERROR: %s Please use a Google project name of length 4-30 characters!" % project_name
)
sys.exit(1)
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# tf account creation and related work
# Create a new service account
resource "google_service_account" "service_account" {
# note: template may have truncated the project name due to character limits
# for Google service accounts. See the following for more information:
# https://cloud.google.com/iam/docs/service-accounts-create#creating
account_id = "{{ cookiecutter.project_name[:21] }}-svc-acct"
account_id = "${var.initiative_label}-svc-acct"
}

#Create a service-account key for the associated service account
Expand Down
12 changes: 12 additions & 0 deletions {{ cookiecutter.project_name }}/terraform/operations/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
variable "project" {
description = "Google Cloud project to create the related resources in."
type = string
validation {
condition = length(var.project) >= 4 && length(var.project) <= 30
error_message = "Project name must be between 4 and 30 characters."
}
}

variable "region" {
Expand All @@ -12,9 +16,17 @@ variable "region" {
variable "bucket_name" {
description = "Name for the bucket being created."
type = string
validation {
condition = length(var.bucket_name) >= 3 && length(var.bucket_name) <= 63
error_message = "Bucket name must be between 3 and 63 characters."
}
}

variable "initiative_label" {
description = "Label for specific initiative useful for differentiating between various resources."
type = string
validation {
condition = length(var.initiative_label) >= 6 && length(var.initiative_label) <= 21
error_message = "Initiative label must be between 6 and 23 characters."
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
variable "project" {
description = "Google Cloud project to create the related resources in."
type = string
validation {
condition = length(var.project) >= 4 && length(var.project) <= 30
error_message = "Project name must be between 4 and 30 characters."
}
}

variable "region" {
Expand All @@ -12,9 +16,17 @@ variable "region" {
variable "bucket_name" {
description = "Name for the bucket being created."
type = string
validation {
condition = length(var.bucket_name) >= 3 && length(var.bucket_name) <= 63
error_message = "Bucket name must be between 3 and 63 characters."
}
}

variable "initiative_label" {
description = "Label for specific initiative useful for differentiating between various resources."
type = string
validation {
condition = length(var.initiative_label) >= 6 && length(var.initiative_label) <= 21
error_message = "Initiative label must be between 6 and 23 characters."
}
}

0 comments on commit 4581607

Please sign in to comment.