Skip to content

Commit

Permalink
Normalize prefix handling in blueprints (#1003)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunzese authored Nov 23, 2022
1 parent d16affd commit e4fc47a
Show file tree
Hide file tree
Showing 98 changed files with 502 additions and 351 deletions.
22 changes: 17 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ module "project" {
]
}
iam = {
"roles/editor" = [
"roles/editor" = [
"serviceAccount:${module.project.service_accounts.cloud_services}"
]
}
Expand All @@ -236,7 +236,7 @@ module "project" {
source = "./modules/project"
name = "project-example"
iam = {
"roles/editor" = [
"roles/editor" = [
"serviceAccount:${module.project.service_accounts.cloud_services}"
]
}
Expand Down Expand Up @@ -543,16 +543,16 @@ locals {

#### The `prefix` variable

If you would like to use a "prefix" variable for resource names, please keep its definition consistent across all code:
If you would like to use a "prefix" variable for resource names, please keep its definition consistent across all modules:
```hcl
# variables.tf
variable "prefix" {
description = "Optional prefix used for resource names."
type = string
default = null
validation {
condition = var.prefix != ""
error_message = "Prefix can not be empty, please use null instead."
condition = var.prefix != ""
error_message = "Prefix cannot be empty, please use null instead."
}
}
Expand All @@ -562,6 +562,18 @@ locals {
}
```

For blueprints the prefix is mandatory:
```hcl
variable "prefix" {
description = "Prefix used for resource names."
type = string
validation {
condition = var.prefix != ""
error_message = "Prefix cannot be empty."
}
}
```

### Interacting with checks, tests and tools

Our modules are designed for composition and live in a monorepo together with several end-to-end blueprints, so it was inevitable that over time we found ways of ensuring that a change does not break consumers.
Expand Down
12 changes: 6 additions & 6 deletions blueprints/cloud-operations/adfs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ Once done testing, you can clean up resources by running `terraform destroy`.
|---|---|:---:|:---:|:---:|
| [ad_dns_domain_name](variables.tf#L15) | AD DNS domain name. | <code>string</code> || |
| [adfs_dns_domain_name](variables.tf#L26) | ADFS DNS domain name. | <code>string</code> || |
| [project_id](variables.tf#L79) | Host project ID. | <code>string</code> || |
| [prefix](variables.tf#L64) | Prefix used for resource names. | <code>string</code> || |
| [project_id](variables.tf#L82) | Host project ID. | <code>string</code> || |
| [ad_ip_cidr_block](variables.tf#L20) | Managed AD IP CIDR block. | <code>string</code> | | <code>&#34;10.0.0.0&#47;24&#34;</code> |
| [disk_size](variables.tf#L31) | Disk size. | <code>number</code> | | <code>50</code> |
| [disk_type](variables.tf#L37) | Disk type. | <code>string</code> | | <code>&#34;pd-ssd&#34;</code> |
| [image](variables.tf#L43) | Image. | <code>string</code> | | <code>&#34;projects&#47;windows-cloud&#47;global&#47;images&#47;family&#47;windows-2022&#34;</code> |
| [instance_type](variables.tf#L49) | Instance type. | <code>string</code> | | <code>&#34;n1-standard-2&#34;</code> |
| [network_config](variables.tf#L55) | Network configuration | <code title="object&#40;&#123;&#10; network &#61; string&#10; subnet &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [prefix](variables.tf#L64) | Prefix for the resources created. | <code>string</code> | | <code>null</code> |
| [project_create](variables.tf#L70) | Parameters for the creation of the new project. | <code title="object&#40;&#123;&#10; billing_account_id &#61; string&#10; parent &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [region](variables.tf#L84) | Region. | <code>string</code> | | <code>&#34;europe-west1&#34;</code> |
| [subnet_ip_cidr_block](variables.tf#L90) | Subnet IP CIDR block. | <code>string</code> | | <code>&#34;10.0.1.0&#47;28&#34;</code> |
| [zone](variables.tf#L96) | Zone. | <code>string</code> | | <code>&#34;europe-west1-c&#34;</code> |
| [project_create](variables.tf#L73) | Parameters for the creation of the new project. | <code title="object&#40;&#123;&#10; billing_account_id &#61; string&#10; parent &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [region](variables.tf#L87) | Region. | <code>string</code> | | <code>&#34;europe-west1&#34;</code> |
| [subnet_ip_cidr_block](variables.tf#L93) | Subnet IP CIDR block. | <code>string</code> | | <code>&#34;10.0.1.0&#47;28&#34;</code> |
| [zone](variables.tf#L99) | Zone. | <code>string</code> | | <code>&#34;europe-west1-c&#34;</code> |

## Outputs

Expand Down
8 changes: 2 additions & 6 deletions blueprints/cloud-operations/adfs/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

locals {
prefix = (var.prefix == null || var.prefix == "") ? "" : "${var.prefix}-"
}

module "project" {
source = "../../../modules/project"
billing_account = (
Expand All @@ -41,7 +37,7 @@ module "vpc" {
count = var.network_config == null ? 1 : 0
source = "../../../modules/net-vpc"
project_id = module.project.project_id
name = "${local.prefix}vpc"
name = "${var.prefix}-vpc"
subnets = [
{
ip_cidr_range = var.subnet_ip_cidr_block
Expand Down Expand Up @@ -98,7 +94,7 @@ module "server" {

module "glb" {
source = "../../../modules/net-glb"
name = "${local.prefix}glb"
name = "${var.prefix}-glb"
project_id = module.project.project_id

https = true
Expand Down
7 changes: 5 additions & 2 deletions blueprints/cloud-operations/adfs/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,12 @@ variable "network_config" {
}

variable "prefix" {
description = "Prefix for the resources created."
description = "Prefix used for resource names."
type = string
default = null
validation {
condition = var.prefix != ""
error_message = "Prefix cannot be empty."
}
}

variable "project_create" {
Expand Down
8 changes: 4 additions & 4 deletions blueprints/cloud-operations/dns-shared-vpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ Note that Terraform 0.13 at least is required due to the use of `for_each` with
|---|---|:---:|:---:|:---:|
| [billing_account_id](variables.tf#L17) | Billing account associated with the GCP Projects that will be created for each team. | <code>string</code> || |
| [folder_id](variables.tf#L28) | Folder ID in which DNS projects will be created. | <code>string</code> || |
| [shared_vpc_link](variables.tf#L48) | Shared VPC self link, used for DNS peering. | <code>string</code> || |
| [prefix](variables.tf#L33) | Prefix used for resource names. | <code>string</code> || |
| [shared_vpc_link](variables.tf#L51) | Shared VPC self link, used for DNS peering. | <code>string</code> || |
| [dns_domain](variables.tf#L22) | DNS domain under which each application team DNS domain will be created. | <code>string</code> | | <code>&#34;example.org&#34;</code> |
| [prefix](variables.tf#L33) | Customer name to use as prefix for resources' naming. | <code>string</code> | | <code>&#34;test-dns&#34;</code> |
| [project_services](variables.tf#L39) | Service APIs enabled by default. | <code>list&#40;string&#41;</code> | | <code title="&#91;&#10; &#34;compute.googleapis.com&#34;,&#10; &#34;dns.googleapis.com&#34;,&#10;&#93;">&#91;&#8230;&#93;</code> |
| [teams](variables.tf#L53) | List of application teams requiring their own Cloud DNS instance. | <code>list&#40;string&#41;</code> | | <code title="&#91;&#10; &#34;team1&#34;,&#10; &#34;team2&#34;,&#10;&#93;">&#91;&#8230;&#93;</code> |
| [project_services](variables.tf#L42) | Service APIs enabled by default. | <code>list&#40;string&#41;</code> | | <code title="&#91;&#10; &#34;compute.googleapis.com&#34;,&#10; &#34;dns.googleapis.com&#34;,&#10;&#93;">&#91;&#8230;&#93;</code> |
| [teams](variables.tf#L56) | List of application teams requiring their own Cloud DNS instance. | <code>list&#40;string&#41;</code> | | <code title="&#91;&#10; &#34;team1&#34;,&#10; &#34;team2&#34;,&#10;&#93;">&#91;&#8230;&#93;</code> |

## Outputs

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ variable "billing_account" {
}

variable "prefix" {
description = "Customer name to use as prefix for resources' naming."
default = "test-dns"
description = "Prefix used for resource names."
type = string
validation {
condition = var.prefix != ""
error_message = "Prefix cannot be empty."
}
}

variable "dns_domain" {
Expand Down
7 changes: 5 additions & 2 deletions blueprints/cloud-operations/dns-shared-vpc/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ variable "folder_id" {
}

variable "prefix" {
description = "Customer name to use as prefix for resources' naming."
description = "Prefix used for resource names."
type = string
default = "test-dns"
validation {
condition = var.prefix != ""
error_message = "Prefix cannot be empty."
}
}

variable "project_services" {
Expand Down
8 changes: 4 additions & 4 deletions blueprints/cloud-operations/network-dashboard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ If you are interested in this and/or would like to contribute, please contact le
| [billing_account](variables.tf#L17) | The ID of the billing account to associate this project with | <code></code> | ✓ | |
| [monitored_projects_list](variables.tf#L36) | ID of the projects to be monitored (where limits and quotas data will be pulled) | <code>list&#40;string&#41;</code> | ✓ | |
| [organization_id](variables.tf#L46) | The organization id for the associated services | <code></code> | ✓ | |
| [prefix](variables.tf#L50) | Customer name to use as prefix for monitoring project | <code></code> | ✓ | |
| [prefix](variables.tf#L50) | Prefix used for resource names. | <code>string</code> | ✓ | |
| [cf_version](variables.tf#L21) | Cloud Function version 2nd Gen or 1st Gen. Possible options: 'V1' or 'V2'.Use CFv2 if your Cloud Function timeouts after 9 minutes. By default it is using CFv1. | <code></code> | | <code>V1</code> |
| [monitored_folders_list](variables.tf#L30) | ID of the projects to be monitored (where limits and quotas data will be pulled) | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
| [monitoring_project_id](variables.tf#L41) | Monitoring project where the dashboard will be created and the solution deployed; a project will be created if set to empty string | <code></code> | | |
| [project_monitoring_services](variables.tf#L54) | Service APIs enabled in the monitoring project if it will be created. | <code></code> | | <code title="&#91;&#10; &#34;artifactregistry.googleapis.com&#34;,&#10; &#34;cloudasset.googleapis.com&#34;,&#10; &#34;cloudbilling.googleapis.com&#34;,&#10; &#34;cloudbuild.googleapis.com&#34;,&#10; &#34;cloudfunctions.googleapis.com&#34;,&#10; &#34;cloudresourcemanager.googleapis.com&#34;,&#10; &#34;cloudscheduler.googleapis.com&#34;,&#10; &#34;compute.googleapis.com&#34;,&#10; &#34;iam.googleapis.com&#34;,&#10; &#34;iamcredentials.googleapis.com&#34;,&#10; &#34;logging.googleapis.com&#34;,&#10; &#34;monitoring.googleapis.com&#34;,&#10; &#34;pubsub.googleapis.com&#34;,&#10; &#34;run.googleapis.com&#34;,&#10; &#34;servicenetworking.googleapis.com&#34;,&#10; &#34;serviceusage.googleapis.com&#34;,&#10; &#34;storage-component.googleapis.com&#34;&#10;&#93;">&#91;&#8230;&#93;</code> |
| [region](variables.tf#L76) | Region used to deploy the cloud functions and scheduler | <code></code> | | <code>europe-west1</code> |
| [schedule_cron](variables.tf#L81) | Cron format schedule to run the Cloud Function. Default is every 10 minutes. | <code></code> | | <code>&#42;&#47;10 &#42; &#42; &#42; &#42;</code> |
| [project_monitoring_services](variables.tf#L59) | Service APIs enabled in the monitoring project if it will be created. | <code></code> | | <code title="&#91;&#10; &#34;artifactregistry.googleapis.com&#34;,&#10; &#34;cloudasset.googleapis.com&#34;,&#10; &#34;cloudbilling.googleapis.com&#34;,&#10; &#34;cloudbuild.googleapis.com&#34;,&#10; &#34;cloudfunctions.googleapis.com&#34;,&#10; &#34;cloudresourcemanager.googleapis.com&#34;,&#10; &#34;cloudscheduler.googleapis.com&#34;,&#10; &#34;compute.googleapis.com&#34;,&#10; &#34;iam.googleapis.com&#34;,&#10; &#34;iamcredentials.googleapis.com&#34;,&#10; &#34;logging.googleapis.com&#34;,&#10; &#34;monitoring.googleapis.com&#34;,&#10; &#34;pubsub.googleapis.com&#34;,&#10; &#34;run.googleapis.com&#34;,&#10; &#34;servicenetworking.googleapis.com&#34;,&#10; &#34;serviceusage.googleapis.com&#34;,&#10; &#34;storage-component.googleapis.com&#34;&#10;&#93;">&#91;&#8230;&#93;</code> |
| [region](variables.tf#L81) | Region used to deploy the cloud functions and scheduler | <code></code> | | <code>europe-west1</code> |
| [schedule_cron](variables.tf#L86) | Cron format schedule to run the Cloud Function. Default is every 10 minutes. | <code></code> | | <code>&#42;&#47;10 &#42; &#42; &#42; &#42;</code> |
<!-- END TFDOC -->
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def count_effective_limit(config, project_id, network_dict, usage_metric_name,
for peered_network in network_dict['peerings']:
if 'usage' not in peered_network:
print(
f"Can not add metrics for peered network in projects/{project_id} as no usage metrics exist due to missing permissions"
f"Cannot add metrics for peered network in projects/{project_id} as no usage metrics exist due to missing permissions"
)
continue
peering_group_usage += peered_network['usage']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ variable "billing_account" {
}

variable "prefix" {
description = "Customer name to use as prefix for resources' naming"
description = "Prefix used for resource names."
type = string
validation {
condition = var.prefix != ""
error_message = "Prefix cannot be empty."
}
}

variable "project_vm_services" {
Expand Down
7 changes: 6 additions & 1 deletion blueprints/cloud-operations/network-dashboard/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ variable "organization_id" {
}

variable "prefix" {
description = "Customer name to use as prefix for monitoring project"
description = "Prefix used for resource names."
type = string
validation {
condition = var.prefix != ""
error_message = "Prefix cannot be empty."
}
}

variable "project_monitoring_services" {
Expand Down
12 changes: 6 additions & 6 deletions blueprints/data-solutions/cloudsql-multiregion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,15 @@ The above command will delete the associated resources so there will be no billa
| name | description | type | required | default |
|---|---|:---:|:---:|:---:|
| [postgres_user_password](variables.tf#L40) | `postgres` user password. | <code>string</code> || |
| [prefix](variables.tf#L45) | Unique prefix used for resource names. Not used for project if 'project_create' is null. | <code>string</code> || |
| [project_id](variables.tf#L59) | Project id, references existing project if `project_create` is null. | <code>string</code> || |
| [prefix](variables.tf#L45) | Prefix used for resource names. | <code>string</code> || |
| [project_id](variables.tf#L63) | Project id, references existing project if `project_create` is null. | <code>string</code> || |
| [data_eng_principals](variables.tf#L17) | Groups with Service Account Token creator role on service accounts in IAM format, only user supported on CloudSQL, eg 'user@domain.com'. | <code>list&#40;string&#41;</code> | | <code>&#91;&#93;</code> |
| [network_config](variables.tf#L23) | Shared VPC network configurations to use. If null networks will be created in projects with preconfigured values. | <code title="object&#40;&#123;&#10; host_project &#61; string&#10; network_self_link &#61; string&#10; subnet_self_link &#61; string&#10; cloudsql_psa_range &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [postgres_database](variables.tf#L34) | `postgres` database. | <code>string</code> | | <code>&#34;guestbook&#34;</code> |
| [project_create](variables.tf#L50) | Provide values if project creation is needed, uses existing project if null. Parent is in 'folders/nnn' or 'organizations/nnn' format. | <code title="object&#40;&#123;&#10; billing_account_id &#61; string&#10; parent &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [regions](variables.tf#L64) | Map of instance_name => location where instances will be deployed. | <code>map&#40;string&#41;</code> | | <code title="&#123;&#10; primary &#61; &#34;europe-west1&#34;&#10; replica &#61; &#34;europe-west3&#34;&#10;&#125;">&#123;&#8230;&#125;</code> |
| [service_encryption_keys](variables.tf#L77) | Cloud KMS keys to use to encrypt resources. Provide a key for each reagion configured. | <code>map&#40;string&#41;</code> | | <code>null</code> |
| [sql_configuration](variables.tf#L83) | Cloud SQL configuration | <code title="object&#40;&#123;&#10; availability_type &#61; string&#10; database_version &#61; string&#10; psa_range &#61; string&#10; tier &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; availability_type &#61; &#34;REGIONAL&#34;&#10; database_version &#61; &#34;POSTGRES_13&#34;&#10; psa_range &#61; &#34;10.60.0.0&#47;16&#34;&#10; tier &#61; &#34;db-g1-small&#34;&#10;&#125;">&#123;&#8230;&#125;</code> |
| [project_create](variables.tf#L54) | Provide values if project creation is needed, uses existing project if null. Parent is in 'folders/nnn' or 'organizations/nnn' format. | <code title="object&#40;&#123;&#10; billing_account_id &#61; string&#10; parent &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [regions](variables.tf#L68) | Map of instance_name => location where instances will be deployed. | <code>map&#40;string&#41;</code> | | <code title="&#123;&#10; primary &#61; &#34;europe-west1&#34;&#10; replica &#61; &#34;europe-west3&#34;&#10;&#125;">&#123;&#8230;&#125;</code> |
| [service_encryption_keys](variables.tf#L81) | Cloud KMS keys to use to encrypt resources. Provide a key for each reagion configured. | <code>map&#40;string&#41;</code> | | <code>null</code> |
| [sql_configuration](variables.tf#L87) | Cloud SQL configuration | <code title="object&#40;&#123;&#10; availability_type &#61; string&#10; database_version &#61; string&#10; psa_range &#61; string&#10; tier &#61; string&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; availability_type &#61; &#34;REGIONAL&#34;&#10; database_version &#61; &#34;POSTGRES_13&#34;&#10; psa_range &#61; &#34;10.60.0.0&#47;16&#34;&#10; tier &#61; &#34;db-g1-small&#34;&#10;&#125;">&#123;&#8230;&#125;</code> |

## Outputs

Expand Down
6 changes: 5 additions & 1 deletion blueprints/data-solutions/cloudsql-multiregion/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ variable "postgres_user_password" {
}

variable "prefix" {
description = "Unique prefix used for resource names. Not used for project if 'project_create' is null."
description = "Prefix used for resource names."
type = string
validation {
condition = var.prefix != ""
error_message = "Prefix cannot be empty."
}
}

variable "project_create" {
Expand Down
Loading

0 comments on commit e4fc47a

Please sign in to comment.