Skip to content

Commit

Permalink
B #537: Make Virtual Router's instances re-contextable.
Browse files Browse the repository at this point in the history
  • Loading branch information
sk4zuzu authored and treywelsh committed May 22, 2024
1 parent 50ab4c3 commit 099209b
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
37 changes: 23 additions & 14 deletions opennebula/resource_opennebula_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{})
}
Expand All @@ -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
Expand Down
56 changes: 56 additions & 0 deletions opennebula/resource_opennebula_virtual_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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" {
Expand Down

0 comments on commit 099209b

Please sign in to comment.