From 5149795becce5d635a8f11e6c07f011c7ea349eb Mon Sep 17 00:00:00 2001 From: Ludovico Magnocavallo Date: Wed, 31 Mar 2021 20:01:10 +0200 Subject: [PATCH] Allow using a non-suffixed name in compute-vm (#217) * allow using a non-suffixed name in compute-vm * update README --- modules/compute-vm/README.md | 1 + modules/compute-vm/main.tf | 12 ++++++++---- modules/compute-vm/variables.tf | 6 ++++++ tests/modules/compute_vm/fixture/main.tf | 1 + tests/modules/compute_vm/fixture/variables.tf | 9 +++++++-- tests/modules/compute_vm/test_plan.py | 7 +++++++ 6 files changed, 30 insertions(+), 6 deletions(-) diff --git a/modules/compute-vm/README.md b/modules/compute-vm/README.md index 3bee1daa..e025e1f6 100644 --- a/modules/compute-vm/README.md +++ b/modules/compute-vm/README.md @@ -283,6 +283,7 @@ module "instance-group" { | *service_account_create* | Auto-create service account. | bool | | false | | *service_account_scopes* | Scopes applied to service account. | list(string) | | [] | | *shielded_config* | Shielded VM configuration of the instances. | object({...}) | | null | +| *single_name* | Do not append progressive count to instance name. | bool | | false | | *tags* | Instance tags. | list(string) | | [] | | *use_instance_template* | Create instance template instead of instances. | bool | | false | | *zones* | Compute zone, instance will cycle through the list, defaults to the 'b' zone in the region. | list(string) | | [] | diff --git a/modules/compute-vm/main.tf b/modules/compute-vm/main.tf index 4c164079..08c0b1b6 100644 --- a/modules/compute-vm/main.tf +++ b/modules/compute-vm/main.tf @@ -43,9 +43,13 @@ locals { "${pair.0}/${pair.1}" => { role = pair.0, name = pair.1, members = var.iam[pair.0] } } names = ( - var.use_instance_template ? { (var.name) = 0 } : { - for i in range(0, var.instance_count) : "${var.name}-${i + 1}" => i - } + var.use_instance_template + ? { (var.name) = 0 } + : ( + var.single_name && var.instance_count == 1 + ? { (var.name) = 0 } + : { for i in range(0, var.instance_count) : "${var.name}-${i + 1}" => i } + ) ) service_account_email = ( var.service_account_create @@ -64,7 +68,7 @@ locals { ? [ "https://www.googleapis.com/auth/cloud-platform", "https://www.googleapis.com/auth/userinfo.email" - ] + ] : [ "https://www.googleapis.com/auth/devstorage.read_only", "https://www.googleapis.com/auth/logging.write", diff --git a/modules/compute-vm/variables.tf b/modules/compute-vm/variables.tf index 489414ca..c96dace7 100644 --- a/modules/compute-vm/variables.tf +++ b/modules/compute-vm/variables.tf @@ -230,6 +230,12 @@ variable "service_account_scopes" { default = [] } +variable "single_name" { + description = "Do not append progressive count to instance name." + type = bool + default = false +} + variable "tags" { description = "Instance tags." type = list(string) diff --git a/tests/modules/compute_vm/fixture/main.tf b/tests/modules/compute_vm/fixture/main.tf index 143333b4..1690e4d5 100644 --- a/tests/modules/compute_vm/fixture/main.tf +++ b/tests/modules/compute_vm/fixture/main.tf @@ -29,4 +29,5 @@ module "test" { iam = var.iam metadata = var.metadata metadata_list = var.metadata_list + single_name = var.single_name } diff --git a/tests/modules/compute_vm/fixture/variables.tf b/tests/modules/compute_vm/fixture/variables.tf index f612dbc7..b0039d2c 100644 --- a/tests/modules/compute_vm/fixture/variables.tf +++ b/tests/modules/compute_vm/fixture/variables.tf @@ -64,12 +64,17 @@ variable "network_interfaces" { }] } -variable "use_instance_template" { +variable "service_account_create" { type = bool default = false } -variable "service_account_create" { +variable "single_name" { + type = bool + default = false +} + +variable "use_instance_template" { type = bool default = false } diff --git a/tests/modules/compute_vm/test_plan.py b/tests/modules/compute_vm/test_plan.py index 01653933..43697d7b 100644 --- a/tests/modules/compute_vm/test_plan.py +++ b/tests/modules/compute_vm/test_plan.py @@ -26,6 +26,13 @@ def test_single_instance(plan_runner): assert resources[0]['type'] == 'google_compute_instance' +def test_single_instance_single_name(plan_runner): + _, resources = plan_runner(FIXTURES_DIR, single_name=1) + assert len(resources) == 1 + assert resources[0]['type'] == 'google_compute_instance' + assert resources[0]['values']['name'] == 'test' + + def test_multiple_instances(plan_runner): _, resources = plan_runner(FIXTURES_DIR, instance_count=2) assert len(resources) == 2