-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5523 from brent-au/f/express-route-gateway
New Resource: `azurerm_express_route_gateway`
- Loading branch information
Showing
6 changed files
with
516 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
azurerm/internal/services/network/resource_arm_express_route_gateway.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
package network | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"time" | ||
|
||
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network" | ||
"github.com/hashicorp/go-azure-helpers/response" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/features" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts" | ||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmExpressRouteGateway() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmExpressRouteGatewayCreateUpdate, | ||
Read: resourceArmExpressRouteGatewayRead, | ||
Update: resourceArmExpressRouteGatewayCreateUpdate, | ||
Delete: resourceArmExpressRouteGatewayDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(60 * time.Minute), | ||
Read: schema.DefaultTimeout(5 * time.Minute), | ||
Update: schema.DefaultTimeout(60 * time.Minute), | ||
Delete: schema.DefaultTimeout(60 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validate.NoEmptyStrings, | ||
}, | ||
|
||
"location": azure.SchemaLocation(), | ||
|
||
"resource_group_name": azure.SchemaResourceGroupName(), | ||
|
||
"virtual_hub_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: azure.ValidateResourceID, | ||
}, | ||
|
||
"scale_units": { | ||
Type: schema.TypeInt, | ||
Required: true, | ||
ValidateFunc: validation.IntBetween(1, 10), | ||
}, | ||
|
||
"tags": tags.Schema(), | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmExpressRouteGatewayCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Network.ExpressRouteGatewaysClient | ||
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
log.Println("[INFO] preparing arguments for ExpressRoute Gateway creation.") | ||
|
||
name := d.Get("name").(string) | ||
resourceGroup := d.Get("resource_group_name").(string) | ||
|
||
if features.ShouldResourcesBeImported() && d.IsNewResource() { | ||
resp, err := client.Get(ctx, resourceGroup, name) | ||
if err != nil { | ||
if !utils.ResponseWasNotFound(resp.Response) { | ||
return fmt.Errorf("Error checking for present of existing ExpressRoute Gateway %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
} | ||
if resp.ID != nil && *resp.ID != "" { | ||
return tf.ImportAsExistsError("azurerm_express_route_gateway", *resp.ID) | ||
} | ||
} | ||
|
||
location := azure.NormalizeLocation(d.Get("location").(string)) | ||
virtualHubId := d.Get("virtual_hub_id").(string) | ||
t := d.Get("tags").(map[string]interface{}) | ||
|
||
minScaleUnits := int32(d.Get("scale_units").(int)) | ||
parameters := network.ExpressRouteGateway{ | ||
Location: utils.String(location), | ||
ExpressRouteGatewayProperties: &network.ExpressRouteGatewayProperties{ | ||
AutoScaleConfiguration: &network.ExpressRouteGatewayPropertiesAutoScaleConfiguration{ | ||
Bounds: &network.ExpressRouteGatewayPropertiesAutoScaleConfigurationBounds{ | ||
Min: &minScaleUnits, | ||
}, | ||
}, | ||
VirtualHub: &network.VirtualHubID{ | ||
ID: &virtualHubId, | ||
}, | ||
}, | ||
Tags: tags.Expand(t), | ||
} | ||
|
||
future, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters) | ||
if err != nil { | ||
return fmt.Errorf("Error creating ExpressRoute Gateway %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
return fmt.Errorf("Error waiting for creation of ExpressRoute Gateway %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
resp, err := client.Get(ctx, resourceGroup, name) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving ExpressRoute Gateway %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
if resp.ID == nil || *resp.ID == "" { | ||
return fmt.Errorf("Cannot read ExpressRoute Gateway %q (Resource Group %q) ID", name, resourceGroup) | ||
} | ||
d.SetId(*resp.ID) | ||
|
||
return resourceArmExpressRouteGatewayRead(d, meta) | ||
} | ||
|
||
func resourceArmExpressRouteGatewayRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Network.ExpressRouteGatewaysClient | ||
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := azure.ParseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
name := id.Path["expressRouteGateways"] | ||
|
||
resp, err := client.Get(ctx, resourceGroup, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
log.Printf("[INFO] ExpressRoute Gateway %q does not exist - removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error reading ExpressRoute Gateway %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("resource_group_name", resourceGroup) | ||
if location := resp.Location; location != nil { | ||
d.Set("location", azure.NormalizeLocation(*location)) | ||
} | ||
|
||
if props := resp.ExpressRouteGatewayProperties; props != nil { | ||
virtualHubId := "" | ||
if props.VirtualHub != nil && props.VirtualHub.ID != nil { | ||
virtualHubId = *props.VirtualHub.ID | ||
} | ||
d.Set("virtual_hub_id", virtualHubId) | ||
|
||
scaleUnits := 0 | ||
if props.AutoScaleConfiguration != nil && props.AutoScaleConfiguration.Bounds != nil && props.AutoScaleConfiguration.Bounds.Min != nil { | ||
scaleUnits = int(*props.AutoScaleConfiguration.Bounds.Min) | ||
} | ||
d.Set("scale_units", scaleUnits) | ||
} | ||
|
||
return tags.FlattenAndSet(d, resp.Tags) | ||
} | ||
|
||
func resourceArmExpressRouteGatewayDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*clients.Client).Network.ExpressRouteGatewaysClient | ||
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) | ||
defer cancel() | ||
|
||
id, err := azure.ParseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resourceGroup := id.ResourceGroup | ||
name := id.Path["expressRouteGateways"] | ||
|
||
future, err := client.Delete(ctx, resourceGroup, name) | ||
if err != nil { | ||
if response.WasNotFound(future.Response()) { | ||
return nil | ||
} | ||
return fmt.Errorf("Error deleting ExpressRoute Gateway %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
|
||
if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { | ||
if !response.WasNotFound(future.Response()) { | ||
return fmt.Errorf("Error waiting for deleting ExpressRoute Gateway %q (Resource Group %q): %+v", name, resourceGroup, err) | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.