Skip to content

Commit

Permalink
Merge pull request #5523 from brent-au/f/express-route-gateway
Browse files Browse the repository at this point in the history
New Resource: `azurerm_express_route_gateway`
  • Loading branch information
tombuildsstuff authored Feb 18, 2020
2 parents 35745e9 + 3123d02 commit 877c7e1
Show file tree
Hide file tree
Showing 6 changed files with 516 additions and 0 deletions.
5 changes: 5 additions & 0 deletions azurerm/internal/services/network/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Client struct {
DDOSProtectionPlansClient *network.DdosProtectionPlansClient
ExpressRouteAuthsClient *network.ExpressRouteCircuitAuthorizationsClient
ExpressRouteCircuitsClient *network.ExpressRouteCircuitsClient
ExpressRouteGatewaysClient *network.ExpressRouteGatewaysClient
ExpressRoutePeeringsClient *network.ExpressRouteCircuitPeeringsClient
InterfacesClient *network.InterfacesClient
LoadBalancersClient *network.LoadBalancersClient
Expand Down Expand Up @@ -68,6 +69,9 @@ func NewClient(o *common.ClientOptions) *Client {
ExpressRouteCircuitsClient := network.NewExpressRouteCircuitsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ExpressRouteCircuitsClient.Client, o.ResourceManagerAuthorizer)

ExpressRouteGatewaysClient := network.NewExpressRouteGatewaysClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ExpressRouteGatewaysClient.Client, o.ResourceManagerAuthorizer)

ExpressRoutePeeringsClient := network.NewExpressRouteCircuitPeeringsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ExpressRoutePeeringsClient.Client, o.ResourceManagerAuthorizer)

Expand Down Expand Up @@ -158,6 +162,7 @@ func NewClient(o *common.ClientOptions) *Client {
DDOSProtectionPlansClient: &DDOSProtectionPlansClient,
ExpressRouteAuthsClient: &ExpressRouteAuthsClient,
ExpressRouteCircuitsClient: &ExpressRouteCircuitsClient,
ExpressRouteGatewaysClient: &ExpressRouteGatewaysClient,
ExpressRoutePeeringsClient: &ExpressRoutePeeringsClient,
InterfacesClient: &InterfacesClient,
LoadBalancersClient: &LoadBalancersClient,
Expand Down
1 change: 1 addition & 0 deletions azurerm/internal/services/network/registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func (r Registration) SupportedResources() map[string]*schema.Resource {
"azurerm_express_route_circuit_authorization": resourceArmExpressRouteCircuitAuthorization(),
"azurerm_express_route_circuit_peering": resourceArmExpressRouteCircuitPeering(),
"azurerm_express_route_circuit": resourceArmExpressRouteCircuit(),
"azurerm_express_route_gateway": resourceArmExpressRouteGateway(),
"azurerm_firewall_application_rule_collection": resourceArmFirewallApplicationRuleCollection(),
"azurerm_firewall_nat_rule_collection": resourceArmFirewallNatRuleCollection(),
"azurerm_firewall_network_rule_collection": resourceArmFirewallNetworkRuleCollection(),
Expand Down
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
}
Loading

0 comments on commit 877c7e1

Please sign in to comment.