diff --git a/CHANGELOG.md b/CHANGELOG.md index b8ce64a6a..755f92ee3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ ENHANCEMENTS: * resources/opennebula_template: enable disk and nic update (#491) +BUG FIXES: + +* resources/opennebula_virtual_router_instance: fix re-contextualization (#537) + # 1.4.0 (January 22nd, 2024) FEATURES: diff --git a/opennebula/resource_opennebula_virtual_machine.go b/opennebula/resource_opennebula_virtual_machine.go index 949408609..8efbe5fde 100644 --- a/opennebula/resource_opennebula_virtual_machine.go +++ b/opennebula/resource_opennebula_virtual_machine.go @@ -605,17 +605,21 @@ func resourceOpennebulaVirtualMachineRead(ctx context.Context, d *schema.Resourc var diags diag.Diagnostics - // read template ID from which the VM was created - templateID, _ := vmInfos.Template.GetInt("TEMPLATE_ID") - d.Set("template_id", templateID) + // NOTE: The template_id attribute is not defined for Virtual Router instances (VMs). + if _, ok := d.GetOk("template_id"); ok { + // read template ID from which the VM was created + templateID, _ := vmInfos.Template.GetInt("TEMPLATE_ID") + d.Set("template_id", templateID) + + if _, ok := d.GetOk("template_nic"); !ok { + d.Set("template_nic", []interface{}{}) + } + } // add empty values for import if _, ok := d.GetOk("template_disk"); !ok { d.Set("template_disk", []interface{}{}) } - if _, ok := d.GetOk("template_nic"); !ok { - d.Set("template_nic", []interface{}{}) - } if _, ok := d.GetOk("template_tags"); !ok { d.Set("template_tags", map[string]interface{}{}) } @@ -632,14 +636,19 @@ func resourceOpennebulaVirtualMachineRead(ctx context.Context, d *schema.Resourc }) return diags } - err = flattenVMNIC(d, &vmInfos.Template) - if err != nil { - diags = append(diags, diag.Diagnostic{ - Severity: diag.Error, - Summary: "Failed to flatten NICs", - Detail: fmt.Sprintf("virtual machine (ID: %s): %s", d.Id(), err), - }) - return diags + + // In case of Virtual Router instances (which are just VMs) there's never anything to "flatten", + // that's because NICs are attached with a help of dedicated resources. + if _, ok := d.GetOk("nic"); ok { + err = flattenVMNIC(d, &vmInfos.Template) + if err != nil { + diags = append(diags, diag.Diagnostic{ + Severity: diag.Error, + Summary: "Failed to flatten NICs", + Detail: fmt.Sprintf("virtual machine (ID: %s): %s", d.Id(), err), + }) + return diags + } } return nil diff --git a/opennebula/resource_opennebula_virtual_router_test.go b/opennebula/resource_opennebula_virtual_router_test.go index e758fc583..e59745847 100644 --- a/opennebula/resource_opennebula_virtual_router_test.go +++ b/opennebula/resource_opennebula_virtual_router_test.go @@ -90,6 +90,22 @@ func TestAccVirtualRouter(t *testing.T) { }, "testacc-vr"), ), }, + { + Config: testAccVirtualRouterContextUpdate, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("opennebula_virtual_router_instance.test2", "context.update_test", "123"), + resource.TestCheckResourceAttrSet("opennebula_virtual_router.test", "uid"), + resource.TestCheckResourceAttrSet("opennebula_virtual_router.test", "gid"), + resource.TestCheckResourceAttrSet("opennebula_virtual_router.test", "uname"), + resource.TestCheckResourceAttrSet("opennebula_virtual_router.test", "gname"), + testAccCheckVirtualRouterPermissions(&shared.Permissions{ + OwnerU: 1, + OwnerM: 1, + GroupU: 1, + OtherM: 1, + }, "testacc-vr"), + ), + }, { Config: testAccVirtualRouterAddNICs, Check: resource.ComposeTestCheckFunc( @@ -371,6 +387,46 @@ resource "opennebula_virtual_router" "test" { } ` +var testAccVirtualRouterContextUpdate = testAccVirtualRouterMachineTemplate + ` + +resource "opennebula_virtual_router_instance" "test" { + name = "testacc-vr-virtual-machine" + group = "oneadmin" + permissions = "642" + memory = 128 + cpu = 0.1 + + virtual_router_id = opennebula_virtual_router.test.id +} + + +resource "opennebula_virtual_router_instance" "test2" { + name = "testacc-vr-virtual-machine-2" + group = "oneadmin" + permissions = "642" + memory = 128 + cpu = 0.1 + + virtual_router_id = opennebula_virtual_router.test.id + + context = { + update_test = "123" + } +} + +resource "opennebula_virtual_router" "test" { + name = "testacc-vr" + permissions = "642" + group = "oneadmin" + + instance_template_id = opennebula_virtual_router_instance_template.test.id + + tags = { + customer = "1" + } +} +` + var testAccVirtualRouterAddNICs = testAccVirtualRouterMachineTemplate + testAccVirtualRouterVNet + ` resource "opennebula_virtual_router_instance" "test" {