Skip to content

Commit

Permalink
Import support for BGP neighbor on GM
Browse files Browse the repository at this point in the history
Signed-off-by: Anna Khmelnitsky <[email protected]>
  • Loading branch information
annakhm committed Oct 3, 2022
1 parent cd11640 commit 83c59ce
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 31 deletions.
23 changes: 17 additions & 6 deletions nsxt/resource_nsxt_policy_bgp_neighbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,25 @@ func resourceNsxtPolicyBgpNeighborImport(d *schema.ResourceData, m interface{})
tier0ID := s[0]
serviceID := s[1]
neighborID := s[2]
connector := getPolicyConnector(m)
client := bgp.NewNeighborsClient(connector)
var parentPath string

neighbor, err := client.Get(tier0ID, serviceID, neighborID)
if err != nil {
return nil, err
connector := getPolicyConnector(m)
if isPolicyGlobalManager(m) {
client := gm_bgp.NewNeighborsClient(connector)
neighbor, err := client.Get(tier0ID, serviceID, neighborID)
if err != nil {
return nil, err
}
parentPath = *neighbor.ParentPath
} else {
client := bgp.NewNeighborsClient(connector)
neighbor, err := client.Get(tier0ID, serviceID, neighborID)
if err != nil {
return nil, err
}
parentPath = *neighbor.ParentPath
}
d.Set("bgp_path", neighbor.ParentPath)
d.Set("bgp_path", parentPath)

d.SetId(neighborID)

Expand Down
99 changes: 74 additions & 25 deletions nsxt/resource_nsxt_policy_bgp_neighbor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var accTestPolicyBgpNeighborConfigCreateAttributes = map[string]string{

var accTestPolicyBgpNeighborConfigUpdateAttributes = map[string]string{
"display_name": getAccTestResourceName(),
"description": "terraform created",
"description": "terraform updated",
"allow_as_in": "false",
"graceful_restart_mode": "GR_AND_HELPER",
"hold_down_time": "950",
Expand Down Expand Up @@ -299,6 +299,30 @@ func TestAccResourceNsxtPolicyBgpNeighbor_subConfigPrefixList(t *testing.T) {
})
}

func TestAccResourceNsxtPolicyBgpNeighbor_importGlobalManager(t *testing.T) {
name := getAccTestResourceName()
testResourceName := "nsxt_policy_bgp_neighbor.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccOnlyGlobalManager(t); testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNsxtPolicyBgpNeighborCheckDestroy(state, name)
},
Steps: []resource.TestStep{
{
Config: testAccNsxtPolicyBgpNeighborGMImportTemplate(),
},
{
ResourceName: testResourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccNSXPolicyBgpNeighborImporterGetIDs,
},
},
})
}

func TestAccResourceNsxtPolicyBgpNeighbor_importBasic(t *testing.T) {
name := getAccTestResourceName()
testResourceName := "nsxt_policy_bgp_neighbor.test"
Expand Down Expand Up @@ -334,7 +358,7 @@ func testAccNSXPolicyBgpNeighborImporterGetIDs(s *terraform.State) (string, erro
}
bgpPath := rs.Primary.Attributes["bgp_path"]
if bgpPath == "" {
return "", fmt.Errorf("NSX Policy BGP Neighbor bgp_path not set in resources ")
return "", fmt.Errorf("NSX Policy BGP Neighbor bgp_path not set in resources")
}
t0ID, serviceID := resourceNsxtPolicyBgpNeighborParseIDs(bgpPath)
return fmt.Sprintf("%s/%s/%s", t0ID, serviceID, resourceID), nil
Expand Down Expand Up @@ -440,11 +464,6 @@ resource "nsxt_policy_tier0_gateway_interface" "test" {
subnets = ["%s"]
}
data "nsxt_policy_realization_info" "bgp_realization_info" {
path = nsxt_policy_tier0_gateway.test.bgp_config.0.path
}
resource "nsxt_policy_bgp_neighbor" "test" {
display_name = "%s"
description = "%s"
Expand Down Expand Up @@ -516,10 +535,6 @@ resource "nsxt_policy_tier0_gateway" "test" {
}
}
data "nsxt_policy_realization_info" "bgp_realization_info" {
path = nsxt_policy_tier0_gateway.test.bgp_config.0.path
}
resource "nsxt_policy_bgp_neighbor" "test" {
bgp_path = nsxt_policy_tier0_gateway.test.bgp_config.0.path
display_name = "tfbgp"
Expand All @@ -541,10 +556,6 @@ resource "nsxt_policy_bgp_neighbor" "test" {
address_family = "L2VPN_EVPN"
maximum_routes = 20
}
}
data "nsxt_policy_realization_info" "realization_info" {
path = nsxt_policy_bgp_neighbor.test.path
}`, getEdgeClusterName())
}

Expand Down Expand Up @@ -725,12 +736,6 @@ resource "nsxt_policy_tier0_gateway_interface" "test" {
site_path = data.nsxt_policy_site.test.path
}
data "nsxt_policy_realization_info" "bgp_realization_info" {
path = nsxt_policy_bgp_config.test.path
site_path = data.nsxt_policy_site.test.path
}
resource "nsxt_policy_bgp_neighbor" "test" {
display_name = "%s"
description = "%s"
Expand All @@ -749,10 +754,54 @@ resource "nsxt_policy_bgp_neighbor" "test" {
scope = "scope1"
tag = "tag1"
}
}`, subnet, attrMap["display_name"], attrMap["description"], attrMap["allow_as_in"], attrMap["graceful_restart_mode"], attrMap["hold_down_time"], attrMap["keep_alive_time"], attrMap["maximum_hop_limit"], attrMap["neighbor_address"], attrMap["remote_as_num"], attrMap["password"])
}

data "nsxt_policy_realization_info" "realization_info" {
path = nsxt_policy_bgp_neighbor.test.path
site_path = data.nsxt_policy_site.test.path
}`, subnet, attrMap["display_name"], attrMap["description"], attrMap["allow_as_in"], attrMap["graceful_restart_mode"], attrMap["hold_down_time"], attrMap["keep_alive_time"], attrMap["maximum_hop_limit"], attrMap["neighbor_address"], attrMap["remote_as_num"], attrMap["password"])
func testAccNsxtPolicyBgpNeighborGMImportTemplate() string {
attrMap := accTestPolicyBgpNeighborConfigCreateAttributes
return testAccNsxtGlobalPolicyEdgeClusterReadTemplate() + testAccNSXGlobalPolicyTransportZoneReadTemplate(true, false) + fmt.Sprintf(`
resource "nsxt_policy_vlan_segment" "test" {
transport_zone_path = data.nsxt_policy_transport_zone.test.path
display_name = "Acceptance Test"
vlan_ids = [11]
subnet {
cidr = "10.2.2.2/24"
}
}
resource "nsxt_policy_tier0_gateway" "test" {
display_name = "terraformt0gw"
description = "Acceptance Test"
locale_service {
edge_cluster_path = data.nsxt_policy_edge_cluster.test.path
}
}
resource "nsxt_policy_bgp_config" "test" {
gateway_path = nsxt_policy_tier0_gateway.test.path
site_path = data.nsxt_policy_site.test.path
local_as_num = "60000"
multipath_relax = true
route_aggregation {
prefix = "12.12.12.0/24"
}
}
resource "nsxt_policy_tier0_gateway_interface" "test" {
display_name = "terraformt0gwintf"
type = "EXTERNAL"
description = "Acceptance Test"
gateway_path = nsxt_policy_tier0_gateway.test.path
segment_path = nsxt_policy_vlan_segment.test.path
subnets = ["12.12.12.1/24"]
site_path = data.nsxt_policy_site.test.path
}
resource "nsxt_policy_bgp_neighbor" "test" {
bgp_path = nsxt_policy_bgp_config.test.path
display_name = "%s"
neighbor_address = "%s"
remote_as_num = "%s"
source_addresses = nsxt_policy_tier0_gateway_interface.test.ip_addresses
}`, attrMap["display_name"], attrMap["neighbor_address"], attrMap["remote_as_num"])
}

0 comments on commit 83c59ce

Please sign in to comment.