Skip to content

Commit

Permalink
Merge pull request #818 from vmware/route-map-on-demand
Browse files Browse the repository at this point in the history
Fix computed attributes in route map set clause
  • Loading branch information
annakhm authored Dec 5, 2022
2 parents 75ef12d + 775d202 commit 453a188
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
22 changes: 15 additions & 7 deletions nsxt/resource_nsxt_policy_gateway_route_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ func getPolicyRouteMapEntrySchema() *schema.Resource {
"local_preference": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Local preference indicates the degree of preference for one BGP route over other BGP routes",
},
"med": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "A lower Multi exit descriminator (MED) is preferred over a higher value",
},
"prefer_global_v6_next_hop": {
Expand Down Expand Up @@ -159,7 +161,7 @@ func resourceNsxtPolicyGatewayRouteMapExists(tier0Id string, id string, connecto
return false, logAPIError("Error retrieving resource", err)
}

func policyGatewayRouteMapBuildEntry(schemaEntry map[string]interface{}) model.RouteMapEntry {
func policyGatewayRouteMapBuildEntry(d *schema.ResourceData, entryNo int, schemaEntry map[string]interface{}) model.RouteMapEntry {

action := schemaEntry["action"].(string)
obj := model.RouteMapEntry{
Expand Down Expand Up @@ -192,14 +194,12 @@ func policyGatewayRouteMapBuildEntry(schemaEntry map[string]interface{}) model.R
data := schemaEntry["set"].([]interface{})[0].(map[string]interface{})
asPathPrepend := data["as_path_prepend"].(string)
community := data["community"].(string)
localPreference := int64(data["local_preference"].(int))
med := int64(data["med"].(int))
localPreferenceValue, lpSet := d.GetOk(fmt.Sprintf("entry.%d.set.0.local_preference", entryNo))
medValue, medSet := d.GetOk(fmt.Sprintf("entry.%d.set.0.med", entryNo))
globalIPv6 := data["prefer_global_v6_next_hop"].(bool)
weight := int64(data["weight"].(int))

entrySet := model.RouteMapEntrySet{
LocalPreference: &localPreference,
Med: &med,
PreferGlobalV6NextHop: &globalIPv6,
Weight: &weight,
}
Expand All @@ -210,6 +210,14 @@ func policyGatewayRouteMapBuildEntry(schemaEntry map[string]interface{}) model.R
if len(community) > 0 {
entrySet.Community = &community
}
if lpSet {
localPreference := int64(localPreferenceValue.(int))
entrySet.LocalPreference = &localPreference
}
if medSet {
med := int64(medValue.(int))
entrySet.Med = &med
}
obj.Set = &entrySet
}

Expand All @@ -223,8 +231,8 @@ func resourceNsxtPolicyGatewayRouteMapPatch(gwID string, id string, d *schema.Re

schemaEntries := d.Get("entry").([]interface{})
var entries []model.RouteMapEntry
for _, entry := range schemaEntries {
entries = append(entries, policyGatewayRouteMapBuildEntry(entry.(map[string]interface{})))
for entryNo, entry := range schemaEntries {
entries = append(entries, policyGatewayRouteMapBuildEntry(d, entryNo, entry.(map[string]interface{})))
}

obj := model.Tier0RouteMap{
Expand Down
43 changes: 41 additions & 2 deletions nsxt/resource_nsxt_policy_gateway_route_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,24 @@ func TestAccResourceNsxtPolicyGatewayRouteMap_basic(t *testing.T) {
testAccNsxtPolicyGatewayRouteMapExists(displayName, testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", displayName),
resource.TestCheckResourceAttr(testResourceName, "description", "terraform created"),

resource.TestCheckResourceAttr(testResourceName, "entry.#", "3"),
resource.TestCheckResourceAttr(testResourceName, "entry.0.action", "PERMIT"),
resource.TestCheckResourceAttr(testResourceName, "entry.0.set.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "entry.0.set.0.local_preference", "100"),
resource.TestCheckResourceAttr(testResourceName, "entry.0.set.0.med", "0"),
resource.TestCheckResourceAttr(testResourceName, "entry.0.prefix_list_matches.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "entry.1.action", "DENY"),
resource.TestCheckResourceAttr(testResourceName, "entry.1.community_list_match.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "entry.1.community_list_match.0.criteria", "11:22"),
resource.TestCheckResourceAttr(testResourceName, "entry.1.set.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "entry.1.set.0.local_preference", "1122"),
resource.TestCheckResourceAttr(testResourceName, "entry.1.set.0.as_path_prepend", "100 100"),
resource.TestCheckResourceAttr(testResourceName, "entry.1.set.0.community", "11:22"),
resource.TestCheckResourceAttr(testResourceName, "entry.1.set.0.prefer_global_v6_next_hop", "true"),
resource.TestCheckResourceAttr(testResourceName, "entry.1.set.0.weight", "12"),
resource.TestCheckResourceAttr(testResourceName, "entry.1.set.0.med", "120"),
resource.TestCheckResourceAttr(testResourceName, "entry.2.action", "DENY"),
resource.TestCheckResourceAttr(testResourceName, "entry.2.set.#", "0"),
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
Expand All @@ -41,7 +58,13 @@ func TestAccResourceNsxtPolicyGatewayRouteMap_basic(t *testing.T) {
testAccNsxtPolicyGatewayRouteMapExists(displayName, testResourceName),
resource.TestCheckResourceAttr(testResourceName, "display_name", displayName),
resource.TestCheckResourceAttr(testResourceName, "description", "terraform updated"),

resource.TestCheckResourceAttr(testResourceName, "entry.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "entry.0.action", "PERMIT"),
resource.TestCheckResourceAttr(testResourceName, "entry.0.set.#", "1"),
resource.TestCheckResourceAttr(testResourceName, "entry.0.set.0.local_preference", "100"),
resource.TestCheckResourceAttr(testResourceName, "entry.0.set.0.med", "5"),
resource.TestCheckResourceAttr(testResourceName, "entry.0.community_list_match.#", "2"),
resource.TestCheckResourceAttr(testResourceName, "entry.0.prefix_list_matches.#", "0"),
resource.TestCheckResourceAttrSet(testResourceName, "nsx_id"),
resource.TestCheckResourceAttrSet(testResourceName, "path"),
resource.TestCheckResourceAttrSet(testResourceName, "revision"),
Expand Down Expand Up @@ -163,6 +186,10 @@ resource "nsxt_policy_gateway_route_map" "test" {
entry {
action = "PERMIT"
prefix_list_matches = [nsxt_policy_gateway_prefix_list.test.path]
set {
weight = 12
}
}
entry {
Expand All @@ -181,6 +208,14 @@ resource "nsxt_policy_gateway_route_map" "test" {
weight = 12
}
}
entry {
action = "DENY"
community_list_match {
criteria = "22:*"
match_operator = "MATCH_COMMUNITY_REGEX"
}
}
tag {
scope = "scope1"
Expand Down Expand Up @@ -219,6 +254,10 @@ resource "nsxt_policy_gateway_route_map" "test" {
criteria = "11:*"
match_operator = "MATCH_LARGE_COMMUNITY_REGEX"
}
set {
weight = 12
med = 5
}
}
tag {
Expand Down

0 comments on commit 453a188

Please sign in to comment.