From 25123515b40e84c8239088cad4482d7b9fb1767e Mon Sep 17 00:00:00 2001 From: Christian Simon Date: Sat, 2 Dec 2017 18:19:50 +0000 Subject: Make add/updateHost a bit more resilient * provide a updateOrAddHost method that first will try to update an existing one, before adding a host entry --- libvirt/resource_libvirt_domain.go | 8 ++++---- libvirt/utils_libvirt.go | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) (limited to 'libvirt') diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 95a7b7fe..ffbdc40b 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -587,9 +587,9 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error if ip == nil { return fmt.Errorf("Could not parse addresses '%s'", address) } - // TODO: we should check the IP is contained in the DHCP addresses served + log.Printf("[INFO] Adding IP/MAC/host=%s/%s/%s to %s", ip.String(), mac, hostname, networkName) - if err := addHost(network, ip.String(), mac, hostname); err != nil { + if err := updateOrAddHost(network, ip.String(), mac, hostname); err != nil { return err } } @@ -720,7 +720,7 @@ func resourceLibvirtDomainCreate(d *schema.ResourceData, meta interface{}) error for _, addressI := range addressesI.([]interface{}) { address := addressI.(string) log.Printf("[INFO] Finally adding IP/MAC/host=%s/%s/%s", address, mac, pending.hostname) - addHost(pending.network, address, mac, pending.hostname) + updateOrAddHost(pending.network, address, mac, pending.hostname) if err != nil { return fmt.Errorf("Could not add IP/MAC/host=%s/%s/%s: %s", address, mac, pending.hostname, err) } @@ -818,7 +818,7 @@ func resourceLibvirtDomainUpdate(d *schema.ResourceData, meta interface{}) error return fmt.Errorf("Could not parse addresses '%s'", address) } log.Printf("[INFO] Updating IP/MAC/host=%s/%s/%s in '%s' network", ip.String(), mac, hostname, networkName) - if err := updateHost(network, ip.String(), mac, hostname); err != nil { + if err := updateOrAddHost(network, ip.String(), mac, hostname); err != nil { return err } } diff --git a/libvirt/utils_libvirt.go b/libvirt/utils_libvirt.go index 22816769..0b1da1ef 100644 --- a/libvirt/utils_libvirt.go +++ b/libvirt/utils_libvirt.go @@ -46,6 +46,15 @@ func updateHost(n *libvirt.Network, ip, mac, name string) error { return n.Update(libvirt.NETWORK_UPDATE_COMMAND_MODIFY, libvirt.NETWORK_SECTION_IP_DHCP_HOST, -1, xmlDesc, libvirt.NETWORK_UPDATE_AFFECT_CURRENT) } +// Tries to update first, if that fails, it will add it +func updateOrAddHost(n *libvirt.Network, ip, mac, name string) error { + err := updateHost(n, ip, mac, name) + if virErr, ok := err.(libvirt.Error); ok && virErr.Code == libvirt.ERR_OPERATION_INVALID && virErr.Domain == libvirt.FROM_NETWORK { + return addHost(n, ip, mac, name) + } + return err +} + func getHostArchitecture(virConn *libvirt.Connect) (string, error) { type HostCapabilities struct { XMLName xml.Name `xml:"capabilities"` -- cgit v1.2.3