providers/azurerm: cancellable storage account creation
This commit is contained in:
parent
d8337920f9
commit
8dafcb36fd
|
@ -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 {
|
||||
|
@ -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
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
package signalwrapper
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
|
@ -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
|
||||
|
@ -57,6 +59,7 @@ func Run(f CancellableFunc) *Wrapped {
|
|||
close(doneCh)
|
||||
|
||||
// Mark completion
|
||||
log.Printf("[DEBUG] signalwrapper: wrapped function execution ended")
|
||||
wrapped.done(err)
|
||||
}()
|
||||
|
||||
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue