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

Fix/qemuagent #873

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/digitalocean/go-libvirt v0.0.0-20210615174804-eaff166426e3
github.com/fatih/color v1.10.0 // indirect
github.com/google/uuid v1.1.2
github.com/hashicorp/go-getter v1.4.0 // indirect
github.com/hashicorp/terraform-plugin-sdk v1.9.0
github.com/hooklift/assert v0.0.0-20170704181755-9d1defd6d214 // indirect
github.com/hooklift/iso9660 v1.0.0
Expand Down
21 changes: 12 additions & 9 deletions libvirt/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,27 +154,30 @@ func domainGetIfacesInfo(virConn *libvirt.Libvirt, domain libvirt.Domain, rd *sc
return []libvirt.DomainInterface{}, nil
}

// setup source of interface address information
var addrsrc uint32
qemuAgentEnabled := rd.Get("qemu_agent").(bool)
if qemuAgentEnabled {
log.Print("[DEBUG] Not implemented")
addrsrc = uint32(libvirt.DomainInterfaceAddressesSrcAgent)
log.Printf("[DEBUG] qemu-agent used to query interface info")
} else {
log.Printf("[DEBUG] qemu-agent is not used")
addrsrc = uint32(libvirt.DomainInterfaceAddressesSrcLease)
log.Printf("[DEBUG] Obtain interface info from dhcp lease file")
}
var interfaces []libvirt.DomainInterface

// get all the interfaces attached to libvirt networks
log.Print("[DEBUG] no interfaces could be obtained with qemu-agent: falling back to the libvirt API")

interfaces, err = virConn.DomainInterfaceAddresses(domain, uint32(libvirt.DomainInterfaceAddressesSrcLease), 0)
var interfaces []libvirt.DomainInterface
interfaces, err = virConn.DomainInterfaceAddresses(domain, addrsrc, 0)
if err != nil {
switch err.(type) {
default:
return interfaces, fmt.Errorf("Error retrieving interface addresses: %s", err)
case libvirt.Error:
virErr := err.(libvirt.Error)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this cast here as it is not needed in this switch type.

// FIXME ErrorDomain.fromQemu not available in libvirt.Error
// || libvirt.ErrorvirErr.Domain != libvirt.FROM_QEMU {
if virErr.Code != uint32(libvirt.ErrOperationInvalid) {

// Agent can be unresponsive if being installed/setup
if addrsrc == uint32(libvirt.DomainInterfaceAddressesSrcLease) && virErr.Code != uint32(libvirt.ErrOperationInvalid) ||
addrsrc == uint32(libvirt.DomainInterfaceAddressesSrcAgent) && virErr.Code != uint32(libvirt.ErrAgentUnresponsive) {
return interfaces, fmt.Errorf("Error retrieving interface addresses: %s", err)
}
}
Expand Down
109 changes: 109 additions & 0 deletions libvirt/resource_libvirt_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (
"net/url"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
"testing"

libvirt "github.com/digitalocean/go-libvirt"
getter "github.com/hashicorp/go-getter"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
Expand Down Expand Up @@ -649,6 +651,113 @@ func TestAccLibvirtDomain_NetworkInterface(t *testing.T) {
})
}

func TestAccLibvirtDomain_NetworkInterfaceQemuGuestAgentStaticIP(t *testing.T) {
skipIfPrivilegedDisabled(t)

var domain libvirt.Domain
randomPoolName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomPoolPath := "/tmp/terraform-provider-libvirt-pool-" + randomPoolName
randomDomainName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)
randomNetworkName := acctest.RandStringFromCharSet(10, acctest.CharSetAlpha)

currentDir, err := os.Getwd()
if err != nil {
t.Fatal("Unexpected error:", err)
}

testvmimg := "alpine-qemu-agent-static-ip.qcow2"
sha256checksum := "210185c04b9350c4c2f654ee021cd034a67e42ae42b0c0ae1f04cf8989ac0d50"
urldir := "https://github.com/maseman/testvm-tflibvirt/releases/download/v1.0.0"

urlstr := fmt.Sprintf(`%s/%s?checksum=sha256:%s`, urldir, testvmimg, sha256checksum)
dstdir := path.Join(currentDir, "testdata")
testvmimgfullpath := path.Join(dstdir, testvmimg)

err = getter.GetAny(dstdir, urlstr)

if err != nil {
// cleanup if necessary
_, errstat := os.Stat(testvmimgfullpath)
if errstat == nil {
os.Remove(testvmimgfullpath)
}

t.Fatal("Download VM image error:", err)
}

var config = fmt.Sprintf(`

resource "libvirt_pool" "%s" {
name = "%s"
type = "dir"
path = "%s"
}

// Runs alpine with qemu-agent and hard coded static IPv4 address on eth0 192.168.111.111
resource "libvirt_volume" "alpine-qga-staticip" {
source = "%s"
name = "alpine-qga-staticip"
pool = "${libvirt_pool.%s.name}"
}

resource "libvirt_network" "%s" {
name = "%s"
mode = "none"
}

resource "libvirt_domain" "%s" {
name = "%s"
qemu_agent = true

network_interface {
network_id = "${libvirt_network.%s.id}"
hostname = "myhost"
mac = "52:54:00:A9:F5:21"
wait_for_lease = true
}

disk {
volume_id = libvirt_volume.alpine-qga-staticip.id
}

console {
type = "pty"
target_port = "0"
target_type = "serial"
}

console {
type = "pty"
target_type = "virtio"
target_port = "1"
}

}`, randomPoolName, randomPoolName, randomPoolPath, testvmimgfullpath, randomPoolName, randomNetworkName, randomNetworkName, randomDomainName, randomDomainName, randomNetworkName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLibvirtDomainDestroy,
Steps: []resource.TestStep{
{
Config: config,
ExpectNonEmptyPlan: false,
Check: resource.ComposeTestCheckFunc(
testAccCheckLibvirtDomainExists("libvirt_domain."+randomDomainName, &domain),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "network_interface.0.network_name", randomNetworkName),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "network_interface.0.mac", "52:54:00:A9:F5:21"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "network_interface.0.addresses.0", "192.168.111.111"),
resource.TestCheckResourceAttr(
"libvirt_domain."+randomDomainName, "network_interface.0.hostname", "myhost"),
),
},
},
})
}

func TestAccLibvirtDomain_CheckDHCPEntries(t *testing.T) {
skipIfPrivilegedDisabled(t)

Expand Down
1 change: 1 addition & 0 deletions libvirt/testdata/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alpine-qemu-agent-static-ip.qcow2