Skip to content

Commit

Permalink
r/virtual_hub: switching back to using the Future
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Nov 29, 2019
1 parent 188cd61 commit 91d54d8
Showing 1 changed file with 3 additions and 55 deletions.
58 changes: 3 additions & 55 deletions azurerm/resource_arm_virtual_hub.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package azurerm

import (
"context"
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
Expand Down Expand Up @@ -134,33 +132,12 @@ func resourceArmVirtualHubCreateUpdate(d *schema.ResourceData, meta interface{})
Tags: tags.Expand(t),
}

// todo: confirm if this is needed
// whilst there's a future here, the API is broken
if _, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters); err != nil {
future, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters)
if err != nil {
return fmt.Errorf("Error creating Virtual Hub %q (Resource Group %q): %+v", name, resourceGroup, err)
}

log.Printf("[DEBUG] Waiting for Virtual Hub %q (Resource Group %q) to become available", name, resourceGroup)
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"available"},
Refresh: virtualHubWaitForCreatedRefreshFunc(ctx, client, resourceGroup, name),
Delay: 30 * time.Second,
PollInterval: 10 * time.Second,
ContinuousTargetOccurence: 3,
}

if features.SupportsCustomTimeouts() {
if d.IsNewResource() {
stateConf.Timeout = d.Timeout(schema.TimeoutCreate)
} else {
stateConf.Timeout = d.Timeout(schema.TimeoutUpdate)
}
} else {
stateConf.Timeout = 90 * time.Minute
}

if _, err := stateConf.WaitForState(); err != nil {
if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting for creation of Virtual Hub %q (Resource Group %q): %+v", name, resourceGroup, err)
}

Expand Down Expand Up @@ -299,32 +276,3 @@ func flattenArmVirtualHubRoute(input *network.VirtualHubRouteTable) []interface{

return results
}

func virtualHubWaitForCreatedRefreshFunc(ctx context.Context, client *network.VirtualHubsClient, resourceGroup, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("[DEBUG] Checking to see if Virtual Hub %q (Resource Group %q) has finished provisioning..", name, resourceGroup)

resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
log.Printf("[DEBUG] Error retrieving Virtual Hub %q (Resource Group %q): %+v", name, resourceGroup, err)
return nil, "error", fmt.Errorf("Error retrieving Virtual Hub %q (Resource Group %q): %+v", name, resourceGroup, err)
}

if resp.VirtualHubProperties == nil {
log.Printf("[DEBUG] Error retrieving Virtual Hub %q (Resource Group %q): `properties` was nil", name, resourceGroup)
return nil, "error", fmt.Errorf("Error retrieving Virtual Hub %q (Resource Group %q): `properties` was nil", name, resourceGroup)
}

log.Printf("[DEBUG] Virtual Hub %q (Resource Group %q) is %q..", name, resourceGroup, string(resp.VirtualHubProperties.ProvisioningState))
switch resp.VirtualHubProperties.ProvisioningState {
case network.Succeeded:
return "available", "available", nil

case network.Failed:
return "error", "error", fmt.Errorf("VPN Gateway %q (Resource Group %q) is in provisioningState `Failed`", name, resourceGroup)

default:
return "pending", "pending", nil
}
}
}

0 comments on commit 91d54d8

Please sign in to comment.