Skip to content

Conversation

Pearl1594
Copy link
Contributor

@Pearl1594 Pearl1594 commented Sep 17, 2025

Tested with following config:

terraform {
  required_providers {
    cloudstack = {
      source = "hashicorp.com/dev/cloudstack"
    }
  }
}

provider "cloudstack" {
  api_url    = "http://xx.xx.xx.xx:8080/client/api"
  api_key    = "LIN6rqXuaJwMPfGYFh13qDwYz5VNNz1J2J6qIOWcd3oLQOq0WtD4CwRundBL6rzXToa3lQOC_vKjI3nkHtiD8Q"
  secret_key = "R6QPwRUz09TVXBjXNwZk7grTjcPtsFRphH6xhN1oPvnc12YUk296t4KHytg8zRLczDA0X5NsLVi4d8rfMMx3yg"
  timeout    = 1800  # 30 minutes for template registration
}


resource "cloudstack_network" "iso_network" {
  name             = "isolated-network"
  display_text     = "Isolated network for VMs"
  cidr             = "10.0.10.0/24"
  network_offering = "DefaultIsolatedNetworkOfferingWithSourceNatService"
  zone             = "ref-trl-9351-k-Mol8-pearl-dsilva"
}

data "cloudstack_template" "existing_template" {
  template_filter = "featured"
  filter {
    name = "name"
    value = "CentOS 5\\.5"
  }
}

resource "cloudstack_instance" "initial_vm" {
  name             = "initial-web-server"
  service_offering = "Small Instance"
  template         = data.cloudstack_template.existing_template.name
  zone             = "ref-trl-9351-k-Mol8-pearl-dsilva"
  network_id       = cloudstack_network.iso_network.id
  expunge          = true
}

data "cloudstack_volume" "vm_root_volume" {
  filter{
    name = "type"
    value="ROOT"
  }
  depends_on = [cloudstack_instance.initial_vm]
}

resource "cloudstack_snapshot_policy" "frequent_backup" {
  volume_id     = data.cloudstack_volume.vm_root_volume.id
  interval_type = "HOURLY"
  max_snaps     = 1 
  schedule      = "1"
  timezone      = "UTC"
  
  
  tags = {
    Environment = "demo"
  }
  
  depends_on = [data.cloudstack_volume.vm_root_volume]
}

resource "cloudstack_snapshot_policy" "frequent_backup_DAILY" {
  volume_id     = data.cloudstack_volume.vm_root_volume.id
  interval_type = "DAILY"
  max_snaps     = 1
  schedule      = "00:00"
  timezone      = "UTC"


  tags = {
    Environment = "demo"
  }

  depends_on = [data.cloudstack_volume.vm_root_volume]
}


output "deployment_summary" {
  description = "Summary of deployed resources"
  value = {
    vm = {
      id         = cloudstack_instance.initial_vm.id
      name       = cloudstack_instance.initial_vm.name
      ip_address = cloudstack_instance.initial_vm.ip_address
    }
    root_volume = {
      id   = data.cloudstack_volume.vm_root_volume.id
      name = data.cloudstack_volume.vm_root_volume.name
    }
    snapshot_policies = {
      hourly_policy_id = cloudstack_snapshot_policy.frequent_backup.id
      daily_policy_id  = cloudstack_snapshot_policy.frequent_backup_DAILY.id
    }
  }
}
  1. First created only with cloudstack_snapshot_policy.frequent_backup - hourly backup - applied the config
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

Outputs:

deployment_summary = {
  "root_volume" = {
    "id" = "cd16f7b6-0382-4afa-9c55-9e35c4f57d1c"
    "name" = "ROOT-63"
  }
  "snapshot_policies" = {
    "hourly_policy_id" = "5abcd1de-7117-4a35-8d89-da6316d0ee82"
  }
  "vm" = {
    "id" = "cfe237c0-172d-4c7f-835b-d964ec1c1eec"
    "ip_address" = "xx.xx.xx.229"
    "name" = "initial-web-server"
  }
}
  1. Then added another cloudstack_snapshot_policy.frequent_backup_DAILY - daily backup and applied the config

Apply complete! Resources: 1 added, 1 changed, 0 destroyed.

Outputs:

deployment_summary = {
  "root_volume" = {
    "id" = "cd16f7b6-0382-4afa-9c55-9e35c4f57d1c"
    "name" = "ROOT-63"
  }
  "snapshot_policies" = {
    "daily_policy_id" = "f6b2e6c9-c1a7-4206-ac59-1e9c296b6ad8"
    "hourly_policy_id" = "5abcd1de-7117-4a35-8d89-da6316d0ee82"
  }
  "vm" = {
    "id" = "cfe237c0-172d-4c7f-835b-d964ec1c1eec"
    "ip_address" = "xx.xx.xx.229"
    "name" = "initial-web-server"
  }
}

@Pearl1594 Pearl1594 linked an issue Sep 17, 2025 that may be closed by this pull request
Copy link
Collaborator

@kiranchavala kiranchavala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Pearl1594

https://cloudstack.apache.org/api/apidocs-4.21/apis/updateSnapshotPolicy.html

Suppose i change max_snaps via terraform after a resource is deployed

its not actually changed in the cloudstack , the terraform.tfstate contains

"max_snaps": 2,


resource "cloudstack_snapshot_policy" "frequent_backup" {
  volume_id     = "fc2e91ee-699c-489d-b005-f0afafdcbf41"
  interval_type = "HOURLY"
  max_snaps     = 2 
  schedule      = "2"
  timezone      = "UTC"
  
  
  tags = {
    Environment = "demo"
  }
  

}


terraform apply
cloudstack_snapshot_policy.frequent_backup_DAILY: Refreshing state... [id=b070fba3-f751-4528-989b-785cf445d5bb]
cloudstack_snapshot_policy.frequent_backup: Refreshing state... [id=b070fba3-f751-4528-989b-785cf445d5bb]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # cloudstack_snapshot_policy.frequent_backup will be updated in-place
  ~ resource "cloudstack_snapshot_policy" "frequent_backup" {
        id            = "b070fba3-f751-4528-989b-785cf445d5bb"
      ~ max_snaps     = 1 -> 2
      ~ schedule      = "00:00" -> "2"
        tags          = {
            "Environment" = "demo"
        }
      ~ zone_ids      = [
          - "05d9863d-bd94-41c2-bba8-251aab44637a",
        ]
        # (3 unchanged attributes hidden)
    }

  # cloudstack_snapshot_policy.frequent_backup_DAILY will be updated in-place
  ~ resource "cloudstack_snapshot_policy" "frequent_backup_DAILY" {
        id            = "b070fba3-f751-4528-989b-785cf445d5bb"
        tags          = {
            "Environment" = "demo"
        }
      ~ zone_ids      = [
          - "05d9863d-bd94-41c2-bba8-251aab44637a",
        ]
        # (5 unchanged attributes hidden)
    }

Plan: 0 to add, 2 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

cloudstack_snapshot_policy.frequent_backup_DAILY: Modifying... [id=b070fba3-f751-4528-989b-785cf445d5bb]
cloudstack_snapshot_policy.frequent_backup: Modifying... [id=b070fba3-f751-4528-989b-785cf445d5bb]
cloudstack_snapshot_policy.frequent_backup: Modifications complete after 1s [id=b070fba3-f751-4528-989b-785cf445d5bb]
cloudstack_snapshot_policy.frequent_backup_DAILY: Modifications complete after 1s [id=b070fba3-f751-4528-989b-785cf445d5bb]

Apply complete! Resources: 0 added, 2 changed, 0 destroyed.

Outputs:

deployment_summary = {
  "snapshot_policies" = {
    "daily_policy_id" = "b070fba3-f751-4528-989b-785cf445d5bb"
    "hourly_policy_id" = "b070fba3-f751-4528-989b-785cf445d5bb"
  }
}

Copy link
Collaborator

@kiranchavala kiranchavala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, tested manually, was able to create snapshot policy and also update it


resource "cloudstack_snapshot_policy" "frequent_backup" {
  volume_id     = "fc2e91ee-699c-489d-b005-f0afafdcbf41"
  interval_type = "HOURLY"
  max_snaps     = 2
  schedule      = "1"
  timezone      = "UTC"
  
  
  tags = {
    Environment = "demo"
  }
  

}

resource "cloudstack_snapshot_policy" "frequent_backup_DAILY" {
  volume_id     = "fc2e91ee-699c-489d-b005-f0afafdcbf41"
  interval_type = "DAILY"
  max_snaps     = 3
  schedule      = "00:00"
  timezone      = "UTC"


  tags = {
    Environment = "demo"
  }

}


output "deployment_summary" {
  description = "Summary of deployed resources"
  value = {
   
    snapshot_policies = {
      hourly_policy_id = cloudstack_snapshot_policy.frequent_backup.id
      daily_policy_id  = cloudstack_snapshot_policy.frequent_backup_DAILY.id
    }
  }
}

terraform apply 

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # cloudstack_snapshot_policy.frequent_backup will be created
  + resource "cloudstack_snapshot_policy" "frequent_backup" {
      + id            = (known after apply)
      + interval_type = "HOURLY"
      + max_snaps     = 1
      + schedule      = "1"
      + tags          = {
          + "Environment" = "demo"
        }
      + timezone      = "UTC"
      + volume_id     = "fc2e91ee-699c-489d-b005-f0afafdcbf41"
    }

  # cloudstack_snapshot_policy.frequent_backup_DAILY will be created
  + resource "cloudstack_snapshot_policy" "frequent_backup_DAILY" {
      + id            = (known after apply)
      + interval_type = "DAILY"
      + max_snaps     = 1
      + schedule      = "00:00"
      + tags          = {
          + "Environment" = "demo"
        }
      + timezone      = "UTC"
      + volume_id     = "fc2e91ee-699c-489d-b005-f0afafdcbf41"
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + deployment_summary = {
      + snapshot_policies = {
          + daily_policy_id  = (known after apply)
          + hourly_policy_id = (known after apply)
        }
    }

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

cloudstack_snapshot_policy.frequent_backup_DAILY: Creating...
cloudstack_snapshot_policy.frequent_backup: Creating...
cloudstack_snapshot_policy.frequent_backup: Creation complete after 1s [id=a54f053a-48b4-48fe-94f8-f181446b8b28]
cloudstack_snapshot_policy.frequent_backup_DAILY: Creation complete after 1s [id=a54f053a-48b4-48fe-94f8-f181446b8b28]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Outputs:

deployment_summary = {
  "snapshot_policies" = {
    "daily_policy_id" = "a54f053a-48b4-48fe-94f8-f181446b8b28"
    "hourly_policy_id" = "a54f053a-48b4-48fe-94f8-f181446b8b28"
  }
}
╭─ ~/Desktop/cloudstack-India-demo/cloudstack-terraform copy                                                                                            ✔ ╱ 3s ╱ Azure subscription 1  ╱ 11:19:12 AM 
╰─ terraform apply
cloudstack_snapshot_policy.frequent_backup_DAILY: Refreshing state... [id=a54f053a-48b4-48fe-94f8-f181446b8b28]
cloudstack_snapshot_policy.frequent_backup: Refreshing state... [id=a54f053a-48b4-48fe-94f8-f181446b8b28]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
-/+ destroy and then create replacement

Terraform will perform the following actions:

  # cloudstack_snapshot_policy.frequent_backup must be replaced
-/+ resource "cloudstack_snapshot_policy" "frequent_backup" {
      ~ id            = "a54f053a-48b4-48fe-94f8-f181446b8b28" -> (known after apply)
      ~ interval_type = "DAILY" -> "HOURLY" # forces replacement
      ~ max_snaps     = 1 -> 2 # forces replacement
      ~ schedule      = "00:00" -> "1" # forces replacement
        tags          = {
            "Environment" = "demo"
        }
      - zone_ids      = [ # forces replacement
          - "05d9863d-bd94-41c2-bba8-251aab44637a",
        ] -> null
        # (2 unchanged attributes hidden)
    }

  # cloudstack_snapshot_policy.frequent_backup_DAILY must be replaced
-/+ resource "cloudstack_snapshot_policy" "frequent_backup_DAILY" {
      ~ id            = "a54f053a-48b4-48fe-94f8-f181446b8b28" -> (known after apply)
      ~ max_snaps     = 1 -> 3 # forces replacement
        tags          = {
            "Environment" = "demo"
        }
      - zone_ids      = [ # forces replacement
          - "05d9863d-bd94-41c2-bba8-251aab44637a",
        ] -> null
        # (4 unchanged attributes hidden)
    }

Plan: 2 to add, 0 to change, 2 to destroy.

Changes to Outputs:
  ~ deployment_summary = {
      ~ snapshot_policies = {
          ~ daily_policy_id  = "a54f053a-48b4-48fe-94f8-f181446b8b28" -> (known after apply)
          ~ hourly_policy_id = "a54f053a-48b4-48fe-94f8-f181446b8b28" -> (known after apply)
        }
    }

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

cloudstack_snapshot_policy.frequent_backup: Destroying... [id=a54f053a-48b4-48fe-94f8-f181446b8b28]
cloudstack_snapshot_policy.frequent_backup_DAILY: Destroying... [id=a54f053a-48b4-48fe-94f8-f181446b8b28]
cloudstack_snapshot_policy.frequent_backup: Destruction complete after 0s
cloudstack_snapshot_policy.frequent_backup_DAILY: Destruction complete after 0s
cloudstack_snapshot_policy.frequent_backup: Creating...
cloudstack_snapshot_policy.frequent_backup_DAILY: Creating...
cloudstack_snapshot_policy.frequent_backup_DAILY: Creation complete after 1s [id=3c2f9ade-f66e-42eb-9947-a6f4d2342eef]
cloudstack_snapshot_policy.frequent_backup: Creation complete after 1s [id=3c2f9ade-f66e-42eb-9947-a6f4d2342eef]

Apply complete! Resources: 2 added, 0 changed, 2 destroyed.

Outputs:

deployment_summary = {
  "snapshot_policies" = {
    "daily_policy_id" = "3c2f9ade-f66e-42eb-9947-a6f4d2342eef"
    "hourly_policy_id" = "3c2f9ade-f66e-42eb-9947-a6f4d2342eef"
  }
}

@kiranchavala
Copy link
Collaborator

@Pearl1594 could you please look at the acceptance tests

@kiranchavala kiranchavala added this to the v0.6.0 milestone Sep 19, 2025
@kiranchavala
Copy link
Collaborator

@DaanHoogland could you please review

Copy link
Contributor

@DaanHoogland DaanHoogland left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clgtm (thought smaller methods are always better ;)

@kiranchavala kiranchavala marked this pull request as ready for review September 22, 2025 17:28
@kiranchavala kiranchavala merged commit 5105978 into main Sep 22, 2025
91 of 93 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support For Scheduled Volume Snapshots
3 participants