Coder Social home page Coder Social logo

Comments (7)

morgante avatar morgante commented on May 25, 2024

Are you using 23:00 or 6:00 as your start time? It looks inconsistent between your plan and config.

from terraform-google-vm.

invalidbassist avatar invalidbassist commented on May 25, 2024

Are you using 23:00 or 6:00 as your start time? It looks inconsistent between your plan and config.

Good call, edited accordingly

from terraform-google-vm.

morgante avatar morgante commented on May 25, 2024

Which value are you actually using though? Because I notice that the plan was attempting to change 6:00 to 06:00` which might be the cause of your permadiff.

from terraform-google-vm.

morgante avatar morgante commented on May 25, 2024

Also it looks like an upstream issue: hashicorp/terraform-provider-google#8460

from terraform-google-vm.

invalidbassist avatar invalidbassist commented on May 25, 2024

Which value are you actually using though? Because I notice that the plan was attempting to change 6:00 to 06:00` which might be the cause of your permadiff.

I noticed that during testing as well and updated first to 06:00 and then to 23:00 where I still experienced the permadiff issues.

from terraform-google-vm.

invalidbassist avatar invalidbassist commented on May 25, 2024

I was able to resolve creating a module that uses the same snapshot_schedule variable format, but uses a different snapshot policy resource:

# Build the region into the proper format
locals {
  region = "https://www.googleapis.com/compute/v1/projects/${var.project_id}/regions/${var.region}"
}

# Build the weekly_schedule variables from the weekly_snapshot_schedule variable if it contains a weekly schedule
locals {
  weekly_schedule             = var.snapshot_schedule.weekly_schedule != null
  weekly_schedule_start_time  = var.snapshot_schedule.weekly_schedule == null ? "" : var.snapshot_schedule.weekly_schedule.day_of_weeks["start_time"]
  weekly_schedule_day         = var.snapshot_schedule.weekly_schedule == null ? "" : var.snapshot_schedule.weekly_schedule.day_of_weeks["day"]
}

resource "null_resource" "module_depends_on" {
  triggers = {
    value = length(var.module_depends_on)
  }
}

# If weekly_schedule is not needed, build the snapshot policy for day or hourly using dynamic blocks
resource "google_compute_resource_policy" "dynamic_snapshot_policy" {
  count   = local.weekly_schedule ? 0 : 1
  name    = "${var.name}-snapshot"
  project = var.project_id
  region  = local.region
  snapshot_schedule_policy {
    retention_policy {
      max_retention_days    = var.snapshot_retention_policy.max_retention_days
      on_source_disk_delete = var.snapshot_retention_policy.on_source_disk_delete
    }

    schedule {
      dynamic "daily_schedule" {
        for_each = var.snapshot_schedule.daily_schedule == null ? [] : [var.snapshot_schedule.daily_schedule]
        content {
          days_in_cycle = daily_schedule.value.days_in_cycle
          start_time    = daily_schedule.value.start_time
        }
      }

      dynamic "hourly_schedule" {
        for_each = var.snapshot_schedule.hourly_schedule == null ? [] : [var.snapshot_schedule.hourly_schedule]
        content {
          hours_in_cycle = hourly_schedule.value["hours_in_cycle"]
          start_time     = hourly_schedule.value["start_time"]
        }
      }

    }

    snapshot_properties {
      labels = {
        name = var.enable_vss ? "windows" : "linux"
      }
      storage_locations = ["us"]
      guest_flush       = var.enable_vss

    }
  }

  depends_on = [null_resource.module_depends_on]
}

# If weekly schedule is declared, build the snapshot policy using the local weekly variables
resource "google_compute_resource_policy" "weekly_snapshot_policy" {
  count   = local.weekly_schedule ? 1 : 0
  name    = "${var.name}-snapshot"
  project = var.project_id
  region  = local.region
  snapshot_schedule_policy {
    retention_policy {
      max_retention_days    = var.snapshot_retention_policy.max_retention_days
      on_source_disk_delete = var.snapshot_retention_policy.on_source_disk_delete
    }

    schedule {
      weekly_schedule {
        day_of_weeks {
          start_time = local.weekly_schedule_start_time
          day        = local.weekly_schedule_day
        }
      }
    }

    snapshot_properties {
      labels = {
        name = var.enable_vss ? "windows" : "linux"
      }
      storage_locations = ["us"]
      guest_flush       = var.enable_vss

    }
  }

  depends_on = [null_resource.module_depends_on]
}

# Determine which schedule resource should drive the attachement
locals {
  snapshot_policy_name = local.weekly_schedule ? google_compute_resource_policy.weekly_snapshot_policy[0].name : google_compute_resource_policy.dynamic_snapshot_policy[0].name
}

# Attach the policy to the disks from the disk variables
resource "google_compute_disk_resource_policy_attachment" "attachment" {
  for_each = toset(var.disks)
  name     = local.snapshot_policy_name
  project  = element(split("/", each.key), index(split("/", each.key), "projects", ) + 1, )
  disk     = element(split("/", each.key), index(split("/", each.key), "disks", ) + 1, )
  zone     = element(split("/", each.key), index(split("/", each.key), "zones", ) + 1, )

  depends_on = [null_resource.module_depends_on, google_compute_resource_policy.dynamic_snapshot_policy, google_compute_resource_policy.weekly_snapshot_policy ]
}

from terraform-google-vm.

invalidbassist avatar invalidbassist commented on May 25, 2024

Confirmed that improperly formatted time was the cause of the issues despite my best efforts at reading plan outputs - closing issue.

from terraform-google-vm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.