Skip to content

Commit

Permalink
adding new vlan_map module
Browse files Browse the repository at this point in the history
  • Loading branch information
scotttyso committed Aug 16, 2021
1 parent e088dd5 commit 747682c
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 17 deletions.
14 changes: 10 additions & 4 deletions modules/domain_vlan/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
resource "intersight_fabric_vlan" "vlan" {
auto_allow_on_uplinks = var.auto_allow_on_uplinks
is_native = var.is_native
name = length(regexall("^[0-9]{4}$", var.vlan)) > 0 ? join(
"-vl", [var.vlan_prefix, var.vlan]) : length(regexall("^[0-9]{3}$", var.vlan)) > 0 ? join(
"-vl0", [var.vlan_prefix, var.vlan]) : length(regexall("^[0-9]{2}$", var.vlan)) > 0 ? join(
"-vl00", [var.vlan_prefix, var.vlan]) : join("-vl000", [var.vlan_prefix, var.vlan])
name = length(regexall("^[0-9]{4}$", var.vlan)) > 0 ? join("-vl", [
var.vlan_prefix, var.vlan
]) : length(regexall("^[0-9]{3}$", var.vlan)) > 0 ? join("-vl0", [
var.vlan_prefix, var.vlan
]) : length(regexall("^[0-9]{2}$", var.vlan)) > 0 ? join("-vl00", [
var.vlan_prefix, var.vlan
]) : join("-vl000", [
var.vlan_prefix, var.vlan
]
)
vlan_id = var.vlan
eth_network_policy {
moid = var.vlan_policy_moid
Expand Down
23 changes: 12 additions & 11 deletions modules/domain_vlan_list/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,28 @@ locals {
vlan_split = length(regexall("-", var.vlan_list)) > 0 ? tolist(
split(",", var.vlan_list)
) : tolist(var.vlan_list)
vlan_lists = [for s in local.vlan_split : length(regexall("-", s)) > 0 ? [
vlan_list = [for s in local.vlan_split : length(regexall("-", s)) > 0 ? [
for v in range(
tonumber(element(split("-", s), 0)),
(tonumber(element(split("-", s), 1)
) + 1)) : tonumber(v)] : [s]
]
flattened_vlan_list = flatten(local.vlan_lists)
vlan_list_set = toset(local.flattened_vlan_list)
}

resource "intersight_fabric_vlan" "vlan_list" {
for_each = local.vlan_list_set
for_each = toset(flatten(local.vlan_list))
auto_allow_on_uplinks = var.auto_allow_on_uplinks
is_native = var.is_native
name = length(regexall("^[0-9]{4}$", each.value)) > 0 ? join(
"-vl", [var.vlan_prefix, each.value]) : length(
regexall("^[0-9]{3}$", each.value)) > 0 ? join(
"-vl0", [var.vlan_prefix, each.value]) : length(
regexall("^[0-9]{2}$", each.value)) > 0 ? join(
"-vl00", [var.vlan_prefix, each.value]) : join(
"-vl000", [var.vlan_prefix, each.value])
name = length(regexall("^[0-9]{4}$", each.value)) > 0 ? join("-vl", [
var.vlan_prefix, each.value
]) : length(regexall("^[0-9]{3}$", each.value)) > 0 ? join("-vl0", [
var.vlan_prefix, each.value
]) : length(regexall("^[0-9]{2}$", each.value)) > 0 ? join("-vl00", [
var.vlan_prefix, each.value
]) : join("-vl000", [
var.vlan_prefix, each.value
]
)
vlan_id = each.value
eth_network_policy {
moid = var.vlan_policy_moid
Expand Down
4 changes: 2 additions & 2 deletions modules/domain_vlan_list/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ variable "tags" {
}

variable "vlan_list" {
default = "2-10,21-30"
type = string
description = "List of VLAN ID's to add to a VLAN Policy. This string can contain ranges and comma seperated values. Like \"2-10,21-30\""
type = string
}

variable "vlan_prefix" {
Expand Down
58 changes: 58 additions & 0 deletions modules/domain_vlan_map/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# domain_vlan_map - Intersight UCS Domain Fabric VLAN Terraform Module

## Usage

```hcl
module "vlan_map" {
source = "terraform-cisco-modules/imm/intersight//modules/domain_vlan_map"
# omitted...
}
```

This module will create a Fabric VLAN List in Intersight. This can be used assign VLANs to a VLAN Policy.

These resources are consumed

* [vlan](https://registry.terraform.io/providers/CiscoDevNet/intersight/latest/docs/data-sources/fabric_vlan)

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| <a name="provider_intersight"></a> [intersight](#provider\_intersight) | n/a |

## Modules

No modules.

## Resources

| Name | Type |
|------|------|
| [intersight_fabric_vlan.vlan_list](https://registry.terraform.io/providers/CiscoDevNet/intersight/latest/docs/resources/fabric_vlan) | resource |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_auto_allow_on_uplinks"></a> [auto\_allow\_on\_uplinks](#input\_auto\_allow\_on\_uplinks) | Used to determine whether this VLAN will be allowed on all uplink ports and PCs in this FI. | `bool` | `true` | no |
| <a name="input_is_native"></a> [is\_native](#input\_is\_native) | Used to determine if this VLAN should be untagged on the interfaces. | `bool` | `false` | no |
| <a name="input_multicast_moid"></a> [multicast\_moid](#input\_multicast\_moid) | Multicast Policy moid map. | `string` | n/a | yes |
| <a name="input_tags"></a> [tags](#input\_tags) | List of Tag Attributes to Assign to the Policy. | `list(map(string))` | `[]` | no |
| <a name="input_vlan_list"></a> [vlan\_list](#input\_vlan\_list) | n/a | `string` | `"2-10,21-30"` | no |
| <a name="input_vlan_policy_moid"></a> [vlan\_policy\_moid](#input\_vlan\_policy\_moid) | VLAN Policy (Ethernet Network Policy) moid. | `string` | n/a | yes |
| <a name="input_vlan_prefix"></a> [vlan\_prefix](#input\_vlan\_prefix) | Prefix Name for VLANs. | `string` | `"VLAN"` | no |

## Outputs

| Name | Description |
|------|-------------|
| <a name="output_moid"></a> [moid](#output\_moid) | Fabric VLAN Managed Object ID (moid). |
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
28 changes: 28 additions & 0 deletions modules/domain_vlan_map/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#____________________________________________________________
#
# Intersight Fabric VLAN List
# GUI Location: Policies > Create Policy
#____________________________________________________________

resource "intersight_fabric_vlan" "vlan_map" {
for_each = {for k, v in var.vlan_map : k => v }
auto_allow_on_uplinks = var.auto_allow_on_uplinks
is_native = var.is_native
name = length(regexall("^[0-9]{4}$", each.value.vlan_id)) > 0 ? join("-vl", [
each.value.prefix, each.value.vlan_id
]) : length(regexall("^[0-9]{3}$", each.value.vlan_id)) > 0 ? join("-vl0", [
each.value.prefix, each.value.vlan_id
]) : length(regexall("^[0-9]{2}$", each.value.vlan_id)) > 0 ? join("-vl00", [
each.value.prefix, each.value.vlan_id
]) : join("-vl000", [
each.value.prefix, each.value.vlan_id
]
)
vlan_id = each.value.vlan_id
eth_network_policy {
moid = var.vlan_policy_moid
}
multicast_policy {
moid = var.multicast_moid
}
}
9 changes: 9 additions & 0 deletions modules/domain_vlan_map/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#____________________________________________________________
#
# Collect the moid of the Fabric VLAN as an Output
#____________________________________________________________

output "moid" {
description = "Fabric VLAN Managed Object ID (moid)."
value = { for v in sort(keys(intersight_fabric_vlan.vlan_map)) : v => intersight_fabric_vlan.vlan_map[v].moid }
}
13 changes: 13 additions & 0 deletions modules/domain_vlan_map/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#_______________________________________________________________________
#
# Terraform Required Parameters - Intersight Provider
# https://registry.terraform.io/providers/CiscoDevNet/intersight/latest
#_______________________________________________________________________

terraform {
required_providers {
intersight = {
source = "CiscoDevNet/intersight"
}
}
}
74 changes: 74 additions & 0 deletions modules/domain_vlan_map/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#____________________________________________________________
#
# UCS Domain VLANs Variables Section.
#____________________________________________________________

variable "apikey" {
description = "Intersight API Key."
sensitive = true
type = string
}

variable "endpoint" {
default = "https://intersight.com"
description = "Intersight URL."
type = string
}

variable "secretkey" {
description = "Intersight Secret Key."
sensitive = true
type = string
}

variable "auto_allow_on_uplinks" {
default = true
description = "Used to determine whether this VLAN will be allowed on all uplink ports and PCs in this FI."
type = bool
}

variable "is_native" {
default = false
description = "Used to determine if this VLAN should be untagged on the interfaces."
type = bool
}

variable "multicast_moid" {
description = "Multicast Policy moid map."
type = string
}

variable "tags" {
default = []
description = "List of Tag Attributes to Assign to the Policy."
type = list(map(string))
}

variable "vlan_map" {
description = <<-EOT
This VLAN Map should contain key/value pairs of prefix and vlan_id. In Example:
vlan_map = [
{
prefix = "test"
vlan_id = 123
},
{
prefix = "test"
vlan_id = 124
}
]
This will be configured as test-vl0123 and test-vl0124 in intersight.
EOT
type = list(map(string))
}

variable "vlan_prefix" {
default = "VLAN"
description = "Prefix Name for VLANs."
type = string
}

variable "vlan_policy_moid" {
description = "VLAN Policy (Ethernet Network Policy) moid."
type = string
}

0 comments on commit 747682c

Please sign in to comment.