From 57e9334866c618730408bd920ca7c7dfac2737aa Mon Sep 17 00:00:00 2001 From: Tim Hogarty Date: Fri, 4 Oct 2024 18:43:10 -0700 Subject: [PATCH] Update outbound_as_prepend_count to string type to account for no given value --- docs/data-sources/fabric_routing_protocol.md | 7 ++ equinix/resource_fabric_routing_protocol.go | 70 +++++++++++++------ ...source_fabric_routing_protocol_acc_test.go | 13 ++-- 3 files changed, 64 insertions(+), 26 deletions(-) diff --git a/docs/data-sources/fabric_routing_protocol.md b/docs/data-sources/fabric_routing_protocol.md index a6a4eff2d..3beb94393 100644 --- a/docs/data-sources/fabric_routing_protocol.md +++ b/docs/data-sources/fabric_routing_protocol.md @@ -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)) @@ -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) @@ -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) diff --git a/equinix/resource_fabric_routing_protocol.go b/equinix/resource_fabric_routing_protocol.go index 9a3c3aefd..5b6a227fe 100644 --- a/equinix/resource_fabric_routing_protocol.go +++ b/equinix/resource_fabric_routing_protocol.go @@ -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", @@ -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", @@ -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, @@ -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() @@ -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() @@ -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{} @@ -485,7 +497,10 @@ 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) } @@ -493,12 +508,18 @@ func routingProtocolPayloadFromType(type_ string, d *schema.ResourceData) fabric 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()) @@ -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 { @@ -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) @@ -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{} @@ -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)) @@ -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{} @@ -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)) @@ -785,7 +815,7 @@ func routingProtocolBgpIpv6TerraformToGo(routingProtocolBgpIpv6Request []interfa rpBgpIpv6.SetOutboundMED(int64(outboundMed)) } - return rpBgpIpv6 + return rpBgpIpv6, nil } func routingProtocolBfdTerraformToGo(routingProtocolBfdRequest []interface{}) fabricv4.RoutingProtocolBFD { @@ -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()), } @@ -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()), } diff --git a/equinix/resource_fabric_routing_protocol_acc_test.go b/equinix/resource_fabric_routing_protocol_acc_test.go index e51e4bd01..bd9103bc0 100644 --- a/equinix/resource_fabric_routing_protocol_acc_test.go +++ b/equinix/resource_fabric_routing_protocol_acc_test.go @@ -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" ) @@ -150,7 +151,7 @@ resource "equinix_fabric_connection" "this" { } link_protocol { type= "DOT1Q" - vlan_tag= 2009 + vlan_tag= 2011 } location { metro_code = "SV" @@ -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" {