diff options
author | Flavio Castelli <flavio@castelli.name> | 2016-07-11 17:58:14 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-07-11 17:58:14 +0200 |
commit | 80172117bcff270c7c066ad0819231423fa26848 (patch) | |
tree | 81bff25cdb014fc9f83c707f00e2d791294f9993 /libvirt | |
parent | 560f32d1a811db6404cce686d893877638715d22 (diff) | |
parent | 549c39bb28bf19bc6c9972b39b87a2f01d9f894c (diff) | |
download | terraform-provider-libvirt-80172117bcff270c7c066ad0819231423fa26848.tar terraform-provider-libvirt-80172117bcff270c7c066ad0819231423fa26848.tar.gz |
Merge pull request #51 from flavio/activate-stopped-networks
Activate stopped networks
Diffstat (limited to 'libvirt')
-rw-r--r-- | libvirt/resource_libvirt_network.go | 37 |
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) |