Skip to content

Commit

Permalink
providers/azurerm: cancellable storage account creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Aug 16, 2016
1 parent d833792 commit 8dafcb3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
21 changes: 19 additions & 2 deletions builtin/providers/azurerm/resource_arm_storage_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/storage"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/signalwrapper"
)

func resourceArmStorageAccount() *schema.Resource {
Expand Down Expand Up @@ -131,9 +132,25 @@ func resourceArmStorageAccountCreate(d *schema.ResourceData, meta interface{}) e
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 {
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
Expand Down
5 changes: 5 additions & 0 deletions helper/signalwrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package signalwrapper

import (
"log"
"os"
"os/signal"
"sync"
Expand Down Expand Up @@ -49,6 +50,7 @@ func Run(f CancellableFunc) *Wrapped {

// Start the function
go func() {
log.Printf("[DEBUG] signalwrapper: executing wrapped function")
err := f(cancelCh)

// Close the done channel _before_ sending the error in case
Expand All @@ -57,6 +59,7 @@ func Run(f CancellableFunc) *Wrapped {
close(doneCh)

// Mark completion
log.Printf("[DEBUG] signalwrapper: wrapped function execution ended")
wrapped.done(err)
}()

Expand All @@ -71,6 +74,8 @@ func Run(f CancellableFunc) *Wrapped {
case <-doneCh:
// Everything happened naturally
case <-sigCh:
log.Printf("[DEBUG] signalwrapper: signal received, cancelling wrapped function")

// Stop the function. Goroutine since we don't care about
// the result and we'd like to end this goroutine as soon
// as possible to avoid any more signals coming in.
Expand Down

0 comments on commit 8dafcb3

Please sign in to comment.