Skip to content

Commit

Permalink
Update outbound_as_prepend_count to string type to account for no giv…
Browse files Browse the repository at this point in the history
…en value
  • Loading branch information
thogarty committed Oct 5, 2024
1 parent 2bbb2c1 commit 57e9334
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
7 changes: 7 additions & 0 deletions docs/data-sources/fabric_routing_protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ output "customer_asn" {

### Read-Only

- `as_override_enabled` (Boolean) Enable AS number override. One of: 0, 1, 3, 5
- `bfd` (Set of Object) Bidirectional Forwarding Detection (see [below for nested schema](#nestedatt--bfd))
- `bgp_auth_key` (String) BGP authorization key
- `bgp_ipv4` (Set of Object) Routing Protocol BGP IPv4 (see [below for nested schema](#nestedatt--bgp_ipv4))
Expand Down Expand Up @@ -114,6 +115,9 @@ Read-Only:
- `customer_peer_ip` (String)
- `enabled` (Boolean)
- `equinix_peer_ip` (String)
- `inbound_med` (Number)
- `outbound_as_prepend_count` (String)
- `outbound_med` (Number)


<a id="nestedatt--bgp_ipv6"></a>
Expand All @@ -124,6 +128,9 @@ Read-Only:
- `customer_peer_ip` (String)
- `enabled` (Boolean)
- `equinix_peer_ip` (String)
- `inbound_med` (Number)
- `outbound_as_prepend_count` (String)
- `outbound_med` (Number)


<a id="nestedatt--change"></a>
Expand Down
70 changes: 50 additions & 20 deletions equinix/resource_fabric_routing_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func createBgpConnectionIpv4Sch() map[string]*schema.Schema {
Description: "Admin status for the BGP session",
},
"outbound_as_prepend_count": {
Type: schema.TypeInt,
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "AS path prepend count",
Expand Down Expand Up @@ -101,7 +101,7 @@ func createBgpConnectionIpv6Sch() map[string]*schema.Schema {
Description: "Admin status for the BGP session",
},
"outbound_as_prepend_count": {
Type: schema.TypeInt,
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "AS path prepend count",
Expand Down Expand Up @@ -281,6 +281,12 @@ func createFabricRoutingProtocolResourceSchema() map[string]*schema.Schema {
Computed: true,
Description: "BGP authorization key",
},
"as_override_enabled": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
Description: "Enable AS number override. One of: 0, 1, 3, 5",
},
"bfd": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -356,7 +362,10 @@ func resourceFabricRoutingProtocolCreate(ctx context.Context, d *schema.Resource
start := time.Now()
type_ := d.Get("type").(string)

createRequest := routingProtocolPayloadFromType(type_, d)
createRequest, err := routingProtocolPayloadFromType(type_, d)
if err != nil {
return diag.Errorf("error creating create request from Terraform configuration values: %s", err)
}

fabricRoutingProtocolData, _, err := client.RoutingProtocolsApi.CreateConnectionRoutingProtocol(ctx, d.Get("connection_uuid").(string)).RoutingProtocolBase(createRequest).Execute()

Expand All @@ -379,7 +388,10 @@ func resourceFabricRoutingProtocolUpdate(ctx context.Context, d *schema.Resource

type_ := d.Get("type").(string)

updateRequest := routingProtocolPayloadFromType(type_, d)
updateRequest, err := routingProtocolPayloadFromType(type_, d)
if err != nil {
return diag.Errorf("error creating update request from Terraform configuration values: %s", err)
}

start := time.Now()
updatedRpResp, _, err := client.RoutingProtocolsApi.ReplaceConnectionRoutingProtocolByUuid(ctx, d.Id(), d.Get("connection_uuid").(string)).RoutingProtocolBase(updateRequest).Execute()
Expand Down Expand Up @@ -454,7 +466,7 @@ func setIdFromAPIResponse(resp *fabricv4.RoutingProtocolData, isChange bool, d *
return changeUuid
}

func routingProtocolPayloadFromType(type_ string, d *schema.ResourceData) fabricv4.RoutingProtocolBase {
func routingProtocolPayloadFromType(type_ string, d *schema.ResourceData) (fabricv4.RoutingProtocolBase, error) {
payload := fabricv4.RoutingProtocolBase{}
if type_ == "BGP" {
bgpRP := fabricv4.RoutingProtocolBGPType{}
Expand Down Expand Up @@ -485,20 +497,29 @@ func routingProtocolPayloadFromType(type_ string, d *schema.ResourceData) fabric

schemaBgpIpv4 := d.Get("bgp_ipv4")
if schemaBgpIpv4 != nil {
bgpIpv4 := routingProtocolBgpIpv4TerraformToGo(schemaBgpIpv4.(*schema.Set).List())
bgpIpv4, err := routingProtocolBgpIpv4TerraformToGo(schemaBgpIpv4.(*schema.Set).List())
if err != nil {
return fabricv4.RoutingProtocolBase{}, err
}
if !reflect.DeepEqual(bgpIpv4, fabricv4.BGPConnectionIpv4{}) {
bgpRP.SetBgpIpv4(bgpIpv4)
}
}

schemaBgpIpv6 := d.Get("bgp_ipv6")
if schemaBgpIpv6 != nil {
bgpIpv6 := routingProtocolBgpIpv6TerraformToGo(schemaBgpIpv6.(*schema.Set).List())
bgpIpv6, err := routingProtocolBgpIpv6TerraformToGo(schemaBgpIpv6.(*schema.Set).List())
if err != nil {
return fabricv4.RoutingProtocolBase{}, err
}
if !reflect.DeepEqual(bgpIpv6, fabricv4.BGPConnectionIpv6{}) {
bgpRP.SetBgpIpv6(bgpIpv6)
}
}

asOverrideEnabled := d.Get("as_override_enabled").(bool)
bgpRP.SetAsOverrideEnabled(asOverrideEnabled)

bfdSchema := d.Get("bfd")
if bfdSchema != nil {
bfd := routingProtocolBfdTerraformToGo(bfdSchema.(*schema.Set).List())
Expand Down Expand Up @@ -532,7 +553,7 @@ func routingProtocolPayloadFromType(type_ string, d *schema.ResourceData) fabric
}
payload = fabricv4.RoutingProtocolDirectTypeAsRoutingProtocolBase(&directRP)
}
return payload
return payload, nil
}

func setFabricRoutingProtocolMap(d *schema.ResourceData, routingProtocolData *fabricv4.RoutingProtocolData) diag.Diagnostics {
Expand All @@ -556,6 +577,7 @@ func FabricRoutingProtocolMap(routingProtocolData *fabricv4.RoutingProtocolData)
routingProtocol["customer_asn"] = rp.GetCustomerAsn()
routingProtocol["equinix_asn"] = rp.GetCustomerAsn()
routingProtocol["bgp_auth_key"] = rp.GetBgpAuthKey()
routingProtocol["as_override_enabled"] = rp.GetAsOverrideEnabled()
if rp.Operation != nil {
operation := rp.GetOperation()
routingProtocol["operation"] = routingProtocolOperationGoToTerraform(&operation)
Expand Down Expand Up @@ -734,9 +756,9 @@ func routingProtocolDirectIpv6TerraformToGo(routingProtocolDirectIpv6Request []i
return rpDirectIpv6
}

func routingProtocolBgpIpv4TerraformToGo(routingProtocolBgpIpv4Request []interface{}) fabricv4.BGPConnectionIpv4 {
func routingProtocolBgpIpv4TerraformToGo(routingProtocolBgpIpv4Request []interface{}) (fabricv4.BGPConnectionIpv4, error) {
if len(routingProtocolBgpIpv4Request) == 0 {
return fabricv4.BGPConnectionIpv4{}
return fabricv4.BGPConnectionIpv4{}, nil
}

rpBgpIpv4 := fabricv4.BGPConnectionIpv4{}
Expand All @@ -748,8 +770,12 @@ func routingProtocolBgpIpv4TerraformToGo(routingProtocolBgpIpv4Request []interfa
enabled := bgpIpv4Map["enabled"].(bool)
rpBgpIpv4.SetEnabled(enabled)

if outboundAsPrependCount := bgpIpv4Map["outbound_as_prepend_count"].(int); outboundAsPrependCount > 0 {
rpBgpIpv4.SetOutboundASPrependCount(int64(outboundAsPrependCount))
if outboundAsPrependCountStr := bgpIpv4Map["outbound_as_prepend_count"].(string); outboundAsPrependCountStr != "" {
outboundAsPrependCount, err := strconv.ParseInt(outboundAsPrependCountStr, 10, 64)
if err != nil {
return fabricv4.BGPConnectionIpv4{}, fmt.Errorf("error converting outbound_as_prepend_count from string to int64: %s", err)
}
rpBgpIpv4.SetOutboundASPrependCount(outboundAsPrependCount)
}
if inboundMed := bgpIpv4Map["inbound_med"].(int); inboundMed > 0 {
rpBgpIpv4.SetInboundMED(int64(inboundMed))
Expand All @@ -758,12 +784,12 @@ func routingProtocolBgpIpv4TerraformToGo(routingProtocolBgpIpv4Request []interfa
rpBgpIpv4.SetOutboundMED(int64(outboundMed))
}

return rpBgpIpv4
return rpBgpIpv4, nil
}

func routingProtocolBgpIpv6TerraformToGo(routingProtocolBgpIpv6Request []interface{}) fabricv4.BGPConnectionIpv6 {
func routingProtocolBgpIpv6TerraformToGo(routingProtocolBgpIpv6Request []interface{}) (fabricv4.BGPConnectionIpv6, error) {
if len(routingProtocolBgpIpv6Request) == 0 {
return fabricv4.BGPConnectionIpv6{}
return fabricv4.BGPConnectionIpv6{}, nil
}

rpBgpIpv6 := fabricv4.BGPConnectionIpv6{}
Expand All @@ -775,8 +801,12 @@ func routingProtocolBgpIpv6TerraformToGo(routingProtocolBgpIpv6Request []interfa
enabled := bgpIpv6Map["enabled"].(bool)
rpBgpIpv6.SetEnabled(enabled)

if outboundAsPrependCount := bgpIpv6Map["outbound_as_prepend_count"].(int); outboundAsPrependCount > 0 {
rpBgpIpv6.SetOutboundASPrependCount(int64(outboundAsPrependCount))
if outboundAsPrependCountStr := bgpIpv6Map["outbound_as_prepend_count"].(string); outboundAsPrependCountStr != "" {
outboundAsPrependCount, err := strconv.ParseInt(outboundAsPrependCountStr, 10, 64)
if err != nil {
return fabricv4.BGPConnectionIpv6{}, err
}
rpBgpIpv6.SetOutboundASPrependCount(outboundAsPrependCount)
}
if inboundMed := bgpIpv6Map["inbound_med"].(int); inboundMed > 0 {
rpBgpIpv6.SetInboundMED(int64(inboundMed))
Expand All @@ -785,7 +815,7 @@ func routingProtocolBgpIpv6TerraformToGo(routingProtocolBgpIpv6Request []interfa
rpBgpIpv6.SetOutboundMED(int64(outboundMed))
}

return rpBgpIpv6
return rpBgpIpv6, nil
}

func routingProtocolBfdTerraformToGo(routingProtocolBfdRequest []interface{}) fabricv4.RoutingProtocolBFD {
Expand Down Expand Up @@ -846,7 +876,7 @@ func routingProtocolBgpConnectionIpv4GoToTerraform(routingProtocolBgpIpv4 *fabri
"customer_peer_ip": routingProtocolBgpIpv4.GetCustomerPeerIp(),
"equinix_peer_ip": routingProtocolBgpIpv4.GetEquinixPeerIp(),
"enabled": routingProtocolBgpIpv4.GetEnabled(),
"outbound_as_prepend_count": int(routingProtocolBgpIpv4.GetOutboundASPrependCount()),
"outbound_as_prepend_count": strconv.FormatInt(routingProtocolBgpIpv4.GetOutboundASPrependCount(), 10),
"inbound_med": int(routingProtocolBgpIpv4.GetInboundMED()),
"outbound_med": int(routingProtocolBgpIpv4.GetOutboundMED()),
}
Expand All @@ -866,7 +896,7 @@ func routingProtocolBgpConnectionIpv6GoToTerraform(routingProtocolBgpIpv6 *fabri
"customer_peer_ip": routingProtocolBgpIpv6.GetCustomerPeerIp(),
"equinix_peer_ip": routingProtocolBgpIpv6.GetEquinixPeerIp(),
"enabled": routingProtocolBgpIpv6.GetEnabled(),
"outbound_as_prepend_count": int(routingProtocolBgpIpv6.GetOutboundASPrependCount()),
"outbound_as_prepend_count": strconv.FormatInt(routingProtocolBgpIpv6.GetOutboundASPrependCount(), 10),
"inbound_med": int(routingProtocolBgpIpv6.GetInboundMED()),
"outbound_med": int(routingProtocolBgpIpv6.GetOutboundMED()),
}
Expand Down
13 changes: 7 additions & 6 deletions equinix/resource_fabric_routing_protocol_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package equinix_test
import (
"context"
"fmt"
"github.com/equinix/terraform-provider-equinix/internal/fabric/testing_helpers"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"testing"
"time"

"github.com/equinix/terraform-provider-equinix/equinix"
"github.com/equinix/terraform-provider-equinix/internal/acceptance"
"github.com/equinix/terraform-provider-equinix/internal/fabric/testing_helpers"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
)
Expand Down Expand Up @@ -150,7 +151,7 @@ resource "equinix_fabric_connection" "this" {
}
link_protocol {
type= "DOT1Q"
vlan_tag= 2009
vlan_tag= 2011
}
location {
metro_code = "SV"
Expand Down Expand Up @@ -180,17 +181,17 @@ resource "equinix_fabric_routing_protocol" "bgp" {
name = "rp_bgp_PFCR"
bgp_ipv4{
customer_peer_ip = "190.1.1.2"
outbound_as_prepend_count = 1
outbound_as_prepend_count = "1"
inbound_med = 4
outbound_med = 7
}
bgp_ipv6{
customer_peer_ip = "190::1:2"
outbound_as_prepend_count = 1
outbound_as_prepend_count = "1"
inbound_med = 4
outbound_med = 7
}
customer_asn = "100"
customer_asn = 100
}
data "equinix_fabric_routing_protocol" "direct" {
Expand Down

0 comments on commit 57e9334

Please sign in to comment.