diff options
author | Thomas Hipp <thomashipp@gmail.com> | 2017-12-08 13:21:07 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-08 13:21:07 +0100 |
commit | a20d2a92668b2d5e077267c50074bf853ef371dd (patch) | |
tree | 4ad60acc121ffc091b4d88addbb69e46bac77641 /libvirt | |
parent | 380076c1943ebd10c5e5f54ad4ac751a736c5c1f (diff) | |
parent | 25123515b40e84c8239088cad4482d7b9fb1767e (diff) | |
download | terraform-provider-libvirt-a20d2a92668b2d5e077267c50074bf853ef371dd.tar terraform-provider-libvirt-a20d2a92668b2d5e077267c50074bf853ef371dd.tar.gz |
Merge pull request #221 from simonswine/fixes-net-updates
Make add/updateHost a bit more resilient
Diffstat (limited to 'libvirt')
-rw-r--r-- | libvirt/resource_libvirt_domain.go | 8 | ||||
-rw-r--r-- | libvirt/utils_libvirt.go | 9 |
2 files changed, 13 insertions, 4 deletions
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"` |