|
| 1 | +package libvirt |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform/helper/resource" |
| 9 | + libvirt "github.com/libvirt/libvirt-go" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + poolExistsID = "EXISTS" |
| 14 | + poolNotExistsID = "NOT-EXISTS" |
| 15 | +) |
| 16 | + |
| 17 | +// poolExists returns "EXISTS" or "NOT-EXISTS" depending on the current pool existence |
| 18 | +func poolExists(virConn *libvirt.Connect, uuid string) resource.StateRefreshFunc { |
| 19 | + return func() (interface{}, string, error) { |
| 20 | + pool, err := virConn.LookupStoragePoolByUUIDString(uuid) |
| 21 | + if err != nil { |
| 22 | + if err.(libvirt.Error).Code == libvirt.ERR_NO_STORAGE_POOL { |
| 23 | + log.Printf("Pool %s does not exist", uuid) |
| 24 | + return virConn, "NOT-EXISTS", nil |
| 25 | + } |
| 26 | + log.Printf("Pool %s: error: %s", uuid, err.(libvirt.Error).Message) |
| 27 | + } |
| 28 | + if pool != nil { |
| 29 | + defer pool.Free() |
| 30 | + } |
| 31 | + return virConn, poolExistsID, err |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +// poolWaitForExists waits for a storage pool to be up and timeout after 5 minutes. |
| 36 | +func poolWaitForExists(virConn *libvirt.Connect, uuid string) error { |
| 37 | + log.Printf("Waiting for pool %s to be active...", uuid) |
| 38 | + stateConf := &resource.StateChangeConf{ |
| 39 | + Pending: []string{poolNotExistsID}, |
| 40 | + Target: []string{poolExistsID}, |
| 41 | + Refresh: poolExists(virConn, uuid), |
| 42 | + Timeout: 1 * time.Minute, |
| 43 | + Delay: 5 * time.Second, |
| 44 | + MinTimeout: 3 * time.Second, |
| 45 | + } |
| 46 | + |
| 47 | + if _, err := stateConf.WaitForState(); err != nil { |
| 48 | + log.Printf("%s", err) |
| 49 | + return fmt.Errorf("unexpected error during pool creation operation. The operation did not complete successfully") |
| 50 | + } |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +// poolWaitDeleted waits for a storage pool to be removed |
| 55 | +func poolWaitDeleted(virConn *libvirt.Connect, uuid string) error { |
| 56 | + log.Printf("Waiting for pool %s to be deleted...", uuid) |
| 57 | + stateConf := &resource.StateChangeConf{ |
| 58 | + Pending: []string{poolExistsID}, |
| 59 | + Target: []string{poolNotExistsID}, |
| 60 | + Refresh: poolExists(virConn, uuid), |
| 61 | + Timeout: 1 * time.Minute, |
| 62 | + Delay: 5 * time.Second, |
| 63 | + MinTimeout: 3 * time.Second, |
| 64 | + } |
| 65 | + |
| 66 | + if _, err := stateConf.WaitForState(); err != nil { |
| 67 | + log.Printf("%s", err) |
| 68 | + return fmt.Errorf("unexpected error during pool destroy operation. The pool was not deleted") |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +// deletePool deletes the pool identified by `uuid` from libvirt |
| 74 | +func deletePool(client *Client, uuid string) error { |
| 75 | + virConn := client.libvirt |
| 76 | + if virConn == nil { |
| 77 | + return fmt.Errorf(LibVirtConIsNil) |
| 78 | + } |
| 79 | + |
| 80 | + pool, err := virConn.LookupStoragePoolByUUIDString(uuid) |
| 81 | + if err != nil { |
| 82 | + return fmt.Errorf("error retrieving storage pool info: %s", err) |
| 83 | + } |
| 84 | + |
| 85 | + poolName, err := pool.GetName() |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("error retrieving storage pool name: %s", err) |
| 88 | + } |
| 89 | + client.poolMutexKV.Lock(poolName) |
| 90 | + defer client.poolMutexKV.Unlock(poolName) |
| 91 | + |
| 92 | + info, err := pool.GetInfo() |
| 93 | + if err != nil { |
| 94 | + return fmt.Errorf("error retrieving storage pool info: %s", err) |
| 95 | + } |
| 96 | + |
| 97 | + if info.State != libvirt.STORAGE_POOL_INACTIVE { |
| 98 | + err := pool.Destroy() |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("error deleting storage pool: %s", err) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + err = pool.Delete(0) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("error deleting storage pool: %s", err) |
| 107 | + } |
| 108 | + |
| 109 | + err = pool.Undefine() |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("error deleting storage pool: %s", err) |
| 112 | + } |
| 113 | + |
| 114 | + return poolWaitDeleted(client.libvirt, uuid) |
| 115 | +} |
0 commit comments