providers/azurerm: cancellable storage account creation

This commit is contained in:
Mitchell Hashimoto 2016-08-15 21:12:32 -07:00
parent d8337920f9
commit 8dafcb36fd
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 24 additions and 2 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/storage" "github.com/Azure/azure-sdk-for-go/arm/storage"
"github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/signalwrapper"
) )
func resourceArmStorageAccount() *schema.Resource { func resourceArmStorageAccount() *schema.Resource {
@ -131,9 +132,25 @@ func resourceArmStorageAccountCreate(d *schema.ResourceData, meta interface{}) e
Tags: expandTags(tags), Tags: expandTags(tags),
} }
_, err := storageClient.Create(resourceGroupName, storageAccountName, opts, make(chan struct{})) // Create the storage account. We wrap this so that it is cancellable
// with a Ctrl-C since this can take a LONG time.
wrap := signalwrapper.Run(func(cancelCh <-chan struct{}) error {
_, err := storageClient.Create(resourceGroupName, storageAccountName, opts, cancelCh)
return err
})
// Check the result of the wrapped function. I put this into a select
// since we will likely also want to introduce a time-based timeout.
var err error
select {
case err = <-wrap.ErrCh:
// Successfully ran (but perhaps not successfully completed)
// the function.
}
if err != nil { if err != nil {
return fmt.Errorf("Error creating Azure Storage Account '%s': %s", storageAccountName, err) return fmt.Errorf(
"Error creating Azure Storage Account '%s': %s",
storageAccountName, err)
} }
// The only way to get the ID back apparently is to read the resource again // The only way to get the ID back apparently is to read the resource again

View File

@ -5,6 +5,7 @@
package signalwrapper package signalwrapper
import ( import (
"log"
"os" "os"
"os/signal" "os/signal"
"sync" "sync"
@ -49,6 +50,7 @@ func Run(f CancellableFunc) *Wrapped {
// Start the function // Start the function
go func() { go func() {
log.Printf("[DEBUG] signalwrapper: executing wrapped function")
err := f(cancelCh) err := f(cancelCh)
// Close the done channel _before_ sending the error in case // Close the done channel _before_ sending the error in case
@ -57,6 +59,7 @@ func Run(f CancellableFunc) *Wrapped {
close(doneCh) close(doneCh)
// Mark completion // Mark completion
log.Printf("[DEBUG] signalwrapper: wrapped function execution ended")
wrapped.done(err) wrapped.done(err)
}() }()
@ -71,6 +74,8 @@ func Run(f CancellableFunc) *Wrapped {
case <-doneCh: case <-doneCh:
// Everything happened naturally // Everything happened naturally
case <-sigCh: case <-sigCh:
log.Printf("[DEBUG] signalwrapper: signal received, cancelling wrapped function")
// Stop the function. Goroutine since we don't care about // Stop the function. Goroutine since we don't care about
// the result and we'd like to end this goroutine as soon // the result and we'd like to end this goroutine as soon
// as possible to avoid any more signals coming in. // as possible to avoid any more signals coming in.