Skip to content

Commit

Permalink
Add support for multiple asn entries in as path prepending (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
juchowan authored May 25, 2024
1 parent 1320c66 commit 3e06101
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 80 deletions.
23 changes: 13 additions & 10 deletions aci_tenants.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2147,11 +2147,18 @@ locals {
description = try(comm.description, "")
}
]
set_as_path = try(policy.set_as_path.criteria, null) != null || try(policy.set_as_path.count, null) != null || try(policy.set_as_path.order, null) != null
set_as_path_criteria = try(policy.set_as_path.criteria, local.defaults.apic.tenants.policies.set_rules.set_as_path.criteria)
set_as_path_count = try(policy.set_as_path.count, local.defaults.apic.tenants.policies.set_rules.set_as_path.count)
set_as_path_order = try(policy.set_as_path.order, local.defaults.apic.tenants.policies.set_rules.set_as_path.order)
set_as_path_asn = try(policy.set_as_path.asn, null)
set_as_paths = [
for as_path in try(policy.set_as_paths, []) : {
criteria = try(as_path.criteria, local.defaults.apic.tenants.policies.set_rules.set_as_paths.criteria)
count = try(as_path.count, local.defaults.apic.tenants.policies.set_rules.set_as_paths.count)
asns = [
for asn in try(as_path.asns, []) : {
asn_number = asn.number
order = try(asn.order, local.defaults.apic.tenants.policies.set_rules.set_as_paths.asns.order)
}
]
}
]
next_hop_propagation = try(policy.next_hop_propagation, local.defaults.apic.tenants.policies.set_rules.next_hop_propagation)
multipath = try(policy.multipath, local.defaults.apic.tenants.policies.set_rules.multipath)
external_endpoint_group = try(policy.external_endpoint_group.name, null) != null ? "${policy.external_endpoint_group.name}${local.defaults.apic.tenants.l3outs.external_endpoint_groups.name_suffix}" : ""
Expand Down Expand Up @@ -2183,11 +2190,7 @@ module "aci_set_rule" {
preference = each.value.preference
metric_type = each.value.metric_type
additional_communities = each.value.additional_communities
set_as_path = each.value.set_as_path
set_as_path_criteria = each.value.set_as_path_criteria
set_as_path_count = each.value.set_as_path_count
set_as_path_order = each.value.set_as_path_order
set_as_path_asn = each.value.set_as_path_asn
set_as_paths = each.value.set_as_paths
next_hop_propagation = each.value.next_hop_propagation
multipath = each.value.multipath
external_endpoint_group = each.value.external_endpoint_group
Expand Down
7 changes: 4 additions & 3 deletions defaults/defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1186,10 +1186,11 @@ defaults:
max_suppress_time: 60
reuse_limit: 750
suppress_limit: 2000
set_as_path:
set_as_paths:
criteria: "prepend"
count: 1
order: 0
count: 0
asns:
order: 0
next_hop_propagation: false
multipath: false
bfd_interface_policies:
Expand Down
23 changes: 12 additions & 11 deletions modules/terraform-aci-set-rule/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@ module "aci_set_rule" {
description = "My Community"
}
]
set_as_path = true
set_as_path_criteria = "prepend"
set_as_path_count = 0
set_as_path_asn = 65001
set_as_path_order = 5
set_as_paths = [
{
criteria = "prepend"
asns = [
{
number = 65001
order = 5
}
]
}
]
next_hop_propagation = true
multipath = true
}
Expand Down Expand Up @@ -79,11 +84,7 @@ module "aci_set_rule" {
| <a name="input_metric"></a> [metric](#input\_metric) | Metric. Allowed values `metric`: 0-4294967295. | `number` | `null` | no |
| <a name="input_metric_type"></a> [metric\_type](#input\_metric\_type) | Metric Type. Choice `metric_type`: `ospf-type1` or `ospf-type1`. | `string` | `""` | no |
| <a name="input_additional_communities"></a> [additional\_communities](#input\_additional\_communities) | Additional communities. | <pre>list(object({<br> community = string<br> description = optional(string, "")<br> }))</pre> | `[]` | no |
| <a name="input_set_as_path"></a> [set\_as\_path](#input\_set\_as\_path) | AS-Path Set. Flag to set AS Path. | `bool` | `false` | no |
| <a name="input_set_as_path_criteria"></a> [set\_as\_path\_criteria](#input\_set\_as\_path\_criteria) | AS-PATH Criteria. Choices `set_as_path_criteria`: `prepend` or `prepend-last-as`. | `string` | `"prepend"` | no |
| <a name="input_set_as_path_count"></a> [set\_as\_path\_count](#input\_set\_as\_path\_count) | AS-PATH Count. Allowed values `set_as_path_count`: 0-10. | `number` | `1` | no |
| <a name="input_set_as_path_order"></a> [set\_as\_path\_order](#input\_set\_as\_path\_order) | AS-PATH Order. Allowed values `set_as_path_order`: 0-31. | `number` | `0` | no |
| <a name="input_set_as_path_asn"></a> [set\_as\_path\_asn](#input\_set\_as\_path\_asn) | AS-PATH ASN. Allowed values `set_as_path_asn`: 0-65535. | `number` | `null` | no |
| <a name="input_set_as_paths"></a> [set\_as\_paths](#input\_set\_as\_paths) | AS-Path Set List. | <pre>list(object({<br> criteria = optional(string, "prepend")<br> count = optional(number, 1)<br> asns = list(object({<br> order = optional(number, 0)<br> asn_number = number<br> }))<br> }))</pre> | `[]` | no |
| <a name="input_next_hop_propagation"></a> [next\_hop\_propagation](#input\_next\_hop\_propagation) | Next Hop Propagation. | `bool` | `false` | no |
| <a name="input_multipath"></a> [multipath](#input\_multipath) | Multipath. | `bool` | `false` | no |
| <a name="input_external_endpoint_group"></a> [external\_endpoint\_group](#input\_external\_endpoint\_group) | External endpoint group name. | `string` | `""` | no |
Expand Down
17 changes: 11 additions & 6 deletions modules/terraform-aci-set-rule/examples/complete/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ module "aci_set_rule" {
description = "My Community"
}
]
set_as_path = true
set_as_path_criteria = "prepend"
set_as_path_count = 0
set_as_path_asn = 65001
set_as_path_order = 5
set_as_paths = [
{
criteria = "prepend"
asns = [
{
number = 65001
order = 5
}
]
}
]
next_hop_propagation = true
multipath = true
}
Expand Down
17 changes: 11 additions & 6 deletions modules/terraform-aci-set-rule/examples/complete/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,17 @@ module "aci_set_rule" {
description = "My Community"
}
]
set_as_path = true
set_as_path_criteria = "prepend"
set_as_path_count = 0
set_as_path_asn = 65001
set_as_path_order = 5

set_as_paths = [
{
criteria = "prepend"
asns = [
{
number = 65001
order = 5
}
]
}
]
next_hop_propagation = true
multipath = true
}
31 changes: 23 additions & 8 deletions modules/terraform-aci-set-rule/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,38 @@ resource "aci_rest_managed" "rtctrlSetAddComm" {
}

resource "aci_rest_managed" "rtctrlSetASPath" {
count = var.set_as_path != false ? 1 : 0
dn = "${aci_rest_managed.rtctrlAttrP.dn}/saspath-${var.set_as_path_criteria}"
for_each = { for as_path in var.set_as_paths : as_path.criteria => as_path }
dn = "${aci_rest_managed.rtctrlAttrP.dn}/saspath-${each.value.criteria}"
class_name = "rtctrlSetASPath"
content = {
"criteria" = var.set_as_path_criteria
"lastnum" = var.set_as_path_count
"criteria" = each.value.criteria
"lastnum" = each.value.count
"type" = "as-path"
}
}

locals {
set_as_paths_prepend = flatten([
for as_path in var.set_as_paths : [
for asn in try(as_path.asns, []) : {
key = "${as_path.criteria}/${asn.order}"
value = {
asn = asn.asn_number
order = asn.order
criteria = as_path.criteria
}
}
] if as_path.criteria == "prepend"
])
}

resource "aci_rest_managed" "rtctrlSetASPathASN" {
count = var.set_as_path != false && var.set_as_path_criteria == "prepend" && var.set_as_path_asn != null ? 1 : 0
dn = "${aci_rest_managed.rtctrlSetASPath[0].dn}/asn-${var.set_as_path_order}"
for_each = { for as_path in local.set_as_paths_prepend : as_path.key => as_path.value }
dn = "${aci_rest_managed.rtctrlSetASPath[each.value.criteria].dn}/asn-${each.value.order}"
class_name = "rtctrlSetASPathASN"
content = {
"asn" = var.set_as_path_asn
"order" = var.set_as_path_order
"asn" = each.value.asn
"order" = each.value.order
}
}

Expand Down
65 changes: 29 additions & 36 deletions modules/terraform-aci-set-rule/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -173,53 +173,46 @@ variable "additional_communities" {
}
}

variable "set_as_path" {
description = "AS-Path Set. Flag to set AS Path."
type = bool
default = false
}

variable "set_as_path_criteria" {
description = "AS-PATH Criteria. Choices `set_as_path_criteria`: `prepend` or `prepend-last-as`."
type = string
default = "prepend"
variable "set_as_paths" {
description = "AS-Path Set List."
type = list(object({
criteria = optional(string, "prepend")
count = optional(number, 1)
asns = list(object({
order = optional(number, 0)
asn_number = number
}))
}))
default = []

validation {
condition = contains(["prepend", "prepend-last-as"], var.set_as_path_criteria)
error_message = "Valid values for `set_as_path_criteria` are `prepend` or `prepend-last-as`."
condition = alltrue([
for c in var.set_as_paths : c.criteria == null || contains(["prepend", "prepend-last-as"], c.criteria)
])
error_message = "Valid values for `criteria` are `prepend` or `prepend-last-as`."
}
}

variable "set_as_path_count" {
description = "AS-PATH Count. Allowed values `set_as_path_count`: 0-10."
type = number
default = 1

validation {
condition = var.set_as_path_count >= 0 && var.set_as_path_count <= 10
error_message = "`set_as_path_count` minimum value: `0`. Maximum value: `10`."
condition = alltrue([
for c in var.set_as_paths : c.count == null || c.count >= 0 && c.count <= 10
])
error_message = "`count` minimum value: `0`. Maximum value: `10`."
}
}

variable "set_as_path_order" {
description = "AS-PATH Order. Allowed values `set_as_path_order`: 0-31."
type = number
default = 0

validation {
condition = var.set_as_path_order >= 0 && var.set_as_path_order <= 31
error_message = "`set_as_path_order` minimum value: `0`. Maximum value: `31`."
condition = alltrue(flatten([
for c in var.set_as_paths : [
for a in c.asns : a.order == null || try(a.order >= 0 && a.order <= 31, false)
]]))
error_message = "`order` minimum value: `0`. Maximum value: `31`."
}
}

variable "set_as_path_asn" {
description = "AS-PATH ASN. Allowed values `set_as_path_asn`: 0-65535."
type = number
default = null

validation {
condition = var.set_as_path_asn == null || try(var.set_as_path_asn >= 0 && var.set_as_path_asn <= 65535, false)
error_message = "`set_as_path_asn` minimum value: `0`. Maximum value: `65535`."
condition = alltrue(flatten([
for c in var.set_as_paths : [
for a in c.asns : try(a.asn_number >= 0 && a.asn_number <= 65535, false)
]]))
error_message = "`asn` minimum value: `0`. Maximum value: `65535`."
}
}

Expand Down

0 comments on commit 3e06101

Please sign in to comment.