Skip to content

Commit 3e2c8cd

Browse files
bharathkkbmorgante
authored andcommitted
Fix: derive project id for instance from instance template (#63)
* get project id from instance template selflink * pass subnet selflink instead of name fixture
1 parent a3d482f commit 3e2c8cd

File tree

5 files changed

+8
-5
lines changed

5 files changed

+8
-5
lines changed

examples/compute_instance/simple/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ This is a simple, minimal example of how to use the compute_instance module
1111
| project\_id | The GCP project to use for integration tests | string | n/a | yes |
1212
| region | The GCP region to create and test resources in | string | `"us-central1"` | no |
1313
| service\_account | Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template.html#service_account. | object | `"null"` | no |
14-
| subnetwork | The subnetwork to host the compute instances in | string | n/a | yes |
14+
| subnetwork | The subnetwork selflink to host the compute instances in | string | n/a | yes |
1515

1616
## Outputs
1717

examples/compute_instance/simple/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616

1717
provider "google" {
1818

19-
project = var.project_id
2019
version = "~> 2.7.0"
2120
}
2221

2322
module "instance_template" {
2423
source = "../../../modules/instance_template"
2524
region = var.region
25+
project_id = var.project_id
2626
subnetwork = var.subnetwork
2727
service_account = var.service_account
2828
}

examples/compute_instance/simple/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ variable "region" {
2828
}
2929

3030
variable "subnetwork" {
31-
description = "The subnetwork to host the compute instances in"
31+
description = "The subnetwork selflink to host the compute instances in"
3232
}
3333

3434
variable "num_instances" {

modules/compute_instance/main.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,16 @@ locals {
2222
# at the end of the list to work around "list does not have any elements so cannot
2323
# determine type" error when var.static_ips is empty
2424
static_ips = concat(var.static_ips, ["NOT_AN_IP"])
25+
project_id = length(regexall("/projects/([^/]*)", var.instance_template)) > 0 ? flatten(regexall("/projects/([^/]*)", var.instance_template))[0] : null
2526
}
2627

2728
###############
2829
# Data Sources
2930
###############
3031

3132
data "google_compute_zones" "available" {
32-
region = var.region
33+
project = local.project_id
34+
region = var.region
3335
}
3436

3537
#############
@@ -40,6 +42,7 @@ resource "google_compute_instance_from_template" "compute_instance" {
4042
provider = google
4143
count = local.num_instances
4244
name = "${local.hostname}-${format("%03d", count.index + 1)}"
45+
project = local.project_id
4346
zone = data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)]
4447

4548
network_interface {

test/fixtures/compute_instance/simple/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module "instance_simple" {
1919
source = "../../../../examples/compute_instance/simple"
2020
project_id = var.project_id
2121
region = "us-central1"
22-
subnetwork = google_compute_subnetwork.main.name
22+
subnetwork = google_compute_subnetwork.main.self_link
2323
num_instances = 4
2424
service_account = var.service_account
2525
}

0 commit comments

Comments
 (0)