Closed davidg251 closed 1 month ago
We should send the id instead of the name for resourcePolicies. Below is the current implementation which has the problem
POST /compute/beta/projects/projects/myproject/zones/us-central1-a/instances?alt=json&prettyPrint=false&sourceInstanceTemplate=projects%2Fmyprojectt%2Fglobal%2FinstanceTemplates%2Fissue9260
Host: www.googleapis.com
{
"disks": [
{
"autoDelete": true,
"boot": true,
"deviceName": "persistent-disk-0",
"initializeParams": {
"diskType": "zones/us-central1-a/diskTypes/pd-standard",
"resourcePolicies": [
"issue9260"
],
"sourceImage": "projects/debian-cloud/global/images/family/debian-9"
},
"interface": "SCSI",
"kind": "compute#attachedDisk",
"mode": "READ_WRITE",
"type": "PERSISTENT"
}
],
"metadata": {},
"name": "instance-from-issue9260",
"scheduling": {
"automaticRestart": true,
"onHostMaintenance": "MIGRATE"
},
"tags": {},
"zone": ""
}
Noticed the data format of resourcePolicies
, it is a shortname returned from api but when creating an instance, it requires the format like projects/%s/regions/%s/resourcePolicies/%s
. The provider rely on whatever returned from api of the template to create an instance.
Add label to upstream
I am facing the same issue here when using the example to develop my module code. Is there any work around to make this work?
I just removed the block.
Was there ever a workaround or resolution to this. Currently running into this issue
It looks like we have workarounds for similar cases for disk.initialize_params.disk_type and disk.source_image: https://github.com/hashicorp/terraform-provider-google/blob/4a1460e095d64cbfade02da4207a66bbbf860894/google/resource_compute_instance_from_template.go#L223 According to the comment, this code has to always override the disks to allow field overrides to work correctly on a per-disk basis.
However, we don't actually currently support disk resource policies on compute instances at all - we just modify the array of disks to add whatever was on the instance template. Since instance templates support disk resource policies, those can get passed through to compute instances implicitly without their formatting getting updated to specify a full path.
I think this is not actually an upstream bug. Ideally the behavior here would be:
In terms of workarounds - managing compute instances directly or using google_compute_instance_group_manager may be a better option.
I wonder if we could consider making this resource not try to override any fields if no overrides are provided on the resource.
Another workaround would be to just use this resource instead of attaching the resource policy on the disk in the template instance:
resource "google_compute_disk_resource_policy_attachment" "<redacted>" {
name = google_compute_resource_policy.weekly_backup_euw4.name
disk = google_compute_instance_from_template.<redacted>.name
zone = google_compute_instance_from_template.<redacted>.zone
}
The trick being that the root disk will have the same name as the VM itself. Of course, this won't work as easy for additional disks.
The fix still remains implemeting this in the provider:
I think this is not actually an upstream bug. Ideally the behavior here would be:
- google_compute_instance supports disk resource policies
- google_compute_instance_from_template handles fixing disk resource policy references
Working on a pr supporting disk resource policies on instance
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.
Community Note
modular-magician
user, it is either in the process of being autogenerated, or is planned to be autogenerated soon. If an issue is assigned to a user, that user is claiming responsibility for the issue. If an issue is assigned tohashibot
, a community member has claimed the issue already.Terraform Version
Affected Resource(s)
Terraform Configuration Files
I'm using the example to create a new instance template https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance_template . Terraform is creating the template successfully. But for the VM Google API is showing the error on Debug Output section.
With this file terraform apply doesn't work. https://gist.github.com/nan140114/2791cce40e746073f99ee96f5e8d9994
After remove the
resource_policies = [google_compute_resource_policy.daily_backup.id]
on disk block. It works. With this file terraform apply does work. https://gist.github.com/nan140114/0f80d962876be5a808f5db51ed55e6b4I tried creating the a new VM from the template via GCP Console and it worked.
Debug Output
https://gist.github.com/nan140114/31651576f90ee48e9e7d910bffddae9e
Panic Output
Expected Behavior
A new virtual created from the specified instance template
A new VM created from the template
Actual Behavior
Error on VM creation.
Steps to Reproduce
terraform init
terraform plan -var-file=./envs/development.tfvars
terraform apply -auto-approve -parallelism=1 -var-file=./envs/development.tfvars
Important Factoids
The terraform plan is OK.
References
b/308756318