providers/azurerm: convert to Stop() usage as example
This commit is contained in:
parent
89647745b0
commit
9089aa24d5
|
@ -5,6 +5,7 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
"net/http/httputil"
|
||||
"time"
|
||||
|
||||
"github.com/Azure/azure-sdk-for-go/arm/cdn"
|
||||
"github.com/Azure/azure-sdk-for-go/arm/compute"
|
||||
|
@ -19,6 +20,7 @@ import (
|
|||
mainStorage "github.com/Azure/azure-sdk-for-go/storage"
|
||||
"github.com/Azure/go-autorest/autorest"
|
||||
"github.com/Azure/go-autorest/autorest/azure"
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/hashicorp/terraform/terraform"
|
||||
riviera "github.com/jen20/riviera/azure"
|
||||
)
|
||||
|
@ -30,6 +32,8 @@ type ArmClient struct {
|
|||
tenantId string
|
||||
subscriptionId string
|
||||
|
||||
stopCh <-chan struct{} // From the provider
|
||||
|
||||
rivieraClient *riviera.Client
|
||||
|
||||
availSetClient compute.AvailabilitySetsClient
|
||||
|
@ -485,3 +489,7 @@ func (armClient *ArmClient) getQueueServiceClientForStorageAccount(resourceGroup
|
|||
queueClient := storageClient.GetQueueService()
|
||||
return &queueClient, true, nil
|
||||
}
|
||||
|
||||
func (armClient *ArmClient) CancelCh(max time.Duration) (<-chan struct{}, chan<- struct{}) {
|
||||
return resource.StopCh(armClient.stopCh, max)
|
||||
}
|
||||
|
|
|
@ -17,7 +17,8 @@ import (
|
|||
|
||||
// Provider returns a terraform.ResourceProvider.
|
||||
func Provider() terraform.ResourceProvider {
|
||||
return &schema.Provider{
|
||||
var p *schema.Provider
|
||||
p = &schema.Provider{
|
||||
Schema: map[string]*schema.Schema{
|
||||
"subscription_id": {
|
||||
Type: schema.TypeString,
|
||||
|
@ -104,8 +105,10 @@ func Provider() terraform.ResourceProvider {
|
|||
"azurerm_sql_firewall_rule": resourceArmSqlFirewallRule(),
|
||||
"azurerm_sql_server": resourceArmSqlServer(),
|
||||
},
|
||||
ConfigureFunc: providerConfigure,
|
||||
ConfigureFunc: providerConfigure(p),
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// Config is the configuration structure used to instantiate a
|
||||
|
@ -140,7 +143,8 @@ func (c *Config) validate() error {
|
|||
return err.ErrorOrNil()
|
||||
}
|
||||
|
||||
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
||||
func providerConfigure(p *schema.Provider) schema.ConfigureFunc {
|
||||
return func(d *schema.ResourceData) (interface{}, error) {
|
||||
config := &Config{
|
||||
SubscriptionID: d.Get("subscription_id").(string),
|
||||
ClientID: d.Get("client_id").(string),
|
||||
|
@ -157,6 +161,8 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
client.stopCh = p.StopCh()
|
||||
|
||||
err = registerAzureResourceProvidersWithSubscription(client.rivieraClient)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -164,6 +170,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
|||
|
||||
return client, nil
|
||||
}
|
||||
}
|
||||
|
||||
func registerProviderWithSubscription(providerName string, client *riviera.Client) error {
|
||||
request := client.NewRequest()
|
||||
|
|
|
@ -11,7 +11,6 @@ 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"
|
||||
"github.com/hashicorp/terraform/helper/validation"
|
||||
)
|
||||
|
||||
|
@ -192,24 +191,11 @@ func resourceArmStorageAccountCreate(d *schema.ResourceData, meta interface{}) e
|
|||
opts.Properties.AccessTier = storage.AccessTier(accessTier.(string))
|
||||
}
|
||||
|
||||
// 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.
|
||||
var createErr error
|
||||
select {
|
||||
case <-time.After(1 * time.Hour):
|
||||
// An hour is way above the expected P99 for this API call so
|
||||
// we premature cancel and error here.
|
||||
createErr = wrap.Cancel()
|
||||
case createErr = <-wrap.ErrCh:
|
||||
// Successfully ran (but perhaps not successfully completed)
|
||||
// the function.
|
||||
}
|
||||
// Create
|
||||
cancelCh, doneCh := client.CancelCh(1 * time.Hour)
|
||||
_, createErr := storageClient.Create(
|
||||
resourceGroupName, storageAccountName, opts, cancelCh)
|
||||
close(doneCh)
|
||||
|
||||
// The only way to get the ID back apparently is to read the resource again
|
||||
read, err := storageClient.GetProperties(resourceGroupName, storageAccountName)
|
||||
|
|
Loading…
Reference in New Issue