func waitForSnapshotState(state string, snapshotId string, client *vultr.Client, timeout time.Duration) error { done := make(chan struct{}) defer close(done) result := make(chan error, 1) go func() { attempts := 0 for { attempts += 1 log.Printf("Checking snapshot status... (attempt: %d)", attempts) snapshotInfo, err := client.GetSnapshot(snapshotId) if err != nil { result <- err return } if snapshotInfo.Status == state { result <- nil return } // Wait 3 seconds in between time.Sleep(3 * time.Second) // Verify we shouldn't exit select { case <-done: // We finished, so just exit the goroutine return default: // Keep going } } }() log.Printf("Waiting for up to %d seconds for snapshot", timeout/time.Second) select { case retval := <-result: return retval case <-time.After(timeout): err := fmt.Errorf("Timeout while waiting to for snapshot") return err } }