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

Add support ipv6 configuration in azurerm_express_route_circuit_peering #9235

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ func resourceArmExpressRouteCircuitPeering() *schema.Resource {
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},

"customer_asn": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
},

"routing_registry_name": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -108,6 +110,63 @@ func resourceArmExpressRouteCircuitPeering() *schema.Resource {
},
},

"ipv6": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"microsoft_peering": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"advertised_public_prefixes": {
Type: schema.TypeList,
MinItems: 1,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.IsCIDR,
},
},

"customer_asn": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
},

"routing_registry_name": {
Type: schema.TypeString,
Optional: true,
Default: "NONE",
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
},

"primary_peer_address_prefix": {
Type: schema.TypeString,
Required: true,
},

"secondary_peer_address_prefix": {
Type: schema.TypeString,
Required: true,
},

"route_filter_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: azure.ValidateResourceID,
},
},
},
},

"azure_asn": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -193,8 +252,22 @@ func resourceArmExpressRouteCircuitPeeringCreateUpdate(d *schema.ResourceData, m
ID: utils.String(route_filter_id),
}
}
} else if route_filter_id != "" {
return fmt.Errorf("`route_filter_id` may only be specified when `peering_type` is set to `MicrosoftPeering`")

ipv6Peering := d.Get("ipv6").([]interface{})
ipv6PeeringConfig, err := expandExpressRouteCircuitIpv6PeeringConfig(ipv6Peering)
if err != nil {
return err
}
parameters.ExpressRouteCircuitPeeringPropertiesFormat.Ipv6PeeringConfig = ipv6PeeringConfig
} else {
if route_filter_id != "" {
return fmt.Errorf("`route_filter_id` may only be specified when `peering_type` is set to `MicrosoftPeering`")
}

ipv6Peering := d.Get("ipv6").([]interface{})
if len(ipv6Peering) != 0 {
return fmt.Errorf("`ipv6` may only be specified when `peering_type` is set to `MicrosoftPeering`")
}
}

future, err := client.CreateOrUpdate(ctx, resourceGroup, circuitName, peeringType, parameters)
Expand Down Expand Up @@ -259,7 +332,10 @@ func resourceArmExpressRouteCircuitPeeringRead(d *schema.ResourceData, meta inte

config := flattenExpressRouteCircuitPeeringMicrosoftConfig(props.MicrosoftPeeringConfig)
if err := d.Set("microsoft_peering_config", config); err != nil {
return fmt.Errorf("Error setting `microsoft_peering_config`: %+v", err)
return fmt.Errorf("setting `microsoft_peering_config`: %+v", err)
}
if err := d.Set("ipv6", flattenExpressRouteCircuitIpv6PeeringConfig(props.Ipv6PeeringConfig)); err != nil {
return fmt.Errorf("setting `ipv6`: %+v", err)
}
}

Expand Down Expand Up @@ -302,6 +378,9 @@ func resourceArmExpressRouteCircuitPeeringDelete(d *schema.ResourceData, meta in
}

func expandExpressRouteCircuitPeeringMicrosoftConfig(input []interface{}) *network.ExpressRouteCircuitPeeringConfig {
if len(input) == 0 {
return nil
}
peering := input[0].(map[string]interface{})

prefixes := make([]string, 0)
Expand All @@ -320,6 +399,29 @@ func expandExpressRouteCircuitPeeringMicrosoftConfig(input []interface{}) *netwo
}
}

func expandExpressRouteCircuitIpv6PeeringConfig(input []interface{}) (*network.Ipv6ExpressRouteCircuitPeeringConfig, error) {
if len(input) == 0 {
return nil, nil
}

v := input[0].(map[string]interface{})
peeringConfig := network.Ipv6ExpressRouteCircuitPeeringConfig{
PrimaryPeerAddressPrefix: utils.String(v["primary_peer_address_prefix"].(string)),
SecondaryPeerAddressPrefix: utils.String(v["secondary_peer_address_prefix"].(string)),
MicrosoftPeeringConfig: expandExpressRouteCircuitPeeringMicrosoftConfig(v["microsoft_peering"].([]interface{})),
}
routeFilterId := v["route_filter_id"].(string)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we add validation to check this is not set when incompatible types are used?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a check for this anywhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still don't see a check to make sure route filter id is not set when type is microsoft in that link..

Copy link
Contributor Author

@xuzhang3 xuzhang3 Nov 20, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ipv6 is a little different with microsoft_peering_config, it is only supported by MicrosoftPeering. I add a check for ipv6 not route_filter_id, if peering type is not MicrosoftPeering and ipv6 is configured, an error message will raise up.
Check ipv6 configure:
https://github.com/xuzhang3/terraform-provider-azurerm/blob/f/expressroutecircuitpeering_enhance/azurerm/internal/services/network/express_route_circuit_peering_resource.go#L267

if routeFilterId != "" {
if _, err := ParseRouteFilterID(routeFilterId); err != nil {
return nil, err
}
peeringConfig.RouteFilter = &network.SubResource{
ID: utils.String(routeFilterId),
}
}
return &peeringConfig, nil
}

func flattenExpressRouteCircuitPeeringMicrosoftConfig(input *network.ExpressRouteCircuitPeeringConfig) interface{} {
if input == nil {
return []interface{}{}
Expand All @@ -340,3 +442,30 @@ func flattenExpressRouteCircuitPeeringMicrosoftConfig(input *network.ExpressRout

return []interface{}{config}
}

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

var primaryPeerAddressPrefix string
if input.PrimaryPeerAddressPrefix != nil {
primaryPeerAddressPrefix = *input.PrimaryPeerAddressPrefix
}
var secondaryPeerAddressPrefix string
if input.SecondaryPeerAddressPrefix != nil {
secondaryPeerAddressPrefix = *input.SecondaryPeerAddressPrefix
}
routeFilterId := ""
if input.RouteFilter != nil && input.RouteFilter.ID != nil {
routeFilterId = *input.RouteFilter.ID
}
return []interface{}{
map[string]interface{}{
"microsoft_peering": flattenExpressRouteCircuitPeeringMicrosoftConfig(input.MicrosoftPeeringConfig),
"primary_peer_address_prefix": primaryPeerAddressPrefix,
"secondary_peer_address_prefix": secondaryPeerAddressPrefix,
"route_filter_id": routeFilterId,
},
}
}
Loading