summaryrefslogtreecommitdiff
path: root/libvirt/resource_libvirt_network.go
diff options
context:
space:
mode:
authorFlavio Castelli <fcastelli@suse.com>2016-07-06 09:17:29 +0200
committerFlavio Castelli <fcastelli@suse.com>2016-07-06 09:17:29 +0200
commit549c39bb28bf19bc6c9972b39b87a2f01d9f894c (patch)
tree402e4ed97be4685759f9ec56b3abf8e917dd9208 /libvirt/resource_libvirt_network.go
parent51329134103b56c74a27289d63dde11bcf800a39 (diff)
downloadterraform-provider-libvirt-549c39bb28bf19bc6c9972b39b87a2f01d9f894c.tar
terraform-provider-libvirt-549c39bb28bf19bc6c9972b39b87a2f01d9f894c.tar.gz
Activate stopped networks
Make sure the network is active after the apply command is issued. This is really handy when running apply on a host that has just been rebooted. Signed-off-by: Flavio Castelli <fcastelli@suse.com>
Diffstat (limited to 'libvirt/resource_libvirt_network.go')
-rw-r--r--libvirt/resource_libvirt_network.go37
1 files changed, 37 insertions, 0 deletions
diff --git a/libvirt/resource_libvirt_network.go b/libvirt/resource_libvirt_network.go
index c8b54c5d..354ded99 100644
--- a/libvirt/resource_libvirt_network.go
+++ b/libvirt/resource_libvirt_network.go
@@ -39,6 +39,7 @@ func resourceLibvirtNetwork() *schema.Resource {
Read: resourceLibvirtNetworkRead,
Delete: resourceLibvirtNetworkDelete,
Exists: resourceLibvirtNetworkExists,
+ Update: resourceLibvirtNetworkUpdate,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
@@ -71,6 +72,12 @@ func resourceLibvirtNetwork() *schema.Resource {
Type: schema.TypeString,
},
},
+ "running": &schema.Schema{
+ Type: schema.TypeBool,
+ Optional: true,
+ Default: true,
+ ForceNew: false,
+ },
},
}
}
@@ -85,6 +92,30 @@ func resourceLibvirtNetworkExists(d *schema.ResourceData, meta interface{}) (boo
return err == nil, err
}
+func resourceLibvirtNetworkUpdate(d *schema.ResourceData, meta interface{}) error {
+ virConn := meta.(*Client).libvirt
+ if virConn == nil {
+ return fmt.Errorf("The libvirt connection was nil.")
+ }
+ network, err := virConn.LookupNetworkByUUIDString(d.Id())
+ defer network.Free()
+
+ active, err := network.IsActive()
+ if err != nil {
+ return err
+ }
+
+ if !active {
+ log.Printf("[DEBUG] Activating network")
+ if err := network.Create(); err != nil {
+ return err
+ }
+ d.Set("running", true)
+ }
+
+ return nil
+}
+
func resourceLibvirtNetworkCreate(d *schema.ResourceData, meta interface{}) error {
// see https://libvirt.org/formatnetwork.html
virConn := meta.(*Client).libvirt
@@ -242,6 +273,12 @@ func resourceLibvirtNetworkRead(d *schema.ResourceData, meta interface{}) error
d.Set("domain", networkDef.Domain.Name)
d.Set("bridge", networkDef.Bridge.Name)
+ active, err := network.IsActive()
+ if err != nil {
+ return err
+ }
+ d.Set("running", active)
+
addresses := []string{}
for _, address := range networkDef.Ips {
// we get the host interface IP (ie, 10.10.8.1) but we want the network CIDR (ie, 10.10.8.0/24)