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 committed Apr 3, 2024
1 parent 83a9cb5 commit 0e1799d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ FEATURES:

* resources/opennebula_virtual_network: allow to modify the user owning the resource (#529)

BUG FIXES:

* resources/opennebula_virtual_router_instance: fix re-contextualization (#537)

# 1.4.0 (January 22nd, 2024)

FEATURES:
Expand Down
56 changes: 32 additions & 24 deletions opennebula/resource_opennebula_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,22 +605,25 @@ 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)

// 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{}{})
}
if _, ok := d.GetOk("template_section_names"); !ok {
d.Set("template_section_names", map[string]interface{}{})
// NOTE: The template_* attributes are 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)

// 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{}{})
}
if _, ok := d.GetOk("template_section_names"); !ok {
d.Set("template_section_names", map[string]interface{}{})
}
}

err := flattenVMDisk(d, &vmInfos.Template)
Expand All @@ -632,14 +635,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 @@ -367,6 +383,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 0e1799d

Please sign in to comment.