Skip to content

Commit

Permalink
feat: Add option to specify zone in compute_instance (#162)
Browse files Browse the repository at this point in the history
* Add option to specify zone in compute_instance

Also added a test that creates two instances and ensure they're in the same specified zone.

* Update docs

* Update examples/compute_instance/simple/variables.tf

Co-authored-by: Bharath KKB <[email protected]>

* Update modules/compute_instance/variables.tf

Co-authored-by: Bharath KKB <[email protected]>

* Remove newlines

* Update docs

Co-authored-by: Bharath KKB <[email protected]>
  • Loading branch information
MartinPetkov and bharathkkb authored Apr 16, 2021
1 parent a535e13 commit e07828d
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ suites:
name: terraform
command_timeout: 1800
root_module_directory: test/fixtures/compute_instance/simple
- name: instance_simple_zone
driver:
name: terraform
command_timeout: 1800
root_module_directory: test/fixtures/compute_instance/simple_zone
- name: disk_snapshot
driver:
name: terraform
Expand Down
1 change: 1 addition & 0 deletions examples/compute_instance/simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This is a simple, minimal example of how to use the compute_instance module
| 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. | <pre>object({<br> email = string,<br> scopes = set(string)<br> })</pre> | `null` | no |
| subnetwork | The subnetwork selflink to host the compute instances in | `any` | n/a | yes |
| zone | The GCP zone to create resources in | `string` | `null` | no |

## Outputs

Expand Down
1 change: 1 addition & 0 deletions examples/compute_instance/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module "instance_template" {
module "compute_instance" {
source = "../../../modules/compute_instance"
region = var.region
zone = var.zone
subnetwork = var.subnetwork
num_instances = var.num_instances
hostname = "instance-simple"
Expand Down
6 changes: 6 additions & 0 deletions examples/compute_instance/simple/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ variable "region" {
default = "us-central1"
}

variable "zone" {
description = "The GCP zone to create resources in"
type = string
default = null
}

variable "subnetwork" {
description = "The subnetwork selflink to host the compute instances in"
}
Expand Down
1 change: 1 addition & 0 deletions modules/compute_instance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ See the [simple](https://github.com/terraform-google-modules/terraform-google-vm
| static\_ips | List of static IPs for VM instances | `list(string)` | `[]` | no |
| subnetwork | Subnet to deploy to. Only one of network or subnetwork should be specified. | `string` | `""` | no |
| subnetwork\_project | The project that subnetwork belongs to | `string` | `""` | no |
| zone | Zone where the instances should be created. If not specified, instances will be spread across available zones in the region. | `string` | `null` | no |

## Outputs

Expand Down
2 changes: 1 addition & 1 deletion modules/compute_instance/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ resource "google_compute_instance_from_template" "compute_instance" {
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)]
zone = var.zone == null ? data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)] : var.zone

network_interface {
network = var.network
Expand Down
5 changes: 5 additions & 0 deletions modules/compute_instance/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,8 @@ variable "region" {
default = null
}

variable "zone" {
type = string
description = "Zone where the instances should be created. If not specified, instances will be spread across available zones in the region."
default = null
}
26 changes: 26 additions & 0 deletions test/fixtures/compute_instance/simple_zone/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


module "instance_simple" {
source = "../../../../examples/compute_instance/simple"
project_id = var.project_id
region = "us-central1"
zone = "us-central1-b"
subnetwork = google_compute_subnetwork.main.self_link
num_instances = 2
service_account = var.service_account
}
1 change: 1 addition & 0 deletions test/fixtures/compute_instance/simple_zone/network.tf
26 changes: 26 additions & 0 deletions test/fixtures/compute_instance/simple_zone/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

output "instances_self_links" {
description = "List of instance self-links"
value = module.instance_simple.instances_self_links
}

output "project_id" {
description = "The GCP project to use for integration tests"
value = var.project_id
}

29 changes: 29 additions & 0 deletions test/fixtures/compute_instance/simple_zone/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

variable "project_id" {
description = "The GCP project to use for integration tests"
}

variable "service_account" {
default = null
type = object({
email = string
scopes = list(string)
})
description = "Service account to attach to the instance. See https://www.terraform.io/docs/providers/google/r/compute_instance_template.html#service_account."
}

19 changes: 19 additions & 0 deletions test/fixtures/compute_instance/simple_zone/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

terraform {
required_version = ">=0.12.6"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

project_id = attribute('project_id')

expected_instances = 2

control "Compute Instances" do
title "Simple Configuration With Zone Specified"

describe command("gcloud --project=#{project_id} compute instances list --format=json --filter='name~instance-simple*'") do
its(:exit_status) { should eq 0 }
its(:stderr) { should eq '' }

let!(:data) do
if subject.exit_status == 0
JSON.parse(subject.stdout)
else
[]
end
end

describe "number of instances" do
it "should be #{expected_instances}" do
expect(data.length).to eq(expected_instances)
end
end

describe "instance 001" do
let(:instance) do
data.find { |i| i['name'] == "instance-simple-001" }
end

it "should be in zone us-central1-b}" do
expect(instance['zone']).to match(/.*us-central1-b$/)
end
end

describe "instance 002" do
let(:instance) do
data.find { |i| i['name'] == "instance-simple-002" }
end

it "should be in zone us-central1-b}" do
expect(instance['zone']).to match(/.*us-central1-b$/)
end
end
end
end
20 changes: 20 additions & 0 deletions test/integration/instance_simple_zone/inspec.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---
name: instance_simple_zone
attributes:
- name: project_id
required: true
type: string

0 comments on commit e07828d

Please sign in to comment.