|
| 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 | +} |
0 commit comments