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"
"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)
}

View File

@ -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,29 +143,33 @@ func (c *Config) validate() error {
return err.ErrorOrNil()
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := &Config{
SubscriptionID: d.Get("subscription_id").(string),
ClientID: d.Get("client_id").(string),
ClientSecret: d.Get("client_secret").(string),
TenantID: d.Get("tenant_id").(string),
}
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),
ClientSecret: d.Get("client_secret").(string),
TenantID: d.Get("tenant_id").(string),
}
if err := config.validate(); err != nil {
return nil, err
}
if err := config.validate(); err != nil {
return nil, err
}
client, err := config.getArmClient()
if err != nil {
return nil, err
}
client, err := config.getArmClient()
if err != nil {
return nil, err
}
err = registerAzureResourceProvidersWithSubscription(client.rivieraClient)
if err != nil {
return nil, err
}
client.stopCh = p.StopCh()
return client, nil
err = registerAzureResourceProvidersWithSubscription(client.rivieraClient)
if err != nil {
return nil, err
}
return client, nil
}
}
func registerProviderWithSubscription(providerName string, client *riviera.Client) error {

View File

@ -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)