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_connection #11320

Merged
merged 36 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -13,6 +13,7 @@ type Client struct {
DDOSProtectionPlansClient *network.DdosProtectionPlansClient
ExpressRouteAuthsClient *network.ExpressRouteCircuitAuthorizationsClient
ExpressRouteCircuitsClient *network.ExpressRouteCircuitsClient
ExpressRouteConnectionsClient *network.ExpressRouteConnectionsClient
ExpressRouteGatewaysClient *network.ExpressRouteGatewaysClient
ExpressRoutePeeringsClient *network.ExpressRouteCircuitPeeringsClient
HubRouteTableClient *network.HubRouteTablesClient
Expand Down Expand Up @@ -79,6 +80,9 @@ func NewClient(o *common.ClientOptions) *Client {
ExpressRouteCircuitsClient := network.NewExpressRouteCircuitsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ExpressRouteCircuitsClient.Client, o.ResourceManagerAuthorizer)

ExpressRouteConnectionsClient := network.NewExpressRouteConnectionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ExpressRouteConnectionsClient.Client, o.ResourceManagerAuthorizer)

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

Expand Down Expand Up @@ -213,6 +217,7 @@ func NewClient(o *common.ClientOptions) *Client {
DDOSProtectionPlansClient: &DDOSProtectionPlansClient,
ExpressRouteAuthsClient: &ExpressRouteAuthsClient,
ExpressRouteCircuitsClient: &ExpressRouteCircuitsClient,
ExpressRouteConnectionsClient: &ExpressRouteConnectionsClient,
ExpressRouteGatewaysClient: &ExpressRouteGatewaysClient,
ExpressRoutePeeringsClient: &ExpressRoutePeeringsClient,
HubRouteTableClient: &HubRouteTableClient,
Expand Down
336 changes: 336 additions & 0 deletions azurerm/internal/services/network/express_route_connection_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,336 @@
package network

import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2020-05-01/network"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
magodo marked this conversation as resolved.
Show resolved Hide resolved
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/network/validate"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceExpressRouteConnection() *schema.Resource {
return &schema.Resource{
Create: resourceExpressRouteConnectionCreateUpdate,
Read: resourceExpressRouteConnectionRead,
Update: resourceExpressRouteConnectionCreateUpdate,
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
Delete: resourceExpressRouteConnectionDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.ExpressRouteConnectionID(id)
return err
}),

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ExpressRouteConnectionName,
},

"express_route_circuit_peering_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ExpressRouteCircuitPeeringID,
},

"express_route_gateway_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.ExpressRouteGatewayID,
},

"authorization_key": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.IsUUID,
},

"enable_internet_security": {
Type: schema.TypeBool,
Optional: true,
},

"routing": {
Type: schema.TypeList,
Optional: true,
Computed: true,
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"associated_route_table_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
ValidateFunc: validate.HubRouteTableID,
},

"propagated_route_table": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"labels": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringIsNotEmpty,
},
},

"route_table_ids": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validate.HubRouteTableID,
},
},
},
},
},
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
},
},
},

"routing_weight": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
ValidateFunc: validation.IntBetween(0, 32000),
},
},
}
}

func resourceExpressRouteConnectionCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.ExpressRouteConnectionsClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

expressRouteGatewayId, err := parse.ExpressRouteGatewayID(d.Get("express_route_gateway_id").(string))
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

id := parse.NewExpressRouteConnectionID(expressRouteGatewayId.SubscriptionId, expressRouteGatewayId.ResourceGroup, expressRouteGatewayId.Name, d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.ExpressRouteGatewayName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for existing %s: %+v", id, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) {
return tf.ImportAsExistsError("azurerm_express_route_connection", id.ID())
}
}

expressRouteConnectionParameters := network.ExpressRouteConnection{
Name: utils.String(id.Name),
ExpressRouteConnectionProperties: &network.ExpressRouteConnectionProperties{
ExpressRouteCircuitPeering: &network.ExpressRouteCircuitPeeringID{
ID: utils.String(d.Get("express_route_circuit_peering_id").(string)),
},
RoutingWeight: utils.Int32(int32(d.Get("routing_weight").(int))),
},
}

if v, ok := d.GetOk("authorization_key"); ok {
expressRouteConnectionParameters.ExpressRouteConnectionProperties.AuthorizationKey = utils.String(v.(string))
}

if v, ok := d.GetOk("enable_internet_security"); ok {
magodo marked this conversation as resolved.
Show resolved Hide resolved
expressRouteConnectionParameters.ExpressRouteConnectionProperties.EnableInternetSecurity = utils.Bool(v.(bool))
}

if v, ok := d.GetOk("routing"); ok {
expressRouteConnectionParameters.ExpressRouteConnectionProperties.RoutingConfiguration = expandExpressRouteConnectionRouting(v.([]interface{}))
magodo marked this conversation as resolved.
Show resolved Hide resolved
}

future, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.ExpressRouteGatewayName, id.Name, expressRouteConnectionParameters)
if err != nil {
return fmt.Errorf("creating/updating %s: %+v", id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for creation/update of %s: %+v", id, err)
}

d.SetId(id.ID())

return resourceExpressRouteConnectionRead(d, meta)
}

func resourceExpressRouteConnectionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.ExpressRouteConnectionsClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ExpressRouteConnectionID(d.Id())
if err != nil {
return err
}

resp, err := client.Get(ctx, id.ResourceGroup, id.ExpressRouteGatewayName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("retrieving %s: %+v", *id, err)
}

d.Set("name", id.Name)
d.Set("express_route_gateway_id", parse.NewExpressRouteGatewayID(id.SubscriptionId, id.ResourceGroup, id.ExpressRouteGatewayName).ID())

if props := resp.ExpressRouteConnectionProperties; props != nil {
d.Set("routing_weight", props.RoutingWeight)

if v := props.ExpressRouteCircuitPeering; v != nil {
d.Set("express_route_circuit_peering_id", v.ID)
}

if v := props.AuthorizationKey; v != nil {
d.Set("authorization_key", v)
}

if v := props.EnableInternetSecurity; v != nil {
d.Set("enable_internet_security", v)
}
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved

if err := d.Set("routing", flattenExpressRouteConnectionRouting(props.RoutingConfiguration)); err != nil {
return fmt.Errorf("setting `routing`: %+v", err)
}
}

return nil
}

func resourceExpressRouteConnectionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Network.ExpressRouteConnectionsClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.ExpressRouteConnectionID(d.Id())
if err != nil {
return err
}

future, err := client.Delete(ctx, id.ResourceGroup, id.ExpressRouteGatewayName, id.Name)
if err != nil {
return fmt.Errorf("deleting %s: %+v", *id, err)
}

if err := future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for deletion of %s: %+v", *id, err)
}

return nil
}

func expandExpressRouteConnectionRouting(input []interface{}) *network.RoutingConfiguration {
if len(input) == 0 {
magodo marked this conversation as resolved.
Show resolved Hide resolved
return &network.RoutingConfiguration{}
}

v := input[0].(map[string]interface{})
result := network.RoutingConfiguration{}

if associatedRouteTableId := v["associated_route_table_id"].(string); associatedRouteTableId != "" {
result.AssociatedRouteTable = &network.SubResource{
ID: utils.String(associatedRouteTableId),
}
}

if propagatedRouteTable := v["propagated_route_table"].([]interface{}); len(propagatedRouteTable) != 0 {
result.PropagatedRouteTables = expandExpressRouteConnectionPropagatedRouteTable(propagatedRouteTable)
}

return &result
}

func expandExpressRouteConnectionPropagatedRouteTable(input []interface{}) *network.PropagatedRouteTable {
if len(input) == 0 {
magodo marked this conversation as resolved.
Show resolved Hide resolved
return &network.PropagatedRouteTable{}
}

v := input[0].(map[string]interface{})

result := network.PropagatedRouteTable{}

if labels := v["labels"].(*schema.Set).List(); len(labels) != 0 {
result.Labels = utils.ExpandStringSlice(labels)
}

if routeTableIds := v["route_table_ids"].(*schema.Set).List(); len(routeTableIds) != 0 {
result.Ids = expandIDsToSubResources(routeTableIds)
}

return &result
}

func flattenExpressRouteConnectionRouting(input *network.RoutingConfiguration) []interface{} {
if input == nil {
return []interface{}{}
}

associatedRouteTableId := ""
if input.AssociatedRouteTable != nil && input.AssociatedRouteTable.ID != nil {
associatedRouteTableId = *input.AssociatedRouteTable.ID
}

return []interface{}{
map[string]interface{}{
"associated_route_table_id": associatedRouteTableId,
neil-yechenwei marked this conversation as resolved.
Show resolved Hide resolved
"propagated_route_table": flattenExpressRouteConnectionPropagatedRouteTable(input.PropagatedRouteTables),
},
}
}

func flattenExpressRouteConnectionPropagatedRouteTable(input *network.PropagatedRouteTable) []interface{} {
if input == nil {
return make([]interface{}, 0)
}

labels := make([]interface{}, 0)
if input.Labels != nil {
labels = utils.FlattenStringSlice(input.Labels)
}

routeTableIds := make([]interface{}, 0)
if input.Ids != nil {
routeTableIds = flattenSubResourcesToIDs(input.Ids)
}

return []interface{}{
map[string]interface{}{
"labels": labels,
"route_table_ids": routeTableIds,
},
}
}
Loading