Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SlurmGCP. Relax reservation_name match, allow for suffix #3170

Merged
merged 1 commit into from
Oct 28, 2024

Conversation

mr0re1
Copy link
Collaborator

@mr0re1 mr0re1 commented Oct 25, 2024

Testing

# main.tf
variable "project_id" { default="var_P" }

variable "reservation_name" {
  type=string
  validation {
    condition     = length(regexall("^((projects/([a-z0-9-]+)/reservations/)?([a-z0-9-]+)(/[a-z0-9-]+/[a-z0-9-]+)?)?$", var.reservation_name)) > 0
    error_message = "Reservation name must be either empty or in the format '[projects/PROJECT_ID/reservations/]RESERVATION_NAME[/SUFF/IX]', [...] are optional parts."
  }
}

locals {
  res_match = regex("^(?P<whole>(?P<prefix>projects/(?P<project>[a-z0-9-]+)/reservations/)?(?<name>[a-z0-9-]+)(?P<suffix>/[a-z0-9-]+/[a-z0-9-]+)?)?$", var.reservation_name)
  
  res_short_name = local.res_match.name
  res_project = coalesce(local.res_match.project, var.project_id)
  res_prefix = coalesce(local.res_match.prefix, "projects/${local.res_project}/reservations/")
  res_suffix = local.res_match.suffix == null ? "" : local.res_match.suffix
  
  reservation_name = local.res_match.whole == null ? "" : "${local.res_prefix}${local.res_short_name}${local.res_suffix}"
}

output "got" {
  value = {
    short_name    = local.res_short_name
    project = local.res_project
    full_name = local.reservation_name
  }}
$ terraform apply -var="reservation_name="
got = {
  "full_name" = ""
  "project" = "var_P"
  "short_name" = tostring(null)
}

$ terraform apply -var="reservation_name=olaf"
got = {
  "full_name" = "projects/var_P/reservations/olaf"
  "project" = "var_P"
  "short_name" = "olaf"
}

$ terraform apply -var="reservation_name=olaf/one/two"
got = {
  "full_name" = "projects/var_P/reservations/olaf/one/two"
  "project" = "var_P"
  "short_name" = "olaf"
}

$ terraform apply -var="reservation_name=projects/he-he/reservations/olaf"
got = {
  "full_name" = "projects/he-he/reservations/olaf"
  "project" = "he-he"
  "short_name" = "olaf"
}

$ terraform apply -var="reservation_name=projects/he-he/reservations/olaf/one/two"
got = {
  "full_name" = "projects/he-he/reservations/olaf/one/two"
  "project" = "he-he"
  "short_name" = "olaf"
}


$ terraform apply -var="reservation_name=olaf/one"
╷
│ Error: Invalid value for variable
│
│   on main.tf line 3:
│    3: variable "reservation_name" {
│     ├────────────────
│     │ var.reservation_name is "olaf/one"
│
│ Reservation name must be in the format '[projects/PROJECT_ID/reservations/]RESERVATION_NAME[/SUFF/IX]', [...] are optional parts.
│
│ This was checked by the validation rule at main.tf:5,3-13.
╵

# Also fails:
$ terraform apply -var="reservation_name=projects/he-he/reservations/olaf/one"

@mr0re1 mr0re1 added the release-chore To not include into release notes label Oct 25, 2024
@mr0re1 mr0re1 requested a review from abbas1902 October 25, 2024 21:25
@abbas1902 abbas1902 assigned mr0re1 and unassigned abbas1902 Oct 28, 2024
@mr0re1 mr0re1 merged commit 19fa5ca into GoogleCloudPlatform:develop Oct 28, 2024
16 of 56 checks passed
@mr0re1 mr0re1 deleted the res_name_suf branch October 28, 2024 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-chore To not include into release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants