Skip to content

Commit

Permalink
providers/azurerm: convert to Stop() usage as example
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Oct 25, 2016
1 parent be34dfe commit 990df48
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 40 deletions.
8 changes: 8 additions & 0 deletions builtin/providers/azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand All @@ -30,6 +32,8 @@ type ArmClient struct {
tenantId string
subscriptionId string

stopCh <-chan struct{} // From the provider

rivieraClient *riviera.Client

availSetClient compute.AvailabilitySetsClient
Expand Down Expand Up @@ -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)
}
49 changes: 28 additions & 21 deletions builtin/providers/azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 5 additions & 19 deletions builtin/providers/azurerm/resource_arm_storage_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 990df48

Please sign in to comment.