Skip to content

Commit c382631

Browse files
wkharoldwardharold
andauthored
feat: Add support for resource_policies (#260)
Co-authored-by: Ward K Harold <[email protected]>
1 parent dfb0523 commit c382631

File tree

6 files changed

+20
-2
lines changed

6 files changed

+20
-2
lines changed

modules/compute_instance/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ See the [simple](https://github.com/terraform-google-modules/terraform-google-vm
2525
| network | Network to deploy to. Only one of network or subnetwork should be specified. | `string` | `""` | no |
2626
| num\_instances | Number of instances to create. This value is ignored if static\_ips is provided. | `string` | `"1"` | no |
2727
| region | Region where the instances should be created. | `string` | `null` | no |
28+
| resource\_policies | (Optional) A list of short names or self\_links of resource policies to attach to the instance. Modifying this list will cause the instance to recreate. Currently a max of 1 resource policy is supported. | `list(string)` | `[]` | no |
2829
| static\_ips | List of static IPs for VM instances | `list(string)` | `[]` | no |
2930
| subnetwork | Subnet to deploy to. Only one of network or subnetwork should be specified. | `string` | `""` | no |
3031
| subnetwork\_project | The project that subnetwork belongs to | `string` | `""` | no |

modules/compute_instance/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ resource "google_compute_instance_from_template" "compute_instance" {
4949
project = local.project_id
5050
zone = var.zone == null ? data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)] : var.zone
5151
deletion_protection = var.deletion_protection
52+
resource_policies = var.resource_policies
5253

5354

5455
dynamic "network_interface" {

modules/compute_instance/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,9 @@ variable "alias_ip_ranges" {
9595
}))
9696
default = []
9797
}
98+
99+
variable "resource_policies" {
100+
description = "(Optional) A list of short names or self_links of resource policies to attach to the instance. Modifying this list will cause the instance to recreate. Currently a max of 1 resource policy is supported."
101+
type = list(string)
102+
default = []
103+
}

modules/instance_template/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ See the [simple](../../examples/instance_template/simple) for a usage example.
1818
| additional\_networks | Additional network interface details for GCE, if any. | <pre>list(object({<br> network = string<br> subnetwork = string<br> subnetwork_project = string<br> network_ip = string<br> access_config = list(object({<br> nat_ip = string<br> network_tier = string<br> }))<br> }))</pre> | `[]` | no |
1919
| alias\_ip\_range | An array of alias IP ranges for this network interface. Can only be specified for network interfaces on subnet-mode networks.<br>ip\_cidr\_range: The IP CIDR range represented by this alias IP range. This IP CIDR range must belong to the specified subnetwork and cannot contain IP addresses reserved by system or used by other network interfaces. At the time of writing only a netmask (e.g. /24) may be supplied, with a CIDR format resulting in an API error.<br>subnetwork\_range\_name: The subnetwork secondary range name specifying the secondary range from which to allocate the IP CIDR range for this alias IP range. If left unspecified, the primary range of the subnetwork will be used. | <pre>object({<br> ip_cidr_range = string<br> subnetwork_range_name = string<br> })</pre> | `null` | no |
2020
| auto\_delete | Whether or not the boot disk should be auto-deleted | `string` | `"true"` | no |
21+
| automatic\_restart | (Optional) Specifies whether the instance should be automatically restarted if it is terminated by Compute Engine (not terminated by a user). | `bool` | `true` | no |
2122
| can\_ip\_forward | Enable IP forwarding, for NAT instances for example | `string` | `"false"` | no |
2223
| disk\_encryption\_key | The id of the encryption key that is stored in Google Cloud KMS to use to encrypt all the disks on this instance | `string` | `null` | no |
2324
| disk\_labels | Labels to be assigned to boot disk, provided as a map | `map(string)` | `{}` | no |

modules/instance_template/main.tf

+5-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ locals {
5050
? "TERMINATE"
5151
: var.on_host_maintenance
5252
)
53+
automatic_restart = (
54+
# must be false when preemptible is true
55+
var.preemptible ? false : var.automatic_restart
56+
)
5357
}
5458

5559
####################
@@ -141,10 +145,9 @@ resource "google_compute_instance_template" "tpl" {
141145
create_before_destroy = "true"
142146
}
143147

144-
# scheduling must have automatic_restart be false when preemptible is true.
145148
scheduling {
146149
preemptible = var.preemptible
147-
automatic_restart = !var.preemptible
150+
automatic_restart = local.automatic_restart
148151
on_host_maintenance = local.on_host_maintenance
149152
}
150153

modules/instance_template/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ variable "preemptible" {
5959
default = false
6060
}
6161

62+
variable "automatic_restart" {
63+
type = bool
64+
description = "(Optional) Specifies whether the instance should be automatically restarted if it is terminated by Compute Engine (not terminated by a user)."
65+
default = true
66+
}
67+
6268
variable "on_host_maintenance" {
6369
type = string
6470
description = "Instance availability Policy"

0 commit comments

Comments
 (0)