Skip to content

Commit e0745ff

Browse files
author
Simon So
committed
add compute_instance module and tests
1 parent ec96e35 commit e0745ff

File tree

15 files changed

+494
-0
lines changed

15 files changed

+494
-0
lines changed

.kitchen.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ suites:
3939
name: terraform
4040
command_timeout: 1800
4141
root_module_directory: test/fixtures/instance_template/additional_disks
42+
- name: instance_simple
43+
driver:
44+
name: terraform
45+
command_timeout: 1800
46+
root_module_directory: test/fixtures/compute_instance/simple
4247
- name: mig_simple
4348
driver:
4449
name: terraform
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# instance-simple
2+
3+
This is a simple, minimal example of how to use the compute_instance module
4+
5+
[^]: (autogen_docs_start)
6+
7+
## Inputs
8+
9+
| Name | Description | Type | Default | Required |
10+
|------|-------------|:----:|:-----:|:-----:|
11+
| credentials\_path | The path to the GCP credentials JSON file | string | n/a | yes |
12+
| num\_instances | Number of instances to create | string | n/a | yes |
13+
| project\_id | The GCP project to use for integration tests | string | n/a | yes |
14+
| region | The GCP region to create and test resources in | string | n/a | yes |
15+
| service\_account | Service account email address and scopes | map | n/a | yes |
16+
| subnetwork | The subnetwork to host the compute instances in | string | n/a | yes |
17+
18+
## Outputs
19+
20+
| Name | Description |
21+
|------|-------------|
22+
| available\_zones | List of available zones in region |
23+
| instances\_self\_links | List of self-links for compute instances |
24+
25+
[^]: (autogen_docs_end)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
provider "google" {
18+
credentials = "${file(var.credentials_path)}"
19+
project = "${var.project_id}"
20+
region = "${var.region}"
21+
version = "~> 1.19"
22+
}
23+
24+
provider "google-beta" {
25+
credentials = "${file(var.credentials_path)}"
26+
project = "${var.project_id}"
27+
region = "${var.region}"
28+
version = "~> 1.19"
29+
}
30+
31+
module "instance_template" {
32+
source = "../../../modules/instance_template"
33+
subnetwork = "${var.subnetwork}"
34+
service_account = "${var.service_account}"
35+
}
36+
37+
module "compute_instance" {
38+
source = "../../../modules/compute_instance"
39+
subnetwork = "${var.subnetwork}"
40+
num_instances = "${var.num_instances}"
41+
hostname = "instance-simple"
42+
instance_template = "${module.instance_template.self_link}"
43+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "instances_self_links" {
18+
description = "List of self-links for compute instances"
19+
value = "${module.compute_instance.instances_self_links}"
20+
}
21+
22+
output "available_zones" {
23+
description = "List of available zones in region"
24+
value = "${module.compute_instance.available_zones}"
25+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "credentials_path" {
18+
description = "The path to the GCP credentials JSON file"
19+
}
20+
21+
variable "project_id" {
22+
description = "The GCP project to use for integration tests"
23+
}
24+
25+
variable "region" {
26+
description = "The GCP region to create and test resources in"
27+
}
28+
29+
variable "subnetwork" {
30+
description = "The subnetwork to host the compute instances in"
31+
}
32+
33+
variable "num_instances" {
34+
description = "Number of instances to create"
35+
}
36+
37+
variable "service_account" {
38+
type = "map"
39+
description = "Service account email address and scopes"
40+
}

modules/compute_instance/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Compute Instance
2+
3+
This module is used to create compute instances (and only compute instances) using
4+
[google_compute_instance_from_template](https://www.terraform.io/docs/providers/google/r/compute_instance_from_template.html), with no instance groups.
5+
6+
## Usage
7+
8+
See the [simple](examples/compute_instance/simple) for a usage example.
9+
10+
## Testing
11+
12+
13+
[^]: (autogen_docs_start)
14+
15+
## Inputs
16+
17+
| Name | Description | Type | Default | Required |
18+
|------|-------------|:----:|:-----:|:-----:|
19+
| hostname | Hostname of instances | string | `""` | no |
20+
| instance\_template | Instance template self_link used to create compute instances | string | n/a | yes |
21+
| network | Network to deploy to. Only one of network or subnetwork should be specified. | string | `""` | no |
22+
| num\_instances | Number of instances to create. This value is ignored if static_ips is provided. | string | `"1"` | no |
23+
| static\_ips | List of static IPs for VM instances | list | `<list>` | no |
24+
| subnetwork | Subnet to deploy to. Only one of network or subnetwork should be specified. | string | `""` | no |
25+
| subnetwork\_project | The project that subnetwork belongs to | string | `""` | no |
26+
27+
## Outputs
28+
29+
| Name | Description |
30+
|------|-------------|
31+
| available\_zones | List of available zones in region |
32+
| instances\_self\_links | List of self-links for compute instances |
33+
34+
[^]: (autogen_docs_end)

modules/compute_instance/main.tf

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
locals {
18+
hostname = "${var.hostname == "" ? "default" : var.hostname}"
19+
num_instances = "${length(var.static_ips) == 0 ? var.num_instances : length(var.static_ips)}"
20+
21+
# local.static_ips is the same as var.static_ips with a dummy element appended
22+
# at the end of the list to work around "list does not have any elements so cannot
23+
# determine type" error when var.static_ips is empty
24+
static_ips = "${concat(var.static_ips, list("NOT_AN_IP"))}"
25+
}
26+
27+
###############
28+
# Data Sources
29+
###############
30+
31+
data "google_compute_zones" "available" {}
32+
33+
#############
34+
# Instances
35+
#############
36+
37+
resource "google_compute_instance_from_template" "compute_instance" {
38+
provider = "google"
39+
count = "${local.num_instances}"
40+
name = "${local.hostname}-${format("%03d", count.index + 1)}"
41+
zone = "${data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)]}"
42+
43+
network_interface {
44+
network = "${var.network}"
45+
subnetwork = "${var.subnetwork}"
46+
subnetwork_project = "${var.subnetwork_project}"
47+
network_ip = "${length(var.static_ips) == 0 ? "" : element(local.static_ips, count.index)}"
48+
}
49+
50+
source_instance_template = "${var.instance_template}"
51+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
output "instances_self_links" {
18+
description = "List of self-links for compute instances"
19+
value = ["${google_compute_instance_from_template.compute_instance.*.self_link}"]
20+
}
21+
22+
output "available_zones" {
23+
description = "List of available zones in region"
24+
value = ["${data.google_compute_zones.available.names}"]
25+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "network" {
18+
description = "Network to deploy to. Only one of network or subnetwork should be specified."
19+
default = ""
20+
}
21+
22+
variable "subnetwork" {
23+
description = "Subnet to deploy to. Only one of network or subnetwork should be specified."
24+
default = ""
25+
}
26+
27+
variable "subnetwork_project" {
28+
description = "The project that subnetwork belongs to"
29+
default = ""
30+
}
31+
32+
variable "hostname" {
33+
description = "Hostname of instances"
34+
default = ""
35+
}
36+
37+
variable "static_ips" {
38+
type = "list"
39+
description = "List of static IPs for VM instances"
40+
default = []
41+
}
42+
43+
variable "num_instances" {
44+
description = "Number of instances to create. This value is ignored if static_ips is provided."
45+
default = "1"
46+
}
47+
48+
variable "instance_template" {
49+
description = "Instance template self_link used to create compute instances"
50+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2018 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
locals {
18+
credentials_path = "${path.module}/${var.credentials_path_relative}"
19+
}
20+
21+
module "instance_simple" {
22+
source = "../../../../examples/compute_instance/simple"
23+
credentials_path = "${local.credentials_path}"
24+
project_id = "${var.project_id}"
25+
region = "us-central1"
26+
subnetwork = "${google_compute_subnetwork.main.name}"
27+
num_instances = 4
28+
service_account = "${var.service_account}"
29+
}

0 commit comments

Comments
 (0)