diff options
-rw-r--r-- | libvirt/resource_libvirt_domain.go | 21 | ||||
-rw-r--r-- | libvirt/resource_libvirt_network.go | 14 | ||||
-rw-r--r-- | libvirt/utils_libvirt.go | 9 | ||||
-rw-r--r-- | website/docs/r/domain.html.markdown | 2 |
4 files changed, 13 insertions, 33 deletions
diff --git a/libvirt/resource_libvirt_domain.go b/libvirt/resource_libvirt_domain.go index 92af802a..ffbdc40b 100644 --- a/libvirt/resource_libvirt_domain.go +++ b/libvirt/resource_libvirt_domain.go @@ -79,12 +79,6 @@ func resourceLibvirtDomain() *schema.Resource { Optional: true, ForceNew: true, }, - "running": { - Type: schema.TypeBool, - Optional: true, - Default: true, - ForceNew: false, - }, "cloudinit": { Type: schema.TypeString, Required: false, @@ -593,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 } } @@ -726,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) } @@ -824,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 } } @@ -905,13 +899,6 @@ func resourceLibvirtDomainRead(d *schema.ResourceData, meta interface{}) error { // Emulator is the same as the default don't set it in domainDef // or it will show as changed d.Set("emulator", domainDef.Devices.Emulator) - - running, err := domainIsRunning(*domain) - if err != nil { - return err - } - d.Set("running", running) - var disks []map[string]interface{} for _, diskDef := range domainDef.Devices.Disks { // network drives do not have a volume associated diff --git a/libvirt/resource_libvirt_network.go b/libvirt/resource_libvirt_network.go index a108d648..4cf6baee 100644 --- a/libvirt/resource_libvirt_network.go +++ b/libvirt/resource_libvirt_network.go @@ -79,12 +79,6 @@ func resourceLibvirtNetwork() *schema.Resource { Optional: true, Required: false, }, - "running": { - Type: schema.TypeBool, - Optional: true, - Default: true, - ForceNew: false, - }, "dns_forwarder": { Type: schema.TypeList, Optional: true, @@ -157,8 +151,6 @@ func resourceLibvirtNetworkUpdate(d *schema.ResourceData, meta interface{}) erro if err := network.Create(); err != nil { return err } - d.Set("running", true) - d.SetPartial("running") } if d.HasChange("autostart") { @@ -377,12 +369,6 @@ func resourceLibvirtNetworkRead(d *schema.ResourceData, meta interface{}) error d.Set("domain", networkDef.Domain.Name) } - active, err := network.IsActive() - if err != nil { - return err - } - d.Set("running", active) - autostart, err := network.GetAutostart() if err != nil { return fmt.Errorf("Error reading network autostart setting: %s", 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"` diff --git a/website/docs/r/domain.html.markdown b/website/docs/r/domain.html.markdown index f008501e..01ed1fc3 100644 --- a/website/docs/r/domain.html.markdown +++ b/website/docs/r/domain.html.markdown @@ -31,8 +31,6 @@ The following arguments are supported: will be created. * `memory` - (Optional) The amount of memory in MiB. If not specified the domain will be created with 512 MiB of memory be used. -* `running` - (Optional) Use `false` to turn off the instance. If not specified, - true is assumed and the instance, if stopped, will be started at next apply. * `disk` - (Optional) An array of one or more disks to attach to the domain. The `disk` object structure is documented [below](#handling-disks). * `network_interface` - (Optional) An array of one or more network interfaces to |