Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource: azurerm_express_route_gateway #5523

Merged
merged 11 commits into from
Feb 18, 2020
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": {
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeInt,
Required: true,
tombuildsstuff marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: validation.IntBetween(1, 6),
},

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

parameters := network.ExpressRouteGateway{
Location: utils.String(location),
ExpressRouteGatewayProperties: &network.ExpressRouteGatewayProperties{
AutoScaleConfiguration: &network.ExpressRouteGatewayPropertiesAutoScaleConfiguration{
Bounds: expandExpressRouteGatewayAutoScaleConfigurationBounds(d),
},
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 {
d.Set("virtual_hub_id", props.VirtualHub.ID)
brent-au marked this conversation as resolved.
Show resolved Hide resolved

if setErr := d.Set("scale_units", props.AutoScaleConfiguration.Bounds.Min); setErr != nil {
brent-au marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("Error setting `scale_units`: %+v", setErr)
}
}

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
}

func expandExpressRouteGatewayAutoScaleConfigurationBounds(d *schema.ResourceData) *network.ExpressRouteGatewayPropertiesAutoScaleConfigurationBounds {
minScaleUnits := int32(d.Get("scale_units").(int))
configuration := network.ExpressRouteGatewayPropertiesAutoScaleConfigurationBounds{
Min: &minScaleUnits,
}
brent-au marked this conversation as resolved.
Show resolved Hide resolved

return &configuration
}
Loading