Skip to content

Commit

Permalink
3083 fix validation (#33)
Browse files Browse the repository at this point in the history
* refs platform/#3083: fix variable validation

* fix: updated changelog

* Update variables.tf

Co-authored-by: Andrea Panisson <[email protected]>

* Update variables.tf

Co-authored-by: Andrea Panisson <[email protected]>

* Update variables.tf

Co-authored-by: Andrea Panisson <[email protected]>

* feart: better regex in condition

* fix: typo iin comment

* feat: update readme and error message

---------

Co-authored-by: Andrea Panisson <[email protected]>
  • Loading branch information
Stevesibilia and andypanix authored Oct 31, 2024
1 parent 3cad8b6 commit 15f0ab0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Update module `terraform-google-gcp-application-bucket-creation-helper` to `0.8.1` with fix compatibility between tags and random suffix in resource creation.
- Update module `terraform-google-gcp-mysql-db-and-user-creation-helper` to `0.3.2` with fix to accidental mysql credential exposure.
- Changed `drupal_projects_list.project_name` max lenght to 16 (was 23).
- Changed `drupal_projects_list.project_name` max lenght to 16 database creation is handled by the module itself.

## [0.12.0] - 2024-08-08

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ the random suffix `bucket_append_random_suffix` for the bucket name.

| Name | Version |
|------|---------|
| <a name="provider_google"></a> [google](#provider\_google) | 5.40.0 |
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | 2.31.0 |
| <a name="provider_google"></a> [google](#provider\_google) | >= 4.47.0 |
| <a name="provider_kubernetes"></a> [kubernetes](#provider\_kubernetes) | >= 2.19 |
| <a name="provider_random"></a> [random](#provider\_random) | 3.6.2 |
| <a name="provider_template"></a> [template](#provider\_template) | 2.2.0 |
| <a name="provider_template"></a> [template](#provider\_template) | >= 2.2.0 |
## Requirements

| Name | Version |
Expand Down Expand Up @@ -169,7 +169,7 @@ the random suffix `bucket_append_random_suffix` for the bucket name.

| Name | Source | Version |
|------|--------|---------|
| <a name="module_drupal_buckets"></a> [drupal\_buckets](#module\_drupal\_buckets) | github.com/sparkfabrik/terraform-google-gcp-application-bucket-creation-helper | 0.8.0 |
| <a name="module_drupal_databases_and_users"></a> [drupal\_databases\_and\_users](#module\_drupal\_databases\_and\_users) | github.com/sparkfabrik/terraform-google-gcp-mysql-db-and-user-creation-helper | 0.3.1 |
| <a name="module_drupal_buckets"></a> [drupal\_buckets](#module\_drupal\_buckets) | github.com/sparkfabrik/terraform-google-gcp-application-bucket-creation-helper | 0.8.1 |
| <a name="module_drupal_databases_and_users"></a> [drupal\_databases\_and\_users](#module\_drupal\_databases\_and\_users) | github.com/sparkfabrik/terraform-google-gcp-mysql-db-and-user-creation-helper | 0.3.2 |

<!-- END_TF_DOCS -->
22 changes: 16 additions & 6 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,31 @@ variable "drupal_projects_list" {
}))

validation {
# The project name must contain only lower caps letters and "-" and "_" and be between 6 and 16 characters long, since the database user name must be less than 32 chars and we append "<PRJ_ID>_<BRANCH>_dp_u" (i.e.:"_759_main_dp_u") to the project name.
# project_name must:
# - start with a lowercase letter or a number
# - contain only lowercase letters, numbers, hyphens and underscores
# and be:
# - 6 to 16 characters long if database_host is not null meaning that the database will be created from the module
# - 6 to 23 characters long if database_host, database_name, database_user_name are not null meaning that database name and user name are passed to the module
# - 6 to 23 characters long if database_host is null meaning that no database will be created from the module
condition = alltrue([
for p in var.drupal_projects_list :
can(regex("^[0-9a-z_-]{6,16}$", p.project_name))
(can(regex("^[0-9a-z][0-9a-z_-]{4,21}[0-9a-z]$", p.project_name)) && length(p.project_name) > 5) &&
(
(p.database_host != null && p.database_name == null && p.database_user_name == null && length(p.project_name) <= 16) ||
(p.database_host != null && p.database_name != null && p.database_user_name != null && length(p.project_name) <= 23) ||
(p.database_host == null && length(p.project_name) <= 23)
)
])
error_message = "The name of the Drupal project should be between 6 and 16 characters long and contains only numbers, lower caps letters and \"_-\"."
error_message = "The project name is invalid. Must be 6 to 16 characters long, with only lowercase letters, numbers, hyphens and underscores if the database must be created by the module or 6 to 23 characters long if we pass database_host database_user_name and database_name to the module."
}

validation {
# The project name must contain only lower caps letters and "-" and "_" and be between 6 and 16 characters long, since the database user name must be less than 32 chars and we append and we append "<PRJ_ID>_<BRANCH>_dp_u" (i.e.:"_759_main_dp_u") to the project name.
condition = alltrue([
for p in var.drupal_projects_list :
can(regex("^[0-9a-z]{1}[0-9a-z_-]+[0-9a-z]{1}$", p.project_name))
(can(regex("^[0-9a-z_-]{6,32}$", p.database_user_name))) || p.database_user_name == null
])
error_message = "The name of the Drupal project can start and end only with a lower caps letters or numbers."
error_message = "The database user name is invalid. Must be 6 to 32 characters long, with only lowercase letters, numbers, hyphens and underscores."
}
}

Expand Down

0 comments on commit 15f0ab0

Please sign in to comment.