Skip to content

Commit cedac01

Browse files
authored
feat: Add module for creating scheduled snapshots for persistent disk (#139)
1 parent b21876a commit cedac01

File tree

23 files changed

+696
-0
lines changed

23 files changed

+696
-0
lines changed

.kitchen.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ suites:
5050
name: terraform
5151
command_timeout: 1800
5252
root_module_directory: test/fixtures/compute_instance/simple
53+
- name: disk_snapshot
54+
driver:
55+
name: terraform
56+
command_timeout: 1800
57+
root_module_directory: test/fixtures/compute_instance/disk_snapshot
5358
- name: mig_simple
5459
driver:
5560
name: terraform
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# disk-snapshot
2+
3+
This is a simple example of how to use the compute_disk_snapshot module
4+
5+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
6+
## Inputs
7+
8+
| Name | Description | Type | Default | Required |
9+
|------|-------------|------|---------|:--------:|
10+
| project\_id | The GCP project to use for integration tests | `string` | n/a | yes |
11+
| region | The GCP region to create and test resources in | `string` | `"us-central1"` | no |
12+
| subnetwork | The subnetwork selflink to host the compute instances in | `any` | n/a | yes |
13+
14+
## Outputs
15+
16+
| Name | Description |
17+
|------|-------------|
18+
| disk\_snapshots | List of disks snapshots and the snapshot policy |
19+
| instances\_self\_links | List of self-links for compute instances |
20+
21+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
version = "~> 3.0"
19+
}
20+
21+
data "google_compute_zones" "available" {
22+
project = var.project_id
23+
region = var.region
24+
}
25+
26+
module "instance_template" {
27+
source = "../../../modules/instance_template"
28+
region = var.region
29+
project_id = var.project_id
30+
subnetwork = var.subnetwork
31+
service_account = null
32+
33+
additional_disks = [
34+
{
35+
auto_delete = true
36+
boot = false
37+
disk_size_gb = 20
38+
disk_type = "pd-standard"
39+
},
40+
{
41+
auto_delete = true
42+
boot = false
43+
disk_size_gb = 30
44+
disk_type = "pd-standard"
45+
}
46+
]
47+
}
48+
49+
module "compute_instance" {
50+
source = "../../../modules/compute_instance"
51+
region = var.region
52+
subnetwork = var.subnetwork
53+
num_instances = 1
54+
hostname = "instance-simple"
55+
instance_template = module.instance_template.self_link
56+
}
57+
58+
module "disk_snapshots" {
59+
source = "../../../modules/compute_disk_snapshot"
60+
name = "backup-policy-test"
61+
project = var.project_id
62+
region = var.region
63+
64+
snapshot_retention_policy = {
65+
max_retention_days = 10
66+
on_source_disk_delete = "KEEP_AUTO_SNAPSHOTS"
67+
}
68+
69+
snapshot_schedule = {
70+
daily_schedule = {
71+
days_in_cycle = 1
72+
start_time = "08:00"
73+
}
74+
hourly_schedule = null
75+
weekly_schedule = null
76+
}
77+
78+
snapshot_properties = {
79+
guest_flush = true
80+
storage_locations = ["EU"]
81+
labels = null
82+
}
83+
84+
module_depends_on = [module.compute_instance]
85+
disks = coalesce(concat([for x, z in module.compute_instance.instances_details[0].attached_disk : z.source]))
86+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 "disk_snapshots" {
23+
description = "List of disks snapshots and the snapshot policy"
24+
value = module.disk_snapshots
25+
}
26+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Copyright 2019 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 "project_id" {
18+
description = "The GCP project to use for integration tests"
19+
type = string
20+
}
21+
22+
variable "region" {
23+
description = "The GCP region to create and test resources in"
24+
type = string
25+
default = "us-central1"
26+
}
27+
28+
variable "subnetwork" {
29+
description = "The subnetwork selflink to host the compute instances in"
30+
}
31+
32+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
terraform {
18+
required_version = ">=0.12.6"
19+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Compute Disk Snapshot
2+
3+
This module is used to create one resource policy for disk snapshots and attaching
4+
persistent disks to the created snapshot policy. This module allows you to have
5+
scheduled snapshots for persistent disks.
6+
7+
## Usage
8+
9+
See the [disk snapshot](https://github.com/terraform-google-modules/terraform-google-vm/tree/master/examples/compute_instance/disk_snapshot) for a usage example.
10+
11+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
12+
## Inputs
13+
14+
| Name | Description | Type | Default | Required |
15+
|------|-------------|------|---------|:--------:|
16+
| disks | List of self\_links persistent disks to attach the snapshot policy to (ie. projects/project\_id/disks/diskname/zones/zone\_name) | `list` | `[]` | no |
17+
| module\_depends\_on | List of modules or resources this module depends on | `list(any)` | `[]` | no |
18+
| name | Name of the resource policy to create | `string` | n/a | yes |
19+
| project | The project ID where the resources will be created | `string` | n/a | yes |
20+
| region | Region where resource policy resides | `string` | n/a | yes |
21+
| snapshot\_properties | The properties of the schedule policy. For more details see https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_resource_policy#snapshot_properties | <pre>object(<br> {<br> guest_flush = bool<br> labels = map(string)<br> storage_locations = list(string)<br> }<br> )</pre> | `null` | no |
22+
| snapshot\_retention\_policy | The retention policy to be applied to the schedule policy. For more details see https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_resource_policy#retention_policy | <pre>object(<br> {<br> max_retention_days = number<br> on_source_disk_delete = string<br> }<br> )</pre> | n/a | yes |
23+
| snapshot\_schedule | The scheduled to be used by the snapshot policy. For more details see https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_resource_policy#schedule | <pre>object(<br> {<br> daily_schedule = object(<br> {<br> days_in_cycle = number<br> start_time = string<br> }<br> )<br> hourly_schedule = object(<br> {<br> hours_in_cycle = number<br> start_time = string<br> }<br> )<br> weekly_schedule = object(<br> {<br> day_of_weeks = set(object(<br> {<br> day = string<br> start_time = string<br> }<br> ))<br> }<br> )<br> }<br> )</pre> | n/a | yes |
24+
25+
## Outputs
26+
27+
| Name | Description |
28+
|------|-------------|
29+
| attachments | Disk attachments to the resource policy |
30+
| policy | Resource snapshot policy details |
31+
32+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
resource "null_resource" "module_depends_on" {
18+
triggers = {
19+
value = length(var.module_depends_on)
20+
}
21+
}
22+
23+
resource "google_compute_resource_policy" "policy" {
24+
name = var.name
25+
project = var.project
26+
region = var.region
27+
28+
snapshot_schedule_policy {
29+
retention_policy {
30+
max_retention_days = var.snapshot_retention_policy.max_retention_days
31+
on_source_disk_delete = var.snapshot_retention_policy.on_source_disk_delete
32+
}
33+
34+
schedule {
35+
dynamic "daily_schedule" {
36+
for_each = var.snapshot_schedule.daily_schedule == null ? [] : [var.snapshot_schedule.daily_schedule]
37+
content {
38+
days_in_cycle = daily_schedule.value.days_in_cycle
39+
start_time = daily_schedule.value.start_time
40+
}
41+
}
42+
43+
dynamic "hourly_schedule" {
44+
for_each = var.snapshot_schedule.hourly_schedule == null ? [] : [var.snapshot_schedule.hourly_schedule]
45+
content {
46+
hours_in_cycle = hourly_schedule.value["hours_in_cycle"]
47+
start_time = hourly_schedule.value["start_time"]
48+
}
49+
}
50+
51+
dynamic "weekly_schedule" {
52+
for_each = var.snapshot_schedule.weekly_schedule == null ? [] : [var.snapshot_schedule.weekly_schedule]
53+
content {
54+
dynamic "day_of_weeks" {
55+
for_each = weekly_schedule.value.day_of_weeks
56+
content {
57+
day = day_of_weeks.value["day"]
58+
start_time = day_of_weeks.value["start_time"]
59+
}
60+
}
61+
}
62+
}
63+
}
64+
65+
dynamic "snapshot_properties" {
66+
for_each = var.snapshot_properties == null ? [] : [var.snapshot_properties]
67+
content {
68+
guest_flush = snapshot_properties.value["guest_flush"]
69+
labels = snapshot_properties.value["labels"]
70+
storage_locations = snapshot_properties.value["storage_locations"]
71+
}
72+
}
73+
}
74+
75+
depends_on = [null_resource.module_depends_on]
76+
}
77+
78+
resource "google_compute_disk_resource_policy_attachment" "attachment" {
79+
for_each = toset(var.disks)
80+
name = google_compute_resource_policy.policy.name
81+
project = element(split("/", each.key), index(split("/", each.key), "projects", ) + 1, )
82+
disk = element(split("/", each.key), index(split("/", each.key), "disks", ) + 1, )
83+
zone = element(split("/", each.key), index(split("/", each.key), "zones", ) + 1, )
84+
85+
depends_on = [null_resource.module_depends_on]
86+
}
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 "policy" {
18+
description = "Resource snapshot policy details"
19+
value = google_compute_resource_policy.policy
20+
}
21+
22+
output "attachments" {
23+
description = "Disk attachments to the resource policy"
24+
value = google_compute_disk_resource_policy_attachment.attachment.*
25+
}

0 commit comments

Comments
 (0)