OpenNebula / terraform-provider-opennebula

Terraform provider for OpenNebula
https://www.terraform.io/docs/providers/opennebula/
Mozilla Public License 2.0
61 stars 53 forks source link

opennebula_virtual_machine_group fails to store state after creation #497

Open sorinpad opened 8 months ago

sorinpad commented 8 months ago

Description

Creation of a VM group is successful, subsequent runs do not try to modify the resource.

In practice, Terraform tries to update VM group with all but the first host in the host_anti_affined attribute.

Terraform and Provider version

Terraform: 1.3.9, 1.4.6, 1.5.7 Provider: 1.2.2, 1.3.1

Affected resources and data sources

opennebula_virtual_machine_group

Terraform configuration

resource "opennebula_virtual_machine_group" "random" {
  name        = "team-random-group"
  group       = "oneadmin"
  permissions = "642"

  role {
    name = "random"
    host_anti_affined = [
      module.hypervisor_prod.opennebula_cluster_hosts["random-host-40"].id,
      module.hypervisor_prod.opennebula_cluster_hosts["random-host-44"].id,
      module.hypervisor_prod.opennebula_cluster_hosts["random-host-47"].id,
      module.hypervisor_prod.opennebula_cluster_hosts["random-host-02"].id,
      module.hypervisor_prod.opennebula_cluster_hosts["random-host-33"].id,
      module.hypervisor_prod.opennebula_cluster_hosts["random-host-21"].id,
      module.hypervisor_prod.opennebula_cluster_hosts["random-host-22"].id,
      module.hypervisor_prod.opennebula_cluster_hosts["random-host-58"].id,
      module.hypervisor_prod.opennebula_cluster_hosts["random-host-59"].id,
      module.hypervisor_prod.opennebula_cluster_hosts["random-host-60"].id,
      module.hypervisor_prod.opennebula_cluster_hosts["random-host-61"].id,
    ]
  }
}

Expected behavior

The VM group gets created with a role called random which lists the 11 hosts in the anti_affined list.

Actual behavior

The VM Group gets created with a role called random which lists the 11 hosts in the anti_affined list, however, on subsequent runs, Terraform tries to add all but the last host to the anti_affined list.

Steps to Reproduce

Create a VM group:

  # opennebula_virtual_machine_group.random will be created
+ resource "opennebula_virtual_machine_group" "random" {
+       default_tags = (known after apply)
+       gid          = (known after apply)
+       gname        = (known after apply)
+       group        = "oneadmin"
+       id           = (known after apply)
+       name         = "team-random-group"
+       permissions  = "642"
+       tags_all     = (known after apply)
+       uid          = (known after apply)
+       uname        = (known after apply)

+       role {
+           host_affined      = (known after apply)
+           host_anti_affined = [
              + 22,
              + 21,
              + 20,
              + 32,
              + 28,
              + 30,
              + 31,
              + 23,
              + 2,
              + 3,
              + 16,
            ]
+           id                = (known after apply)
+           name              = "random"
+           policy            = (known after apply)
        }
    }
...
opennebula_virtual_machine_group.random: Creating...
opennebula_virtual_machine_group.random: Creation complete after 0s [id=18]

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

Run the plan again; Terraform wants to add all but the first host in the list:

  # opennebula_virtual_machine_group.random will be updated in-place
! resource "opennebula_virtual_machine_group" "random" {
        id           = "18"
        name         = "team-random-group"
        # (8 unchanged attributes hidden)

!       role {
!           host_anti_affined = [
              + 22,
              + 21,
              + 20,
              + 32,
              + 28,
              + 30,
              + 31,
              + 23,
              + 2,
              + 3,
                16,
            ]
            id                = 0
            name              = "random"
            # (1 unchanged attribute hidden)
        }
    }

Trying to apply the change fails (but that's on Nebula's API side), stating the group has some VMs attached:

╷
│ Error: Failed to update content
│ 
│   with opennebula_virtual_machine_group.random,
│   on vm_groups_team.tf line 100, in resource "opennebula_virtual_machine_group" "random":
│  100: resource "opennebula_virtual_machine_group" "random" {
│ 
│ VM group (ID: 11): OpenNebula error [INTERNAL]: [one.vmgroup.update] Cannot
│ update template. VM Group has 2 VMs
╵

We can see Nebula itself already has the hosts in the VM group:

$ onevmgroup show 18
VM GROUP 18 INFORMATION
ID             : 18
NAME           : team-random-group
USER           : oneadmin
GROUP          : oneadmin
LOCK           : None
PERMISSIONS

OWNER          : um-
GROUP          : u--
OTHER          : -m-

ROLES
ID   NAME     POLICY       AFFINED_HOSTS      ANTI_AFFINED_HOST  VIRTUAL_MACHINES
0    random                                   22,21,20,32,28,30,

TEMPLATE CONTENTS

Debug output

No response

Panic output

No response

Important factoids

No response

References

No response

github-actions[bot] commented 7 months ago

This issue is stale because it has been open for 30 days with no activity and it has not the 'status: confirmed' label or it is not in a milestone. Remove the 'status: stale' label or comment, or this will be closed in 5 days.

treywelsh commented 5 days ago

Sorry for the late reply, it seems it was a small error in the code, feel free to test the fix in the PR I just created