Skip to content

Commit

Permalink
feat: [compute_instance] Add deletion_protection variable (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
sirhopcount authored Feb 1, 2022
1 parent f04eb64 commit de34522
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
1 change: 1 addition & 0 deletions examples/compute_instance/simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This is a simple, minimal example of how to use the compute_instance module

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| deletion\_protection | Enable deletion protection on this instance. Note: you must disable deletion protection before removing the resource, or the instance cannot be deleted and the Terraform run will not complete successfully. | `bool` | `false` | no |
| nat\_ip | Public ip address | `any` | `null` | no |
| network\_tier | Network network\_tier | `string` | `"PREMIUM"` | no |
| num\_instances | Number of instances to create | `any` | n/a | yes |
Expand Down
16 changes: 9 additions & 7 deletions examples/compute_instance/simple/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ 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"
instance_template = module.instance_template.self_link
source = "../../../modules/compute_instance"
region = var.region
zone = var.zone
subnetwork = var.subnetwork
num_instances = var.num_instances
hostname = "instance-simple"
instance_template = module.instance_template.self_link
deletion_protection = false

access_config = [{
nat_ip = var.nat_ip
network_tier = var.network_tier
Expand Down
5 changes: 5 additions & 0 deletions examples/compute_instance/simple/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ variable "network_tier" {
default = "PREMIUM"
}

variable "deletion_protection" {
type = bool
description = "Enable deletion protection on this instance. Note: you must disable deletion protection before removing the resource, or the instance cannot be deleted and the Terraform run will not complete successfully."
default = false
}

variable "service_account" {
default = null
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 @@ -17,6 +17,7 @@ See the [simple](https://github.com/terraform-google-modules/terraform-google-vm
|------|-------------|------|---------|:--------:|
| access\_config | Access configurations, i.e. IPs via which the VM instance can be accessed via the Internet. | <pre>list(object({<br> nat_ip = string<br> network_tier = string<br> }))</pre> | `[]` | no |
| add\_hostname\_suffix | Adds a suffix to the hostname | `bool` | `true` | no |
| deletion\_protection | Enable deletion protection on this instance. Note: you must disable deletion protection before removing the resource, or the instance cannot be deleted and the Terraform run will not complete successfully. | `bool` | `false` | no |
| hostname | Hostname of instances | `string` | `""` | no |
| hostname\_suffix\_separator | Separator character to compose hostname when add\_hostname\_suffix is set to true. | `string` | `"-"` | no |
| instance\_template | Instance template self\_link used to create compute instances | `any` | n/a | yes |
Expand Down
12 changes: 7 additions & 5 deletions modules/compute_instance/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ data "google_compute_zones" "available" {
#############

resource "google_compute_instance_from_template" "compute_instance" {
provider = google
count = local.num_instances
name = var.add_hostname_suffix ? format("%s%s%s", local.hostname, var.hostname_suffix_separator, format("%03d", count.index + 1)) : local.hostname
project = local.project_id
zone = var.zone == null ? data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)] : var.zone
provider = google
count = local.num_instances
name = var.add_hostname_suffix ? format("%s%s%s", local.hostname, var.hostname_suffix_separator, format("%03d", count.index + 1)) : local.hostname
project = local.project_id
zone = var.zone == null ? data.google_compute_zones.available.names[count.index % length(data.google_compute_zones.available.names)] : var.zone
deletion_protection = var.deletion_protection


network_interface {
network = var.network
Expand Down
6 changes: 6 additions & 0 deletions modules/compute_instance/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ variable "hostname_suffix_separator" {
description = "Separator character to compose hostname when add_hostname_suffix is set to true."
default = "-"
}

variable "deletion_protection" {
type = bool
description = "Enable deletion protection on this instance. Note: you must disable deletion protection before removing the resource, or the instance cannot be deleted and the Terraform run will not complete successfully."
default = false
}

0 comments on commit de34522

Please sign in to comment.