providers/azurerm: convert to Stop() usage as example

This commit is contained in:
Mitchell Hashimoto 2016-10-23 18:26:06 -07:00
parent 89647745b0
commit 9089aa24d5
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 41 additions and 40 deletions

View File

@ -5,6 +5,7 @@ import (
"log" "log"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"time"
"github.com/Azure/azure-sdk-for-go/arm/cdn" "github.com/Azure/azure-sdk-for-go/arm/cdn"
"github.com/Azure/azure-sdk-for-go/arm/compute" "github.com/Azure/azure-sdk-for-go/arm/compute"
@ -19,6 +20,7 @@ import (
mainStorage "github.com/Azure/azure-sdk-for-go/storage" mainStorage "github.com/Azure/azure-sdk-for-go/storage"
"github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/azure" "github.com/Azure/go-autorest/autorest/azure"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform" "github.com/hashicorp/terraform/terraform"
riviera "github.com/jen20/riviera/azure" riviera "github.com/jen20/riviera/azure"
) )
@ -30,6 +32,8 @@ type ArmClient struct {
tenantId string tenantId string
subscriptionId string subscriptionId string
stopCh <-chan struct{} // From the provider
rivieraClient *riviera.Client rivieraClient *riviera.Client
availSetClient compute.AvailabilitySetsClient availSetClient compute.AvailabilitySetsClient
@ -485,3 +489,7 @@ func (armClient *ArmClient) getQueueServiceClientForStorageAccount(resourceGroup
queueClient := storageClient.GetQueueService() queueClient := storageClient.GetQueueService()
return &queueClient, true, nil return &queueClient, true, nil
} }
func (armClient *ArmClient) CancelCh(max time.Duration) (<-chan struct{}, chan<- struct{}) {
return resource.StopCh(armClient.stopCh, max)
}

View File

@ -17,7 +17,8 @@ import (
// Provider returns a terraform.ResourceProvider. // Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider { func Provider() terraform.ResourceProvider {
return &schema.Provider{ var p *schema.Provider
p = &schema.Provider{
Schema: map[string]*schema.Schema{ Schema: map[string]*schema.Schema{
"subscription_id": { "subscription_id": {
Type: schema.TypeString, Type: schema.TypeString,
@ -104,8 +105,10 @@ func Provider() terraform.ResourceProvider {
"azurerm_sql_firewall_rule": resourceArmSqlFirewallRule(), "azurerm_sql_firewall_rule": resourceArmSqlFirewallRule(),
"azurerm_sql_server": resourceArmSqlServer(), "azurerm_sql_server": resourceArmSqlServer(),
}, },
ConfigureFunc: providerConfigure, ConfigureFunc: providerConfigure(p),
} }
return p
} }
// Config is the configuration structure used to instantiate a // Config is the configuration structure used to instantiate a
@ -140,7 +143,8 @@ func (c *Config) validate() error {
return err.ErrorOrNil() 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{ config := &Config{
SubscriptionID: d.Get("subscription_id").(string), SubscriptionID: d.Get("subscription_id").(string),
ClientID: d.Get("client_id").(string), ClientID: d.Get("client_id").(string),
@ -157,6 +161,8 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return nil, err return nil, err
} }
client.stopCh = p.StopCh()
err = registerAzureResourceProvidersWithSubscription(client.rivieraClient) err = registerAzureResourceProvidersWithSubscription(client.rivieraClient)
if err != nil { if err != nil {
return nil, err return nil, err
@ -164,6 +170,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return client, nil return client, nil
} }
}
func registerProviderWithSubscription(providerName string, client *riviera.Client) error { func registerProviderWithSubscription(providerName string, client *riviera.Client) error {
request := client.NewRequest() request := client.NewRequest()

View File

@ -11,7 +11,6 @@ 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"
"github.com/hashicorp/terraform/helper/validation" "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)) opts.Properties.AccessTier = storage.AccessTier(accessTier.(string))
} }
// Create the storage account. We wrap this so that it is cancellable // Create
// with a Ctrl-C since this can take a LONG time. cancelCh, doneCh := client.CancelCh(1 * time.Hour)
wrap := signalwrapper.Run(func(cancelCh <-chan struct{}) error { _, createErr := storageClient.Create(
_, err := storageClient.Create(resourceGroupName, storageAccountName, opts, cancelCh) resourceGroupName, storageAccountName, opts, cancelCh)
return err close(doneCh)
})
// 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.
}
// 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
read, err := storageClient.GetProperties(resourceGroupName, storageAccountName) read, err := storageClient.GetProperties(resourceGroupName, storageAccountName)