Skip to content

Commit

Permalink
Fix: derive project id for instance from instance template (#63)
Browse files Browse the repository at this point in the history
* get project id from instance template selflink

* pass subnet selflink instead of name fixture
  • Loading branch information
bharathkkb authored and morgante committed Jan 21, 2020
1 parent a3d482f commit 3e2c8cd
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/compute_instance/simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This is a simple, minimal example of how to use the compute_instance module
| project\_id | The GCP project to use for integration tests | string | n/a | yes |
| region | The GCP region to create and test resources in | string | `"us-central1"` | no |
| 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 |
| subnetwork | The subnetwork to host the compute instances in | string | n/a | yes |
| subnetwork | The subnetwork selflink to host the compute instances in | string | n/a | yes |

## Outputs

Expand Down
2 changes: 1 addition & 1 deletion examples/compute_instance/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

provider "google" {

project = var.project_id
version = "~> 2.7.0"
}

module "instance_template" {
source = "../../../modules/instance_template"
region = var.region
project_id = var.project_id
subnetwork = var.subnetwork
service_account = var.service_account
}
Expand Down
2 changes: 1 addition & 1 deletion examples/compute_instance/simple/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ variable "region" {
}

variable "subnetwork" {
description = "The subnetwork to host the compute instances in"
description = "The subnetwork selflink to host the compute instances in"
}

variable "num_instances" {
Expand Down
5 changes: 4 additions & 1 deletion modules/compute_instance/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@ locals {
# at the end of the list to work around "list does not have any elements so cannot
# determine type" error when var.static_ips is empty
static_ips = concat(var.static_ips, ["NOT_AN_IP"])
project_id = length(regexall("/projects/([^/]*)", var.instance_template)) > 0 ? flatten(regexall("/projects/([^/]*)", var.instance_template))[0] : null
}

###############
# Data Sources
###############

data "google_compute_zones" "available" {
region = var.region
project = local.project_id
region = var.region
}

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

network_interface {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/compute_instance/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ module "instance_simple" {
source = "../../../../examples/compute_instance/simple"
project_id = var.project_id
region = "us-central1"
subnetwork = google_compute_subnetwork.main.name
subnetwork = google_compute_subnetwork.main.self_link
num_instances = 4
service_account = var.service_account
}

0 comments on commit 3e2c8cd

Please sign in to comment.