Skip to content

Commit

Permalink
Update example usage of location_from_id, project_from_id, and region…
Browse files Browse the repository at this point in the history
…_from_zone functions (#10265)
  • Loading branch information
SarahFrench authored Mar 23, 2024
1 parent f0f29d2 commit 06c363e
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,27 @@ terraform {
}
}
resource "google_cloud_run_service" "default" {
name = "my-service"
location = "us-central1"
template {
spec {
containers {
image = "us-docker.pkg.dev/cloudrun/container/hello"
}
}
}
traffic {
percent = 100
latest_revision = true
}
}
# Value is "us-central1"
output "function_output" {
value = provider::google::location_from_id("https://run.googleapis.com/v2/projects/my-project/locations/us-central1/services/my-service")
output "location_from_id" {
value = provider::google::location_from_id(google_cloud_run_service.default.id)
}
```

Expand All @@ -40,9 +58,29 @@ terraform {
}
}
resource "google_cloud_run_service" "default" {
# provider argument omitted - provisioning by google or google-beta doesn't impact this example
name = "my-service"
location = "us-central1"
template {
spec {
containers {
image = "us-docker.pkg.dev/cloudrun/container/hello"
}
}
}
traffic {
percent = 100
latest_revision = true
}
}
# Value is "us-central1"
output "function_output" {
value = provider::google-beta::location_from_id("https://run.googleapis.com/v2/projects/my-project/locations/us-central1/services/my-service")
output "location_from_id" {
value = provider::google-beta::location_from_id(google_cloud_run_service.default.id)
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ terraform {
}
}
resource "google_pubsub_topic" "default" {
project = "my-project"
name = "my-topic"
}
# Value is "my-project"
output "function_output" {
value = provider::google::project_from_id("https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/instances/my-instance")
output "project_from_id" {
value = provider::google::project_from_id(google_pubsub_topic.default.id)
}
```

Expand All @@ -40,9 +45,15 @@ terraform {
}
}
resource "google_pubsub_topic" "default" {
# provider argument omitted - provisioning by google or google-beta doesn't impact this example
project = "my-project"
name = "my-topic"
}
# Value is "my-project"
output "function_output" {
value = provider::google-beta::project_from_id("https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-c/instances/my-instance")
output "project_from_id" {
value = provider::google-beta::project_from_id(google_pubsub_topic.default.id)
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,48 @@ terraform {
}
}
# Value is "us-central1"
output "function_output" {
value = provider::google::region_from_zone("us-central1-b")
resource "google_compute_instance" "default" {
name = "my-instance"
machine_type = "n2-standard-2"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
labels = {
my_label = "value"
}
}
}
network_interface {
network = "default"
subnetwork = google_compute_subnetwork.default.id
access_config {
// Ephemeral public IP
}
}
metadata_startup_script = "echo hi > /test.txt"
}
data "google_compute_network" "default" {
name = "default"
}
resource "google_compute_subnetwork" "default" {
name = "my-subnet"
region = "us-central1"
network = data.google_compute_network.default.id
ip_cidr_range = "192.168.10.0/24"
}
// The region_from_zone function is used to assert that the VM and subnet are in the same region
check "vm_subnet_compatibility_check" {
assert {
condition = google_compute_subnetwork.default.region == provider::google::region_from_zone(google_compute_instance.default.zone)
error_message = "Subnet ${google_compute_subnetwork.default.id} and VM ${google_compute_instance.default.id} are not in the same region"
}
}
```

Expand All @@ -40,9 +79,51 @@ terraform {
}
}
# Value is "us-central1"
output "function_output" {
value = provider::google-beta::region_from_zone("us-central1-b")
resource "google_compute_instance" "default" {
provider = google-beta
name = "my-instance"
machine_type = "n2-standard-2"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
labels = {
my_label = "value"
}
}
}
network_interface {
network = "default"
subnetwork = google_compute_subnetwork.default.id
access_config {
// Ephemeral public IP
}
}
metadata_startup_script = "echo hi > /test.txt"
}
data "google_compute_network" "default" {
provider = google-beta
name = "default"
}
resource "google_compute_subnetwork" "default" {
provider = google-beta
name = "my-subnet"
region = "us-central1"
network = data.google_compute_network.default.id
ip_cidr_range = "192.168.10.0/24"
}
// The region_from_zone function is used to assert that the VM and subnet are in the same region
check "vm_subnet_compatibility_check" {
assert {
condition = google_compute_subnetwork.default.region == provider::google-beta::region_from_zone(google_compute_instance.default.zone)
error_message = "Subnet ${google_compute_subnetwork.default.id} and VM ${google_compute_instance.default.id} are not in the same region"
}
}
```

Expand Down

0 comments on commit 06c363e

Please sign in to comment.