Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

B #537: Make Virtual Router's instances re-contextable. #538

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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 {
treywelsh marked this conversation as resolved.
Show resolved Hide resolved
// 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 @@ -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
Loading