Problem:
Google Cloud CLI and Terraform Provider for Google Cloud do not provide clear enough documentation on usage of Regular Expressions, and inconsistency when executing identical expressions extracted from HTTP traces of successful regular expressions generated by the CLI.
Diagnosis:
Google Cloud CLI command to list all Public Images (alhough by default does not require the operator): gcloud compute images list --standard-images
.
This shows many Public Images:
$ gcloud compute images list --standard-images
NAME PROJECT FAMILY DEPRECATED STATUS
...
ubuntu-2210-kinetic-amd64-v20230125 ubuntu-os-cloud ubuntu-2210-amd64 READY
...
Using the legacy Google Cloud CLI command operator --regexp
:
$ gcloud compute images list --regexp ".*ubuntu.*minimal.*2210.*amd64.*"
WARNING: Flag `--regexp` is deprecated. Use `--filter="name~'REGEXP'"` instead.
NAME PROJECT FAMILY DEPRECATED STATUS
ubuntu-minimal-2210-kinetic-amd64-v20230126 ubuntu-os-cloud ubuntu-minimal-2210-amd64 READY
Using the Google Cloud CLI command operator --filter
:
$ gcloud compute images list --filter="name~ubuntu.*minimal.*2210.*amd64"
NAME PROJECT FAMILY DEPRECATED STATUS
ubuntu-minimal-2210-kinetic-amd64-v20230126 ubuntu-os-cloud ubuntu-minimal-2210-amd64 READY
The following are the two full regular expressions seen in the HTTP Trace:
# Google Cloud CLI with --regexp and --log-http
/images?filter=name+eq+".*(^.*ubuntu.*minimal.*2210.*amd64.*$).*"
# Google Cloud CLI with --filter and --log-http
/images?filter=name+eq+".*(ubuntu.*minimal.*2210.*amd64).*"
Error:
Replicating either of these regex commands, do not work and return 0 items.
Error when using CLI
Error when using regex used in the legacy Google Cloud CLI command operator --regexp
:
$ gcloud compute images list --filter='name eq ".*(^.*ubuntu.*minimal.*2210.*amd64.*$).*"'
Listed 0 items.
Error when using regex used in the Google Cloud CLI command operator --filter
:
$ gcloud compute images list --standard-images --filter='name eq ".*(ubuntu.*minimal.*2210.*amd64).*"'
Listed 0 items.
Error when using Terraform
There is no Terraform Resource for data lookup of all GCP OS Images, therefore the filter argument is used to select a single OS Image using the regular expression.
The following regular expression works:
data "google_compute_image" "select_os_image" {
project = "ubuntu-os-cloud" // The project must be set to the GCP Marketplace Product owner as the GCP OS 'Public Image Project', e.g. ubuntu-os-cloud
filter = "(name eq 'ubuntu-minimal-2210-kinetic-amd64-v20230126')"
}
Terraform success:
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Whereas reusing either of the unsuccessful regular expressions with Google Cloud CLI, also does not work:
data "google_compute_image" "select_os_image" {
project = "ubuntu-os-cloud" // The project must be set to the GCP Marketplace Product owner as the GCP OS 'Public Image Project', e.g. ubuntu-os-cloud
filter = "(name eq '.*(ubuntu.*minimal.*2210.*amd64).*')"
}
Terraform error:
│ Error: your filter has returned more than one image or no image. Please refine your filter to return exactly one image
│
│ with data.google_compute_image.bastion_os_image,
│ on ../data_os_image.tf line 10, in data "google_compute_image" "select_os_image":
│ 10: data "google_compute_image" "select_os_image" {